github.com/elves/elvish@v0.15.0/pkg/cli/addons/instant/instant_test.go (about) 1 package instant 2 3 import ( 4 "errors" 5 "testing" 6 7 . "github.com/elves/elvish/pkg/cli/clitest" 8 "github.com/elves/elvish/pkg/cli/term" 9 "github.com/elves/elvish/pkg/ui" 10 ) 11 12 func setupStarted(t *testing.T) *Fixture { 13 f := Setup() 14 Start(f.App, Config{ 15 Execute: func(code string) ([]string, error) { 16 var err error 17 if code == "!" { 18 err = errors.New("error") 19 } 20 return []string{"result of", code}, err 21 }, 22 }) 23 f.TestTTY(t, 24 term.DotHere, "\n", 25 " INSTANT \n", Styles, 26 "*********", 27 "result of\n", 28 "", 29 ) 30 return f 31 } 32 33 func TestUpdate(t *testing.T) { 34 f := setupStarted(t) 35 defer f.Stop() 36 37 f.TTY.Inject(term.K('a')) 38 bufA := f.MakeBuffer( 39 "a", term.DotHere, "\n", 40 " INSTANT \n", Styles, 41 "*********", 42 "result of\n", 43 "a", 44 ) 45 f.TTY.TestBuffer(t, bufA) 46 47 f.TTY.Inject(term.K(ui.Right)) 48 f.TTY.TestBuffer(t, bufA) 49 } 50 51 func TestError(t *testing.T) { 52 f := setupStarted(t) 53 defer f.Stop() 54 55 f.TTY.Inject(term.K('!')) 56 f.TestTTY(t, 57 "!", term.DotHere, "\n", 58 " INSTANT \n", Styles, 59 "*********", 60 // Error shown. 61 "error\n", Styles, 62 "!!!!!", 63 // Buffer not updated. 64 "result of\n", 65 "", 66 ) 67 } 68 69 func TestStart_NoExecutor(t *testing.T) { 70 f := Setup() 71 Start(f.App, Config{}) 72 f.TestTTYNotes(t, "executor is required") 73 }