-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcontext_test.go
102 lines (85 loc) · 2.02 KB
/
context_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
package juliet
import (
"testing"
)
func TestGet(t *testing.T) {
ctx := NewContext()
ctx.values["foo"] = "bar"
ctx.values["faa"] = nil
if val, ok := ctx.Get("foo"); ok {
if val != "bar" {
t.Fatalf("Invalid value for key foo. Expected 'bar' but was '%v'", val)
}
} else {
t.Fatalf("Missing value for key foo")
}
if val, ok := ctx.Get("faa"); ok {
if val != nil {
t.Fatalf("Invalid value for key faa. Expected nil but was '%v'", val)
}
} else {
t.Fatalf("Missing value for key faa")
}
if val, ok := ctx.Get("xyz"); ok {
t.Fatalf("Invalid value '%v' for key xyz", val)
}
}
func TestSet(t *testing.T) {
ctx := NewContext()
ctx.Set("foo", "bar")
if val, ok := ctx.values["foo"]; ok {
if val != "bar" {
t.Fatalf("Invalid value for key foo. Expected 'bar' but was '%v'", val)
}
} else {
t.Fatalf("Missing value for key foo")
}
}
func TestDelete(t *testing.T) {
ctx := NewContext()
ctx.values["foo"] = "bar"
ctx.Delete("foo")
if val, ok := ctx.Get("foo"); ok {
t.Fatalf("Invalid value '%v' for key foo", val)
}
ctx.Delete("faa")
}
func TestClear(t *testing.T) {
ctx := NewContext()
ctx.values["foo"] = "bar"
ctx.values["plip"] = "plop"
ctx.Clear()
if len(ctx.values) > 0 {
t.Fatalf("Invalid value count %d, Expected 0", len(ctx.values))
}
ctx.Delete("faa")
}
func TestCopy(t *testing.T) {
ctx := NewContext()
ctx.Set("foo", "bar")
copy := ctx.Copy()
copy.Set("foo", "baz")
if val, ok := ctx.Get("foo"); ok {
if val != "bar" {
t.Fatalf("Invalid value for key foo. Expected 'bar' but was '%v'", val)
}
} else {
t.Fatalf("Missing value for key foo")
}
if val, ok := copy.Get("foo"); ok {
if val != "baz" {
t.Fatalf("Invalid value for key foo. Expected 'baz' but was '%v'", val)
}
} else {
t.Fatalf("Missing value for key plop")
}
}
func TestString(t *testing.T) {
ctx := NewContext()
ctx.Set("foo", "bar")
expected := "foo => bar\n"
str := ctx.String()
if str != expected {
t.Fatalf("Invalid context string representation %s, expected %s", str, expected)
}
}