-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathupdates_parser_test.go
More file actions
214 lines (198 loc) · 5.66 KB
/
updates_parser_test.go
File metadata and controls
214 lines (198 loc) · 5.66 KB
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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
package botmeans
import (
"fmt"
"github.com/go-telegram-bot-api/telegram-bot-api"
"sync"
"testing"
)
func TestUpdatesParser(t *testing.T) {
handlersProvider := func(id string) (ActionHandler, bool) {
switch id {
case "cmd1":
return func(ActionContextInterface) {}, true
case "":
return func(ActionContextInterface) {}, true
}
return nil, false
}
actionFactory := func(
sessionBase SessionBase,
sessionFactory SessionFactory,
getters actionExecuterFactoryConfig,
out chan Executer,
) {
ActionFactory(
sessionBase,
sessionFactory,
getters,
func(senderSession) SenderInterface {
return nil
},
out,
handlersProvider,
)
}
mutex := sync.Mutex{}
sessionIdFlags := make(map[string]struct{})
sessionFactory := func(base SessionBase) (SessionInterface, error) {
stringID := fmt.Sprintf("%v:%v:%v", base.TelegramChatID, base.TelegramUserID, base.TelegramUserName)
mutex.Lock()
_, ok := sessionIdFlags[stringID]
sessionIdFlags[stringID] = struct{}{}
mutex.Unlock()
return &Session{SessionBase: base, db: nil, isNew: !ok}, nil
}
aliaser := func(string) (string, Args, bool) { return "", args{}, false }
argsParser := func(tgUpdate tgbotapi.Update) (r Args) {
r = ArgsParser(tgUpdate, sessionFactory, aliaser)
// t.Logf("%+v", r)
return
}
cmdParser := func(tgUpdate tgbotapi.Update) string {
return CmdParser(tgUpdate, aliaser)
}
botMsgFactory := func(chatID int64, msgId int64, callbackID string) BotMessageInterface {
return &BotMessage{TelegramChatID: chatID, TelegramMsgID: msgId, callbackID: callbackID}
}
updatesChan := make(chan tgbotapi.Update)
actionsChan := createTGUpdatesParser(
updatesChan,
parserConfig{
sessionFactory,
actionFactory,
botMsgFactory,
cmdParser,
argsParser,
},
)
type TestEntry struct {
tgUpdate tgbotapi.Update
result []*Action
}
testData := []TestEntry{
TestEntry{
tgbotapi.Update{
Message: &tgbotapi.Message{
From: &tgbotapi.User{ID: 42, UserName: "fuuu"},
Chat: &tgbotapi.Chat{ID: 24, Title: "Chat2"},
Text: "blabla",
},
},
[]*Action{
&Action{
session: &Session{SessionBase: SessionBase{42, "fuuu", 24, false, false}, isNew: true},
getters: actionExecuterFactoryConfig{
cmdGetter: func() string { return "" },
argsGetter: func() Args { return args{[]arg{arg{}}, "session"} },
},
},
&Action{
session: &Session{SessionBase: SessionBase{42, "fuuu", 24, false, false}, isNew: true},
getters: actionExecuterFactoryConfig{
cmdGetter: func() string { return "" },
argsGetter: func() Args { return args{[]arg{arg{"blabla"}}, "blabla"} },
},
},
},
},
TestEntry{
tgbotapi.Update{
Message: &tgbotapi.Message{
From: &tgbotapi.User{ID: 42, UserName: "fuuu"},
Chat: &tgbotapi.Chat{ID: 24, Title: "Chat2"},
Text: "/cmd1",
},
},
[]*Action{
&Action{
session: &Session{SessionBase: SessionBase{42, "fuuu", 24, false, false}},
getters: actionExecuterFactoryConfig{
cmdGetter: func() string { return "cmd1" },
argsGetter: func() Args { return args{[]arg{arg{"/cmd1"}}, "/cmd1"} },
},
},
},
},
TestEntry{
tgbotapi.Update{
Message: &tgbotapi.Message{
From: &tgbotapi.User{ID: 42, UserName: "fuuu"},
Chat: &tgbotapi.Chat{ID: 24, Title: "Chat2"},
Text: "/cmd1 ffuuu 9.75",
},
},
[]*Action{
&Action{
session: &Session{SessionBase: SessionBase{42, "fuuu", 24, false, false}},
getters: actionExecuterFactoryConfig{
cmdGetter: func() string { return "cmd1" },
argsGetter: func() Args { return args{[]arg{arg{"/cmd1"}, arg{"ffuuu"}, arg{"9.75"}}, "/cmd1 ffuuu 9.75"} },
},
},
},
},
}
fail := false
for _, testEntry := range testData {
updatesChan <- testEntry.tgUpdate
lastIndex := 0
for a := range actionsChan {
if lastIndex == len(testEntry.result) {
fail = true
t.Log("Too many actions spawned")
for _ = range actionsChan {
}
break
}
switch {
case a == nil && testEntry.result[lastIndex] != nil:
t.Log("Should not be nil")
fail = true
case a != nil && testEntry.result[lastIndex] == nil:
t.Log("Should be nil")
fail = true
case a != nil && testEntry.result[lastIndex] != nil:
if action, ok := a.(*Action); ok {
if *(action.session.(*Session)) != *testEntry.result[lastIndex].session.(*Session) {
fail = true
t.Log("Session content is wrong: ", *(action.session.(*Session)), *testEntry.result[lastIndex].session.(*Session))
}
if action.getters.cmdGetter() != testEntry.result[lastIndex].getters.cmdGetter() {
fail = true
t.Log("Wrong cmd", action.getters.cmdGetter(), testEntry.result[lastIndex].getters.cmdGetter())
}
if action.getters.argsGetter().Count() != testEntry.result[lastIndex].getters.argsGetter().Count() {
fail = true
t.Log("Wrong args len", action.getters.argsGetter().Raw(), testEntry.result[lastIndex].getters.argsGetter().Raw())
} else {
for i := range make([]int, action.getters.argsGetter().Count()) {
_, ok := action.getters.argsGetter().At(i).NewSession()
a1, _ := action.getters.argsGetter().At(i).String()
a2, _ := testEntry.result[lastIndex].getters.argsGetter().At(i).String()
if a1 != a2 && ok != true {
fail = true
t.Log("Wrong arg: ", a1, a2)
break
}
}
}
} else {
fail = true
t.Log("Should be Action type")
}
}
lastIndex++
if lastIndex == len(testEntry.result) {
break
}
}
if lastIndex != len(testEntry.result) {
fail = true
t.Log("Too less actions spawned")
}
}
close(updatesChan)
if fail {
t.Fail()
}
}