-
Notifications
You must be signed in to change notification settings - Fork 49
/
decoder_test.go
47 lines (43 loc) · 1.1 KB
/
decoder_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
package eventsource
import (
"io"
"reflect"
"strings"
"testing"
)
func TestDecode(t *testing.T) {
tests := []struct {
rawInput string
wantedEvents []*publication
}{
{
rawInput: "event: eventName\ndata: {\"sample\":\"value\"}\n\n",
wantedEvents: []*publication{{event: "eventName", data: "{\"sample\":\"value\"}"}},
},
{
// the newlines should not be parsed as empty event
rawInput: "\n\n\nevent: event1\n\n\n\n\nevent: event2\n\n",
wantedEvents: []*publication{{event: "event1"}, {event: "event2"}},
},
}
for _, test := range tests {
decoder := NewDecoder(strings.NewReader(test.rawInput))
i := 0
for {
event, err := decoder.Decode()
if err == io.EOF {
break
}
if err != nil {
t.Fatalf("Unexpected error on decoding event: %s", err)
}
if !reflect.DeepEqual(event, test.wantedEvents[i]) {
t.Fatalf("Parsed event %+v does not equal wanted event %+v", event, test.wantedEvents[i])
}
i++
}
if i != len(test.wantedEvents) {
t.Fatalf("Unexpected number of events: %d does not equal wanted: %d", i, len(test.wantedEvents))
}
}
}