-
Notifications
You must be signed in to change notification settings - Fork 1
/
reader_test.go
154 lines (141 loc) · 3.5 KB
/
reader_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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
package gosc
import (
"bufio"
"bytes"
"errors"
"io"
"reflect"
"testing"
)
func Test_readArguments(t *testing.T) {
buf := bytes.Buffer{}
r := bufio.NewReader(&buf)
w := bufio.NewWriter(&buf)
t.Run("correctFormat", func(t *testing.T) {
_ = writeArguments(w, []any{float32(1.0), "Test", int32(2)})
_ = w.Flush()
res, err := readArguments(r)
if err != nil {
t.Errorf("expected no error but got: %v", err)
}
if len(res) != 3 {
t.Errorf("expected result to contain 3 elements but was got: %d", len(res))
}
if reflect.TypeOf(res[0]) != reflect.TypeOf(float32(0)) {
t.Errorf("expected first argument to be float32 but got: %T", res[0])
}
if reflect.TypeOf(res[1]) != reflect.TypeOf("") {
t.Errorf("expected second argument to be string but got: %T", res[0])
}
if reflect.TypeOf(res[2]) != reflect.TypeOf(int32(0)) {
t.Errorf("expected second argument to be int32 but got: %T", res[0])
}
buf.Reset()
})
}
func Test_readBundle(t *testing.T) {
buf := bytes.Buffer{}
r := bufio.NewReader(&buf)
w := bufio.NewWriter(&buf)
t.Run("correctBundle", func(t *testing.T) {
_ = writePackage(&Bundle{
Timetag: 0,
Messages: []*Message{
{
Address: "/test",
Arguments: []any{},
},
},
Bundles: nil,
Name: "Test",
}, w)
_ = w.Flush()
res, err := readBundle(r)
if err != nil {
t.Errorf("expected no error but got: %v", err)
}
if res.Name != "Test" {
t.Errorf("expected bundle name to be \"Test\" but got: %s", res.Name)
}
if len(res.Messages) != 1 {
t.Errorf("expected bundle to contain 1 message but was: %d", len(res.Messages))
}
buf.Reset()
})
}
func Test_readMessage(t *testing.T) {
buf := bytes.Buffer{}
r := bufio.NewReader(&buf)
w := bufio.NewWriter(&buf)
t.Run("correctMessage", func(t *testing.T) {
_ = writePackage(&Message{
Address: "/test",
Arguments: []any{},
}, w)
_ = w.Flush()
res, err := readMessage(r)
if err != nil {
t.Errorf("expected no error but got: %v", err)
}
if res.Address != "/test" {
t.Errorf("expected address to be \"/test\" but got: %s", res.Address)
}
buf.Reset()
})
}
func Test_readPackage(t *testing.T) {
buf := bytes.Buffer{}
r := bufio.NewReader(&buf)
w := bufio.NewWriter(&buf)
t.Run("correctPackage", func(t *testing.T) {
_ = writePackage(&Message{
Address: "/test",
Arguments: []any{},
}, w)
_ = w.Flush()
res, err := readPackage(r)
if err != nil {
t.Errorf("expected no error but got: %v", err)
}
if res.GetType() != PackageTypeMessage {
t.Errorf("expected package type to be PackageTypeMessage but got: %s", res.GetType())
}
})
}
func Test_readPaddedString(t *testing.T) {
buf := bytes.Buffer{}
r := bufio.NewReader(&buf)
w := bufio.NewWriter(&buf)
t.Run("withoutPadding", func(t *testing.T) {
_ = writePaddedString(w, "abc")
_ = w.Flush()
res, err := readPaddedString(r)
if err != nil {
t.Errorf("expected no error but got: %v", err)
}
if res != "abc" {
t.Errorf("expected \"abc\" but got: %s", res)
}
buf.Reset()
})
t.Run("withPadding", func(t *testing.T) {
_ = writePaddedString(w, "testing")
_ = w.Flush()
res, err := readPaddedString(r)
if err != nil {
t.Errorf("expected no error but got: %v", err)
}
if res != "testing" {
t.Errorf("expected \"testing\" but got: %s", res)
}
buf.Reset()
})
t.Run("wrongDelim", func(t *testing.T) {
_, _ = w.WriteString("test")
_, err := readPaddedString(r)
if !errors.Is(err, io.EOF) {
t.Errorf("expected error io.EOF but got: %v", err)
}
buf.Reset()
})
}