Repository navigation
Expand file tree
/
Copy pathcommand_test.go
More file actions
131 lines (119 loc) · 3.27 KB
/
Copy pathcommand_test.go
File metadata and controls
131 lines (119 loc) · 3.27 KB
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
package main
import (
"strings"
"testing"
tea "charm.land/bubbletea/v2"
"github.com/stretchr/testify/require"
)
func TestParseCall(t *testing.T) {
tests := []struct {
line string
want call
}{
{"", call{}},
{"5", call{name: "5"}},
{"-2", call{name: "-2"}},
{" 12 ", call{name: "12"}},
{"q", call{name: "q"}},
{"w", call{name: "w"}},
{"w out.json", call{name: "w", arg: "out.json"}},
{"write out.json ", call{name: "write", arg: "out.json"}},
{"w!", call{name: "w", bang: true}},
{"w! out.json", call{name: "w", bang: true, arg: "out.json"}},
{"w!out.json", call{name: "w", bang: true, arg: "out.json"}},
{"w ~/a b.json", call{name: "w", arg: "~/a b.json"}},
{"!", call{bang: true}},
{"w-1", call{name: "w", arg: "-1"}},
}
for _, tt := range tests {
t.Run(tt.line, func(t *testing.T) {
require.Equal(t, tt.want, parseCall(tt.line))
})
}
}
func TestFindCommand(t *testing.T) {
tests := []struct {
name string
want string // name of the matched command, "" for none
}{
{"5", "<n>"},
{"-2", "<n>"},
{"q", "q[uit]"},
{"qu", "q[uit]"},
{"quit", "q[uit]"},
{"quit!", ""},
{"quitting", ""},
{"w", "w[rite]"},
{"wr", "w[rite]"},
{"write", "w[rite]"},
{"wq", "wq"},
{"wqa", ""},
{"W", ""},
{"writes", ""},
{"", ""},
{"invalid", ""},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
c := findCommand(tt.name)
if tt.want == "" {
require.Nil(t, c)
return
}
require.NotNil(t, c)
require.Equal(t, tt.want, c.name)
})
}
}
func TestCommandUsage(t *testing.T) {
require.Equal(t, "<n>", command{name: "<n>"}.usage())
require.Equal(t, "q[uit]", command{name: "q[uit]"}.usage())
require.Equal(t, "w[rite][!] [file]", command{name: "w[rite]", bang: true, args: "[file]"}.usage())
}
func TestCommandUnknownShowsError(t *testing.T) {
m := newQueryModel(t, `{"a": 1}`)
typeKeys(m, ":invalid")
enter(m)
require.NotNil(t, m.message)
require.Equal(t, "Not an editor command: invalid", m.message.text)
require.True(t, m.message.isErr)
require.Equal(t, m.termHeight-2, m.viewHeight())
require.Contains(t, view(m), "Not an editor command: invalid")
// The next key clears the message and is handled as usual.
m.Update(press("down"))
require.Nil(t, m.message)
require.Equal(t, 1, m.cursor)
require.Equal(t, m.termHeight-1, m.viewHeight())
}
func TestCommandUnknownWithoutName(t *testing.T) {
for _, line := range []string{"$", "!", "+5", "%s/a/b/", " $ "} {
m := newQueryModel(t, `{"a": 1}`)
typeKeys(m, ":"+line)
enter(m)
require.NotNil(t, m.message, line)
require.Equal(t, "Not an editor command: "+strings.TrimSpace(line), m.message.text)
}
}
func TestCommandBangNotAllowed(t *testing.T) {
m := newQueryModel(t, `{"a": 1}`)
typeKeys(m, ":5!")
enter(m)
require.NotNil(t, m.message)
require.Equal(t, "No ! allowed: 5!", m.message.text)
}
func TestCommandEmptyDoesNothing(t *testing.T) {
m := newQueryModel(t, `{"a": 1}`)
typeKeys(m, ":")
enter(m)
require.Nil(t, m.message)
require.False(t, m.commandInput.Focused())
}
func TestCommandQuit(t *testing.T) {
for _, line := range []string{"q", "quit", "q!", "quit!"} {
m := newQueryModel(t, `{"a": 1}`)
typeKeys(m, ":"+line)
_, cmd := m.Update(press("enter"))
require.NotNil(t, cmd, line)
require.IsType(t, tea.QuitMsg{}, cmd(), line)
}
}