-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcache_test.go
More file actions
154 lines (139 loc) Β· 2.82 KB
/
cache_test.go
File metadata and controls
154 lines (139 loc) Β· 2.82 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
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 color
import (
"maps"
"testing"
"github.com/stretchr/testify/assert"
)
func TestMakeKey(t *testing.T) {
tests := []struct {
name string
input []Attr
want string
}{
{
name: "empty argument",
input: []Attr{},
want: "",
},
{
name: "single attribute",
input: []Attr{AttrFgRed},
want: string([]byte{1, 0, 0, 0, 0, 0, 0, 0}),
},
{
name: "multiple attributes",
input: []Attr{AttrFgRed, AttrFgGreen},
want: string([]byte{
1, 0, 0, 0, 0, 0, 0, 0,
2, 0, 0, 0, 0, 0, 0, 0,
}),
},
{
name: "larger values",
input: []Attr{256, 65536},
want: string([]byte{
0, 1, 0, 0, 0, 0, 0, 0,
0, 0, 1, 0, 0, 0, 0, 0,
}),
},
{
name: "check sorting",
input: []Attr{AttrFgGreen, AttrFgRed},
want: string([]byte{
1, 0, 0, 0, 0, 0, 0, 0,
2, 0, 0, 0, 0, 0, 0, 0,
}),
},
}
for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
result := makeKey(tc.input)
assert.Equal(t, tc.want, result)
})
}
}
func TestCacheGet(t *testing.T) {
tests := []struct {
name string
initData map[string]string
input []Attr
expected string
found bool
}{
{
name: "empty cache",
initData: map[string]string{},
input: []Attr{AttrFgGreen},
expected: "",
found: false,
},
{
name: "found in cache",
initData: map[string]string{
makeKey([]Attr{AttrFgRed}): makeStartSeq([]Attr{AttrFgRed}),
},
input: []Attr{AttrFgRed},
expected: makeStartSeq([]Attr{AttrFgRed}),
found: true,
},
{
name: "not found in cache",
initData: map[string]string{
makeKey([]Attr{AttrFgBlue}): makeStartSeq([]Attr{AttrFgBlue}),
},
input: []Attr{AttrFgGreen},
expected: "",
found: false,
},
}
for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
cache := newCache()
cache.mu.Lock()
maps.Copy(cache.seqs, tc.initData)
cache.mu.Unlock()
result, found := cache.get(tc.input)
assert.Equal(t, tc.found, found)
assert.Equal(t, tc.expected, result)
})
}
}
func TestCacheSet(t *testing.T) {
tests := []struct {
name string
attrs []Attr
seq string
found bool
}{
{
name: "Zero attrs",
attrs: []Attr{},
seq: "",
found: true,
},
{
name: "one attr",
attrs: []Attr{AttrBgBlue},
seq: makeStartSeq([]Attr{AttrBgBlue}),
found: true,
},
{
name: "more attrs",
attrs: []Attr{AttrBgGreen, AttrFgBrightYellow, AttrItalic},
seq: makeStartSeq([]Attr{AttrBgGreen, AttrFgBrightYellow, AttrItalic}),
found: true,
},
}
for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
cache := newCache()
cache.set(tc.attrs, tc.seq)
key := makeKey(tc.attrs)
cache.mu.RLock()
defer cache.mu.RUnlock()
seq, found := cache.seqs[key]
assert.Equal(t, tc.found, found)
assert.Equal(t, tc.seq, seq)
})
}
}