File: decode_test.go

package info (click to toggle)
golang-github-cue-lang-cue 0.12.0.-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 19,072 kB
  • sloc: sh: 57; makefile: 17
file content (944 lines) | stat: -rw-r--r-- 20,173 bytes parent folder | download
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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
// Copyright 2024 The CUE Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package toml_test

import (
	"bytes"
	"encoding/json"
	"io"
	"math/bits"
	"path"
	"reflect"
	"strings"
	"testing"

	"github.com/go-quicktest/qt"
	gotoml "github.com/pelletier/go-toml/v2"

	"cuelang.org/go/cue/ast"
	"cuelang.org/go/cue/ast/astutil"
	"cuelang.org/go/cue/cuecontext"
	"cuelang.org/go/cue/errors"
	"cuelang.org/go/cue/format"
	"cuelang.org/go/cue/token"
	"cuelang.org/go/encoding/toml"
	"cuelang.org/go/internal/astinternal"
	"cuelang.org/go/internal/cuetxtar"
)

func TestDecoder(t *testing.T) {
	t.Parallel()
	// Note that we use backquoted Go string literals with indentation for readability.
	// The whitespace doesn't affect the input TOML, and we cue/format on the "want" CUE source,
	// so the added newlines and tabs don't change the test behavior.
	tests := []struct {
		name    string
		input   string
		wantCUE string
		wantErr string
	}{{
		name:    "Empty",
		input:   "",
		wantCUE: "",
	}, {
		name: "LoneComment",
		input: `
			# Just a comment
			`,
		wantCUE: "",
	}, {
		name: "RootKeyMissing",
		input: `
			# A comment to verify that parser positions work.
			= "no key name"
			`,
		wantErr: `
			invalid character at start of key: =:
			    test.toml:2:1
			`,
	}, {
		name: "RootKeysOne",
		input: `
			key = "value"
			`,
		wantCUE: `
			key: "value"
			`,
	}, {
		name: "RootMultiple",
		input: `
			key1 = "value1"
			key2 = "value2"
			key3 = "value3"
			`,
		wantCUE: `
			key1: "value1"
			key2: "value2"
			key3: "value3"
			`,
	}, {
		name: "RootKeysDots",
		input: `
			a1       = "A"
			b1.b2    = "B"
			c1.c2.c3 = "C"
			`,
		wantCUE: `
			a1: "A"
			b1: b2: "B"
			c1: c2: c3: "C"
			`,
	}, {
		name: "RootKeysCharacters",
		input: `
			a-b = "dashes"
			a_b = "underscore unquoted"
			_   = "underscore quoted"
			_ab = "underscore prefix quoted"
			123 = "numbers"
			x._.y._ = "underscores quoted"
			`,
		wantCUE: `
			"a-b": "dashes"
			a_b:   "underscore unquoted"
			"_":   "underscore quoted"
			"_ab": "underscore prefix quoted"
			"123": "numbers"
			x: "_": y: "_": "underscores quoted"
			`,
	}, {
		name: "RootKeysQuoted",
		input: `
			"1.2.3" = "quoted dots"
			"foo bar" = "quoted space"
			'foo "bar"' = "nested quotes"
			`,
		wantCUE: `
			"1.2.3":       "quoted dots"
			"foo bar":     "quoted space"
			"foo \"bar\"": "nested quotes"
			`,
	}, {
		name: "RootKeysMixed",
		input: `
			site."foo.com".title = "foo bar"
			`,
		wantCUE: `
			site: "foo.com": title: "foo bar"
			`,
	}, {
		name: "KeysDuplicateSimple",
		input: `
			foo = "same key"
			foo = "same key"
			`,
		wantErr: `
			duplicate key: foo:
			    test.toml:2:1
			`,
	}, {
		name: "KeysDuplicateQuoted",
		input: `
			"foo" = "same key"
			foo = "same key"
			`,
		wantErr: `
			duplicate key: foo:
			    test.toml:2:1
			`,
	}, {
		name: "KeysDuplicateWhitespace",
		input: `
			foo . bar = "same key"
			foo.bar = "same key"
			`,
		wantErr: `
			duplicate key: foo.bar:
			    test.toml:2:1
			`,
	}, {
		name: "KeysDuplicateDots",
		input: `
			foo."bar.baz".zzz = "same key"
			foo."bar.baz".zzz = "same key"
			`,
		wantErr: `
			duplicate key: foo."bar.baz".zzz:
			    test.toml:2:1
			`,
	}, {
		name: "KeysNotDuplicateDots",
		input: `
			foo."bar.baz" = "different key"
			"foo.bar".baz = "different key"
			`,
		wantCUE: `
			foo: "bar.baz": "different key"
			"foo.bar": baz: "different key"
			`,
	}, {
		name: "BasicStrings",
		input: `
			escapes = "foo \"bar\" \n\t\\ baz"
			unicode = "foo \u00E9"
			`,
		wantCUE: `
			escapes: "foo \"bar\" \n\t\\ baz"
			unicode: "foo é"
			`,
	}, {
		// Leading tabs do matter in this test.
		// TODO: use our own multiline strings where it gives better results.
		name: "MultilineBasicStrings",
		input: `
nested = """ can contain "" quotes """
four   = """"four""""
double = """
line one
line two"""
double_indented = """
	line one
	line two
	"""
escaped = """\
line one \
line two.\
"""
			`,
		wantCUE: `
			nested:           " can contain \"\" quotes "
			four:             "\"four\""
			double:           "line one\nline two"
			double_indented:  "\tline one\n\tline two\n\t"
			escaped:          "line one line two."
			`,
	}, {
		// TODO: we can probably do better in many cases, e.g. #""
		name: "LiteralStrings",
		input: `
			winpath  = 'C:\Users\nodejs\templates'
			winpath2 = '\\ServerX\admin$\system32\'
			quoted   = 'Tom "Dubs" Preston-Werner'
			regex    = '<\i\c*\s*>'
			`,
		wantCUE: `
			winpath:  "C:\\Users\\nodejs\\templates"
			winpath2: "\\\\ServerX\\admin$\\system32\\"
			quoted:   "Tom \"Dubs\" Preston-Werner"
			regex:    "<\\i\\c*\\s*>"
			`,
	}, {
		// Leading tabs do matter in this test.
		// TODO: use our own multiline strings where it gives better results.
		name: "MultilineLiteralStrings",
		input: `
nested = ''' can contain '' quotes '''
four   = ''''four''''
double = '''
line one
line two'''
double_indented = '''
	line one
	line two
	'''
escaped = '''\
line one \
line two.\
'''
			`,
		wantCUE: `
			nested:           " can contain '' quotes "
			four:             "'four'"
			double:           "line one\nline two"
			double_indented:  "\tline one\n\tline two\n\t"
			escaped:          "\\\nline one \\\nline two.\\\n"
			`,
	}, {
		name: "Integers",
		input: `
			zero        = 0
			positive    = 123
			plus        = +40
			minus       = -40
			underscores = 1_002_003
			hexadecimal = 0xdeadBEEF
			octal       = 0o755
			binary      = 0b11010110
			`,
		wantCUE: `
			zero:        0
			positive:    123
			plus:        +40
			minus:       -40
			underscores: 1_002_003
			hexadecimal: 0xdeadBEEF
			octal:       0o755
			binary:      0b11010110
			`,
	}, {
		name: "Floats",
		input: `
			pi             = 3.1415
			plus           = +1.23
			minus          = -4.56
			exponent       = 1e067
			exponent_plus  = 5e+20
			exponent_minus = -2E-4
			exponent_dot   = 6.789e-30
			`,
		wantCUE: `
			pi:             3.1415
			plus:           +1.23
			minus:          -4.56
			exponent:       1e067
			exponent_plus:  5e+20
			exponent_minus: -2E-4
			exponent_dot:   6.789e-30
			`,
	}, {
		name: "Bools",
		input: `
			positive = true
			negative = false
			`,
		wantCUE: `
			positive: true
			negative: false
			`,
	}, {
		name: "DateTimes",
		input: `
			offsetDateTime1 = 1979-05-27T07:32:00Z
			offsetDateTime2 = 1979-05-27T00:32:00-07:00
			offsetDateTime3 = 1979-05-27T00:32:00.999999-07:00
			localDateTime1 = 1979-05-27T07:32:00
			localDateTime2 = 1979-05-27T00:32:00.999999
			localDate1 = 1979-05-27
			localTime1 = 07:32:00
			localTime2 = 00:32:00.999999

			inlineArray = [1979-05-27, 07:32:00]

			notActuallyDate = "1979-05-27"
			notActuallyTime = "07:32:00"
			inlineArrayNotActually = ["1979-05-27", "07:32:00"]
			`,
		wantCUE: `
			import "time"

			offsetDateTime1: "1979-05-27T07:32:00Z" & time.Format(time.RFC3339)
			offsetDateTime2: "1979-05-27T00:32:00-07:00" & time.Format(time.RFC3339)
			offsetDateTime3: "1979-05-27T00:32:00.999999-07:00" & time.Format(time.RFC3339)
			localDateTime1: "1979-05-27T07:32:00" & time.Format("2006-01-02T15:04:05")
			localDateTime2: "1979-05-27T00:32:00.999999" & time.Format("2006-01-02T15:04:05")
			localDate1: "1979-05-27" & time.Format(time.RFC3339Date)
			localTime1: "07:32:00" & time.Format("15:04:05")
			localTime2: "00:32:00.999999" & time.Format("15:04:05")
			inlineArray: ["1979-05-27" & time.Format(time.RFC3339Date), "07:32:00" & time.Format("15:04:05")]
			notActuallyDate: "1979-05-27"
			notActuallyTime: "07:32:00"
			inlineArrayNotActually: ["1979-05-27", "07:32:00"]
			`,
	}, {
		name: "Arrays",
		input: `
			integers      = [1, 2, 3]
			colors        = ["red", "yellow", "green"]
			nested_ints   = [[1, 2], [3, 4, 5]]
			nested_mixed  = [[1, 2], ["a", "b", "c"], {extra = "keys"}]
			strings       = ["all", 'strings', """are the same""", '''type''']
			mixed_numbers = [0.1, 0.2, 0.5, 1, 2, 5]
			`,
		wantCUE: `
			integers:      [1, 2, 3]
			colors:        ["red", "yellow", "green"]
			nested_ints:   [[1, 2], [3, 4, 5]]
			nested_mixed:  [[1, 2], ["a", "b", "c"], {extra: "keys"}]
			strings:       ["all", "strings", "are the same", "type"]
			mixed_numbers: [0.1, 0.2, 0.5, 1, 2, 5]
			`,
	}, {
		name: "InlineTables",
		input: `
			empty  = {}
			point  = {x = 1, y = 2}
			animal = {type.name = "pug"}
			deep   = {l1 = {l2 = {l3 = "leaf"}}}
			`,
		wantCUE: `
			empty:  {}
			point:  {x: 1, y: 2}
			animal: {type: name: "pug"}
			deep:   {l1: {l2: {l3: "leaf"}}}
			`,
	}, {
		name: "InlineTablesDuplicate",
		input: `
			point = {x = "same key", x = "same key"}
			`,
		wantErr: `
			duplicate key: point.x:
			    test.toml:1:26
			`,
	}, {
		name: "ArrayInlineTablesDuplicate",
		input: `
			point = [{}, {}, {x = "same key", x = "same key"}]
			`,
		wantErr: `
			duplicate key: point.2.x:
			    test.toml:1:35
			`,
	}, {
		name: "InlineTablesNotDuplicateScoping",
		input: `
			repeat = {repeat = {repeat = "leaf"}}
			struct1 = {sibling = "leaf"}
			struct2 = {sibling = "leaf"}
			arrays = [{sibling = "leaf"}, {sibling = "leaf"}]
			`,
		wantCUE: `
			repeat: {repeat: {repeat: "leaf"}}
			struct1: {sibling: "leaf"}
			struct2: {sibling: "leaf"}
			arrays: [{sibling: "leaf"}, {sibling: "leaf"}]
			`,
	}, {
		name: "TablesEmpty",
		input: `
			[foo]
			[bar]
			`,
		wantCUE: `
			foo: {}
			bar: {}
			`,
	}, {
		name: "TablesOne",
		input: `
			[foo]
			single = "single"
			`,
		wantCUE: `
			foo: {
				single: "single"
			}
			`,
	}, {
		name: "TablesMultiple",
		input: `
			root1 = "root1 value"
			root2 = "root2 value"
			[foo]
			foo1 = "foo1 value"
			foo2 = "foo2 value"
			[bar]
			bar1 = "bar1 value"
			bar2 = "bar2 value"
			`,
		wantCUE: `
			root1: "root1 value"
			root2: "root2 value"
			foo: {
				foo1: "foo1 value"
				foo2: "foo2 value"
			}
			bar: {
				bar1: "bar1 value"
				bar2: "bar2 value"
			}
			`,
	}, {
		// A lot of these edge cases are covered by RootKeys tests already.
		name: "TablesKeysComplex",
		input: `
			[foo.bar . "baz.zzz zzz"]
			one = "1"
			[123-456]
			two = "2"
			`,
		wantCUE: `
			foo: bar: "baz.zzz zzz": {
				one: "1"
			}
			"123-456": {
				two: "2"
			}
			`,
	}, {
		name: "TableKeysDuplicateSimple",
		input: `
			[foo]
			[foo]
			`,
		wantErr: `
			duplicate key: foo:
			    test.toml:2:2
			`,
	}, {
		name: "TableKeysDuplicateOverlap",
		input: `
			[foo]
			bar = "leaf"
			[foo.bar]
			baz = "second leaf"
			`,
		wantErr: `
			duplicate key: foo.bar:
			    test.toml:3:2
			`,
	}, {
		name: "TableInnerKeysDuplicateSimple",
		input: `
			[foo]
			bar = "same key"
			bar = "same key"
			`,
		wantErr: `
			duplicate key: foo.bar:
			    test.toml:3:1
			`,
	}, {
		name: "TablesNotDuplicateScoping",
		input: `
			[repeat]
			repeat.repeat = "leaf"
			[struct1]
			sibling = "leaf"
			[struct2]
			sibling = "leaf"
			`,
		wantCUE: `
			repeat: {
				repeat: repeat: "leaf"
			}
			struct1: {
				sibling: "leaf"
			}
			struct2: {
				sibling: "leaf"
			}
			`,
	}, {
		name: "ArrayTablesEmpty",
		input: `
			[[foo]]
			`,
		wantCUE: `
			foo: [
				{},
			]
			`,
	}, {
		name: "ArrayTablesOne",
		input: `
			[[foo]]
			single = "single"
			`,
		wantCUE: `
			foo: [
				{
					single: "single"
				},
			]
			`,
	}, {
		name: "ArrayTablesMultiple",
		input: `
			root = "root value"
			[[foo]]
			foo1 = "foo1 value"
			foo2 = "foo2 value"
			[[foo]]
			foo3 = "foo3 value"
			foo4 = "foo4 value"
			[[foo]]
			[[foo]]
			single = "single"
			`,
		wantCUE: `
			root: "root value"
			foo: [
				{
					foo1: "foo1 value"
					foo2: "foo2 value"
				},
				{
					foo3: "foo3 value"
					foo4: "foo4 value"
				},
				{},
				{
					single: "single"
				},
			]
			`,
	}, {
		name: "ArrayTablesSeparate",
		input: `
			root = "root value"
			[[foo]]
			foo1 = "foo1 value"
			[[bar]]
			bar1 = "bar1 value"
			[[baz]]
			`,
		wantCUE: `
			root: "root value"
			foo: [
				{
					foo1: "foo1 value"
				},
			]
			bar: [
				{
					bar1: "bar1 value"
				},
			]
			baz: [
				{},
			]
			`,
	}, {
		name: "ArrayTablesSubtable",
		input: `
			[[foo]]
			foo1 = "foo1 value"
			[foo.subtable1]
			sub1 = "sub1 value"
			[foo.subtable2]
			sub2 = "sub2 value"
			[foo.subtable2.deeper]
			sub2d = "sub2d value"
			[[foo]]
			foo2 = "foo2 value"
			`,
		wantCUE: `
			foo: [
				{
					foo1: "foo1 value"
					subtable1: {
						sub1: "sub1 value"
					}
					subtable2: {
						sub2: "sub2 value"
					}
					subtable2: deeper: {
						sub2d: "sub2d value"
					}
				},
				{
					foo2: "foo2 value"
				},
			]
			`,
	}, {
		name: "ArrayTablesNested",
		input: `
			[[foo]]
			foo1 = "foo1 value"
			[[foo.nested1]]
			nest1a = "nest1a value"
			[[foo.nested1]]
			nest1b = "nest1b value"
			[[foo.nested2]]
			nest2 = "nest2 value"
			[[foo.nested2.deeper]]
			nest2d = "nest2d value"
			[[foo.nested3.directly.deeper]]
			nest3d = "nest3d value"
			[[foo]]
			foo2 = "foo2 value"
			`,
		wantCUE: `
			foo: [
				{
					foo1: "foo1 value"
					nested1: [
						{
							nest1a: "nest1a value"
						},
						{
							nest1b: "nest1b value"
						},
					]
					nested2: [
						{
							nest2: "nest2 value"
							deeper: [
								{
									nest2d: "nest2d value"
								}
							]
						},
					]
					nested3: directly: deeper: [
						{
							nest3d: "nest3d value"
						},
					]
				},
				{
					foo2: "foo2 value"
				},
			]
			`,
	}, {
		name: "RedeclareKeyAsTableArray",
		input: `
			foo = "foo value"
			[middle]
			middle = "to ensure we don't rely on the last key"
			[[foo]]
			baz = "baz value"
			`,
		wantErr: `
			cannot redeclare key "foo" as a table array:
			    test.toml:4:3
			`,
	}, {
		name: "RedeclareTableAsTableArray",
		input: `
			[foo]
			bar = "bar value"
			[middle]
			middle = "to ensure we don't rely on the last key"
			[[foo]]
			baz = "baz value"
			`,
		wantErr: `
			cannot redeclare key "foo" as a table array:
			    test.toml:5:3
			`,
	}, {
		name: "RedeclareArrayAsTableArray",
		input: `
			foo = ["inline array"]
			[middle]
			middle = "to ensure we don't rely on the last key"
			[[foo]]
			baz = "baz value"
			`,
		wantErr: `
			cannot redeclare key "foo" as a table array:
			    test.toml:4:3
			`,
	}, {
		name: "RedeclareTableArrayAsKey",
		input: `
			[[foo.foo2]]
			bar = "bar value"
			[middle]
			middle = "to ensure we don't rely on the last key"
			[foo]
			foo2 = "redeclaring"
			`,
		wantErr: `
			cannot redeclare table array "foo.foo2" as a table:
			    test.toml:6:1
			`,
	}, {
		name: "RedeclareTableArrayAsTable",
		input: `
			[[foo]]
			bar = "bar value"
			[middle]
			middle = "to ensure we don't rely on the last key"
			[foo]
			baz = "baz value"
			`,
		wantErr: `
			cannot redeclare table array "foo" as a table:
			    test.toml:5:2
			`,
	}, {
		name: "KeysNotDuplicateTableArrays",
		input: `
			[[foo]]
			bar = "foo.0.bar"
			[[foo]]
			bar = "foo.1.bar"
			[[foo]]
			bar = "foo.2.bar"
			[[foo.nested]]
			bar = "foo.2.nested.0.bar"
			[[foo.nested]]
			bar = "foo.2.nested.1.bar"
			[[foo.nested]]
			bar = "foo.2.nested.2.bar"
			`,
		wantCUE: `
			foo: [
				{
					bar: "foo.0.bar"
				},
				{
					bar: "foo.1.bar"
				},
				{
					bar: "foo.2.bar"
					nested: [
						{
							bar: "foo.2.nested.0.bar"
						},
						{
							bar: "foo.2.nested.1.bar"
						},
						{
							bar: "foo.2.nested.2.bar"
						},
					]
				},
			]
			`,
	}}
	for _, test := range tests {
		t.Run(test.name, func(t *testing.T) {
			t.Parallel()

			input := unindentMultiline(test.input)
			dec := toml.NewDecoder("test.toml", strings.NewReader(input))

			node, err := dec.Decode()
			if test.wantErr != "" {
				gotErr := strings.TrimSuffix(errors.Details(err, nil), "\n")
				wantErr := unindentMultiline(test.wantErr)

				qt.Assert(t, qt.Equals(gotErr, wantErr))
				qt.Assert(t, qt.IsNil(node))
				// We don't continue, so we can't expect any decoded CUE.
				qt.Assert(t, qt.Equals(test.wantCUE, ""))

				// Validate that go-toml's Unmarshal also rejects this input.
				err = gotoml.Unmarshal([]byte(input), new(any))
				qt.Assert(t, qt.IsNotNil(err))
				return
			}
			qt.Assert(t, qt.IsNil(err))

			file, err := astutil.ToFile(node)
			qt.Assert(t, qt.IsNil(err))

			node2, err := dec.Decode()
			qt.Assert(t, qt.IsNil(node2))
			qt.Assert(t, qt.Equals(err, io.EOF))

			wantFormatted, err := format.Source([]byte(test.wantCUE))
			qt.Assert(t, qt.IsNil(err), qt.Commentf("wantCUE:\n%s", test.wantCUE))

			formatted, err := format.Node(file)
			qt.Assert(t, qt.IsNil(err))
			t.Logf("CUE:\n%s", formatted)
			qt.Assert(t, qt.Equals(string(formatted), string(wantFormatted)))

			// Ensure that the CUE node can be compiled into a cue.Value and validated.
			ctx := cuecontext.New()
			val := ctx.BuildFile(file)
			qt.Assert(t, qt.IsNil(val.Err()))
			qt.Assert(t, qt.IsNil(val.Validate()))

			// Validate that the decoded CUE value is equivalent
			// to the Go value that go-toml's Unmarshal produces.
			// We use JSON equality as some details such as which integer types are used
			// are not actually relevant to an "equal data" check.
			var unmarshalTOML any
			err = gotoml.Unmarshal([]byte(input), &unmarshalTOML)
			qt.Assert(t, qt.IsNil(err))
			jsonTOML, err := json.Marshal(unmarshalTOML)
			qt.Assert(t, qt.IsNil(err))
			t.Logf("json.Marshal via go-toml:\t%s\n", jsonTOML)

			jsonCUE, err := json.Marshal(val)
			qt.Assert(t, qt.IsNil(err))
			t.Logf("json.Marshal via CUE:\t%s\n", jsonCUE)
			qt.Assert(t, qt.JSONEquals(jsonCUE, unmarshalTOML))

			// Ensure that the decoded CUE can be re-encoded as TOML,
			// and the resulting TOML is still JSON-equivalent.
			t.Run("reencode", func(t *testing.T) {
				switch test.name {
				case "Integers":
					if bits.UintSize == 32 {
						t.Skip("TODO(mvdan): big integers always encode as TOML strings today; can be resolved once CUE_EXPERIMENT=decodeint64 is enabled")
					}
				case "DateTimes":
					t.Skip("TODO(mvdan): dates and times always encode as TOML strings today")
				}
				sb := new(strings.Builder)
				enc := toml.NewEncoder(sb)

				err := enc.Encode(val)
				qt.Assert(t, qt.IsNil(err))
				cueTOML := sb.String()
				t.Logf("reencoded TOML:\n%s", cueTOML)

				var unmarshalCueTOML any
				err = gotoml.Unmarshal([]byte(cueTOML), &unmarshalCueTOML)
				qt.Assert(t, qt.IsNil(err))

				qt.Assert(t, qt.CmpEquals(unmarshalCueTOML, unmarshalTOML))
			})
		})
	}
}

