-
-
Notifications
You must be signed in to change notification settings - Fork 5
/
sub_component_test.go
106 lines (96 loc) · 2.91 KB
/
sub_component_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 (
"testing"
"time"
"github.com/stretchr/testify/assert"
)
func TestNewSubComponent(t *testing.T) {
tests := []struct {
name string
data []byte
escape byte
want SubComponent
}{
{"empty (nil)", []byte(nil), '\\', SubComponent(nil)},
{"empty (not nil)", []byte{}, '\\', SubComponent{}},
{"not empty", []byte("MSH|..."), '\\', SubComponent("MSH|...")},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := newSubComponent(tt.escape, tt.data)
assert.Equal(t, tt.want, got)
})
}
}
func TestSubComponentInt(t *testing.T) {
tests := []struct {
name string
value SubComponent
want int
wantErr bool
}{
{"integer", SubComponent("1"), 1, false},
{"letter", SubComponent("a"), 0, true},
{"date", SubComponent("20060102"), 20060102, false},
{"empty", SubComponent(""), 0, true},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := tt.value.Int()
if tt.wantErr {
assert.Error(t, err)
} else {
assert.Nil(t, err)
}
assert.Equal(t, tt.want, got)
})
}
}
func TestSubComponentDirtyString(t *testing.T) {
tests := []struct {
name string
value SubComponent
want string
}{
{"integer", SubComponent("1"), "1"},
{"letter", SubComponent("a"), "a"},
{"date", SubComponent("20060102"), "20060102"},
{"empty", SubComponent(""), ""},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := tt.value.DirtyString()
assert.Equal(t, tt.want, got)
})
}
}
func TestSubComponentTime(t *testing.T) {
tests := []struct {
name string
value SubComponent
want time.Time
wantErr bool
}{
{"date", SubComponent("20120505"), time.Date(2012, 5, 5, 0, 0, 0, 0, time.UTC), false},
{"date with hour", SubComponent("2012050509"), time.Date(2012, 5, 5, 9, 0, 0, 0, time.UTC), false},
{"date with time", SubComponent("201205050925"), time.Date(2012, 5, 5, 9, 25, 0, 0, time.UTC), false},
{"date with seconds", SubComponent("20120505092505"), time.Date(2012, 5, 5, 9, 25, 5, 0, time.UTC), false},
{"date with fractional seconds", SubComponent("20120505092505.1"), time.Date(2012, 5, 5, 9, 25, 5, 100000000, time.UTC), false},
{"date with fractional seconds", SubComponent("20120505092505.12"), time.Date(2012, 5, 5, 9, 25, 5, 120000000, time.UTC), false},
{"date with fractional seconds", SubComponent("20120505092505.123"), time.Date(2012, 5, 5, 9, 25, 5, 123000000, time.UTC), false},
{"date with fractional seconds", SubComponent("20120505092505.1234"), time.Date(2012, 5, 5, 9, 25, 5, 123400000, time.UTC), false},
{"invalid format", SubComponent("2012-05-05"), time.Time{}, true},
{"invalid number of characters", SubComponent("2"), time.Time{}, true},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := tt.value.Time()
if tt.wantErr {
assert.Error(t, err)
} else {
assert.Nil(t, err)
}
assert.Equal(t, tt.want, got)
})
}
}