-
Notifications
You must be signed in to change notification settings - Fork 1
/
netstringer_test.go
66 lines (57 loc) · 1.49 KB
/
netstringer_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
package netstringer
import (
"testing"
)
func TestNewNetStringDecoder(t *testing.T) {
decoder := NewDecoder()
//decoder.SetDebugMode(true)
testInputs := []string{
"12:hello world!,",
"17:5:hello,6:world!,,",
"5:hello,6:world!,",
"12:How are you?,9:I am fine,12:this is cool,",
"12:hello", // Partial messages
" world!,",
}
expectedOutputs := []string{
"hello world!",
"5:hello,6:world!,",
"hello",
"world!",
"How are you?",
"I am fine",
"this is cool",
"hello world!",
}
go func(outputs []string, dataChannel chan []byte) {
for _, output := range outputs {
got := string(<-dataChannel)
if got != output {
t.Error("Got", got, "Expected", output)
}
}
}(expectedOutputs, decoder.DataOutput)
for _, testInput := range testInputs {
decoder.FeedData([]byte(testInput))
}
}
func TestEncode(t *testing.T) {
type TestCase struct {
Input string
Expected string
}
testCases := []TestCase{
TestCase{Input: "hello world!", Expected: "12:hello world!,"},
TestCase{Input: "5:hello,6:world!,", Expected: "17:5:hello,6:world!,,"},
TestCase{Input: "hello", Expected: "5:hello,"},
TestCase{Input: "world!", Expected: "6:world!,"},
TestCase{Input: "How are you?", Expected: "12:How are you?,"},
TestCase{Input: "I am fine", Expected: "9:I am fine,"},
}
for _, testCase := range testCases {
output := Encode([]byte(testCase.Input))
if string(output) != testCase.Expected {
t.Error("Got", string(output), "Expected", testCase.Expected)
}
}
}