-
-
Notifications
You must be signed in to change notification settings - Fork 27
Expand file tree
/
Copy pathmessages_test.go
More file actions
129 lines (105 loc) · 2.51 KB
/
messages_test.go
File metadata and controls
129 lines (105 loc) · 2.51 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
// Copyright (c) Liam Stanley <[email protected]>. All rights reserved. Use of
// this source code is governed by the MIT license that can be found in
// the LICENSE file.
package zone
import (
"testing"
"time"
tea "charm.land/bubbletea/v2"
)
var _ tea.Model = (*testModel)(nil)
type testModel struct {
received []tea.Msg
done chan struct{} // closed when MsgZoneInBounds has been received (for test sync)
}
func newTestModel() *testModel {
return &testModel{
done: make(chan struct{}),
}
}
func (m *testModel) Init() tea.Cmd {
return nil
}
func (m *testModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
switch msg := msg.(type) {
case tea.MouseMsg:
go AnyInBounds(m, msg)
return m, nil
case MsgZoneInBounds:
m.received = append(m.received, msg)
select {
case m.done <- struct{}{}:
default:
}
}
return m, nil
}
func (m *testModel) View() tea.View {
return tea.NewView(Scan("test\nfoo\naaa " + Mark("foo", "bar\ntest123456789") + " aaa\nbaz"))
}
func TestAnyInBounds(t *testing.T) {
m := newTestModel()
_ = m.View()
time.Sleep(100 * time.Millisecond)
xy := Get("foo")
if xy.IsZero() {
t.Error("id not found")
}
_, _ = m.Update(tea.MouseMotionMsg{X: 4, Y: 2})
<-m.done // wait for async AnyInBounds to deliver MsgZoneInBounds before reading m.received
var contains bool
for _, msg := range m.received {
if evt, ok := msg.(MsgZoneInBounds); ok {
if evt.Zone.id == xy.id {
contains = true
break
}
}
}
if !contains {
t.Error("expected true")
}
}
var _ tea.Model = (*testModelValue)(nil)
type testModelValue struct {
received []tea.Msg
}
func (m testModelValue) Init() tea.Cmd {
return nil
}
func (m testModelValue) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
switch msg := msg.(type) {
case tea.MouseMsg:
return AnyInBoundsAndUpdate(m, msg)
case MsgZoneInBounds:
m.received = append(m.received, msg)
}
return m, nil
}
func (m testModelValue) View() tea.View {
return tea.NewView(Scan("test\nfoo\naaa " + Mark("foo", "bar\ntest123456789") + " aaa\nbaz"))
}
func TestAnyInBoundsAndUpdate(t *testing.T) {
m := testModelValue{}
_ = m.View()
time.Sleep(100 * time.Millisecond)
xy := Get("foo")
if xy.IsZero() {
t.Error("id not found")
}
newModel, _ := m.Update(tea.MouseMotionMsg{X: 4, Y: 2})
m = newModel.(testModelValue)
time.Sleep(100 * time.Millisecond)
var contains bool
for _, msg := range m.received {
if evt, ok := msg.(MsgZoneInBounds); ok {
if evt.Zone.id == xy.id {
contains = true
break
}
}
}
if !contains {
t.Error("expected true")
}
}