-
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathmessage_test.go
126 lines (114 loc) · 2.52 KB
/
message_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
package hl7
import (
"bufio"
"bytes"
"testing"
"github.com/stretchr/testify/assert"
)
func TestNewMessage(t *testing.T) {
tests := []struct {
name string
data []byte
want *Message
wantErr bool
}{
{"Empty (nil)", []byte(nil), nil, true},
{"Empty (not nil)", []byte{}, nil, true},
{"Too short", []byte(`MSH|^~\`), nil, true},
{
"Minimal example",
[]byte(`MSH|^~\&`),
&Message{
reader: bufio.NewReader(bytes.NewBuffer([]byte(`MSH|^~\&`))),
fieldSep: '|',
compSep: '^',
subCompSep: '&',
repeat: '~',
escape: '\\',
},
false,
},
{
"Custom separators",
[]byte("MSH....."),
&Message{
reader: bufio.NewReader(bytes.NewBuffer([]byte("MSH....."))),
fieldSep: '.',
compSep: '.',
subCompSep: '.',
repeat: '.',
escape: '.',
},
false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := NewMessage(tt.data)
if tt.wantErr {
assert.Error(t, err)
} else {
assert.Nil(t, err)
}
assert.Equal(t, tt.want, got)
})
}
}
func TestMessageParse(t *testing.T) {
tests := []struct {
name string
data []byte
counts map[string]int
}{
{
"one segment",
[]byte("MSH|^~\\&"),
map[string]int{"MSH": 1},
},
{
"two segments",
[]byte("MSH|^~\\&\rMSH|^~\\&"),
map[string]int{"MSH": 2},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
msg, _ := NewMessage(tt.data)
msg.Parse()
// Verify that the parsed segments match up with what we expect (this can
// catch unexpected segments showing up).
for stype, segments := range msg.segments {
wantCount := tt.counts[stype]
assert.Equal(t, wantCount, len(segments))
}
// Verify that all the counts we expect match with what was parsed (this
// can catch missing segments).
for stype, wantCount := range tt.counts {
segments := msg.segments[stype]
assert.Equal(t, wantCount, len(segments))
}
})
}
}
func TestMessageReadSegment(t *testing.T) {
tests := []struct {
name string
data []byte
count int
}{
{"one segment", []byte("MSH|^~\\&"), 1},
{"two segments", []byte("MSH|^~\\&\rMSH|^~\\&"), 2},
{"two segments, extra whitespace", []byte("MSH|^~\\&\r\nMSH|^~\\&"), 2},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
msg, _ := NewMessage(tt.data)
for i := 0; i < tt.count; i++ {
_, err := msg.ReadSegment()
assert.Nil(t, err)
}
_, err := msg.ReadSegment()
assert.Error(t, err)
})
}
}