-
-
Notifications
You must be signed in to change notification settings - Fork 17
/
manager_test.go
217 lines (189 loc) · 6.73 KB
/
manager_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
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
214
215
216
217
// 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 (
"strings"
"testing"
"time"
"github.com/charmbracelet/lipgloss"
)
func TestMain(m *testing.M) {
NewGlobal()
testsScan = []scanTestCase{
{"empty", "", "", nil},
{"single", "a", "a", nil},
{"double", "aa", "aa", nil},
{"triple", "aaa", "aaa", nil},
{"quad", "aaaa", "aaaa", nil},
{"lipgloss-empty", testStyle.Render(""), testStyle.Render(""), nil},
{"lipgloss-basic", testStyle.Render("testing"), testStyle.Render("testing"), nil},
{"lipgloss-basic-start", "a" + testStyle.Render("testing"), "a" + testStyle.Render("testing"), nil},
{"lipgloss-basic-end", testStyle.Render("testing") + "a", testStyle.Render("testing") + "a", nil},
{"lipgloss-basic-start-end", "a" + testStyle.Render("testing") + "a", "a" + testStyle.Render("testing") + "a", nil},
{"lipgloss-basic-between", testStyle.Render("testing") + "a" + testStyle.Render("testing"), testStyle.Render("testing") + "a" + testStyle.Render("testing"), nil},
{"id-empty", Mark("testing1", ""), "", nil},
{"id-single-start", "a" + Mark("testing2", "a"), "aa", []string{"testing2"}},
{"id-single-end", Mark("testing3", "a") + "a", "aa", []string{"testing3"}},
{"id-single-start-end", "a" + Mark("testing4", "b") + "a", "aba", []string{"testing4"}},
{"id-single-between", Mark("testing5", "b") + "a" + Mark("testing6", "b"), "bab", []string{"testing5", "testing6"}},
{"id-with-lipgloss-start", testStyle.Render(Mark("testing7", "testing") + "testing"), testStyle.Render("testingtesting"), []string{"testing7"}},
{"id-with-lipgloss-end", testStyle.Render("testing" + Mark("testing8", "testing")), testStyle.Render("testingtesting"), []string{"testing8"}},
{"id-multi-empty", Mark("foo1", "") + Mark("bar1", ""), "", nil},
{"id-multi-start", "a" + Mark("foo2", "b") + Mark("bar2", "c"), "abc", []string{"foo2", "bar2"}},
{"id-multi-end", Mark("foo3", "a") + Mark("bar3", "b") + "c", "abc", []string{"foo3", "bar3"}},
{"id-multi-start-end", "a" + Mark("foo4", "b") + Mark("bar4", "c") + "d", "abcd", []string{"foo4", "bar4"}},
{"inception", Mark("foo", Mark("bar", "b")), "b", []string{"foo", "bar"}},
{"long-x1", "a" + Mark("longtest5", longStyle.Render("testing")) + "a", "a" + longStyle.Render("testing") + "a", []string{"longtest5"}},
{"long-x2", strings.Repeat("a"+Mark("longtest", longStyle.Render("testing"))+"a", 1), strings.Repeat("a"+longStyle.Render("testing")+"a", 1), []string{"longtest"}},
{"long-x4", strings.Repeat("a"+Mark("longtest", longStyle.Render("testing"))+"a", 4), strings.Repeat("a"+longStyle.Render("testing")+"a", 4), []string{"longtest"}},
{"long-x6", strings.Repeat("a"+Mark("longtest", longStyle.Render("testing"))+"a", 6), strings.Repeat("a"+longStyle.Render("testing")+"a", 6), []string{"longtest"}},
{"long-x8", strings.Repeat("a"+Mark("longtest", longStyle.Render("testing"))+"a", 8), strings.Repeat("a"+longStyle.Render("testing")+"a", 8), []string{"longtest"}},
{"long-x10", strings.Repeat("a"+Mark("longtest", longStyle.Render("testing"))+"a", 10), strings.Repeat("a"+longStyle.Render("testing")+"a", 10), []string{"longtest"}},
{"invalid-no-bracket", "a\x1B12345Zb", "a\x1B12345Zb", nil},
{"invalid-no-bracket-end", "a\x1B", "a\x1B", nil},
{"invalid-no-numbers", "a\x1BZb", "a\x1BZb", nil},
{"invalid-no-numbers-end", "a\x1BZ", "a\x1BZ", nil},
{"invalid-marker-end", "a\x1B12345b", "a\x1B12345b", nil},
{"invalid-marker-end-2", "a\x1B12345", "a\x1B12345", nil},
{"invalid-run-of-numbers", "a\x1B12345b6Z", "a\x1B12345b6Z", nil},
{"invalid-misc", "\x1Ba\x1B\x1B\x1B12345b6Z\x1B", "\x1Ba\x1B\x1B\x1B12345b6Z\x1B", nil},
}
m.Run()
Close()
}
type scanTestCase struct {
name string
in string
want string
ids []string
}
var (
testStyle = lipgloss.NewStyle().
Foreground(lipgloss.Color("#FFFFFF")).
Background(lipgloss.Color("#383838")).
Bold(true).
Italic(true).
Blink(true)
longStyle = lipgloss.NewStyle().
Foreground(lipgloss.Color("#FFFFFF")).
Background(lipgloss.Color("#383838")).
Bold(true).
Italic(true).
Blink(true).
Underline(true).
Border(lipgloss.RoundedBorder()).
BorderForeground(lipgloss.Color("#F12356")).
BorderBackground(lipgloss.Color("#459082")).
Padding(5, 4)
testsScan []scanTestCase
)
func BenchmarkScan(b *testing.B) {
for _, test := range testsScan {
b.Run(test.name, func(b *testing.B) {
for i := 0; i < b.N; i++ {
_ = Scan(test.in)
}
})
}
}
func TestScan(t *testing.T) {
for _, test := range testsScan {
t.Run(test.name, func(t *testing.T) {
got := Scan(test.in)
if got != test.want {
t.Errorf("got %q, want %q", got, test.want)
}
if len(test.ids) > 0 {
time.Sleep(15 * time.Millisecond)
for _, id := range test.ids {
if xy := Get(id); xy.IsZero() {
t.Errorf("id %q not found", id)
}
}
}
})
}
}
func FuzzScan(f *testing.F) {
for _, test := range testsScan {
f.Add(test.in)
f.Add(test.want)
}
f.Fuzz(func(_ *testing.T, a string) {
_ = Scan(a)
})
}
func TestScanDisabled(t *testing.T) {
zm := New()
defer zm.Close()
zm.SetEnabled(false)
for _, test := range testsScan {
t.Run(test.name, func(t *testing.T) {
got := zm.Scan(test.in)
if got != test.want {
t.Errorf("got %q, want %q", got, test.want)
}
})
}
}
func TestMark(_ *testing.T) {
var out string
for _, test := range testsScan[0:10] {
got := Mark(test.name, test.in)
out += got
}
_ = Scan(out)
time.Sleep(100 * time.Millisecond)
for _, test := range testsScan[0:10] {
_ = Get(test.name)
}
}
func BenchmarkMark(b *testing.B) {
for _, test := range testsScan {
b.Run(test.name, func(b *testing.B) {
for i := 0; i < b.N; i++ {
_ = Mark(test.name, test.in)
}
})
}
}
func TestMarkDisabled(t *testing.T) {
zm := New()
defer zm.Close()
zm.SetEnabled(false)
for _, test := range testsScan[0:10] {
if got := zm.Mark(test.name, test.in); got != test.in {
t.Errorf("got %q, want %q", got, test.in)
}
}
}
func TestWorkerClear(t *testing.T) {
_ = Scan("a" + Mark("foo", "b") + "c")
_ = Scan("a" + Mark("bar", "b") + "c")
time.Sleep(100 * time.Millisecond)
if xy := Get("foo"); !xy.IsZero() {
t.Errorf("%#v not cleared (after %#v)", xy, Get("bar"))
}
}
func TestClear(t *testing.T) {
_ = Scan("a" + Mark("foo", "b") + "c")
Clear("foo")
if xy := Get("foo"); !xy.IsZero() {
t.Errorf("%#v not cleared", xy)
}
}
func TestClose(t *testing.T) {
mgr := New()
mgr.Close()
time.Sleep(100 * time.Millisecond)
_ = mgr.Scan("a" + Mark("foo", "b") + "c")
time.Sleep(100 * time.Millisecond)
if xy := mgr.Get("foo"); !xy.IsZero() {
t.Errorf("%#v fetched, but closed", xy)
}
}
func TestGlobalInitialize(_ *testing.T) {
NewGlobal()
NewGlobal()
}