github.com/kolbycrouch/elvish@v0.14.1-0.20210614162631-215b9ac1c423/pkg/cli/mode/instant_test.go (about)

     1  package mode
     2  
     3  import (
     4  	"errors"
     5  	"testing"
     6  
     7  	. "src.elv.sh/pkg/cli/clitest"
     8  	"src.elv.sh/pkg/cli/term"
     9  	"src.elv.sh/pkg/ui"
    10  )
    11  
    12  func setupStartedInstant(t *testing.T) *Fixture {
    13  	f := Setup()
    14  	w, err := NewInstant(f.App, InstantSpec{
    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  	startMode(f.App, w, err)
    24  	f.TestTTY(t,
    25  		term.DotHere, "\n",
    26  		" INSTANT \n", Styles,
    27  		"*********",
    28  		"result of\n",
    29  		"",
    30  	)
    31  	return f
    32  }
    33  
    34  func TestInstant_ShowsResult(t *testing.T) {
    35  	f := setupStartedInstant(t)
    36  	defer f.Stop()
    37  
    38  	f.TTY.Inject(term.K('a'))
    39  	bufA := f.MakeBuffer(
    40  		"a", term.DotHere, "\n",
    41  		" INSTANT \n", Styles,
    42  		"*********",
    43  		"result of\n",
    44  		"a",
    45  	)
    46  	f.TTY.TestBuffer(t, bufA)
    47  
    48  	f.TTY.Inject(term.K(ui.Right))
    49  	f.TTY.TestBuffer(t, bufA)
    50  }
    51  
    52  func TestInstant_ShowsError(t *testing.T) {
    53  	f := setupStartedInstant(t)
    54  	defer f.Stop()
    55  
    56  	f.TTY.Inject(term.K('!'))
    57  	f.TestTTY(t,
    58  		"!", term.DotHere, "\n",
    59  		" INSTANT \n", Styles,
    60  		"*********",
    61  		// Error shown.
    62  		"error\n", Styles,
    63  		"!!!!!",
    64  		// Buffer not updated.
    65  		"result of\n",
    66  		"",
    67  	)
    68  }
    69  
    70  func TestNewInstant_NoExecutor(t *testing.T) {
    71  	f := Setup()
    72  	_, err := NewInstant(f.App, InstantSpec{})
    73  	if err != errExecutorIsRequired {
    74  		t.Error("expect errExecutorIsRequired")
    75  	}
    76  }