-
Notifications
You must be signed in to change notification settings - Fork 1
/
tokenizer_test.go
139 lines (131 loc) · 2.56 KB
/
tokenizer_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
package nmea_test
import (
"testing"
"github.com/alecthomas/assert/v2"
"github.com/twpayne/go-nmea"
)
func TestTokenizer(t *testing.T) {
tok := nmea.NewTokenizer([]byte("GPGGA,123"))
assert.Equal(t, "GPGGA", tok.String())
tok.Comma()
assert.Equal(t, 123, tok.UnsignedInt())
assert.NoError(t, tok.Err())
}
func TestTokenizer_Float(t *testing.T) {
for _, tc := range []struct {
s string
expectedErr error
expected float64
}{
{
s: "1",
expected: 1,
},
{
s: "1.23",
expected: 1.23,
},
{
s: "-1.23",
expected: -1.23,
},
} {
t.Run(tc.s, func(t *testing.T) {
tok := nmea.NewTokenizer([]byte(tc.s))
actual := tok.Float()
err := tok.Err()
if tc.expectedErr != nil {
assert.IsError(t, err, tc.expectedErr)
} else {
assert.NoError(t, err)
assert.Equal(t, tc.expected, actual)
}
})
}
}
func TestTokenizer_Hex(t *testing.T) {
for _, tc := range []struct {
s string
expectedErr error
expected int
}{
{
s: "a",
expected: 0xa,
},
{
s: "abcdef",
expected: 0xabcdef,
},
} {
t.Run(tc.s, func(t *testing.T) {
tok := nmea.NewTokenizer([]byte(tc.s))
actual := tok.Hex()
err := tok.Err()
if tc.expectedErr != nil {
assert.IsError(t, err, tc.expectedErr)
} else {
assert.NoError(t, err)
assert.Equal(t, tc.expected, actual)
}
})
}
}
func TestTokenizer_HexBytes(t *testing.T) {
for _, tc := range []struct {
s string
expectedErr error
expected []byte
}{
{
s: "0123456789ABCDEFabcdef",
expected: []byte{0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef, 0xab, 0xcd, 0xef},
},
} {
t.Run(tc.s, func(t *testing.T) {
tok := nmea.NewTokenizer([]byte(tc.s))
actual := tok.HexBytes()
err := tok.Err()
if tc.expectedErr != nil {
assert.IsError(t, err, tc.expectedErr)
} else {
assert.NoError(t, err)
assert.Equal(t, tc.expected, actual)
}
})
}
}
func TestTokenizer_OptionalInt(t *testing.T) {
for _, tc := range []struct {
s string
expectedErr error
expected nmea.Optional[int]
}{
{
s: "",
},
{
s: ",",
},
{
s: "123",
expected: nmea.NewOptional(123),
},
{
s: "-123",
expected: nmea.NewOptional(-123),
},
} {
t.Run(tc.s, func(t *testing.T) {
tok := nmea.NewTokenizer([]byte(tc.s))
actual := tok.OptionalInt()
err := tok.Err()
if tc.expectedErr != nil {
assert.IsError(t, err, tc.expectedErr)
} else {
assert.NoError(t, err)
assert.Equal(t, tc.expected, actual)
}
})
}
}