@@ -205,17 +205,19 @@ object1 = {
205
205
206
206
func TestRead (t * testing.T ) {
207
207
cases := map [string ]struct {
208
- input string
209
- query string
210
- options []hcledit.Option
211
- want map [string ]interface {}
208
+ input string
209
+ query string
210
+ options []hcledit.Option
211
+ expectErr bool
212
+ want map [string ]interface {}
212
213
}{
213
214
"Attribute" : {
214
215
input : `
215
216
attribute = "R"
216
217
` ,
217
- query : "attribute" ,
218
- options : make ([]hcledit.Option , 0 ),
218
+ query : "attribute" ,
219
+ options : make ([]hcledit.Option , 0 ),
220
+ expectErr : false ,
219
221
want : map [string ]interface {}{
220
222
"attribute" : "R" ,
221
223
},
@@ -228,9 +230,10 @@ block "label1" "label2" {
228
230
attribute = "str"
229
231
}
230
232
` ,
231
- options : make ([]hcledit.Option , 0 ),
232
- query : "block" ,
233
- want : map [string ]interface {}{},
233
+ options : make ([]hcledit.Option , 0 ),
234
+ expectErr : false ,
235
+ query : "block" ,
236
+ want : map [string ]interface {}{},
234
237
},
235
238
236
239
"AttributeInBlock1" : {
@@ -239,8 +242,9 @@ block "label1" "label2" {
239
242
attribute = "R"
240
243
}
241
244
` ,
242
- options : make ([]hcledit.Option , 0 ),
243
- query : "block.label1.label2.attribute" ,
245
+ options : make ([]hcledit.Option , 0 ),
246
+ expectErr : false ,
247
+ query : "block.label1.label2.attribute" ,
244
248
want : map [string ]interface {}{
245
249
"block.label1.label2.attribute" : "R" ,
246
250
},
@@ -254,8 +258,9 @@ block1 "label1" "label2" {
254
258
}
255
259
}
256
260
` ,
257
- options : make ([]hcledit.Option , 0 ),
258
- query : "block1.label1.label2.block2.label3.label4.attribute" ,
261
+ options : make ([]hcledit.Option , 0 ),
262
+ expectErr : false ,
263
+ query : "block1.label1.label2.block2.label3.label4.attribute" ,
259
264
want : map [string ]interface {}{
260
265
"block1.label1.label2.block2.label3.label4.attribute" : "R" ,
261
266
},
@@ -272,8 +277,9 @@ block "label" "label2" {
272
277
}
273
278
274
279
` ,
275
- options : make ([]hcledit.Option , 0 ),
276
- query : "block.label.*.attribute" ,
280
+ options : make ([]hcledit.Option , 0 ),
281
+ expectErr : false ,
282
+ query : "block.label.*.attribute" ,
277
283
want : map [string ]interface {}{
278
284
"block.label.label1.attribute" : "R" ,
279
285
"block.label.label2.attribute" : "R" ,
@@ -286,8 +292,9 @@ object = {
286
292
attribute = "R"
287
293
}
288
294
` ,
289
- options : make ([]hcledit.Option , 0 ),
290
- query : "object.attribute" ,
295
+ options : make ([]hcledit.Option , 0 ),
296
+ expectErr : false ,
297
+ query : "object.attribute" ,
291
298
want : map [string ]interface {}{
292
299
"object.attribute" : "R" ,
293
300
},
@@ -300,8 +307,9 @@ object1 = {
300
307
}
301
308
}
302
309
` ,
303
- options : make ([]hcledit.Option , 0 ),
304
- query : "object1.object2.attribute" ,
310
+ options : make ([]hcledit.Option , 0 ),
311
+ expectErr : false ,
312
+ query : "object1.object2.attribute" ,
305
313
want : map [string ]interface {}{
306
314
"object1.object2.attribute" : "R" ,
307
315
},
@@ -311,8 +319,9 @@ object1 = {
311
319
input : `
312
320
attribute = 1
313
321
` ,
314
- options : make ([]hcledit.Option , 0 ),
315
- query : "attribute" ,
322
+ options : make ([]hcledit.Option , 0 ),
323
+ expectErr : false ,
324
+ query : "attribute" ,
316
325
want : map [string ]interface {}{
317
326
"attribute" : 1 ,
318
327
},
@@ -322,8 +331,9 @@ attribute = 1
322
331
input : `
323
332
attribute = "str"
324
333
` ,
325
- options : make ([]hcledit.Option , 0 ),
326
- query : "attribute" ,
334
+ options : make ([]hcledit.Option , 0 ),
335
+ expectErr : false ,
336
+ query : "attribute" ,
327
337
want : map [string ]interface {}{
328
338
"attribute" : "str" ,
329
339
},
@@ -333,8 +343,9 @@ attribute = "str"
333
343
input : `
334
344
attribute = true
335
345
` ,
336
- options : make ([]hcledit.Option , 0 ),
337
- query : "attribute" ,
346
+ options : make ([]hcledit.Option , 0 ),
347
+ expectErr : false ,
348
+ query : "attribute" ,
338
349
want : map [string ]interface {}{
339
350
"attribute" : true ,
340
351
},
@@ -344,8 +355,9 @@ attribute = true
344
355
input : `
345
356
attribute = false
346
357
` ,
347
- options : make ([]hcledit.Option , 0 ),
348
- query : "attribute" ,
358
+ options : make ([]hcledit.Option , 0 ),
359
+ expectErr : false ,
360
+ query : "attribute" ,
349
361
want : map [string ]interface {}{
350
362
"attribute" : false ,
351
363
},
@@ -355,8 +367,9 @@ attribute = false
355
367
input : `
356
368
attribute = ["str1", "str2", "str3"]
357
369
` ,
358
- options : make ([]hcledit.Option , 0 ),
359
- query : "attribute" ,
370
+ options : make ([]hcledit.Option , 0 ),
371
+ expectErr : false ,
372
+ query : "attribute" ,
360
373
want : map [string ]interface {}{
361
374
"attribute" : []string {"str1" , "str2" , "str3" },
362
375
},
@@ -366,8 +379,9 @@ attribute = ["str1", "str2", "str3"]
366
379
input : `
367
380
attribute = [1, 2, 3]
368
381
` ,
369
- options : make ([]hcledit.Option , 0 ),
370
- query : "attribute" ,
382
+ options : make ([]hcledit.Option , 0 ),
383
+ expectErr : false ,
384
+ query : "attribute" ,
371
385
want : map [string ]interface {}{
372
386
"attribute" : []int {1 , 2 , 3 },
373
387
},
@@ -377,8 +391,9 @@ attribute = [1, 2, 3]
377
391
input : `
378
392
attribute = [true, false, true]
379
393
` ,
380
- options : make ([]hcledit.Option , 0 ),
381
- query : "attribute" ,
394
+ options : make ([]hcledit.Option , 0 ),
395
+ expectErr : false ,
396
+ query : "attribute" ,
382
397
want : map [string ]interface {}{
383
398
"attribute" : []bool {true , false , true },
384
399
},
@@ -388,8 +403,9 @@ attribute = [true, false, true]
388
403
input : `
389
404
attribute = local.var
390
405
` ,
391
- options : []hcledit.Option {hcledit .WithReadFallbackToRawString ()},
392
- query : "attribute" ,
406
+ options : []hcledit.Option {hcledit .WithReadFallbackToRawString ()},
407
+ expectErr : true ,
408
+ query : "attribute" ,
393
409
want : map [string ]interface {}{
394
410
"attribute" : "local.var" ,
395
411
},
@@ -399,8 +415,9 @@ attribute = local.var
399
415
input : `
400
416
attribute = "some-${local.var}"
401
417
` ,
402
- options : []hcledit.Option {hcledit .WithReadFallbackToRawString ()},
403
- query : "attribute" ,
418
+ options : []hcledit.Option {hcledit .WithReadFallbackToRawString ()},
419
+ expectErr : true ,
420
+ query : "attribute" ,
404
421
want : map [string ]interface {}{
405
422
"attribute" : `"some-${local.var}"` ,
406
423
},
@@ -416,7 +433,7 @@ attribute = "some-${local.var}"
416
433
}
417
434
418
435
got , err := editor .Read (tc .query , tc .options ... )
419
- if err != nil {
436
+ if ! tc . expectErr && err != nil {
420
437
t .Fatal (err )
421
438
}
422
439
0 commit comments