-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathudt_write_test.go
133 lines (117 loc) · 3.49 KB
/
udt_write_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
package gologix
import (
"strings"
"testing"
)
func TestMultiWriteToDict(t *testing.T) {
type TestUDT struct {
Field1 int32
Field2 float32
}
type test_str struct {
TestSint byte `gologix:"TestSint"`
TestInt int16 `gologix:"TestInt"`
TestDint int32 `gologix:"TestDint"`
TestReal float32 `gologix:"TestReal"`
TestDintArr0 int32 `gologix:"testdintarr[0]"`
TestDintArr0_0 bool `gologix:"testdintarr[0].0"`
TestDintArr0_9 bool `gologix:"testdintarr[0].9"`
TestDintArr2 int32 `gologix:"testdintarr[2]"`
TestUDTField1 int32 `gologix:"testudt.field1"`
TestUDTField2 float32 `gologix:"testudt.field2"`
TestUDTArr2Field1 int32 `gologix:"testudtarr[2].field1"`
TestUDTArr2Field2 float32 `gologix:"testudtarr[2].field2"`
}
type test_str2 struct {
TestSint byte `gologix:"TestSint"`
TestInt int16 `gologix:"TestInt"`
TestDint int32 `gologix:"TestDint"`
TestReal float32 `gologix:"TestReal"`
TestDintArr0 int32 `gologix:"testdintarr[0]"`
TestDintArr0_0 bool `gologix:"testdintarr[0].0"`
TestDintArr0_9 bool `gologix:"testdintarr[0].9"`
TestDintArr2 int32 `gologix:"testdintarr[2]"`
TestUDT TestUDT `gologix:"testudt"`
TestUDTArr2Field1 int32 `gologix:"testudtarr[2].field1"`
TestUDTArr2Field2 float32 `gologix:"testudtarr[2].field2"`
}
read := test_str{
TestSint: 117,
TestInt: 999,
TestDint: 36,
TestReal: 93.45,
TestDintArr0: 4351,
TestDintArr0_0: true,
TestDintArr0_9: false,
TestDintArr2: 4353,
TestUDTField1: 85456,
TestUDTField2: 123.456,
TestUDTArr2Field1: 16,
TestUDTArr2Field2: 15.0,
}
read2 := test_str2{
TestSint: 117,
TestInt: 999,
TestDint: 36,
TestReal: 93.45,
TestDintArr0: 4351,
TestDintArr0_0: true,
TestDintArr0_9: false,
TestDintArr2: 4353,
TestUDT: TestUDT{85456, 123.456},
TestUDTArr2Field1: 16,
TestUDTArr2Field2: 15.0,
}
wants := map[string]interface{}{
"testsint": byte(117),
"testint": int16(999),
"testdint": int32(36),
"testreal": float32(93.45),
"testdintarr[0]": int32(4351),
"testdintarr[0].0": true,
"testdintarr[0].9": false,
"testdintarr[2]": int32(4353),
"testudt.field1": int32(85456),
"testudt.field2": float32(123.456),
"testudtarr[2].field1": int32(16),
"testudtarr[2].field2": float32(15.0),
}
have, err := multi_to_dict(read)
if err != nil {
t.Errorf("problem creating dict. %v", err)
}
for k := range have {
if wants[strings.ToLower(k)] != have[k] {
t.Errorf("key %s is not a match. Have %v want %v", k, have[k], wants[k])
}
}
have, err = multi_to_dict(read2)
if err != nil {
t.Errorf("problem creating dict 2. %v", err)
}
for k := range have {
if wants[strings.ToLower(k)] != have[k] {
t.Errorf("key %s is not a match. Have %v want %v", k, have[k], wants[k])
}
}
}
func TestStructToDict(t *testing.T) {
type TestUDT struct {
Field1 int32
Field2 float32
}
udt := TestUDT{Field1: 15, Field2: 5.1}
d, err := udt_to_dict("prefix", udt)
if err != nil {
t.Errorf("problem creating dict. %v", err)
}
want := map[string]interface{}{
"prefix.Field1": int32(15),
"prefix.Field2": float32(5.1),
}
for k := range d {
if d[k] != want[k] {
t.Errorf("want %v got %v", want[k], d[k])
}
}
}