forked from lenaten/hl7
-
Notifications
You must be signed in to change notification settings - Fork 2
/
message_test.go
106 lines (93 loc) · 1.91 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
package hl7
import (
"golang.org/x/net/html/charset"
"io/ioutil"
"os"
"testing"
)
func readFile(fname string) ([]byte, error) {
file, err := os.Open(fname)
if err != nil {
return nil, err
}
defer file.Close()
reader, err := charset.NewReader(file, "text/plain")
if err != nil {
return nil, err
}
data, err := ioutil.ReadAll(reader)
if err != nil {
return nil, err
}
return data, nil
}
func TestMessage(t *testing.T) {
data, err := readFile("./testdata/msg.hl7")
if err != nil {
t.Fatal(err)
}
msg := &Message{Value: []rune(string(data))}
msg.parse()
if err != nil {
t.Error(err)
}
if len(msg.Segments) != 5 {
t.Errorf("Expected 5 segments got %d\n", len(msg.Segments))
}
data, err = readFile("./testdata/msg2.hl7")
if err != nil {
t.Fatal(err)
}
msg = &Message{Value: []rune(string(data))}
msg.parse()
if err != nil {
t.Error(err)
}
if len(msg.Segments) != 5 {
t.Errorf("Expected 5 segments got %d\n", len(msg.Segments))
}
data, err = readFile("./testdata/msg3.hl7")
if err != nil {
t.Fatal(err)
}
msg = &Message{Value: []rune(string(data))}
msg.parse()
if err != nil {
t.Error(err)
}
if len(msg.Segments) != 9 {
t.Errorf("Expected 9 segments got %d\n", len(msg.Segments))
}
data, err = readFile("./testdata/msg4.hl7")
if err != nil {
t.Fatal(err)
}
msg = &Message{Value: []rune(string(data))}
msg.parse()
if err != nil {
t.Error(err)
}
if len(msg.Segments) != 9 {
t.Errorf("Expected 9 segments got %d\n", len(msg.Segments))
}
}
func TestMsgUnmarshal(t *testing.T) {
fname := "./testdata/msg.hl7"
file, err := os.Open(fname)
if err != nil {
t.Fatal(err)
}
defer file.Close()
msgs, err := NewDecoder(file).Messages()
if err != nil {
t.Fatal(err)
}
st := my7{}
msgs[0].Unmarshal(&st)
if st.FirstName != "John" {
t.Errorf("Expected John got %s\n", st.FirstName)
}
if st.LastName != "Jones" {
t.Errorf("Expected Jones got %s\n", st.LastName)
}
}