gitstack

grindlemire/go-tui code browser

4.8 KB Go 213 lines 2026-06-12 · 56e8c04 raw
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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
package tui

import "testing"

func TestKey_KeyPattern(t *testing.T) {
	type tc struct {
		key  Key
		want KeyPattern
	}

	tests := map[string]tc{
		"bare key excludes all modifiers": {
			key:  KeyEnter,
			want: KeyPattern{Key: KeyEnter, ExcludeMods: ModCtrl | ModAlt | ModShift},
		},
		"escape excludes all modifiers": {
			key:  KeyEscape,
			want: KeyPattern{Key: KeyEscape, ExcludeMods: ModCtrl | ModAlt | ModShift},
		},
		"function key excludes all modifiers": {
			key:  KeyF5,
			want: KeyPattern{Key: KeyF5, ExcludeMods: ModCtrl | ModAlt | ModShift},
		},
	}

	for name, tt := range tests {
		t.Run(name, func(t *testing.T) {
			got := tt.key.keyPattern()
			if got != tt.want {
				t.Errorf("Key(%v).keyPattern() = %+v, want %+v", tt.key, got, tt.want)
			}
		})
	}
}

func TestKey_ModifierHelpers(t *testing.T) {
	type tc struct {
		spec KeySpec
		want KeyPattern
	}

	tests := map[string]tc{
		"Key.Ctrl requires ctrl": {
			spec: KeyUp.Ctrl(),
			want: KeyPattern{Key: KeyUp, Mod: ModCtrl},
		},
		"Key.Alt requires alt": {
			spec: KeyLeft.Alt(),
			want: KeyPattern{Key: KeyLeft, Mod: ModAlt},
		},
		"Key.Shift requires shift": {
			spec: KeyTab.Shift(),
			want: KeyPattern{Key: KeyTab, Mod: ModShift},
		},
	}

	for name, tt := range tests {
		t.Run(name, func(t *testing.T) {
			got := tt.spec.keyPattern()
			if got != tt.want {
				t.Errorf("keyPattern() = %+v, want %+v", got, tt.want)
			}
		})
	}
}

func TestKeySpec_ModifiersAccumulate(t *testing.T) {
	type tc struct {
		spec KeySpec
		want KeyPattern
	}

	tests := map[string]tc{
		"ctrl then alt": {
			spec: KeyUp.Ctrl().Alt(),
			want: KeyPattern{Key: KeyUp, Mod: ModCtrl | ModAlt},
		},
		"ctrl then shift": {
			spec: KeyEnter.Ctrl().Shift(),
			want: KeyPattern{Key: KeyEnter, Mod: ModCtrl | ModShift},
		},
		"alt then shift": {
			spec: KeyDown.Alt().Shift(),
			want: KeyPattern{Key: KeyDown, Mod: ModAlt | ModShift},
		},
		"all three modifiers": {
			spec: KeyRight.Ctrl().Alt().Shift(),
			want: KeyPattern{Key: KeyRight, Mod: ModCtrl | ModAlt | ModShift},
		},
		"repeated modifier is idempotent": {
			spec: KeyHome.Ctrl().Ctrl(),
			want: KeyPattern{Key: KeyHome, Mod: ModCtrl},
		},
		"zero-modifier spec excludes all modifiers": {
			spec: KeySpec{key: KeyEnd},
			want: KeyPattern{Key: KeyEnd, ExcludeMods: ModCtrl | ModAlt | ModShift},
		},
	}

	for name, tt := range tests {
		t.Run(name, func(t *testing.T) {
			got := tt.spec.keyPattern()
			if got != tt.want {
				t.Errorf("keyPattern() = %+v, want %+v", got, tt.want)
			}
		})
	}
}

func TestRuneSpec_KeyPattern(t *testing.T) {
	type tc struct {
		spec RuneSpec
		want KeyPattern
	}

	tests := map[string]tc{
		"bare rune excludes ctrl and alt but allows shift": {
			spec: Rune('a'),
			want: KeyPattern{Rune: 'a', ExcludeMods: ModCtrl | ModAlt},
		},
		"unicode rune": {
			spec: Rune('日'),
			want: KeyPattern{Rune: '日', ExcludeMods: ModCtrl | ModAlt},
		},
		"ctrl modifier": {
			spec: Rune('c').Ctrl(),
			want: KeyPattern{Rune: 'c', Mod: ModCtrl},
		},
		"alt modifier": {
			spec: Rune('x').Alt(),
			want: KeyPattern{Rune: 'x', Mod: ModAlt},
		},
		"shift modifier": {
			spec: Rune('s').Shift(),
			want: KeyPattern{Rune: 's', Mod: ModShift},
		},
		"ctrl and alt accumulate": {
			spec: Rune('k').Ctrl().Alt(),
			want: KeyPattern{Rune: 'k', Mod: ModCtrl | ModAlt},
		},
		"all three modifiers accumulate": {
			spec: Rune('z').Ctrl().Alt().Shift(),
			want: KeyPattern{Rune: 'z', Mod: ModCtrl | ModAlt | ModShift},
		},
	}

	for name, tt := range tests {
		t.Run(name, func(t *testing.T) {
			got := tt.spec.keyPattern()
			if got != tt.want {
				t.Errorf("keyPattern() = %+v, want %+v", got, tt.want)
			}
		})
	}
}

func TestCtrlLetterHelpers(t *testing.T) {
	type tc struct {
		spec RuneSpec
		want KeyPattern
	}

	tests := map[string]tc{
		"KeyCtrlA": {
			spec: KeyCtrlA,
			want: KeyPattern{Rune: 'a', Mod: ModCtrl},
		},
		"KeyCtrlZ": {
			spec: KeyCtrlZ,
			want: KeyPattern{Rune: 'z', Mod: ModCtrl},
		},
		"KeyCtrlSpace": {
			spec: KeyCtrlSpace,
			want: KeyPattern{Rune: ' ', Mod: ModCtrl},
		},
	}

	for name, tt := range tests {
		t.Run(name, func(t *testing.T) {
			got := tt.spec.keyPattern()
			if got != tt.want {
				t.Errorf("keyPattern() = %+v, want %+v", got, tt.want)
			}
		})
	}
}

func TestAnyMatchers_KeyPattern(t *testing.T) {
	type tc struct {
		matcher KeyMatcher
		want    KeyPattern
	}

	tests := map[string]tc{
		"AnyRune matches printables but excludes ctrl and alt": {
			matcher: AnyRune,
			want:    KeyPattern{AnyRune: true, ExcludeMods: ModCtrl | ModAlt},
		},
		"AnyKey matches everything": {
			matcher: AnyKey,
			want:    KeyPattern{AnyKey: true},
		},
	}

	for name, tt := range tests {
		t.Run(name, func(t *testing.T) {
			got := tt.matcher.keyPattern()
			if got != tt.want {
				t.Errorf("keyPattern() = %+v, want %+v", got, tt.want)
			}
		})
	}
}