@@ -35,25 +35,32 @@ func TestDecoder(t *testing.T) {
35
35
// The whitespace doesn't affect the input TOML, and we cue/format on the "want" CUE source,
36
36
// so the added newlines and tabs don't change the test behavior.
37
37
tests := []struct {
38
- name string
39
- input string
40
- want string
38
+ name string
39
+ input string
40
+ wantCUE string
41
+ wantErr string
41
42
}{{
42
- name : "Empty" ,
43
- input : "" ,
44
- want : "" ,
43
+ name : "Empty" ,
44
+ input : "" ,
45
+ wantCUE : "" ,
45
46
}, {
46
47
name : "LoneComment" ,
47
48
input : `
48
49
# Just a comment
49
50
` ,
50
- want : "" ,
51
+ wantCUE : "" ,
52
+ }, {
53
+ name : "RootKeyMissing" ,
54
+ input : `
55
+ = "no key name"
56
+ ` ,
57
+ wantErr : "invalid character at start of key: =" ,
51
58
}, {
52
59
name : "RootKeysOne" ,
53
60
input : `
54
61
key = "value"
55
62
` ,
56
- want : `
63
+ wantCUE : `
57
64
key: "value"
58
65
` ,
59
66
}, {
@@ -63,7 +70,7 @@ func TestDecoder(t *testing.T) {
63
70
key2 = "value2"
64
71
key3 = "value3"
65
72
` ,
66
- want : `
73
+ wantCUE : `
67
74
key1: "value1"
68
75
key2: "value2"
69
76
key3: "value3"
@@ -75,7 +82,7 @@ func TestDecoder(t *testing.T) {
75
82
b1.b2 = "B"
76
83
c1.c2.c3 = "C"
77
84
` ,
78
- want : `
85
+ wantCUE : `
79
86
a1: "A"
80
87
b1: b2: "B"
81
88
c1: c2: c3: "C"
@@ -87,7 +94,7 @@ func TestDecoder(t *testing.T) {
87
94
a_b = "underscores"
88
95
123 = "numbers"
89
96
` ,
90
- want : `
97
+ wantCUE : `
91
98
"a-b": "dashes"
92
99
a_b: "underscores"
93
100
"123": "numbers"
@@ -99,7 +106,7 @@ func TestDecoder(t *testing.T) {
99
106
"foo bar" = "quoted space"
100
107
'foo "bar"' = "nested quotes"
101
108
` ,
102
- want : `
109
+ wantCUE : `
103
110
"1.2.3": "quoted dots"
104
111
"foo bar": "quoted space"
105
112
"foo \"bar\"": "nested quotes"
@@ -109,28 +116,23 @@ func TestDecoder(t *testing.T) {
109
116
input : `
110
117
site."foo.com".title = "foo bar"
111
118
` ,
112
- want : `
119
+ wantCUE : `
113
120
site: "foo.com": title: "foo bar"
114
121
` ,
115
122
}, {
116
- // TODO(mvdan): the TOML spec says that defining a key multiple times is invalid,
117
- // we should error even though this can be OK in CUE as long as the values unify.
118
123
name : "RootKeysDuplicate" ,
119
124
input : `
120
125
foo = "same value"
121
126
foo = "same value"
122
127
` ,
123
- want : `
124
- foo: "same value"
125
- foo: "same value"
126
- ` ,
128
+ wantErr : `duplicate key: foo` ,
127
129
}, {
128
130
name : "BasicStrings" ,
129
131
input : `
130
132
escapes = "foo \"bar\" \n\t\\ baz"
131
133
unicode = "foo \u00E9"
132
134
` ,
133
- want : `
135
+ wantCUE : `
134
136
escapes: "foo \"bar\" \n\t\\ baz"
135
137
unicode: "foo é"
136
138
` ,
@@ -153,7 +155,7 @@ line one \
153
155
line two.\
154
156
"""
155
157
` ,
156
- want : `
158
+ wantCUE : `
157
159
nested: " can contain \"\" quotes "
158
160
four: "\"four\""
159
161
double: "line one\nline two"
@@ -169,7 +171,7 @@ line two.\
169
171
quoted = 'Tom "Dubs" Preston-Werner'
170
172
regex = '<\i\c*\s*>'
171
173
` ,
172
- want : `
174
+ wantCUE : `
173
175
winpath: "C:\\Users\\nodejs\\templates"
174
176
winpath2: "\\\\ServerX\\admin$\\system32\\"
175
177
quoted: "Tom \"Dubs\" Preston-Werner"
@@ -194,7 +196,7 @@ line one \
194
196
line two.\
195
197
'''
196
198
` ,
197
- want : `
199
+ wantCUE : `
198
200
nested: " can contain '' quotes "
199
201
four: "'four'"
200
202
double: "line one\nline two"
@@ -213,7 +215,7 @@ line two.\
213
215
octal = 0o755
214
216
binary = 0b11010110
215
217
` ,
216
- want : `
218
+ wantCUE : `
217
219
zero: 0
218
220
positive: 123
219
221
plus: +40
@@ -234,7 +236,7 @@ line two.\
234
236
exponent_minus = -2E-4
235
237
exponent_dot = 6.789e-30
236
238
` ,
237
- want : `
239
+ wantCUE : `
238
240
pi: 3.1415
239
241
plus: +1.23
240
242
minus: -4.56
@@ -249,7 +251,7 @@ line two.\
249
251
positive = true
250
252
negative = false
251
253
` ,
252
- want : `
254
+ wantCUE : `
253
255
positive: true
254
256
negative: false
255
257
` ,
@@ -263,7 +265,7 @@ line two.\
263
265
strings = [ "all", 'strings', """are the same""", '''type''' ]
264
266
mixed_numbers = [ 0.1, 0.2, 0.5, 1, 2, 5 ]
265
267
` ,
266
- want : `
268
+ wantCUE : `
267
269
integers: [1, 2, 3]
268
270
colors: ["red", "yellow", "green"]
269
271
nested_ints: [[1, 2], [3, 4, 5]]
@@ -280,13 +282,20 @@ line two.\
280
282
dec := toml .NewDecoder (strings .NewReader (test .input ))
281
283
282
284
node , err := dec .Decode ()
285
+ if test .wantErr != "" {
286
+ qt .Assert (t , qt .ErrorMatches (err , test .wantErr ))
287
+ qt .Assert (t , qt .IsNil (node ))
288
+ // We don't continue, so we can't expect any decoded CUE.
289
+ qt .Assert (t , qt .Equals (test .wantCUE , "" ))
290
+ return
291
+ }
283
292
qt .Assert (t , qt .IsNil (err ))
284
293
285
294
node2 , err := dec .Decode ()
286
295
qt .Assert (t , qt .IsNil (node2 ))
287
296
qt .Assert (t , qt .Equals (err , io .EOF ))
288
297
289
- wantFormatted , err := format .Source ([]byte (test .want ))
298
+ wantFormatted , err := format .Source ([]byte (test .wantCUE ))
290
299
qt .Assert (t , qt .IsNil (err ))
291
300
292
301
formatted , err := format .Node (node )
@@ -301,12 +310,6 @@ line two.\
301
310
qt .Assert (t , qt .IsNil (val .Err ()))
302
311
qt .Assert (t , qt .IsNil (val .Validate ()))
303
312
304
- // See the TODO above; go-toml rejects duplicate keys per the spec,
305
- // but our decoder does not yet.
306
- if test .name == "RootKeysDuplicate" {
307
- return
308
- }
309
-
310
313
// Validate that the decoded CUE value is equivalent
311
314
// to the Go value that a direct TOML unmarshal produces.
312
315
// We use JSON equality as some details such as which integer types are used
0 commit comments