-
Notifications
You must be signed in to change notification settings - Fork 0
/
kvs_test.go
60 lines (51 loc) · 1.22 KB
/
kvs_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
package main
import (
"fmt"
"testing"
)
func TestAdd(t *testing.T) {
for _, scenario := range []struct {
about string
key string
value string
}{
{
about: "add key foo value bar",
key: "foo",
value: "bar",
},
{
about: "add key bar value foo",
key: "bar",
value: "foo",
},
} {
mw := mockWriter{
storage: "",
}
kvs := new(&mw)
t.Run(scenario.about, func(t *testing.T) {
if err := kvs.add(scenario.key, scenario.value); err != nil {
t.Fatalf("Unexpected error when adding value '%s' to key '%s': %s", scenario.value, scenario.key, err.Error())
}
if value, exists := kvs.entries[scenario.key]; exists {
if value != scenario.value {
t.Fatalf("Expected value '%s' for key '%s', got '%s'", scenario.key, scenario.key, value)
}
} else {
t.Fatalf("Could not find expected entry for key '%s'", scenario.key)
}
expectedStored := fmt.Sprintf("%s:%s\n", scenario.key, scenario.value)
if mw.storage != expectedStored {
t.Fatalf("Expected '%s' string stored, found '%s'", expectedStored, mw.storage)
}
})
}
}
type mockWriter struct {
storage string
}
func (mw *mockWriter) Write(p []byte) (int, error) {
mw.storage += string(p)
return len(p), nil
}