File: completion_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 (82 lines) | stat: -rw-r--r-- 1,769 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
package modes

import (
	"testing"

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

func TestCompletion_Filter(t *testing.T) {
	f := setupStartedCompletion(t)
	defer f.Stop()

	f.TTY.Inject(term.K('b'), term.K('a'))
	f.TestTTY(t,
		"'foo bar'\n", Styles,
		"_________",
		" COMPLETING WORD  ba", Styles,
		"*****************   ", term.DotHere, "\n",
		"foo bar", Styles,
		"#######",
	)
}

func TestCompletion_Accept(t *testing.T) {
	f := setupStartedCompletion(t)
	defer f.Stop()

	f.TTY.Inject(term.K(ui.Enter))
	f.TestTTY(t, "foo", term.DotHere)
}

func TestCompletion_Dismiss(t *testing.T) {
	f := setupStartedCompletion(t)
	defer f.Stop()

	f.App.PopAddon()
	f.App.Redraw()
	f.TestTTY(t /* nothing */)
}

func TestNewCompletion_NoItems(t *testing.T) {
	f := Setup()
	defer f.Stop()
	_, err := NewCompletion(f.App, CompletionSpec{Items: []CompletionItem{}})
	if err != errNoCandidates {
		t.Errorf("should return errNoCandidates")
	}
}

func TestNewCompletion_FocusedWidgetNotCodeArea(t *testing.T) {
	testFocusedWidgetNotCodeArea(t, func(app cli.App) error {
		_, err := NewCompletion(app, CompletionSpec{Items: []CompletionItem{{}}})
		return err
	})
}

func setupStartedCompletion(t *testing.T) *Fixture {
	f := Setup()
	w, _ := NewCompletion(f.App, CompletionSpec{
		Name:    "WORD",
		Replace: diag.Ranging{From: 0, To: 0},
		Items: []CompletionItem{
			{ToShow: ui.T("foo"), ToInsert: "foo"},
			{ToShow: ui.T("foo bar", ui.FgBlue), ToInsert: "'foo bar'"},
		},
	})
	f.App.PushAddon(w)
	f.App.Redraw()
	f.TestTTY(t,
		"foo\n", Styles,
		"___",
		" COMPLETING WORD  ", Styles,
		"***************** ", term.DotHere, "\n",
		"foo  foo bar", Styles,
		"+++  ///////",
	)
	return f
}