Skip to content

Commit

Permalink
encoding/toml: add debugprint txtar tests for the decoder
Browse files Browse the repository at this point in the history
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í <mvdan@mvdan.cc>
Change-Id: I9c6e18ecc499f792eea8a824f43e246aba86b42e
  • Loading branch information
mvdan committed Aug 29, 2024
1 parent 482e8b5 commit e26d1a5
Show file tree
Hide file tree
Showing 2 changed files with 272 additions and 0 deletions.
40 changes: 40 additions & 0 deletions encoding/toml/decode_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,19 +15,26 @@
package toml_test

import (
"bytes"
"encoding/json"
"io"
"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) {
Expand Down Expand Up @@ -858,3 +865,36 @@ func unindentMultiline(s string) string {
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)
}
})
}
232 changes: 232 additions & 0 deletions encoding/toml/testdata/decode/example.txtar
Original file line number Diff line number Diff line change
@@ -0,0 +1,232 @@
-- example.toml --
# This is a TOML document.
# Copied from various examples from the TOML spec website.

title = "TOML Example"

[owner]
name = "Tom Preston-Werner"
# TODO: support dates
# dob = 1979-05-27T07:32:00-08:00

[database]
enabled = true
ports = [ 8000, 8001, 8002 ]
data = [ ["delta", "phi"], [3.14] ]
temp_targets = { cpu = 79.5, case = 72.0 }

[servers]

[servers.alpha]
ip = "10.0.0.1"
role = "frontend"

[servers.beta]
ip = "10.0.0.2"
role = "backend"

[[products]]
name = "Hammer"
sku = 738594937

[[products]] # empty table within the array

[[products]]
name = "Nail"
sku = 284758393

color = "gray"
-- out/decode/example.toml/positions --
*ast.StructLit{
Elts: []ast.Decl{
*ast.Field{
Label: *ast.Ident{
NamePos: token.Pos("-").WithRel("newline")
}
}
*ast.Field{
Label: *ast.Ident{
NamePos: token.Pos("-").WithRel("newline")
}
Value: *ast.StructLit{
Lbrace: token.Pos("-").WithRel("blank")
Elts: []ast.Decl{
*ast.Field{
Label: *ast.Ident{
NamePos: token.Pos("-").WithRel("newline")
}
}
}
Rbrace: token.Pos("-").WithRel("newline")
}
}
*ast.Field{
Label: *ast.Ident{
NamePos: token.Pos("-").WithRel("newline")
}
Value: *ast.StructLit{
Lbrace: token.Pos("-").WithRel("blank")
Elts: []ast.Decl{
*ast.Field{
Label: *ast.Ident{
NamePos: token.Pos("-").WithRel("newline")
}
}
*ast.Field{
Label: *ast.Ident{
NamePos: token.Pos("-").WithRel("newline")
}
}
*ast.Field{
Label: *ast.Ident{
NamePos: token.Pos("-").WithRel("newline")
}
}
*ast.Field{
Label: *ast.Ident{
NamePos: token.Pos("-").WithRel("newline")
}
Value: *ast.StructLit{
Lbrace: token.Pos("-").WithRel("blank")
Elts: []ast.Decl{
*ast.Field{
Label: *ast.Ident{
NamePos: token.Pos("-").WithRel("blank")
}
}
*ast.Field{
Label: *ast.Ident{
NamePos: token.Pos("-").WithRel("blank")
}
}
}
Rbrace: token.Pos("-").WithRel("blank")
}
}
}
Rbrace: token.Pos("-").WithRel("newline")
}
}
*ast.Field{
Label: *ast.Ident{
NamePos: token.Pos("-").WithRel("newline")
}
Value: *ast.StructLit{
Lbrace: token.Pos("-").WithRel("blank")
Rbrace: token.Pos("-").WithRel("newline")
}
}
*ast.Field{
Label: *ast.Ident{
NamePos: token.Pos("-").WithRel("newline")
}
Value: *ast.StructLit{
Elts: []ast.Decl{
*ast.Field{
Label: *ast.Ident{
NamePos: token.Pos("-").WithRel("blank")
}
Value: *ast.StructLit{
Lbrace: token.Pos("-").WithRel("blank")
Elts: []ast.Decl{
*ast.Field{
Label: *ast.Ident{
NamePos: token.Pos("-").WithRel("newline")
}
}
*ast.Field{
Label: *ast.Ident{
NamePos: token.Pos("-").WithRel("newline")
}
}
}
Rbrace: token.Pos("-").WithRel("newline")
}
}
}
}
}
*ast.Field{
Label: *ast.Ident{
NamePos: token.Pos("-").WithRel("newline")
}
Value: *ast.StructLit{
Elts: []ast.Decl{
*ast.Field{
Label: *ast.Ident{
NamePos: token.Pos("-").WithRel("blank")
}
Value: *ast.StructLit{
Lbrace: token.Pos("-").WithRel("blank")
Elts: []ast.Decl{
*ast.Field{
Label: *ast.Ident{
NamePos: token.Pos("-").WithRel("newline")
}
}
*ast.Field{
Label: *ast.Ident{
NamePos: token.Pos("-").WithRel("newline")
}
}
}
Rbrace: token.Pos("-").WithRel("newline")
}
}
}
}
}
*ast.Field{
Label: *ast.Ident{
NamePos: token.Pos("-").WithRel("newline")
}
Value: *ast.ListLit{
Lbrack: token.Pos("-").WithRel("blank")
Elts: []ast.Expr{
*ast.StructLit{
Lbrace: token.Pos("-").WithRel("newline")
Elts: []ast.Decl{
*ast.Field{
Label: *ast.Ident{
NamePos: token.Pos("-").WithRel("newline")
}
}
*ast.Field{
Label: *ast.Ident{
NamePos: token.Pos("-").WithRel("newline")
}
}
}
Rbrace: token.Pos("-").WithRel("newline")
}
*ast.StructLit{
Lbrace: token.Pos("-").WithRel("newline")
Rbrace: token.Pos("-").WithRel("newline")
}
*ast.StructLit{
Lbrace: token.Pos("-").WithRel("newline")
Elts: []ast.Decl{
*ast.Field{
Label: *ast.Ident{
NamePos: token.Pos("-").WithRel("newline")
}
}
*ast.Field{
Label: *ast.Ident{
NamePos: token.Pos("-").WithRel("newline")
}
}
*ast.Field{
Label: *ast.Ident{
NamePos: token.Pos("-").WithRel("newline")
}
}
}
Rbrace: token.Pos("-").WithRel("newline")
}
}
Rbrack: token.Pos("-").WithRel("newline")
}
}
}
}

0 comments on commit e26d1a5

Please sign in to comment.