-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpayload_test.go
148 lines (142 loc) · 3.2 KB
/
payload_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
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
package goevent
import (
"strconv"
"testing"
)
func TestCreatePayload(t *testing.T) {
p := CreatePayload()
t.Run("empty_date", func (t *testing.T) {
if len(p.data) != 0 {
t.Errorf("Expected the payload data to be empty, got %d elements", len(p.data))
}
})
}
func TestPayload_Set(t *testing.T) {
t.Run("new_pair_added", func (t *testing.T) {
p := CreatePayload()
key := "k1"
p.Set(key, "")
if len(p.data) != 1 {
t.Errorf("Expected payload to have 1 pair of key-value, got %d", len(p.data))
}
})
t.Run("new_key_added", func (t *testing.T) {
p := CreatePayload()
key := "k1"
p.Set(key, "")
if _, ok := p.data[key]; !ok {
t.Errorf("Expected key to be set in payload map")
}
})
t.Run("new_value_added", func (t *testing.T) {
p := CreatePayload()
key := "k1"
val := ""
p.Set(key, val)
if p.data[key] != val {
t.Errorf("Expected payload data value to be %v, got %v", val, p.data[key])
}
})
t.Run("different_kinds_of_values_added", func (t *testing.T) {
p := CreatePayload()
key := "k1"
values := []interface{} {
"",
nil,
1,
"A",
"a",
" ",
"ا",//persian alef
"ِ",
"|",
"|",//line
0,
Payload{},
Event{},
1.90220,
true,
" ",
"\t",
}
for val := range values {
p.Set(key, val)
if p.data[key] != val {
t.Errorf("Expected payload data value to be %v, got %v", val, p.data[key])
}
}
})
t.Run("existing_key_updates", func (t *testing.T) {
p := CreatePayload()
key := "k1"
val1 := "A"
val2 := "B"
p.Set(key, val1)
p.Set(key, val2)
if p.data[key] != val2 {
t.Errorf("Expected payload data value to update to %v, got %v", val2, p.data[key])
}
})
t.Run("multiple_keys_works", func (t *testing.T) {
for i:=0; i<10; i++ {
t.Run(strconv.Itoa(i)+" keys", func (t *testing.T) {
p := CreatePayload()
for j:=0;j<i;j++ {
key := "k"+strconv.Itoa(j)
v := 34*(j+8)
p.Set(key, v)
}
if len(p.data) != i {
t.Errorf("Expected %d pairs of data in the paylaod, got %d", i, len(p.data))
}
for j:=0;j<i;j++ {
key := "k"+strconv.Itoa(j)
if _, ok := p.data[key]; !ok {
t.Errorf("Expected key %s to exist in the paylaod, found nothing", key)
}
v := 34*(j+8)
if p.data[key] != v {
t.Errorf("Expected values of %s in payload to be %v, got %v", key, v, p.data[key])
}
}
})
}
})
t.Run("chaining_works", func (t *testing.T) {
p := CreatePayload()
p.Set("k1", 1).
Set("k2", 2).
Set("k3", 3).
Set("k4", 4)
if len(p.data) != 4 {
t.Errorf("Expected %d chain calls to work, got %d elements instead", 4, len(p.data))
}
})
}
func TestPayload_All(t *testing.T) {
p := CreatePayload().Set("k1", 1).Set("k2", 2)
if len(p.data) != len(p.All()) {
t.Errorf("Expected %d arguments returned, got %d", len(p.data), len(p.All()))
}
}
func TestPayload_Get(t *testing.T) {
pairs := map[string]interface{} {
"k1": 91209,
"k2": nil,
"k3": "AAAAAAAAA",
"k4": true,
"k5": make(chan int),
}
payload := CreatePayload()
for k, v := range pairs {
payload.Set(k, v)
}
for k, v := range pairs {
t.Run(k, func (t *testing.T) {
getV, _ := payload.Get(k)
if getV != v {
t.Errorf("Expected value %v, got %v", v, getV)
}
})
}
}