-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathflash_test.go
More file actions
240 lines (180 loc) · 4.88 KB
/
flash_test.go
File metadata and controls
240 lines (180 loc) · 4.88 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
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
package session
import (
"testing"
"github.com/stretchr/testify/assert"
)
func TestFlash(t *testing.T) {
t.Parallel()
t.Run("New", func(t *testing.T) {
f := new(Flash)
assert.Empty(t, f.v, "data should be empty")
assert.Zero(t, f.Count(), "count should be zero")
assert.False(t, f.Changed(), "should not in changed state")
b, err := f.encode()
if assert.NoError(t, err, "should be able to encode") {
assert.NotNil(t, b, "encoded value should not be nil")
assert.Empty(t, b, "encoded value should be empty")
assert.False(t, f.Changed(), "should still not in changed state")
}
})
t.Run("Add", func(t *testing.T) {
f := new(Flash)
f.Add("a", 1)
assert.True(t, f.Changed(), "should be in changed state")
assert.EqualValues(t, 1, f.Count(), "count should changed")
})
t.Run("Clear Empty", func(t *testing.T) {
f := new(Flash)
f.Clear()
assert.Empty(t, f.v, "data should be empty")
assert.False(t, f.Changed(), "should not be in changed state")
})
t.Run("Clear Not Empty", func(t *testing.T) {
f := new(Flash)
f.Add("a", 1)
f.Clear()
assert.Empty(t, f.v, "data should be empty")
assert.Zero(t, f.Count(), "count should be zero")
assert.True(t, f.Changed(), "should in changed state")
})
t.Run("Clear Not Empty More than 1 time", func(t *testing.T) {
f := new(Flash)
f.Add("a", 1)
f.Clear()
f.Clear()
assert.Empty(t, f.v, "data should be empty")
assert.Zero(t, f.Count(), "count should be zero")
assert.True(t, f.Changed(), "should in changed state")
})
t.Run("Del", func(t *testing.T) {
f := new(Flash)
f.Add("a", 1)
f.Del("a")
assert.True(t, f.Changed(), "should in changed state after delete key")
})
t.Run("Count", func(t *testing.T) {
t.Run("Empty", func(t *testing.T) {
f := new(Flash)
assert.EqualValues(t, f.Count(), 0)
})
t.Run("1 value", func(t *testing.T) {
f := new(Flash)
f.Set("a", true)
assert.EqualValues(t, f.Count(), 1)
})
t.Run("2 values", func(t *testing.T) {
f := new(Flash)
f.Set("a", 1)
f.Set("b", 2)
assert.EqualValues(t, f.Count(), 2)
})
})
t.Run("Clone", func(t *testing.T) {
f := new(Flash)
f.Add("a", "1")
f.Add("a", "2")
f.Add("b", "3")
p := f.Clone()
assert.NotEqual(t, f, p, "should not point to original")
assert.Equal(t, f.Count(), p.Count(), "should have same count")
assert.Equal(t, f.v, p.v, "should have same data value")
f.Clear()
assert.NotEqual(t, f.v, p.v)
})
t.Run("Values", func(t *testing.T) {
f := new(Flash)
v := f.Values("a")
assert.NotNil(t, v, "not exists key should not nill")
assert.Empty(t, v, "not exists key should empty")
f.Add("a", 1)
f.Add("a", 2)
f.Add("a", 3)
v = f.Values("a")
assert.Equal(t, []interface{}{1, 2, 3}, v)
assert.False(t, f.Has("a"), "key should deleted after call values")
})
}
func TestFlashEncodeDecode(t *testing.T) {
t.Parallel()
t.Run("Valid data", func(t *testing.T) {
f := new(Flash)
f.Add("a", 1)
b, err := f.encode()
if assert.NoError(t, err) {
assert.NotNil(t, b)
assert.NotEmpty(t, b)
assert.True(t, f.Changed())
p := new(Flash)
if assert.NoError(t, p.decode(b)) {
assert.Equal(t, f.Count(), p.Count())
assert.Equal(t, f.v, p.v)
}
}
})
t.Run("Invalid data", func(t *testing.T) {
f := new(Flash)
f.Set("key", &struct{}{})
b, err := f.encode()
assert.Error(t, err)
assert.Empty(t, b)
})
t.Run("Decode empty bytes", func(t *testing.T) {
f := new(Flash)
if assert.NoError(t, f.decode([]byte{})) {
assert.Empty(t, f.v)
assert.False(t, f.Changed())
}
})
t.Run("Decode invalid bytes", func(t *testing.T) {
f := new(Flash)
if assert.Error(t, f.decode([]byte("invalid data"))) {
assert.Empty(t, f.v)
assert.False(t, f.Changed())
}
})
}
func TestFlashGet(t *testing.T) {
t.Parallel()
t.Run("Get empty", func(t *testing.T) {
f := new(Flash)
assert.Nil(t, f.Get("a"))
assert.False(t, f.Changed())
})
t.Run("Get valid value", func(t *testing.T) {
f := new(Flash)
f.Set("a", 1)
assert.True(t, f.Has("a"))
assert.Equal(t, 1, f.Get("a"))
assert.False(t, f.Has("a"))
})
t.Run("GetString from string value", func(t *testing.T) {
f := new(Flash)
f.Set("a", "hello")
assert.Equal(t, "hello", f.GetString("a"))
})
t.Run("GetInt from string value", func(t *testing.T) {
f := new(Flash)
f.Set("a", "hello")
assert.Zero(t, f.GetInt("a"))
})
t.Run("GetInt64 from string value", func(t *testing.T) {
f := new(Flash)
f.Set("a", "hello")
assert.Zero(t, f.GetInt64("a"))
})
t.Run("GetFloat32 from string value", func(t *testing.T) {
f := new(Flash)
f.Set("a", "hello")
assert.Zero(t, f.GetFloat32("a"))
})
t.Run("GetFloat64 from string value", func(t *testing.T) {
f := new(Flash)
f.Set("a", "hello")
assert.Zero(t, f.GetFloat64("a"))
})
t.Run("GetBool from string value", func(t *testing.T) {
f := new(Flash)
f.Set("a", "hello")
assert.Zero(t, f.GetBool("a"))
})
}