-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmemory_test.go
67 lines (59 loc) · 1.61 KB
/
memory_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
package ezdb
import (
"fmt"
"testing"
)
func TestMemory(t *testing.T) {
type Student struct {
Name string `json:"name"`
Age int `json:"age"`
}
collection := Memory[*Student](nil)
cmp := func(expected, actual *Student) error {
if expected == nil && actual != nil {
return fmt.Errorf("expected no student (got %v)", actual)
} else if expected != nil && actual == nil {
return fmt.Errorf("expected student %v (got nil)", expected)
}
if actual.Name != expected.Name {
return fmt.Errorf("student has wrong name (expected '%s', got '%s')", expected.Name, actual.Name)
} else if actual.Age != expected.Age {
return fmt.Errorf("student has wrong age (expected '%d', got '%d')", expected.Age, actual.Age)
}
return nil
}
tester := &CollectionTester[*Student]{
C: collection,
Cmp: cmp,
Data: map[string]*Student{
"annie": {Name: "Annie", Age: 32},
"ben": {Name: "Ben", Age: 50},
"clive": {Name: "Clive", Age: 21},
},
}
named := func(name string, do func() error) func() {
return func() {
if err := do(); err != nil {
t.Errorf("failed test %q: %v", name, err)
} else {
t.Logf("passed test %q", name)
}
}
}
sequence := []func(){
named("init 1", tester.Init),
named("has", tester.Has),
named("get", tester.Get),
named("getAll", tester.GetAll),
named("deleteOne 1", tester.DeleteOne),
named("deleteOne 2", tester.DeleteOne),
named("init 2", tester.Init),
named("iterCount", tester.IterCount),
named("iterSortKeys", tester.IterSortKeys),
named("deleteAll", tester.DeleteAll),
named("close", tester.Close),
}
for _, do := range sequence {
do()
}
}