github.com/markusbkk/elvish@v0.0.0-20231204143114-91dc52438621/pkg/cli/modes/instant_test.go (about)

     1  package modes
     2  
     3  import (
     4  	"errors"
     5  	"testing"
     6  
     7  	"github.com/markusbkk/elvish/pkg/cli"
     8  	. "github.com/markusbkk/elvish/pkg/cli/clitest"
     9  	"github.com/markusbkk/elvish/pkg/cli/term"
    10  	"github.com/markusbkk/elvish/pkg/ui"
    11  )
    12  
    13  func setupStartedInstant(t *testing.T) *Fixture {
    14  	f := Setup()
    15  	w, err := NewInstant(f.App, InstantSpec{
    16  		Execute: func(code string) ([]string, error) {
    17  			var err error
    18  			if code == "!" {
    19  				err = errors.New("error")
    20  			}
    21  			return []string{"result of", code}, err
    22  		},
    23  	})
    24  	startMode(f.App, w, err)
    25  	f.TestTTY(t,
    26  		term.DotHere, "\n",
    27  		" INSTANT \n", Styles,
    28  		"*********",
    29  		"result of\n",
    30  		"",
    31  	)
    32  	return f
    33  }
    34  
    35  func TestInstant_ShowsResult(t *testing.T) {
    36  	f := setupStartedInstant(t)
    37  	defer f.Stop()
    38  
    39  	f.TTY.Inject(term.K('a'))
    40  	bufA := f.MakeBuffer(
    41  		"a", term.DotHere, "\n",
    42  		" INSTANT \n", Styles,
    43  		"*********",
    44  		"result of\n",
    45  		"a",
    46  	)
    47  	f.TTY.TestBuffer(t, bufA)
    48  
    49  	f.TTY.Inject(term.K(ui.Right))
    50  	f.TTY.TestBuffer(t, bufA)
    51  }
    52  
    53  func TestInstant_ShowsError(t *testing.T) {
    54  	f := setupStartedInstant(t)
    55  	defer f.Stop()
    56  
    57  	f.TTY.Inject(term.K('!'))
    58  	f.TestTTY(t,
    59  		"!", term.DotHere, "\n",
    60  		" INSTANT \n", Styles,
    61  		"*********",
    62  		// Error shown.
    63  		"error\n", Styles,
    64  		"!!!!!",
    65  		// Buffer not updated.
    66  		"result of\n",
    67  		"",
    68  	)
    69  }
    70  
    71  func TestNewInstant_NoExecutor(t *testing.T) {
    72  	f := Setup()
    73  	_, err := NewInstant(f.App, InstantSpec{})
    74  	if err != errExecutorIsRequired {
    75  		t.Error("expect errExecutorIsRequired")
    76  	}
    77  }
    78  
    79  func TestNewInstant_FocusedWidgetNotCodeArea(t *testing.T) {
    80  	testFocusedWidgetNotCodeArea(t, func(app cli.App) error {
    81  		_, err := NewInstant(app, InstantSpec{
    82  			Execute: func(string) ([]string, error) { return nil, nil }})
    83  		return err
    84  	})
    85  }