-
-
Notifications
You must be signed in to change notification settings - Fork 5
/
reader_test.go
108 lines (93 loc) · 2.86 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
package hl7
import (
"bytes"
"errors"
"testing"
"testing/iotest"
"github.com/stretchr/testify/assert"
)
func TestReaderReadMessage(t *testing.T) {
tests := []struct {
name string
data string
count int
}{
{"empty", "", 0},
{"one message", "MSH|....", 1},
{"two messages (CR)", "MSH|....\rMSH|.....", 2},
{"two messages (CRLF)", "MSH|....\r\nMSH|.....", 2},
{"two messages (FF)", "MSH|....\fMSH|.....", 2},
{"one message with trailing delimiter (CR)", "MSH|....\r", 1},
{"one message with trailing delimiter (CRLF)", "MSH|....\r\n", 1},
{"one message with trailing delimiter (FF)", "MSH|....\f", 1},
{"two messages with trailing delimiter (CR)", "MSH|....\rMSH|.....\r", 2},
{"two messages with trailing delimiter (CRLF)", "MSH|....\r\nMSH|.....\r\n", 2},
{"two messages with trailing delimiter (FF)", "MSH|....\fMSH|.....\f", 2},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
buf := bytes.NewBufferString(tt.data)
reader := NewReader(buf)
for i := 0; i < tt.count; i++ {
_, err := reader.ReadMessage()
assert.Nil(t, err)
}
_, err := reader.ReadMessage()
assert.Error(t, err)
})
}
t.Run("read error is propagated", func(t *testing.T) {
buf := bytes.NewBufferString("MSH|....")
reader := NewReader(iotest.TimeoutReader(buf))
_, err := reader.ReadMessage()
assert.Error(t, err)
})
}
func TestReaderEachMessage(t *testing.T) {
tests := []struct {
name string
data string
count int
}{
{"empty", "", 0},
{"one message", "MSH|....", 1},
{"two messages (CR)", "MSH|....\rMSH|.....", 2},
{"two messages (CRLF)", "MSH|....\r\nMSH|.....", 2},
{"two messages (FF)", "MSH|....\fMSH|.....", 2},
{"one message with trailing delimiter (CR)", "MSH|....\r", 1},
{"one message with trailing delimiter (CRLF)", "MSH|....\r\n", 1},
{"one message with trailing delimiter (FF)", "MSH|....\f", 1},
{"two messages with trailing delimiter (CR)", "MSH|....\rMSH|.....\r", 2},
{"two messages with trailing delimiter (CRLF)", "MSH|....\r\nMSH|.....\r\n", 2},
{"two messages with trailing delimiter (FF)", "MSH|....\fMSH|.....\f", 2},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
buf := bytes.NewBufferString(tt.data)
reader := NewReader(buf)
i := 0
err := reader.EachMessage(func(msg *Message) error {
i++
return nil
})
assert.Equal(t, tt.count, i)
assert.Nil(t, err)
})
}
t.Run("read error is propagated", func(t *testing.T) {
buf := bytes.NewBufferString("MSH|....")
reader := NewReader(iotest.TimeoutReader(buf))
err := reader.EachMessage(func(msg *Message) error {
return nil
})
assert.Error(t, err)
})
t.Run("error is propagated", func(t *testing.T) {
buf := bytes.NewBufferString("MSH|....")
reader := NewReader(buf)
err := reader.EachMessage(func(msg *Message) error {
return errors.New("foo")
})
assert.Error(t, err)
})
}