File: instant_test.go

package info (click to toggle)
elvish 0.21.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 6,372 kB
  • sloc: javascript: 236; sh: 130; python: 104; makefile: 88; xml: 9
file content (85 lines) | stat: -rw-r--r-- 1,631 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
package modes

import (
	"errors"
	"testing"

	"src.elv.sh/pkg/cli"
	. "src.elv.sh/pkg/cli/clitest"
	"src.elv.sh/pkg/cli/term"
	"src.elv.sh/pkg/ui"
)

func setupStartedInstant(t *testing.T) *Fixture {
	f := Setup()
	w, err := NewInstant(f.App, InstantSpec{
		Execute: func(code string) ([]string, error) {
			var err error
			if code == "!" {
				err = errors.New("error")
			}
			return []string{"result of", code}, err
		},
	})
	startMode(f.App, w, err)
	f.TestTTY(t,
		term.DotHere, "\n",
		" INSTANT \n", Styles,
		"*********",
		"result of\n",
		"",
	)
	return f
}

func TestInstant_ShowsResult(t *testing.T) {
	f := setupStartedInstant(t)
	defer f.Stop()

	f.TTY.Inject(term.K('a'))
	bufA := f.MakeBuffer(
		"a", term.DotHere, "\n",
		" INSTANT \n", Styles,
		"*********",
		"result of\n",
		"a",
	)
	f.TTY.TestBuffer(t, bufA)

	f.TTY.Inject(term.K(ui.Right))
	f.TTY.TestBuffer(t, bufA)
}

func TestInstant_ShowsError(t *testing.T) {
	f := setupStartedInstant(t)
	defer f.Stop()

	f.TTY.Inject(term.K('!'))
	f.TestTTY(t,
		"!", term.DotHere, "\n",
		" INSTANT \n", Styles,
		"*********",
		// Error shown.
		"error\n", Styles,
		"!!!!!",
		// Buffer not updated.
		"result of\n",
		"",
	)
}

func TestNewInstant_NoExecutor(t *testing.T) {
	f := Setup()
	_, err := NewInstant(f.App, InstantSpec{})
	if err != errExecutorIsRequired {
		t.Error("expect errExecutorIsRequired")
	}
}

func TestNewInstant_FocusedWidgetNotCodeArea(t *testing.T) {
	testFocusedWidgetNotCodeArea(t, func(app cli.App) error {
		_, err := NewInstant(app, InstantSpec{
			Execute: func(string) ([]string, error) { return nil, nil }})
		return err
	})
}