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
|
package bot
import "testing"
func TestParseCommand(t *testing.T) {
cases := []struct {
text string
ok bool
name string
target string
argument string
}{
{text: "/cep 01001000", ok: true, name: "cep", argument: "01001000"},
{text: "/cep@cepisbot 01001-000", ok: true, name: "cep", target: "cepisbot", argument: "01001-000"},
{text: " /CEP@CepisBot 01001000 ", ok: true, name: "cep", target: "CepisBot", argument: "01001000"},
{text: "/start", ok: true, name: "start"},
{text: "/help@cepisbot", ok: true, name: "help", target: "cepisbot"},
{text: "01001000", ok: false},
{text: "", ok: false},
{text: "/", ok: false},
{text: "não é comando /cep", ok: false},
}
for _, tc := range cases {
command, ok := ParseCommand(tc.text)
if ok != tc.ok {
t.Fatalf("ParseCommand(%q) ok = %v, want %v", tc.text, ok, tc.ok)
}
if !ok {
continue
}
if command.Name != tc.name || command.Target != tc.target || command.Argument != tc.argument {
t.Fatalf("ParseCommand(%q) = %+v, want {%q %q %q}",
tc.text, command, tc.name, tc.target, tc.argument)
}
}
}
func TestAddressedTo(t *testing.T) {
cases := []struct {
text string
want bool
}{
{"/cep 01001000", true},
{"/cep@cepisbot 01001000", true},
{"/cep@CEPISBOT 01001000", true},
{"/cep@outrobot 01001000", false},
}
for _, tc := range cases {
command, ok := ParseCommand(tc.text)
if !ok {
t.Fatalf("ParseCommand(%q) did not parse", tc.text)
}
if got := command.AddressedTo("cepisbot"); got != tc.want {
t.Fatalf("AddressedTo(%q) = %v, want %v", tc.text, got, tc.want)
}
}
}
|