Skip to content

Commit edc117e

Browse files
committed
encoding/toml: add debugprint txtar tests for the decoder
So that we can verify that the decoded CUE syntax nodes have reasonable positions attached where we expect. For #3379. Signed-off-by: Daniel Martí <[email protected]> Change-Id: I9c6e18ecc499f792eea8a824f43e246aba86b42e Reviewed-on: https://review.gerrithub.io/c/cue-lang/cue/+/1199937 Unity-Result: CUE porcuepine <[email protected]> Reviewed-by: Matthew Sackman <[email protected]> TryBot-Result: CUEcueckoo <[email protected]>
1 parent 6e5ec5a commit edc117e

File tree

2 files changed

+272
-0
lines changed

2 files changed

+272
-0
lines changed

encoding/toml/decode_test.go

+40
Original file line numberDiff line numberDiff line change
@@ -15,19 +15,26 @@
1515
package toml_test
1616

1717
import (
18+
"bytes"
1819
"encoding/json"
1920
"io"
21+
"path"
22+
"reflect"
2023
"strings"
2124
"testing"
2225

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

29+
"cuelang.org/go/cue/ast"
2630
"cuelang.org/go/cue/ast/astutil"
2731
"cuelang.org/go/cue/cuecontext"
2832
"cuelang.org/go/cue/errors"
2933
"cuelang.org/go/cue/format"
34+
"cuelang.org/go/cue/token"
3035
"cuelang.org/go/encoding/toml"
36+
"cuelang.org/go/internal/astinternal"
37+
"cuelang.org/go/internal/cuetxtar"
3138
)
3239

