-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
registry_test.go
246 lines (204 loc) · 5.77 KB
/
registry_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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
// 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 tint
import (
"reflect"
"testing"
"github.com/charmbracelet/lipgloss"
)
func TestNewRegistry(t *testing.T) {
type args struct {
defaultTint Tint
tints []Tint
}
tests := []struct {
name string
args args
want *Registry
}{
{
name: "no default",
args: args{defaultTint: nil, tints: []Tint{TintDraculaPlus}},
want: &Registry{
currentTint: "",
tints: map[string]Tint{
TintDraculaPlus.ID(): TintDraculaPlus,
},
},
},
{
name: "no default and no tints",
args: args{defaultTint: nil, tints: nil},
want: &Registry{
currentTint: "",
tints: map[string]Tint{},
},
},
{
name: "with default and tints",
args: args{defaultTint: Tint3024Day, tints: []Tint{TintDraculaPlus}},
want: &Registry{
currentTint: Tint3024Day.ID(),
tints: map[string]Tint{
TintDraculaPlus.ID(): TintDraculaPlus,
Tint3024Day.ID(): Tint3024Day,
},
},
},
{
name: "with default and no tints",
args: args{defaultTint: Tint3024Day, tints: nil},
want: &Registry{
currentTint: Tint3024Day.ID(),
tints: map[string]Tint{
Tint3024Day.ID(): Tint3024Day,
},
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := NewRegistry(tt.args.defaultTint, tt.args.tints...); !reflect.DeepEqual(got, tt.want) {
t.Errorf("NewRegistry() = %v, want %v", got, tt.want)
}
})
}
}
func TestRegistry_Unregister(t *testing.T) {
reg := NewRegistry(nil, Tint3024Day, TintDraculaPlus)
if len(reg.Tints()) != 2 {
t.Errorf("expected 2 tints, got %d", len(reg.Tints()))
}
reg.Unregister(Tint3024Day)
if len(reg.Tints()) != 1 {
t.Errorf("expected 1 tint, got %d", len(reg.Tints()))
}
if other := reg.Tints()[0]; other != TintDraculaPlus {
t.Errorf("expected %v, got %v", TintDraculaPlus, other)
}
reg.Register(Tint3024Day)
if len(reg.Tints()) != 2 {
t.Errorf("expected 2 tints, got %d", len(reg.Tints()))
}
reg.UnregisterIDs(TintDraculaPlus.ID())
if len(reg.Tints()) != 1 {
t.Errorf("expected 1 tint, got %d", len(reg.Tints()))
}
if other := reg.Tints()[0]; other.ID() != Tint3024Day.ID() {
t.Errorf("expected %v, got %v", Tint3024Day, other)
}
reg = NewRegistry(nil, Tint3024Day, TintDraculaPlus)
reg.UnregisterAll()
if len(reg.Tints()) != 0 {
t.Errorf("expected 0 tints, got %d", len(reg.Tints()))
}
}
func TestRegistry_SetTint(t *testing.T) {
reg := NewRegistry(nil, Tint3024Day, TintDraculaPlus)
reg.SetTint(Tint3024Day)
if reg.GetCurrentTint().ID() != Tint3024Day.ID() {
t.Errorf("expected %v, got %v", Tint3024Day, reg.GetCurrentTint())
}
reg.SetTintID(TintDraculaPlus.ID())
if reg.GetCurrentTint().ID() != TintDraculaPlus.ID() {
t.Errorf("expected %v, got %v", TintDraculaPlus, reg.GetCurrentTint())
}
if ok := reg.SetTintID("invalid"); ok {
t.Errorf("expected false, got true")
}
}
func TestRegistry_PreviousNextTint(t *testing.T) {
// Intentionally adding tints in the list out of order.
reg := NewRegistry(TintDraculaPlus, TintDraculaPlus, Tint3024Day, TintSynthwave)
if reg.GetCurrentTint().ID() != TintDraculaPlus.ID() {
t.Errorf("expected %v, got %v", TintDraculaPlus, reg.GetCurrentTint())
}
reg.NextTint()
if reg.GetCurrentTint().ID() != TintSynthwave.ID() {
t.Errorf("expected %v, got %v", TintSynthwave, reg.GetCurrentTint())
}
reg.NextTint()
if reg.GetCurrentTint().ID() != TintSynthwave.ID() {
t.Errorf("expected %v, got %v", TintSynthwave, reg.GetCurrentTint())
}
reg.PreviousTint()
if reg.GetCurrentTint().ID() != TintDraculaPlus.ID() {
t.Errorf("expected %v, got %v", TintDraculaPlus, reg.GetCurrentTint())
}
reg.PreviousTint()
if reg.GetCurrentTint().ID() != Tint3024Day.ID() {
t.Errorf("expected %v, got %v", Tint3024Day, reg.GetCurrentTint())
}
reg.PreviousTint()
if reg.GetCurrentTint().ID() != Tint3024Day.ID() {
t.Errorf("expected %v, got %v", Tint3024Day, reg.GetCurrentTint())
}
// If there currently isn't a default, then the first tint in the list is used.
reg = NewRegistry(nil, TintDraculaPlus, Tint3024Day, TintSynthwave)
reg.NextTint()
if reg.GetCurrentTint().ID() != Tint3024Day.ID() {
t.Errorf("expected %v, got %v", Tint3024Day, reg.GetCurrentTint())
}
reg = NewRegistry(nil, TintDraculaPlus, Tint3024Day, TintSynthwave)
reg.PreviousTint()
if reg.GetCurrentTint().ID() != Tint3024Day.ID() {
t.Errorf("expected %v, got %v", Tint3024Day, reg.GetCurrentTint())
}
}
func TestRegistry_AllColors(t *testing.T) {
NewDefaultRegistry()
ids := DefaultRegistry.TintIDs()
for _, id := range ids {
t.Run(id, func(t *testing.T) {
DefaultRegistry.SetTintID(id)
colors := []lipgloss.TerminalColor{
Fg(),
Bg(),
SelectionBg(),
Cursor(),
BrightBlack(),
BrightBlue(),
BrightCyan(),
BrightGreen(),
BrightPurple(),
BrightRed(),
BrightWhite(),
BrightYellow(),
Black(),
Blue(),
Cyan(),
Green(),
Purple(),
Red(),
White(),
Yellow(),
DefaultRegistry.Fg(),
DefaultRegistry.Bg(),
DefaultRegistry.SelectionBg(),
DefaultRegistry.Cursor(),
DefaultRegistry.BrightBlack(),
DefaultRegistry.BrightBlue(),
DefaultRegistry.BrightCyan(),
DefaultRegistry.BrightGreen(),
DefaultRegistry.BrightPurple(),
DefaultRegistry.BrightRed(),
DefaultRegistry.BrightWhite(),
DefaultRegistry.BrightYellow(),
DefaultRegistry.Black(),
DefaultRegistry.Blue(),
DefaultRegistry.Cyan(),
DefaultRegistry.Green(),
DefaultRegistry.Purple(),
DefaultRegistry.Red(),
DefaultRegistry.White(),
DefaultRegistry.Yellow(),
}
for _, c := range colors {
if c == nil {
t.Errorf("expected color, got nil")
}
}
})
}
}