Use an interface for peco.Hub()

This allows us to plug a dummy hub.Hub instance, and avoid nasty
code for testing
This commit is contained in:
Daisuke Maki 2016-10-22 10:50:55 +09:00
parent 82a9d29697
commit c8a43049f3
5 changed files with 36 additions and 9 deletions

View file

@ -63,7 +63,7 @@ type Peco struct {
Stdin io.Reader
Stdout io.Writer
Stderr io.Writer
hub *hub.Hub
hub MessageHub
args []string
bufferSize int
@ -534,3 +534,20 @@ type ExternalCmdFilter struct {
query string
thresholdBufsiz int
}
// MessageHub is the interface that must be satisfied by the
// message hub component. Unless we're in testing, github.com/peco/peco/hub.Hub
// is used.
type MessageHub interface {
Batch(func(), bool)
DrawCh() chan hub.Payload
PagingCh() chan hub.Payload
QueryCh() chan hub.Payload
SendDraw(interface{})
SendDrawPrompt()
SendPaging(interface{})
SendQuery(string)
SendStatusMsg(string)
SendStatusMsgAndClear(string, time.Duration)
StatusMsgCh() chan hub.Payload
}

View file

@ -209,7 +209,7 @@ func (p *Peco) Caret() *Caret {
return &p.caret
}
func (p *Peco) Hub() *hub.Hub {
func (p *Peco) Hub() MessageHub {
return p.hub
}

View file

@ -11,11 +11,26 @@ import (
"time"
"github.com/nsf/termbox-go"
"github.com/peco/peco/hub"
"github.com/peco/peco/internal/util"
"github.com/stretchr/testify/assert"
"golang.org/x/net/context"
)
type nullHub struct{}
func (h nullHub) Batch(_ func(), _ bool) {}
func (h nullHub) DrawCh() chan hub.Payload { return nil }
func (h nullHub) PagingCh() chan hub.Payload { return nil }
func (h nullHub) QueryCh() chan hub.Payload { return nil }
func (h nullHub) SendDraw(_ interface{}) {}
func (h nullHub) SendDrawPrompt() {}
func (h nullHub) SendPaging(_ interface{}) {}
func (h nullHub) SendQuery(_ string) {}
func (h nullHub) SendStatusMsg(_ string) {}
func (h nullHub) SendStatusMsgAndClear(_ string, _ time.Duration) {}
func (h nullHub) StatusMsgCh() chan hub.Payload { return nil }
type interceptorArgs []interface{}
type interceptor struct {
m sync.Mutex

View file

@ -41,12 +41,7 @@ func (s *Source) Setup(ctx context.Context, state *Peco) {
defer close(s.setupDone)
draw := func(state *Peco) {
// Not a great thing to do, allowing nil to be passed
// as state, but for testing I couldn't come up with anything
// better for the moment
if state != nil {
state.Hub().SendDraw(nil)
}
state.Hub().SendDraw(nil)
}
go func() {

View file

@ -44,7 +44,7 @@ func TestSource(t *testing.T) {
r := addReadDelay(strings.NewReader(strings.Join(lines, "\n")), 2*time.Second)
s := NewSource(r, ig, 0, false)
go s.Setup(ctx, nil)
go s.Setup(ctx, &Peco{hub: nullHub{}})
timeout := time.After(5 * time.Second)
waitout := time.After(1 * time.Second)