3340
func TestDecoder(t *testing.T) {
@@ -858,3 +865,36 @@ func unindentMultiline(s string) string {
858865
s = strings.TrimSuffix(s, "\n")
859866
return s
860867
}
868+
869+
var (
870+
typNode = reflect.TypeFor[ast.Node]()
871+
typPos = reflect.TypeFor[token.Pos]()
872+
)
873+
874+
func TestDecoderTxtar(t *testing.T) {
875+
test := cuetxtar.TxTarTest{
876+
Root: "testdata",
877+
Name: "decode",
878+
}
879+
880+
test.Run(t, func(t *cuetxtar.Test) {
881+
for _, file := range t.Archive.Files {
882+
if strings.HasPrefix(file.Name, "out/") {
883+
continue
884+
}
885+
dec := toml.NewDecoder(file.Name, bytes.NewReader(file.Data))
886+
node, err := dec.Decode()
887+
qt.Assert(t, qt.IsNil(err))
888+
889+
// Show all valid node positions.
890+
out := astinternal.AppendDebug(nil, node, astinternal.DebugConfig{
891+
OmitEmpty: true,
892+
Filter: func(v reflect.Value) bool {
893+
t := v.Type()
894+
return t.Implements(typNode) || t.Kind() == reflect.Slice || t == typPos
895+
},
896+
})
897+
t.Writer(path.Join(file.Name, "positions")).Write(out)
898+
}
899+
})
900+
}
+232
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,232 @@
1+
-- example.toml --
2+
# This is a TOML document.
3+
# Copied from various examples from the TOML spec website.
4+
5+
title = "TOML Example"
6+
7+
[owner]
8+
name = "Tom Preston-Werner"
9+
# TODO: support dates
10+
# dob = 1979-05-27T07:32:00-08:00
11+
12+
[database]
13+
enabled = true
14+
ports = [ 8000, 8001, 8002 ]
15+
data = [ ["delta", "phi"], [3.14] ]
16+
temp_targets = { cpu = 79.5, case = 72.0 }
17+
18+
[servers]
19+
20+
[servers.alpha]
21+
ip = "10.0.0.1"
22+
role = "frontend"
23+
24+
[servers.beta]
25+
ip = "10.0.0.2"
26+
role = "backend"
27+
28+
[[products]]
29+
name = "Hammer"
30+
sku = 738594937
31+
32+
[[products]] # empty table within the array
33+
34+
[[products]]
35+
name = "Nail"
36+
sku = 284758393
37+
38+
color = "gray"
39+
-- out/decode/example.toml/positions --
40+
*ast.StructLit{
41+
Elts: []ast.Decl{
42+
*ast.Field{
43+
Label: *ast.Ident{
44+
NamePos: token.Pos("-").WithRel("newline")
45+
}
46+
}
47+
*ast.Field{
48+
Label: *ast.Ident{
49+
NamePos: token.Pos("-").WithRel("newline")
50+
}
51+
Value: *ast.StructLit{
52+
Lbrace: token.Pos("-").WithRel("blank")
53+
Elts: []ast.Decl{
54+
*ast.Field{
55+
Label: *ast.Ident{
56+
NamePos: token.Pos("-").WithRel("newline")
57+
}
58+
}
59+
}
60+
Rbrace: token.Pos("-").WithRel("newline")
61+
}
62+
}
63+
*ast.Field{
64+
Label: *ast.Ident{
65+
NamePos: token.Pos("-").WithRel("newline")
66+
}
67+
Value: *ast.StructLit{
68+
Lbrace: token.Pos("-").WithRel("blank")
69+
Elts: []ast.Decl{
70+
*ast.Field{
71+
Label: *ast.Ident{
72+
NamePos: token.Pos("-").WithRel("newline")
73+
}
74+
}
75+
*ast.Field{
76+
Label: *ast.Ident{
77+
NamePos: token.Pos("-").WithRel("newline")
78+
}
79+
}
80+
*ast.Field{
81+
Label: *ast.Ident{
82+
NamePos: token.Pos("-").WithRel("newline")
83+
}
84+
}
85+
*ast.Field{
86+
Label: *ast.Ident{
87+
NamePos: token.Pos("-").WithRel("newline")
88+
}
89+
Value: *ast.StructLit{
90+
Lbrace: token.Pos("-").WithRel("blank")
91+
Elts: []ast.Decl{
92+
*ast.Field{
93+
Label: *ast.Ident{
94+
NamePos: token.Pos("-").WithRel("blank")
95+
}
96+
}
97+
*ast.Field{
98+
Label: *ast.Ident{
99+
NamePos: token.Pos("-").WithRel("blank")
100+
}
101+
}
102+
}
103+
Rbrace: token.Pos("-").WithRel("blank")
104+
}
105+
}
106+
}
107+
Rbrace: token.Pos("-").WithRel("newline")
108+
}
109+
}
110+
*ast.Field{
111+
Label: *ast.Ident{
112+
NamePos: token.Pos("-").WithRel("newline")
113+
}
114+
Value: *ast.StructLit{
115+
Lbrace: token.Pos("-").WithRel("blank")
116+
Rbrace: token.Pos("-").WithRel("newline")
117+
}
118+
}
119+
*ast.Field{
120+
Label: *ast.Ident{
121+
NamePos: token.Pos("-").WithRel("newline")
122+
}
123+
Value: *ast.StructLit{
124+
Elts: []ast.Decl{
125+
*ast.Field{
126+
Label: *ast.Ident{
127+
NamePos: token.Pos("-").WithRel("blank")
128+
}
129+
Value: *ast.StructLit{
130+
Lbrace: token.Pos("-").WithRel("blank")
131+
Elts: []ast.Decl{
132+
*ast.Field{
133+
Label: *ast.Ident{
134+
NamePos: token.Pos("-").WithRel("newline")
135+
}
136+
}
137+
*ast.Field{
138+
Label: *ast.Ident{
139+
NamePos: token.Pos("-").WithRel("newline")
140+
}
141+
}
142+
}
143+
Rbrace: token.Pos("-").WithRel("newline")
144+
}
145+
}
146+
}
147+
}
148+
}
149+
*ast.Field{
150+
Label: *ast.Ident{
151+
NamePos: token.Pos("-").WithRel("newline")
152+
}
153+
Value: *ast.StructLit{
154+
Elts: []ast.Decl{
155+
*ast.Field{
156+
Label: *ast.Ident{
157+
NamePos: token.Pos("-").WithRel("blank")
158+
}
159+
Value: *ast.StructLit{
160+
Lbrace: token.Pos("-").WithRel("blank")
161+
Elts: []ast.Decl{
162+
*ast.Field{
163+
Label: *ast.Ident{
164+
NamePos: token.Pos("-").WithRel("newline")
165+
}
166+
}
167+
*ast.Field{
168+
Label: *ast.Ident{
169+
NamePos: token.Pos("-").WithRel("newline")
170+
}
171+
}
172+
}
173+
Rbrace: token.Pos("-").WithRel("newline")
174+
}
175+
}
176+
}
177+
}
178+
}
179+
*ast.Field{
180+
Label: *ast.Ident{
181+
NamePos: token.Pos("-").WithRel("newline")
182+
}
183+
Value: *ast.ListLit{
184+
Lbrack: token.Pos("-").WithRel("blank")
185+
Elts: []ast.Expr{
186+
*ast.StructLit{
187+
Lbrace: token.Pos("-").WithRel("newline")
188+
Elts: []ast.Decl{
189+
*ast.Field{
190+
Label: *ast.Ident{
191+
NamePos: token.Pos("-").WithRel("newline")
192+
}
193+
}
194+
*ast.Field{
195+
Label: *ast.Ident{
196+
NamePos: token.Pos("-").WithRel("newline")
197+
}
198+
}
199+
}
200+
Rbrace: token.Pos("-").WithRel("newline")
201+
}
202+
*ast.StructLit{
203+
Lbrace: token.Pos("-").WithRel("newline")
204+
Rbrace: token.Pos("-").WithRel("newline")
205+
}
206+
*ast.StructLit{
207+
Lbrace: token.Pos("-").WithRel("newline")
208+
Elts: []ast.Decl{
209+
*ast.Field{
210+
Label: *ast.Ident{
211+
NamePos: token.Pos("-").WithRel("newline")
212+
}
213+
}
214+
*ast.Field{
215+
Label: *ast.Ident{
216+
NamePos: token.Pos("-").WithRel("newline")
217+
}
218+
}
219+
*ast.Field{
220+
Label: *ast.Ident{
221+
NamePos: token.Pos("-").WithRel("newline")
222+
}
223+
}
224+
}
225+
Rbrace: token.Pos("-").WithRel("newline")
226+
}
227+
}
228+
Rbrack: token.Pos("-").WithRel("newline")
229+
}
230+
}
231+
}
232+
}

0 commit comments

Comments
 (0)