// unindentMultiline mimics CUE's behavior with `"""` multi-line strings,
// where a leading newline is omitted, and any whitespace preceding the trailing newline
// is removed from the start of all lines.
func unindentMultiline(s string) string {
	i := strings.LastIndexByte(s, '\n')
	if i < 0 {
		// Not a multi-line string.
		return s
	}
	trim := s[i:]
	s = strings.ReplaceAll(s, trim, "\n")
	s = strings.TrimPrefix(s, "\n")
	s = strings.TrimSuffix(s, "\n")
	return s
}

var (
	typNode = reflect.TypeFor[ast.Node]()
	typPos  = reflect.TypeFor[token.Pos]()
)

func TestDecoderTxtar(t *testing.T) {
	test := cuetxtar.TxTarTest{
		Root: "testdata",
		Name: "decode",
	}

	test.Run(t, func(t *cuetxtar.Test) {
		for _, file := range t.Archive.Files {
			if strings.HasPrefix(file.Name, "out/") {
				continue
			}
			dec := toml.NewDecoder(file.Name, bytes.NewReader(file.Data))
			node, err := dec.Decode()
			qt.Assert(t, qt.IsNil(err))

			// Show all valid node positions.
			out := astinternal.AppendDebug(nil, node, astinternal.DebugConfig{
				OmitEmpty: true,
				Filter: func(v reflect.Value) bool {
					t := v.Type()
					return t.Implements(typNode) || t.Kind() == reflect.Slice || t == typPos
				},
			})
			t.Writer(path.Join(file.Name, "positions")).Write(out)
		}
	})
}