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
|
package tui
import "testing"
func TestClick_Type(t *testing.T) {
ref := NewRef()
called := false
c := Click(ref, func() { called = true })
if c.Ref != ref {
t.Fatal("ref not set")
}
c.Fn()
if !called {
t.Fatal("fn not called")
}
}
func TestHandleClicks_Hit(t *testing.T) {
ref := NewRef()
called := false
el := New(WithWidth(10), WithHeight(5))
el.layout.Rect = NewRect(0, 0, 10, 5)
ref.Set(el)
handled := HandleClicks(
MouseEvent{Button: MouseLeft, Action: MousePress, X: 5, Y: 2},
Click(ref, func() { called = true }),
)
if !handled {
t.Fatal("expected click to be handled")
}
if !called {
t.Fatal("handler not called")
}
}
func TestHandleClicks_Miss(t *testing.T) {
ref := NewRef()
called := false
el := New(WithWidth(10), WithHeight(5))
el.layout.Rect = NewRect(0, 0, 10, 5)
ref.Set(el)
handled := HandleClicks(
MouseEvent{Button: MouseLeft, Action: MousePress, X: 15, Y: 10},
Click(ref, func() { called = true }),
)
if handled {
t.Fatal("expected click to not be handled")
}
if called {
t.Fatal("handler should not be called")
}
}
func TestHandleClicks_NilRef(t *testing.T) {
ref := NewRef()
handled := HandleClicks(
MouseEvent{Button: MouseLeft, Action: MousePress, X: 5, Y: 2},
Click(ref, func() { t.Fatal("should not be called") }),
)
if handled {
t.Fatal("expected nil ref to not handle click")
}
}
func TestHandleClicks_NilRefPointer(t *testing.T) {
me := MouseEvent{Button: MouseLeft, Action: MousePress, X: 5, Y: 5}
result := HandleClicks(me, Click(nil, func() {
t.Error("handler should not be called for nil ref")
}))
if result {
t.Error("HandleClicks should return false for nil ref binding")
}
}
func TestHandleClicks_MultipleBindings(t *testing.T) {
ref1 := NewRef()
ref2 := NewRef()
var clicked string
el1 := New(WithWidth(10), WithHeight(5))
el1.layout.Rect = NewRect(0, 0, 10, 5)
ref1.Set(el1)
el2 := New(WithWidth(10), WithHeight(5))
el2.layout.Rect = NewRect(20, 0, 10, 5)
ref2.Set(el2)
handled := HandleClicks(
MouseEvent{Button: MouseLeft, Action: MousePress, X: 25, Y: 2},
Click(ref1, func() { clicked = "first" }),
Click(ref2, func() { clicked = "second" }),
)
if !handled {
t.Fatal("expected click to be handled")
}
if clicked != "second" {
t.Fatalf("expected 'second', got '%s'", clicked)
}
}
func TestHandleClicks_IgnoresNonLeftClick(t *testing.T) {
ref := NewRef()
el := New(WithWidth(10), WithHeight(5))
el.layout.Rect = NewRect(0, 0, 10, 5)
ref.Set(el)
handled := HandleClicks(
MouseEvent{Button: MouseRight, Action: MousePress, X: 5, Y: 2},
Click(ref, func() { t.Fatal("should not be called") }),
)
if handled {
t.Fatal("expected right click to not be handled")
}
}
func TestHandleClicks_IgnoresRelease(t *testing.T) {
ref := NewRef()
el := New(WithWidth(10), WithHeight(5))
el.layout.Rect = NewRect(0, 0, 10, 5)
ref.Set(el)
handled := HandleClicks(
MouseEvent{Button: MouseLeft, Action: MouseRelease, X: 5, Y: 2},
Click(ref, func() { t.Fatal("should not be called") }),
)
if handled {
t.Fatal("expected release to not be handled")
}
}
|