forked from suyashkumar/dicom
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathelement_test.go
146 lines (136 loc) · 3.35 KB
/
element_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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
package dicom
import (
"encoding/json"
"testing"
"github.com/google/go-cmp/cmp"
"github.com/suyashkumar/dicom/pkg/tag"
)
func TestElement_MarshalJSON_NestedElements(t *testing.T) {
nestedData := [][]*Element{
{
{
Tag: tag.PatientName,
ValueRepresentation: tag.VRString,
Value: &stringsValue{
value: []string{"Bob"},
},
},
},
}
seqElement := makeSequenceElement(tag.AddOtherSequence, nestedData)
want := `{"tag":{"Group":70,"Element":258},"VR":9,"rawVR":"SQ","valueLength":0,"value":[[{"tag":{"Group":16,"Element":16},"VR":2,"rawVR":"","valueLength":0,"value":["Bob"]}]]}`
j, err := json.Marshal(seqElement)
if err != nil {
t.Errorf("unexpected error marshaling json: %v", err)
}
if string(j) != want {
t.Errorf("json.Marshal(%v) produced incorrect output. got: %s, want: %s", seqElement, string(j), want)
}
}
func TestElement_String(t *testing.T) {
e := &Element{
Tag: tag.Rows,
ValueRepresentation: tag.VRInt32List,
RawValueRepresentation: "US",
Value: &intsValue{
value: []int{100},
},
}
want := "[\n" +
" Tag: (0028,0010)\n" +
" Tag Name: Rows\n" +
" VR: VRInt32List\n" +
" VR Raw: US\n" +
" VL: 0\n" +
" Value: [100]\n" +
"]\n\n"
got := e.String()
if want != got {
t.Errorf("String(%v) unexpected diff. got:\n%s want:\n%s", e, got, want)
}
}
func TestNewValue(t *testing.T) {
cases := []struct {
name string
data interface{}
wantValue Value
wantError error
}{
{
name: "strings",
data: []string{"a", "b"},
wantValue: &stringsValue{value: []string{"a", "b"}},
wantError: nil,
},
{
name: "floats",
data: []float64{1.11, 1.22},
wantValue: &floatsValue{value: []float64{1.11, 1.22}},
wantError: nil,
},
{
name: "ints",
data: []int{1, 2},
wantValue: &intsValue{value: []int{1, 2}},
wantError: nil,
},
{
name: "bytes",
data: []byte{0x00, 0x01},
wantValue: &bytesValue{value: []byte{0x00, 0x01}},
wantError: nil,
},
{
// TODO: maybe enhance this case
name: "PixelDataInfo",
data: PixelDataInfo{IsEncapsulated: true},
wantValue: &pixelDataValue{PixelDataInfo{IsEncapsulated: true}},
wantError: nil,
},
{
name: "sequence",
data: [][]*Element{
{
{
Tag: tag.PatientName,
ValueRepresentation: tag.VRString,
Value: &stringsValue{
value: []string{"Bob"},
},
},
},
},
wantValue: &sequencesValue{value: []*SequenceItemValue{
{
elements: []*Element{
{
Tag: tag.PatientName,
ValueRepresentation: tag.VRString,
Value: &stringsValue{
value: []string{"Bob"},
},
},
},
},
}},
},
}
for _, tc := range cases {
t.Run(tc.name, func(t *testing.T) {
v, err := NewValue(tc.data)
if err != tc.wantError {
t.Fatalf("NewValue(%v) returned unexpected error: %v", tc.data, err)
}
if diff := cmp.Diff(v, tc.wantValue, cmp.AllowUnexported(allValues...)); diff != "" {
t.Errorf("NewValue(%v) unexpected value. diff: %v", tc.data, diff)
}
})
}
}
func TestNewValue_UnexpectedType(t *testing.T) {
data := 10
_, err := NewValue(data)
if err != ErrorUnexpectedDataType {
t.Errorf("NewValue(%v) expected an error. got: %v, want: %v", data, err, ErrorUnexpectedDataType)
}
}