diff --git a/action_test.go b/action_test.go index 491ad8f..076a97c 100644 --- a/action_test.go +++ b/action_test.go @@ -172,16 +172,10 @@ func expectQueryString(t *testing.T, q *Query, expect string) bool { } func TestDoDeleteForwardChar(t *testing.T) { - state := newPeco() + state, ctx := setupPecoTest(t) q := state.Query() c := state.Caret() - ctx, cancel := context.WithCancel(context.Background()) - go state.Run(ctx) - defer cancel() - - <-state.Ready() - q.Set("Hello, World!") c.SetPos(5) @@ -208,16 +202,10 @@ func TestDoDeleteForwardChar(t *testing.T) { } func TestDoDeleteForwardWord(t *testing.T) { - state := newPeco() + state, ctx := setupPecoTest(t) q := state.Query() c := state.Caret() - ctx, cancel := context.WithCancel(context.Background()) - go state.Run(ctx) - defer cancel() - - <-state.Ready() - q.Set("Hello, World!") c.SetPos(5) @@ -264,16 +252,10 @@ func TestDoDeleteForwardWord(t *testing.T) { } func TestDoDeleteBackwardChar(t *testing.T) { - state := newPeco() + state, ctx := setupPecoTest(t) q := state.Query() c := state.Caret() - ctx, cancel := context.WithCancel(context.Background()) - go state.Run(ctx) - defer cancel() - - <-state.Ready() - q.Set("Hello, World!") c.SetPos(5) @@ -296,16 +278,10 @@ func TestDoDeleteBackwardChar(t *testing.T) { } func TestDoDeleteBackwardWord(t *testing.T) { - state := newPeco() + state, ctx := setupPecoTest(t) q := state.Query() c := state.Caret() - ctx, cancel := context.WithCancel(context.Background()) - go state.Run(ctx) - defer cancel() - - <-state.Ready() - // In case of an overflow (bug) q.Set("foo") c.SetPos(5) @@ -358,13 +334,7 @@ func writeQueryToPrompt(t *testing.T, screen Screen, message string) { } func TestDoAcceptChar(t *testing.T) { - state := newPeco() - - ctx, cancel := context.WithCancel(context.Background()) - go state.Run(ctx) - defer cancel() - - <-state.Ready() + state, _ := setupPecoTest(t) message := "Hello, World!" writeQueryToPrompt(t, state.screen, message) @@ -382,13 +352,7 @@ func TestDoAcceptChar(t *testing.T) { } func TestRotateFilter(t *testing.T) { - state := newPeco() - - ctx, cancel := context.WithCancel(context.Background()) - go state.Run(ctx) - defer cancel() - - <-state.Ready() + state, _ := setupPecoTest(t) size := state.filters.Size() if size <= 1 { @@ -417,13 +381,7 @@ func TestRotateFilter(t *testing.T) { } func TestBeginningOfLineAndEndOfLine(t *testing.T) { - state := newPeco() - - ctx, cancel := context.WithCancel(context.Background()) - go state.Run(ctx) - defer cancel() - - <-state.Ready() + state, _ := setupPecoTest(t) message := "Hello, World!" writeQueryToPrompt(t, state.screen, message) @@ -441,13 +399,7 @@ func TestBeginningOfLineAndEndOfLine(t *testing.T) { } func TestBackToInitialFilter(t *testing.T) { - state := newPeco() - - ctx, cancel := context.WithCancel(context.Background()) - go state.Run(ctx) - defer cancel() - - <-state.Ready() + state, _ := setupPecoTest(t) state.config.Keymap["C-q"] = "peco.BackToInitialFilter" if !assert.NoError(t, state.populateKeymap(), "populateKeymap expected to succeed") { diff --git a/issues_test.go b/issues_test.go index c980106..513735d 100644 --- a/issues_test.go +++ b/issues_test.go @@ -18,12 +18,7 @@ import ( ) func TestIssue212_SanityCheck(t *testing.T) { - state := newPeco() - ctx, cancel := context.WithCancel(context.Background()) - go state.Run(ctx) - defer cancel() - - <-state.Ready() + state, ctx := setupPecoTest(t) // Check if the default layout type is honored */ // This the main issue on 212, but while we're at it, we're just diff --git a/peco_test.go b/peco_test.go index 418f226..6adc378 100644 --- a/peco_test.go +++ b/peco_test.go @@ -95,6 +95,16 @@ func newPeco() *Peco { return state } +func setupPecoTest(t *testing.T) (*Peco, context.Context) { + t.Helper() + state := newPeco() + ctx, cancel := context.WithCancel(context.Background()) + t.Cleanup(cancel) + go state.Run(ctx) + <-state.Ready() + return state, ctx +} + // keyseqToTcellKey maps peco keyseq navigation/function key constants back // to tcell key constants, for injecting events into SimulationScreen. var keyseqToTcellKey = map[keyseq.KeyType]tcell.Key{