-
-
Notifications
You must be signed in to change notification settings - Fork 17
/
messages_test.go
117 lines (97 loc) · 2.25 KB
/
messages_test.go
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
// 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 "github.com/charmbracelet/bubbletea"
)
type testModel struct {
received []tea.Msg
}
func newTestModel() *testModel {
return &testModel{}
}
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)
}
return m, nil
}
func (m *testModel) View() string {
// Starts at X:4, Y:2, ends at X:12, Y:3.
return "test\nfoo\naaa " + Mark("foo", "bar\ntest123456789") + " aaa\nbaz"
}
func TestAnyInBounds(t *testing.T) {
m := newTestModel()
_ = Scan(m.View())
time.Sleep(100 * time.Millisecond)
xy := Get("foo")
if xy.IsZero() {
t.Error("id not found")
}
_, _ = m.Update(tea.MouseMsg{X: 4, Y: 2})
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")
}
}
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() string {
// Starts at X:4, Y:2, ends at X:12, Y:3.
return "test\nfoo\naaa " + Mark("foo", "bar\ntest123456789") + " aaa\nbaz"
}
func TestAnyInBoundsAndUpdate(t *testing.T) {
var m tea.Model = testModelValue{}
_ = Scan(m.View())
time.Sleep(100 * time.Millisecond)
xy := Get("foo")
if xy.IsZero() {
t.Error("id not found")
}
m, _ = m.Update(tea.MouseMsg{X: 4, Y: 2})
time.Sleep(100 * time.Millisecond)
var contains bool
for _, msg := range m.(testModelValue).received {
if evt, ok := msg.(MsgZoneInBounds); ok {
if evt.Zone.id == xy.id {
contains = true
break
}
}
}
if !contains {
t.Error("expected true")
}
}