-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathunparse_test.go
132 lines (122 loc) · 6.04 KB
/
unparse_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
package cnfg_test
import (
"fmt"
"net"
"strconv"
"strings"
"testing"
"time"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"golift.io/cnfg"
)
const base10 = 10
type MarshalTest struct {
Name string `xml:"name,omitempty"`
Pass string `xml:"pass,omitempty"`
IP net.IP `xml:"ip,omitempty"`
Smap map[string]string `xml:"smap,omitempty"`
Imap map[string]interface{} `xml:"imap,omitempty"`
List []string `xml:"list,omitempty"`
Byte []byte `xml:"byte,omitempty"`
Dur time.Duration `xml:"dur,omitempty"`
Time time.Time `xml:"time,omitempty"`
Err error `xml:"err,omitempty"`
Bool bool `xml:"bool,omitempty"`
Uint uint8 `xml:"uint,omitempty"`
Un16 uint16 `xml:"un16,omitempty"`
Un32 uint32 `xml:"un32,omitempty"`
Un64 uint64 `xml:"un64,omitempty"`
Int int `xml:"int,omitempty"`
In8 int8 `xml:"in8,omitempty"`
In16 int16 `xml:"in16,omitempty"`
In32 int32 `xml:"in32,omitempty"`
In64 int64 `xml:"in64,omitempty"`
Fl32 float32 `xml:"fl32,omitempty"`
Fl64 float64 `xml:"fl64,omitempty"`
Test2 marshalTest2 `xml:"test2"`
Test *MarshalTest `xml:"test"`
}
type marshalTest2 struct {
// not setting Name2 creates empty variables (no omitempty)
Name2 string `xml:""` // non-anonymous struct members will use their name if no struct tag name.
Ignore string `xml:"-"`
*MarshalTest // anonymous struct memebrs do not have their names exposed.
}
func marshalTestData() (*MarshalTest, int) {
return &MarshalTest{
Name: "my name is golift", // 1
Pass: "supersecret", // 2
IP: net.ParseIP("127.0.0.1"), // 3
Smap: map[string]string{"blue": "sky", "red": "lava"}, // 4, 5
List: []string{"humble", "beginnings"}, // 6, 7
Byte: []byte("some bytes for dinner"), // 8
Uint: 1, // 9
Un16: 16, // 10
Un32: 32, // 11
Un64: 64, // 12
Time: time.Now(), // 13
Dur: 15 * time.Minute, // 14
Int: 1, // 15
In8: 8, // 16
In16: 16, // 17
In32: 32, // 18
In64: 64, // 19
Bool: true, // 20
Fl32: 32.32, // 21
Fl64: 64.64, // 22
Test2: marshalTest2{MarshalTest: &MarshalTest{Name: "supersubname"}}, // 23
Test: &MarshalTest{
Name: "subtest", // 24
Err: fmt.Errorf("this long error is here 2 line up the comments"), // 25
}, Imap: map[string]interface{}{
"orange": "sunset", // 26
"pink": "sunrise", // 27
"counter": 8967, // 28
"floater": 3.1415926, // 29
}, // + 3 more from marshalTest2.Name2. That puts the total var count at 32.
}, 32 // set the count here.
}
func TestDeconStruct(t *testing.T) {
t.Parallel()
assert := assert.New(t)
data, count := marshalTestData()
pairs, err := cnfg.MarshalENV(data, "PFX")
require.NoError(t, err)
assert.Equal(data.Name, pairs["PFX_NAME"])
assert.Equal(data.Pass, pairs["PFX_PASS"])
assert.Equal(data.IP.String(), pairs["PFX_IP"])
assert.Equal(data.Smap["blue"], pairs["PFX_SMAP_blue"])
assert.Equal(data.Smap["red"], pairs["PFX_SMAP_red"])
assert.Equal(data.Imap["orange"], pairs["PFX_IMAP_orange"])
assert.Equal(data.Imap["pink"], pairs["PFX_IMAP_pink"])
assert.Equal(fmt.Sprint(data.Imap["counter"]), pairs["PFX_IMAP_counter"])
assert.Equal(fmt.Sprint(data.Imap["floater"]), pairs["PFX_IMAP_floater"])
assert.Equal(data.List[0], pairs["PFX_LIST_0"])
assert.Equal(data.List[1], pairs["PFX_LIST_1"])
assert.Equal(string(data.Byte), pairs["PFX_BYTE"])
assert.Equal(strconv.FormatUint(uint64(data.Uint), base10), pairs["PFX_UINT"])
assert.Equal(strconv.FormatUint(uint64(data.Un16), base10), pairs["PFX_UN16"])
assert.Equal(strconv.FormatUint(uint64(data.Un32), base10), pairs["PFX_UN32"])
assert.Equal(strconv.FormatUint(data.Un64, base10), pairs["PFX_UN64"])
assert.Equal(data.Dur.String(), pairs["PFX_DUR"])
assert.Equal(strconv.FormatInt(int64(data.Int), base10), pairs["PFX_INT"])
assert.Equal(strconv.FormatInt(int64(data.In8), base10), pairs["PFX_IN8"])
assert.Equal(strconv.FormatInt(int64(data.In16), base10), pairs["PFX_IN16"])
assert.Equal(strconv.FormatInt(int64(data.In32), base10), pairs["PFX_IN32"])
assert.Equal(strconv.FormatInt(data.In64, base10), pairs["PFX_IN64"])
assert.Equal("true", pairs["PFX_BOOL"])
assert.Equal(data.Test2.MarshalTest.Name, pairs["PFX_TEST2_NAME"])
assert.Equal(data.Test2.Name, pairs["PFX_TEST2_NAME"])
assert.Equal("32.32", pairs["PFX_FL32"])
assert.Equal("64.64", pairs["PFX_FL64"])
assert.Equal(data.Test.Name, pairs["PFX_TEST_NAME"])
assert.Equal(data.Test.Err.Error(), pairs["PFX_TEST_ERR"])
assert.Len(pairs, count,
fmt.Sprintf("%d variables are created in marshalTestData, update as more tests are added.", count))
for _, v := range pairs.Quoted() {
// fmt.Println(v)
p := strings.Split(v, "=")
assert.Equal(`"`+pairs[p[0]]+`"`, p[1], "returned Slice() value is wrong")
}
}