-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcomponent_test.go
More file actions
188 lines (162 loc) · 5.34 KB
/
component_test.go
File metadata and controls
188 lines (162 loc) · 5.34 KB
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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
// Copyright 2026 Joshua Jones <joshua.jones.software@gmail.com>
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package hl7
import "testing"
func TestSubcomponent(t *testing.T) {
d := DefaultDelimiters()
t.Run("String", func(t *testing.T) {
sc := Subcomponent{Value: Value{raw: []byte("hello"), delims: d}}
if got := sc.String(); got != "hello" {
t.Errorf("String() = %q, want %q", got, "hello")
}
})
t.Run("Bytes", func(t *testing.T) {
sc := Subcomponent{Value: Value{raw: []byte("hello"), delims: d}}
if got := string(sc.Bytes()); got != "hello" {
t.Errorf("Bytes() = %q, want %q", got, "hello")
}
})
t.Run("IsEmpty", func(t *testing.T) {
sc := Subcomponent{Value: Value{raw: []byte{}, delims: d}}
if !sc.IsEmpty() {
t.Error("expected IsEmpty() = true")
}
sc2 := Subcomponent{Value: Value{raw: []byte("x"), delims: d}}
if sc2.IsEmpty() {
t.Error("expected IsEmpty() = false")
}
})
t.Run("with escape", func(t *testing.T) {
sc := Subcomponent{Value: Value{raw: []byte(`hello\F\world`), delims: d}}
if got := sc.String(); got != "hello|world" {
t.Errorf("String() = %q, want %q", got, "hello|world")
}
})
}
func TestComponent(t *testing.T) {
d := DefaultDelimiters()
t.Run("no subcomponents", func(t *testing.T) {
c := Component{Value: Value{raw: []byte("simple"), delims: d}}
if got := c.String(); got != "simple" {
t.Errorf("String() = %q, want %q", got, "simple")
}
if c.SubComponentCount() != 1 {
t.Errorf("SubComponentCount() = %d, want 1", c.SubComponentCount())
}
})
t.Run("with subcomponents", func(t *testing.T) {
c := Component{Value: Value{raw: []byte("sub1&sub2&sub3"), delims: d}}
if c.SubComponentCount() != 3 {
t.Errorf("SubComponentCount() = %d, want 3", c.SubComponentCount())
}
if got := c.SubComponent(1).String(); got != "sub1" {
t.Errorf("SubComponent(1) = %q, want %q", got, "sub1")
}
if got := c.SubComponent(2).String(); got != "sub2" {
t.Errorf("SubComponent(2) = %q, want %q", got, "sub2")
}
if got := c.SubComponent(3).String(); got != "sub3" {
t.Errorf("SubComponent(3) = %q, want %q", got, "sub3")
}
})
t.Run("out of range", func(t *testing.T) {
c := Component{Value: Value{raw: []byte("only"), delims: d}}
sub := c.SubComponent(5)
if !sub.IsEmpty() {
t.Error("expected out-of-range SubComponent to be empty")
}
sub0 := c.SubComponent(0)
if !sub0.IsEmpty() {
t.Error("expected index 0 SubComponent to be empty (1-based)")
}
})
t.Run("empty component", func(t *testing.T) {
c := Component{Value: Value{raw: []byte{}, delims: d}}
if !c.IsEmpty() {
t.Error("expected IsEmpty() = true")
}
})
}
func TestComponentIsNull(t *testing.T) {
d := DefaultDelimiters()
t.Run("null component", func(t *testing.T) {
c := Component{Value: Value{raw: []byte(`""`), delims: d}}
if !c.IsNull() {
t.Error("expected IsNull() = true for component with \"\"")
}
})
t.Run("non-null component", func(t *testing.T) {
c := Component{Value: Value{raw: []byte("value"), delims: d}}
if c.IsNull() {
t.Error("expected IsNull() = false for non-null component")
}
})
}
func TestComponentHasValue(t *testing.T) {
d := DefaultDelimiters()
t.Run("has value", func(t *testing.T) {
c := Component{Value: Value{raw: []byte("hello"), delims: d}}
if !c.HasValue() {
t.Error("expected HasValue() = true")
}
})
t.Run("empty has no value", func(t *testing.T) {
c := Component{Value: Value{raw: []byte{}, delims: d}}
if c.HasValue() {
t.Error("expected HasValue() = false for empty component")
}
})
t.Run("null has no value", func(t *testing.T) {
c := Component{Value: Value{raw: []byte(`""`), delims: d}}
if c.HasValue() {
t.Error("expected HasValue() = false for null component")
}
})
}
func TestSubcomponentIsNull(t *testing.T) {
d := DefaultDelimiters()
t.Run("null subcomponent", func(t *testing.T) {
sc := Subcomponent{Value: Value{raw: []byte(`""`), delims: d}}
if !sc.IsNull() {
t.Error("expected IsNull() = true for subcomponent with \"\"")
}
})
t.Run("non-null subcomponent", func(t *testing.T) {
sc := Subcomponent{Value: Value{raw: []byte("value"), delims: d}}
if sc.IsNull() {
t.Error("expected IsNull() = false for non-null subcomponent")
}
})
}
func TestSubcomponentHasValue(t *testing.T) {
d := DefaultDelimiters()
t.Run("has value", func(t *testing.T) {
sc := Subcomponent{Value: Value{raw: []byte("hello"), delims: d}}
if !sc.HasValue() {
t.Error("expected HasValue() = true")
}
})
t.Run("empty has no value", func(t *testing.T) {
sc := Subcomponent{Value: Value{raw: []byte{}, delims: d}}
if sc.HasValue() {
t.Error("expected HasValue() = false for empty subcomponent")
}
})
t.Run("null has no value", func(t *testing.T) {
sc := Subcomponent{Value: Value{raw: []byte(`""`), delims: d}}
if sc.HasValue() {
t.Error("expected HasValue() = false for null subcomponent")
}
})
}