-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhelpers_test.go
65 lines (60 loc) · 1.72 KB
/
helpers_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
package teleBot
import (
"testing"
)
func TestIsValidPointer(t *testing.T) {
a := 0
if IsValidPointer(a) {
t.Error("failed pointer check for ", a)
}
if !IsValidPointer(&a) {
t.Error("failed pointer check for ", &a)
}
if IsValidPointer(nil) {
t.Error("failed pointer check for nil")
}
}
func compareMaps(m1, m2 map[int][]Update) bool {
for id, updates1 := range m1 {
updates2, ok := m2[id]
if !ok {
return false
}
if len(updates1) != len(updates2) {
return false
}
for inx, upd := range updates1 {
if upd != updates2[inx] {
return false
}
}
}
return true
}
func TestGroupByChatId(t *testing.T) {
updates := []Update{
{Message: Message{Chat: Chat{Id: 0}, Text: "0"}},
{Message: Message{Chat: Chat{Id: 3}, Text: "1"}},
{Message: Message{Chat: Chat{Id: 0}, Text: "2"}},
{Message: Message{Chat: Chat{Id: 2}, Text: "3"}},
{Message: Message{Chat: Chat{Id: 1}, Text: "4"}},
{Message: Message{Chat: Chat{Id: 3}, Text: "5"}},
{Message: Message{Chat: Chat{Id: 2}, Text: "6"}},
{Message: Message{Chat: Chat{Id: 1}, Text: "7"}},
}
ans := map[int][]Update{
0: {{Message: Message{Chat: Chat{Id: 0}, Text: "0"}},
{Message: Message{Chat: Chat{Id: 0}, Text: "2"}}},
1: {{Message: Message{Chat: Chat{Id: 1}, Text: "4"}},
{Message: Message{Chat: Chat{Id: 1}, Text: "7"}}},
2: {{Message: Message{Chat: Chat{Id: 2}, Text: "3"}},
{Message: Message{Chat: Chat{Id: 2}, Text: "6"}}},
3: {{Message: Message{Chat: Chat{Id: 3}, Text: "1"}},
{Message: Message{Chat: Chat{Id: 3}, Text: "5"}}},
}
resp := GetUpdatesResponse{Ok: true, Res: updates}
grouped := GroupByChatId(&resp)
if !compareMaps(grouped, ans) || !compareMaps(ans, grouped) {
t.Error("Failed grouping: ", grouped, " != ", ans)
}
}