github.com/markusbkk/elvish@v0.0.0-20231204143114-91dc52438621/pkg/cli/modes/mode_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/tk"
    10  	"github.com/markusbkk/elvish/pkg/tt"
    11  	"github.com/markusbkk/elvish/pkg/ui"
    12  )
    13  
    14  func TestModeLine(t *testing.T) {
    15  	testModeLine(t, tt.Fn("Line", modeLine))
    16  }
    17  
    18  func TestModePrompt(t *testing.T) {
    19  	testModeLine(t, tt.Fn("Prompt",
    20  		func(s string, b bool) ui.Text { return modePrompt(s, b)() }))
    21  }
    22  
    23  func testModeLine(t *testing.T, fn *tt.FnToTest) {
    24  	tt.Test(t, fn, tt.Table{
    25  		tt.Args("TEST", false).Rets(
    26  			ui.T("TEST", ui.Bold, ui.FgWhite, ui.BgMagenta)),
    27  		tt.Args("TEST", true).Rets(
    28  			ui.Concat(
    29  				ui.T("TEST", ui.Bold, ui.FgWhite, ui.BgMagenta),
    30  				ui.T(" "))),
    31  	})
    32  }
    33  
    34  // Common test utilities.
    35  
    36  var errMock = errors.New("mock error")
    37  
    38  var withNonCodeAreaAddon = clitest.WithSpec(func(spec *cli.AppSpec) {
    39  	spec.State.Addons = []tk.Widget{tk.Label{}}
    40  })
    41  
    42  func startMode(app cli.App, w tk.Widget, err error) {
    43  	if w != nil {
    44  		app.PushAddon(w)
    45  		app.Redraw()
    46  	}
    47  	if err != nil {
    48  		app.Notify(ErrorText(err))
    49  	}
    50  }
    51  
    52  func testFocusedWidgetNotCodeArea(t *testing.T, fn func(cli.App) error) {
    53  	t.Helper()
    54  
    55  	f := clitest.Setup(withNonCodeAreaAddon)
    56  	defer f.Stop()
    57  	if err := fn(f.App); err != ErrFocusedWidgetNotCodeArea {
    58  		t.Errorf("should return ErrFocusedWidgetNotCodeArea, got %v", err)
    59  	}
    60  }