-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrow_test.go
133 lines (105 loc) · 2.86 KB
/
row_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 envi
import (
"strings"
"testing"
)
func TestNewRow(t *testing.T) {
row := NewRow(`test-key`, `text`)
if row.Value != `text` {
t.Fatalf("should be `text`")
}
if row.Key != `TEST_KEY` {
t.Fatalf("should be `TEST_KEY`")
}
if row.Comment != `` {
t.Fatalf("should be ``")
}
row.SetComment(`Comment`)
if row.Comment != `Comment` {
t.Fatalf("should be `Comment`")
}
}
func TestRow_Marshal(t *testing.T) {
exp := `"text2"`
row := NewRow(`test-key`, `text2`)
str, err := row.Marshal()
if err != nil {
t.Fatalf("should be `nil`")
}
if str != `TEST_KEY=`+exp {
t.Fatalf("`%s` should be `TEST_KEY=%s`", str, exp)
}
exp = `"Hello tesxt!"`
row = NewRow(`test-key`, `Hello tesxt!`)
str, err = row.Marshal()
if err != nil {
t.Fatalf("should be `nil`")
}
if str != `TEST_KEY=`+exp {
t.Fatalf("`%s` should be `TEST_KEY=%s`", str, exp)
}
}
func TestRow_Unmarshal_1(t *testing.T) {
exp := `"Hello tesxt!"`
line := `TEST_KEY=` + exp
r := &row{}
err := r.Unmarshal(line)
if err != nil || r.Key != `TEST_KEY` || r.Value != `Hello tesxt!` || r.Comment != `` {
t.Fatal("Wrong!`")
}
}
func TestRow_Unmarshal_MultiComment(t *testing.T) {
line := []string{` #### <-- Comment --> ####`, `# sub-comment`, `TEST_KEY=432`}
//line := []string{` #### --[ Comment ]-- ####`, `# sub-comment`, `TEST_KEY=432`}
r := &row{}
err := r.Unmarshal(strings.Join(line, "\n"))
if err != nil || r.Key != `TEST_KEY` || r.Value != `432` || r.Comment != "<-- Comment -->\nsub-comment" {
t.Fatal("Wrong!`")
}
}
func TestNormalizeKey(t *testing.T) {
list := map[string]string{
`test-test`: `TEST_TEST`,
`tes-2__-t-test`: `TES_2_T_TEST`,
`t---sa---=-ss-es-2__-t-test`: `T_SA_SS_ES_2_T_TEST`,
`test.test`: `TEST.TEST`,
}
for in, exp := range list {
if out := normalizeKey(in); out != exp {
t.Fatalf(`Must be equal: '%s' == '%s'`, out, exp)
}
}
}
func TestNormalizeValue(t *testing.T) {
list := map[string]string{
``: ``,
`1`: `1`,
`0122`: `122`,
`"012"`: `"012"`,
`'0122'`: `"0122"`,
`0.5`: `"0.5"`,
`3m`: `"3m"`,
`"rob,ken,robert1"`: `"rob,ken,robert1"`,
`'rob,ken,robert2'`: `"rob,ken,robert2"`,
`rob,ken,robert3`: `"rob,ken,robert3"`,
`red:1,green:2,blue:3`: `"red:1,green:2,blue:3"`,
`true`: `true`,
`false`: `false`,
}
for in, exp := range list {
if out := normalizeValue(in); out != exp {
t.Fatalf(`Must be equal: [%s] == [%s]`, out, exp)
}
}
}
func TestMergeRowMap(t *testing.T) {
rows1 := map[string]*row{
`rob`: NewRow(`rob`, `"robert3"`),
`bool`: NewRow(`bool`, `true`),
}
rows2 := map[string]*row{
`test`: NewRow(`test`, `text`),
`bool`: NewRow(`bool`, `false`),
}
mergeRowMap(rows1, rows2)
}