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
|
package scfg
import (
"bytes"
"testing"
)
func TestWrite(t *testing.T) {
for _, tc := range []struct {
src Block
want string
err error
}{
{
src: Block{},
want: "",
},
{
src: Block{{
Name: "dir",
Children: Block{{
Name: "blk1",
Params: []string{"p1", `"p2"`},
Children: Block{
{
Name: "sub1",
Params: []string{"arg11", "arg12"},
},
{
Name: "sub2",
Params: []string{"arg21", "arg22"},
},
{
Name: "sub3",
Params: []string{"arg31", "arg32"},
Children: Block{
{
Name: "sub-sub1",
},
{
Name: "sub-sub2",
Params: []string{"arg321", "arg322"},
},
},
},
},
}},
}},
want: `dir {
blk1 p1 "\"p2\"" {
sub1 arg11 arg12
sub2 arg21 arg22
sub3 arg31 arg32 {
sub-sub1
sub-sub2 arg321 arg322
}
}
}
`,
},
{
src: Block{{Name: "dir1"}},
want: "dir1\n",
},
{
src: Block{{Name: "dir\"1"}},
want: "\"dir\\\"1\"\n",
},
{
src: Block{{Name: "dir'1"}},
want: "\"dir'1\"\n",
},
{
src: Block{{Name: "dir:}"}},
want: "\"dir:}\"\n",
},
{
src: Block{{Name: "dir:{"}},
want: "\"dir:{\"\n",
},
{
src: Block{{Name: "dir\t1"}},
want: `"dir` + "\t" + `1"` + "\n",
},
{
src: Block{{Name: "dir 1"}},
want: "\"dir 1\"\n",
},
{
src: Block{{Name: "dir1", Params: []string{"arg1", "arg2", `"arg3"`}}},
want: "dir1 arg1 arg2 " + `"\"arg3\""` + "\n",
},
{
src: Block{{Name: "dir1", Params: []string{"arg1", "arg 2", "arg'3"}}},
want: "dir1 arg1 \"arg 2\" \"arg'3\"\n",
},
{
src: Block{{Name: "dir1", Params: []string{"arg1", "", "arg3"}}},
want: "dir1 arg1 \"\" arg3\n",
},
{
src: Block{{Name: "dir1", Params: []string{"arg1", `"` + "\"\"" + `"`, "arg3"}}},
want: "dir1 arg1 " + `"\"\"\"\""` + " arg3\n",
},
{
src: Block{{
Name: "dir1",
Children: Block{
{Name: "sub1"},
{Name: "sub2", Params: []string{"arg1", "arg2"}},
},
}},
want: `dir1 {
sub1
sub2 arg1 arg2
}
`,
},
{
src: Block{{Name: ""}},
err: errDirEmptyName,
},
{
src: Block{{
Name: "dir",
Children: Block{
{Name: "sub1"},
{Name: "", Children: Block{{Name: "sub21"}}},
},
}},
err: errDirEmptyName,
},
} {
t.Run("", func(t *testing.T) {
var buf bytes.Buffer
err := Write(&buf, tc.src)
switch {
case err != nil && tc.err != nil:
if got, want := err.Error(), tc.err.Error(); got != want {
t.Fatalf("invalid error:\ngot= %q\nwant=%q", got, want)
}
return
case err == nil && tc.err != nil:
t.Fatalf("got err=nil, want=%q", tc.err.Error())
case err != nil && tc.err == nil:
t.Fatalf("could not marshal: %+v", err)
case err == nil && tc.err == nil:
// ok.
}
if got, want := buf.String(), tc.want; got != want {
t.Fatalf(
"invalid marshal representation:\ngot:\n%s\nwant:\n%s\n---",
got, want,
)
}
})
}
}
|