Skip to content

Commit

Permalink
internal/astinternal: add DebugPrint cuetxtar tests
Browse files Browse the repository at this point in the history
So that we can clearly see the current behavior
and review any diffs in upcoming changes.

Signed-off-by: Daniel Martí <mvdan@mvdan.cc>
Change-Id: Ib0b325d0926a47530b09df88afbe762ba60db782
Dispatch-Trailer: {"type":"trybot","CL":1199935,"patchset":1,"ref":"refs/changes/35/1199935/1","targetBranch":"master"}
  • Loading branch information
mvdan authored and cueckoo committed Aug 23, 2024
1 parent 8e24843 commit 54c01be
Show file tree
Hide file tree
Showing 5 changed files with 488 additions and 0 deletions.
46 changes: 46 additions & 0 deletions internal/astinternal/debug_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
// Copyright 2024 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 astinternal_test

import (
"strings"
"testing"

"cuelang.org/go/cue/parser"
"cuelang.org/go/internal/astinternal"
"cuelang.org/go/internal/cuetxtar"

"github.com/go-quicktest/qt"
)

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

test.Run(t, func(t *cuetxtar.Test) {
for _, file := range t.Archive.Files {
if strings.HasPrefix(file.Name, "out/") {
continue
}
f, err := parser.ParseFile(file.Name, file.Data, parser.ParseComments)
qt.Assert(t, qt.IsNil(err))

w := t.Writer(file.Name)
astinternal.DebugPrint(w, f)
}
})
}
109 changes: 109 additions & 0 deletions internal/astinternal/testdata/debugprint/comprehensions.txtar
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
-- comprehensions.cue --
if condition {
a: true
}
for k, v in input if v > 2 {
(k): v
}
-- out/debugprint/comprehensions.cue --
*ast.File{
Filename: "comprehensions.cue"
Decls: []ast.Decl{
*ast.Comprehension{
Clauses: []ast.Clause{
*ast.IfClause{
If: token.Pos("comprehensions.cue:1:1")
Condition: *ast.Ident{
NamePos: token.Pos("comprehensions.cue:1:4")
Name: "condition"
}
}
}
Value: *ast.StructLit{
Lbrace: token.Pos("comprehensions.cue:1:14")
Elts: []ast.Decl{
*ast.Field{
Label: *ast.Ident{
NamePos: token.Pos("comprehensions.cue:2:2")
Name: "a"
}
Optional: token.Pos("-")
Constraint: token.Token("ILLEGAL")
TokenPos: token.Pos("comprehensions.cue:2:3")
Token: token.Token(":")
Value: *ast.BasicLit{
ValuePos: token.Pos("comprehensions.cue:2:5")
Kind: token.Token("true")
Value: "true"
}
Attrs: []*ast.Attribute{}
}
}
Rbrace: token.Pos("comprehensions.cue:3:1")
}
}
*ast.Comprehension{
Clauses: []ast.Clause{
*ast.ForClause{
For: token.Pos("comprehensions.cue:4:1")
Key: *ast.Ident{
NamePos: token.Pos("comprehensions.cue:4:5")
Name: "k"
}
Colon: token.Pos("comprehensions.cue:4:6")
Value: *ast.Ident{
NamePos: token.Pos("comprehensions.cue:4:8")
Name: "v"
}
In: token.Pos("comprehensions.cue:4:10")
Source: *ast.Ident{
NamePos: token.Pos("comprehensions.cue:4:13")
Name: "input"
}
}
*ast.IfClause{
If: token.Pos("comprehensions.cue:4:19")
Condition: *ast.BinaryExpr{
X: *ast.Ident{
NamePos: token.Pos("comprehensions.cue:4:22")
Name: "v"
}
OpPos: token.Pos("comprehensions.cue:4:24")
Op: token.Token(">")
Y: *ast.BasicLit{
ValuePos: token.Pos("comprehensions.cue:4:26")
Kind: token.Token("INT")
Value: "2"
}
}
}
}
Value: *ast.StructLit{
Lbrace: token.Pos("comprehensions.cue:4:28")
Elts: []ast.Decl{
*ast.Field{
Label: *ast.ParenExpr{
Lparen: token.Pos("comprehensions.cue:5:2")
X: *ast.Ident{
NamePos: token.Pos("comprehensions.cue:5:3")
Name: "k"
}
Rparen: token.Pos("comprehensions.cue:5:4")
}
Optional: token.Pos("-")
Constraint: token.Token("ILLEGAL")
TokenPos: token.Pos("comprehensions.cue:5:5")
Token: token.Token(":")
Value: *ast.Ident{
NamePos: token.Pos("comprehensions.cue:5:7")
Name: "v"
}
Attrs: []*ast.Attribute{}
}
}
Rbrace: token.Pos("comprehensions.cue:6:1")
}
}
}
Imports: []*ast.ImportSpec{}
}
208 changes: 208 additions & 0 deletions internal/astinternal/testdata/debugprint/fields.txtar
Original file line number Diff line number Diff line change
@@ -0,0 +1,208 @@
-- fields.cue --
a: 1 @xml(,attr)
b: 2 @foo(a,b=4) @go(Foo)
c!: d?: string
X=e: [Y=string]: int
#Schema: {
name: string
...
}
embed: {
#Schema
}
-- out/debugprint/fields.cue --
*ast.File{
Filename: "fields.cue"
Decls: []ast.Decl{
*ast.Field{
Label: *ast.Ident{
NamePos: token.Pos("fields.cue:1:1")
Name: "a"
}
Optional: token.Pos("-")
Constraint: token.Token("ILLEGAL")
TokenPos: token.Pos("fields.cue:1:2")
Token: token.Token(":")
Value: *ast.BasicLit{
ValuePos: token.Pos("fields.cue:1:4")
Kind: token.Token("INT")
Value: "1"
}
Attrs: []*ast.Attribute{
{
At: token.Pos("fields.cue:1:6")
Text: "@xml(,attr)"
}
}
}
*ast.Field{
Label: *ast.Ident{
NamePos: token.Pos("fields.cue:2:1")
Name: "b"
}
Optional: token.Pos("-")
Constraint: token.Token("ILLEGAL")
TokenPos: token.Pos("fields.cue:2:2")
Token: token.Token(":")
Value: *ast.BasicLit{
ValuePos: token.Pos("fields.cue:2:4")
Kind: token.Token("INT")
Value: "2"
}
Attrs: []*ast.Attribute{
{
At: token.Pos("fields.cue:2:6")
Text: "@foo(a,b=4)"
}
{
At: token.Pos("fields.cue:2:18")
Text: "@go(Foo)"
}
}
}
*ast.Field{
Label: *ast.Ident{
NamePos: token.Pos("fields.cue:3:1")
Name: "c"
}
Optional: token.Pos("fields.cue:3:2")
Constraint: token.Token("!")
TokenPos: token.Pos("fields.cue:3:3")
Token: token.Token(":")
Value: *ast.StructLit{
Lbrace: token.Pos("-")
Elts: []ast.Decl{
*ast.Field{
Label: *ast.Ident{
NamePos: token.Pos("fields.cue:3:5")
Name: "d"
}
Optional: token.Pos("fields.cue:3:6")
Constraint: token.Token("?")
TokenPos: token.Pos("fields.cue:3:7")
Token: token.Token(":")
Value: *ast.Ident{
NamePos: token.Pos("fields.cue:3:9")
Name: "string"
}
Attrs: []*ast.Attribute{}
}
}
Rbrace: token.Pos("-")
}
Attrs: []*ast.Attribute{}
}
*ast.Field{
Label: *ast.Alias{
Ident: *ast.Ident{
NamePos: token.Pos("fields.cue:4:1")
Name: "X"
}
Equal: token.Pos("fields.cue:4:2")
Expr: *ast.Ident{
NamePos: token.Pos("fields.cue:4:3")
Name: "e"
}
}
Optional: token.Pos("-")
Constraint: token.Token("ILLEGAL")
TokenPos: token.Pos("fields.cue:4:4")
Token: token.Token(":")
Value: *ast.StructLit{
Lbrace: token.Pos("-")
Elts: []ast.Decl{
*ast.Field{
Label: *ast.ListLit{
Lbrack: token.Pos("fields.cue:4:6")
Elts: []ast.Expr{
*ast.Alias{
Ident: *ast.Ident{
NamePos: token.Pos("fields.cue:4:7")
Name: "Y"
}
Equal: token.Pos("fields.cue:4:8")
Expr: *ast.Ident{
NamePos: token.Pos("fields.cue:4:9")
Name: "string"
}
}
}
Rbrack: token.Pos("fields.cue:4:15")
}
Optional: token.Pos("-")
Constraint: token.Token("ILLEGAL")
TokenPos: token.Pos("fields.cue:4:16")
Token: token.Token(":")
Value: *ast.Ident{
NamePos: token.Pos("fields.cue:4:18")
Name: "int"
}
Attrs: []*ast.Attribute{}
}
}
Rbrace: token.Pos("-")
}
Attrs: []*ast.Attribute{}
}
*ast.Field{
Label: *ast.Ident{
NamePos: token.Pos("fields.cue:5:1")
Name: "#Schema"
}
Optional: token.Pos("-")
Constraint: token.Token("ILLEGAL")
TokenPos: token.Pos("fields.cue:5:8")
Token: token.Token(":")
Value: *ast.StructLit{
Lbrace: token.Pos("fields.cue:5:10")
Elts: []ast.Decl{
*ast.Field{
Label: *ast.Ident{
NamePos: token.Pos("fields.cue:6:2")
Name: "name"
}
Optional: token.Pos("-")
Constraint: token.Token("ILLEGAL")
TokenPos: token.Pos("fields.cue:6:6")
Token: token.Token(":")
Value: *ast.Ident{
NamePos: token.Pos("fields.cue:6:8")
Name: "string"
}
Attrs: []*ast.Attribute{}
}
*ast.Ellipsis{
Ellipsis: token.Pos("fields.cue:7:2")
Type: nil
}
}
Rbrace: token.Pos("fields.cue:8:1")
}
Attrs: []*ast.Attribute{}
}
*ast.Field{
Label: *ast.Ident{
NamePos: token.Pos("fields.cue:9:1")
Name: "embed"
}
Optional: token.Pos("-")
Constraint: token.Token("ILLEGAL")
TokenPos: token.Pos("fields.cue:9:6")
Token: token.Token(":")
Value: *ast.StructLit{
Lbrace: token.Pos("fields.cue:9:8")
Elts: []ast.Decl{
*ast.EmbedDecl{
Expr: *ast.Ident{
NamePos: token.Pos("fields.cue:10:2")
Name: "#Schema"
}
}
}
Rbrace: token.Pos("fields.cue:11:1")
}
Attrs: []*ast.Attribute{}
}
}
Imports: []*ast.ImportSpec{}
}
Loading

0 comments on commit 54c01be

Please sign in to comment.