okami.mom/ultraviolet

git clone https://git.nkp.pet/okami.mom/ultraviolet

Ayman Bagabasrefactor: rename WindowPixelSizeEvent to PixelSizeEventd69fee1

upstream
9.9 KiB425 linesraw
1package uv
2
3import (
4	"image"
5	"image/color"
6	"testing"
7
8	"github.com/charmbracelet/x/ansi"
9	"github.com/charmbracelet/x/ansi/kitty"
10)
11
12// TestUnknownEventString tests String methods for all Unknown event types
13func TestUnknownEventString(t *testing.T) {
14	tests := []struct {
15		name  string
16		event interface{ String() string }
17		want  string
18	}{
19		{
20			name:  "UnknownEvent",
21			event: UnknownEvent("test"),
22			want:  `"test"`,
23		},
24		{
25			name:  "UnknownCsiEvent",
26			event: UnknownCsiEvent("csi"),
27			want:  `"csi"`,
28		},
29		{
30			name:  "UnknownSs3Event",
31			event: UnknownSs3Event("ss3"),
32			want:  `"ss3"`,
33		},
34		{
35			name:  "UnknownOscEvent",
36			event: UnknownOscEvent("osc"),
37			want:  `"osc"`,
38		},
39		{
40			name:  "UnknownDcsEvent",
41			event: UnknownDcsEvent("dcs"),
42			want:  `"dcs"`,
43		},
44		{
45			name:  "UnknownSosEvent",
46			event: UnknownSosEvent("sos"),
47			want:  `"sos"`,
48		},
49		{
50			name:  "UnknownPmEvent",
51			event: UnknownPmEvent("pm"),
52			want:  `"pm"`,
53		},
54		{
55			name:  "UnknownApcEvent",
56			event: UnknownApcEvent("apc"),
57			want:  `"apc"`,
58		},
59	}
60
61	for _, tt := range tests {
62		t.Run(tt.name, func(t *testing.T) {
63			got := tt.event.String()
64			if got != tt.want {
65				t.Errorf("String() = %q, want %q", got, tt.want)
66			}
67		})
68	}
69}
70
71// TestMultiEventString tests the String method for MultiEvent
72func TestMultiEventString(t *testing.T) {
73	events := MultiEvent{
74		KeyPressEvent{Code: 'a'},
75		KeyPressEvent{Code: 'b'},
76		KeyPressEvent{Code: 'c'},
77	}
78
79	got := events.String()
80	want := "a\nb\nc\n"
81	if got != want {
82		t.Errorf("MultiEvent.String() = %q, want %q", got, want)
83	}
84}
85
86// TestSizeBounds tests the Bounds method for Size and related types
87func TestSizeBounds(t *testing.T) {
88	tests := []struct {
89		name string
90		size interface{ Bounds() Rectangle }
91		want Rectangle
92	}{
93		{
94			name: "Size",
95			size: Size{Width: 80, Height: 24},
96			want: Rectangle{
97				Min: image.Point{X: 0, Y: 0},
98				Max: image.Point{X: 80, Y: 24},
99			},
100		},
101		{
102			name: "WindowSizeEvent",
103			size: WindowSizeEvent{Width: 100, Height: 50},
104			want: Rectangle{
105				Min: image.Point{X: 0, Y: 0},
106				Max: image.Point{X: 100, Y: 50},
107			},
108		},
109		{
110			name: "WindowPixelSizeEvent",
111			size: PixelSizeEvent{Width: 1920, Height: 1080},
112			want: Rectangle{
113				Min: image.Point{X: 0, Y: 0},
114				Max: image.Point{X: 1920, Y: 1080},
115			},
116		},
117		{
118			name: "CellSizeEvent",
119			size: CellSizeEvent{Width: 10, Height: 20},
120			want: Rectangle{
121				Min: image.Point{X: 0, Y: 0},
122				Max: image.Point{X: 10, Y: 20},
123			},
124		},
125	}
126
127	for _, tt := range tests {
128		t.Run(tt.name, func(t *testing.T) {
129			got := tt.size.Bounds()
130			if got != tt.want {
131				t.Errorf("Bounds() = %v, want %v", got, tt.want)
132			}
133		})
134	}
135}
136
137// TestKeyPressEventMethods tests all methods of KeyPressEvent
138func TestKeyPressEventMethods(t *testing.T) {
139	k := KeyPressEvent{Code: 'a', Mod: ModCtrl}
140
141	t.Run("MatchString", func(t *testing.T) {
142		if !k.MatchString("ctrl+a") {
143			t.Error("MatchString(\"ctrl+a\") = false, want true")
144		}
145		if k.MatchString("ctrl+b") {
146			t.Error("MatchString(\"ctrl+b\") = true, want false")
147		}
148	})
149
150	t.Run("MatchStrings", func(t *testing.T) {
151		if !k.MatchString("ctrl+b", "ctrl+a", "ctrl+c") {
152			t.Error("MatchStrings() = false, want true")
153		}
154		if k.MatchString("ctrl+b", "ctrl+c") {
155			t.Error("MatchStrings() = true, want false")
156		}
157	})
158
159	t.Run("String", func(t *testing.T) {
160		got := k.String()
161		want := "ctrl+a"
162		if got != want {
163			t.Errorf("String() = %q, want %q", got, want)
164		}
165	})
166
167	t.Run("Keystroke", func(t *testing.T) {
168		got := k.Keystroke()
169		want := "ctrl+a"
170		if got != want {
171			t.Errorf("Keystroke() = %q, want %q", got, want)
172		}
173	})
174
175	t.Run("Key", func(t *testing.T) {
176		got := k.Key()
177		want := Key{Code: 'a', Mod: ModCtrl}
178		if got != want {
179			t.Errorf("Key() = %v, want %v", got, want)
180		}
181	})
182}
183
184// TestKeyReleaseEventMethods tests all methods of KeyReleaseEvent
185func TestKeyReleaseEventMethods(t *testing.T) {
186	k := KeyReleaseEvent{Code: 'b', Mod: ModAlt}
187
188	t.Run("MatchString", func(t *testing.T) {
189		if !k.MatchString("alt+b") {
190			t.Error("MatchString(\"alt+b\") = false, want true")
191		}
192		if k.MatchString("alt+a") {
193			t.Error("MatchString(\"alt+a\") = true, want false")
194		}
195	})
196
197	t.Run("MatchStrings", func(t *testing.T) {
198		if !k.MatchString("alt+a", "alt+b", "alt+c") {
199			t.Error("MatchStrings() = false, want true")
200		}
201		if k.MatchString("alt+a", "alt+c") {
202			t.Error("MatchStrings() = true, want false")
203		}
204	})
205
206	t.Run("String", func(t *testing.T) {
207		got := k.String()
208		want := "alt+b"
209		if got != want {
210			t.Errorf("String() = %q, want %q", got, want)
211		}
212	})
213
214	t.Run("Keystroke", func(t *testing.T) {
215		got := k.Keystroke()
216		want := "alt+b"
217		if got != want {
218			t.Errorf("Keystroke() = %q, want %q", got, want)
219		}
220	})
221
222	t.Run("Key", func(t *testing.T) {
223		got := k.Key()
224		want := Key{Code: 'b', Mod: ModAlt}
225		if got != want {
226			t.Errorf("Key() = %v, want %v", got, want)
227		}
228	})
229}
230
231// TestMouseEventMethods tests all mouse event types and their methods
232func TestMouseEventMethods(t *testing.T) {
233	mouse := Mouse{X: 10, Y: 20, Button: MouseLeft}
234
235	t.Run("MouseClickEvent", func(t *testing.T) {
236		e := MouseClickEvent(mouse)
237		if got := e.String(); got != "left" {
238			t.Errorf("String() = %q, want %q", got, "left")
239		}
240		if got := e.Mouse(); got != mouse {
241			t.Errorf("Mouse() = %v, want %v", got, mouse)
242		}
243	})
244
245	t.Run("MouseReleaseEvent", func(t *testing.T) {
246		e := MouseReleaseEvent(mouse)
247		if got := e.String(); got != "left" {
248			t.Errorf("String() = %q, want %q", got, "left")
249		}
250		if got := e.Mouse(); got != mouse {
251			t.Errorf("Mouse() = %v, want %v", got, mouse)
252		}
253	})
254
255	t.Run("MouseWheelEvent", func(t *testing.T) {
256		e := MouseWheelEvent(Mouse{X: 10, Y: 20, Button: MouseWheelUp})
257		if got := e.String(); got != "wheelup" {
258			t.Errorf("String() = %q, want %q", got, "wheelup")
259		}
260		if got := e.Mouse(); got.Button != MouseWheelUp {
261			t.Errorf("Mouse().Button = %v, want %v", got.Button, MouseWheelUp)
262		}
263	})
264
265	t.Run("MouseMotionEvent", func(t *testing.T) {
266		// Test with button
267		e := MouseMotionEvent(mouse)
268		if got := e.String(); got != "left+motion" {
269			t.Errorf("String() = %q, want %q", got, "left+motion")
270		}
271		if got := e.Mouse(); got != mouse {
272			t.Errorf("Mouse() = %v, want %v", got, mouse)
273		}
274
275		// Test without button
276		e2 := MouseMotionEvent(Mouse{X: 10, Y: 20, Button: 0})
277		if got := e2.String(); got != "motion" {
278			t.Errorf("String() = %q, want %q", got, "motion")
279		}
280	})
281}
282
283// TestKittyEnhancementsEventContains tests the Contains method
284func TestKittyEnhancementsEventContains(t *testing.T) {
285	e := KeyboardEnhancementsEvent{0b111} // Has bits 0, 1, and 2 set
286
287	tests := []struct {
288		name         string
289		enhancements int
290		want         bool
291	}{
292		{
293			name:         "has single bit",
294			enhancements: 0b001,
295			want:         true,
296		},
297		{
298			name:         "has multiple bits",
299			enhancements: 0b011,
300			want:         true,
301		},
302		{
303			name:         "has all bits",
304			enhancements: 0b111,
305			want:         true,
306		},
307		{
308			name:         "missing bit",
309			enhancements: 0b1000,
310			want:         false,
311		},
312		{
313			name:         "partially missing bits",
314			enhancements: 0b1011,
315			want:         false,
316		},
317	}
318
319	for _, tt := range tests {
320		t.Run(tt.name, func(t *testing.T) {
321			got := e.Contains(tt.enhancements)
322			if got != tt.want {
323				t.Errorf("Contains(%b) = %v, want %v", tt.enhancements, got, tt.want)
324			}
325		})
326	}
327}
328
329// TestColorEventMethods tests color event methods
330func TestColorEventMethods(t *testing.T) {
331	redColor := color.RGBA{R: 255, G: 0, B: 0, A: 255}
332	blackColor := color.RGBA{R: 0, G: 0, B: 0, A: 255}
333	whiteColor := color.RGBA{R: 255, G: 255, B: 255, A: 255}
334
335	t.Run("ForegroundColorEvent", func(t *testing.T) {
336		e := ForegroundColorEvent{redColor}
337		if got := e.String(); got != "#ff0000" {
338			t.Errorf("String() = %q, want %q", got, "#ff0000")
339		}
340		if got := e.IsDark(); got != false {
341			t.Errorf("IsDark() = %v, want %v", got, false)
342		}
343
344		e2 := ForegroundColorEvent{blackColor}
345		if got := e2.IsDark(); got != true {
346			t.Errorf("IsDark() = %v, want %v", got, true)
347		}
348	})
349
350	t.Run("BackgroundColorEvent", func(t *testing.T) {
351		e := BackgroundColorEvent{whiteColor}
352		if got := e.String(); got != "#ffffff" {
353			t.Errorf("String() = %q, want %q", got, "#ffffff")
354		}
355		if got := e.IsDark(); got != false {
356			t.Errorf("IsDark() = %v, want %v", got, false)
357		}
358
359		e2 := BackgroundColorEvent{blackColor}
360		if got := e2.IsDark(); got != true {
361			t.Errorf("IsDark() = %v, want %v", got, true)
362		}
363	})
364
365	t.Run("CursorColorEvent", func(t *testing.T) {
366		e := CursorColorEvent{redColor}
367		if got := e.String(); got != "#ff0000" {
368			t.Errorf("String() = %q, want %q", got, "#ff0000")
369		}
370		if got := e.IsDark(); got != false {
371			t.Errorf("IsDark() = %v, want %v", got, false)
372		}
373
374		e2 := CursorColorEvent{blackColor}
375		if got := e2.IsDark(); got != true {
376			t.Errorf("IsDark() = %v, want %v", got, true)
377		}
378	})
379}
380
381// TestClipboardEventString tests the String method for ClipboardEvent
382func TestClipboardEventString(t *testing.T) {
383	e := ClipboardEvent{
384		Content:   "test content",
385		Selection: SystemClipboard,
386	}
387
388	got := e.String()
389	want := "test content"
390	if got != want {
391		t.Errorf("String() = %q, want %q", got, want)
392	}
393}
394
395// TestEventTypes tests that various event types exist and can be created
396func TestEventTypes(t *testing.T) {
397	// Test that these types can be instantiated without panic
398	_ = CursorPositionEvent(image.Point{X: 10, Y: 20})
399	_ = FocusEvent{}
400	_ = BlurEvent{}
401	_ = DarkColorSchemeEvent{}
402	_ = LightColorSchemeEvent{}
403	_ = PasteEvent{"pasted text"}
404	_ = PasteStartEvent{}
405	_ = PasteEndEvent{}
406	_ = TerminalVersionEvent{"1.0.0"}
407	_ = ModifyOtherKeysEvent{1}
408	_ = KittyGraphicsEvent{
409		Options: kitty.Options{},
410		Payload: []byte("test"),
411	}
412	_ = PrimaryDeviceAttributesEvent{1, 2, 3}
413	_ = SecondaryDeviceAttributesEvent{1, 2, 3}
414	_ = TertiaryDeviceAttributesEvent("test")
415	_ = ModeReportEvent{
416		Mode:  ansi.DECMode(1000),
417		Value: ansi.ModeSet,
418	}
419	_ = WindowOpEvent{
420		Op:   1,
421		Args: []int{100, 200},
422	}
423	_ = CapabilityEvent{"RGB"}
424	_ = ignoredEvent("ignored")
425}