-
Notifications
You must be signed in to change notification settings - Fork 10
/
parser_test.go
80 lines (75 loc) · 1.78 KB
/
parser_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
package ucp
import (
"reflect"
"testing"
)
func TestParseSessionResp(t *testing.T) {
parseSessionRespTestCases := []struct {
name string
input string
expected string
}{
{
"authenticated",
"\x0200/00037/R/60/A/BIND AUTHENTICATED/6D\x03",
"",
},
{
"nack",
"\x0200/00022/R/60/N/01/checksum error/04\x03",
"[ucp error] code: 01 message: checksum error",
},
{
"empty packet",
"",
"invalid packet",
},
{
"invalid packet",
"\x0204/00024/R/31/A/0003/5D\x03",
"invalid packet",
},
}
for _, testCase := range parseSessionRespTestCases {
actual := parseSessionResp(testCase.input)
if actual != nil && actual.Error() != testCase.expected {
t.Errorf("testcase %s: Expected %v, got %v\n", testCase.name, testCase.expected, actual)
}
}
}
func TestParseResp(t *testing.T) {
parseRespTestCases := []struct {
name string
input string
expectedOpType string
expectedFields []string
expectedErr error
}{
{
"submit packet",
"\x0202/00041/R/51/A//09495696599:120917113002/83\x03",
opSubmitShortMessage,
[]string{"02", "00041", "R", "51", "A", "", "09495696599:120917113002", "83"},
nil,
},
{
"empty packet",
"",
"",
[]string{},
errInvalidPacket,
},
}
for _, testCase := range parseRespTestCases {
opType, fields, err := parseResp(testCase.input)
if opType != testCase.expectedOpType {
t.Errorf("testcase %s: Expected %v, got %v\n", testCase.name, testCase.expectedOpType, opType)
}
if !reflect.DeepEqual(fields, testCase.expectedFields) {
t.Errorf("testcase %s: Expected %v, got %v\n", testCase.name, testCase.expectedFields, fields)
}
if err != testCase.expectedErr {
t.Errorf("testcase %s: Expected %v, got %v\n", testCase.name, testCase.expectedErr, err)
}
}
}