Skip to content

Commit 8f841ee

Browse files
committed
cue/format: port format tests to cuetxtar
This allows us to save about forty lines of logic, as it was largely duplicated with what cuetxtar already does. While here, also start using an external test package, as there's no reason for the tests to need to use unexported APIs. Signed-off-by: Daniel Martí <[email protected]> Change-Id: Iabf75ae4a32983703ec1b245a78ce7747210abae Reviewed-on: https://review.gerrithub.io/c/cue-lang/cue/+/1215471 Reviewed-by: Matthew Sackman <[email protected]> TryBot-Result: CUEcueckoo <[email protected]> Unity-Result: CUE porcuepine <[email protected]>
1 parent 8946dc9 commit 8f841ee

File tree

8 files changed

+63
-104
lines changed

8 files changed

+63
-104
lines changed

cue/format/format_test.go

Lines changed: 42 additions & 83 deletions
Original file line numberDiff line numberDiff line change
@@ -12,107 +12,66 @@
1212
// See the License for the specific language governing permissions and
1313
// limitations under the License.
1414

15-
package format
15+
package format_test
1616

1717
// TODO: port more of the tests of go/printer
1818

1919
import (
20-
"io/fs"
21-
"os"
22-
"path"
23-
"path/filepath"
2420
"strings"
2521
"testing"
2622

2723
"github.com/go-quicktest/qt"
28-
"golang.org/x/tools/txtar"
2924

3025
"cuelang.org/go/cue/ast"
26+
"cuelang.org/go/cue/format"
3127
"cuelang.org/go/cue/parser"
3228
"cuelang.org/go/cue/token"
3329
"cuelang.org/go/internal"
34-
"cuelang.org/go/internal/cuetest"
30+
"cuelang.org/go/internal/cuetxtar"
3531
)
3632

37-
var (
38-
defaultConfig = newConfig([]Option{})
39-
Fprint = defaultConfig.fprint
40-
)
33+
const debug = false
4134

4235
func TestFiles(t *testing.T) {
43-
txtarFiles, err := filepath.Glob("testdata/*.txtar")
44-
qt.Assert(t, qt.IsNil(err))
45-
for _, txtarFile := range txtarFiles {
46-
ar, err := txtar.ParseFile(txtarFile)
47-
qt.Assert(t, qt.IsNil(err))
48-
49-
opts := []Option{TabIndent(true)}
50-
for _, word := range strings.Fields(string(ar.Comment)) {
51-
switch word {
52-
case "simplify":
53-
opts = append(opts, Simplify())
54-
case "sort-imports":
55-
opts = append(opts, sortImportsOption())
56-
}
36+
test := cuetxtar.TxTarTest{
37+
Root: "./testdata",
38+
Name: "format",
39+
}
40+
test.Run(t, func(t *cuetxtar.Test) {
41+
opts := []format.Option{format.TabIndent(true)}
42+
if t.HasTag("simplify") {
43+
opts = append(opts, format.Simplify())
5744
}
45+
// TODO(mvdan): note that this option is not exposed in the API,
46+
// nor does it seem to be actually tested in any of the txtar testdata files.
47+
// if t.HasTag("sort-imports") {
48+
// opts = append(opts, format.sortImportsOption())
49+
// }
50+
51+
for _, f := range t.Archive.Files {
52+
if !strings.HasSuffix(f.Name, ".input") {
53+
continue
54+
}
55+
res, err := format.Source(f.Data, opts...)
56+
qt.Assert(t, qt.IsNil(err))
5857

59-
tfs, err := txtar.FS(ar)
60-
qt.Assert(t, qt.IsNil(err))
61-
inputFiles, err := fs.Glob(tfs, "*.input")
62-
qt.Assert(t, qt.IsNil(err))
63-
64-
for _, inputFile := range inputFiles {
65-
goldenFile := strings.TrimSuffix(inputFile, ".input") + ".golden"
66-
t.Run(path.Join(txtarFile, inputFile), func(t *testing.T) {
67-
src, err := fs.ReadFile(tfs, inputFile)
68-
qt.Assert(t, qt.IsNil(err))
69-
70-
res, err := Source(src, opts...)
71-
qt.Assert(t, qt.IsNil(err))
72-
73-
// make sure formatted output is syntactically correct
74-
_, err = parser.ParseFile("", res, parser.AllErrors)
75-
qt.Assert(t, qt.IsNil(err))
76-
77-
// update golden files if necessary
78-
// TODO(mvdan): deduplicate this code with UpdateGoldenFiles on txtar files?
79-
if cuetest.UpdateGoldenFiles {
80-
for i := range ar.Files {
81-
file := &ar.Files[i]
82-
if file.Name == goldenFile {
83-
file.Data = res
84-
return
85-
}
86-
}
87-
ar.Files = append(ar.Files, txtar.File{
88-
Name: goldenFile,
89-
Data: res,
90-
})
91-
return
92-
}
93-
94-
// get golden
95-
gld, err := fs.ReadFile(tfs, goldenFile)
96-
qt.Assert(t, qt.IsNil(err))
97-
98-
// formatted source and golden must be the same
99-
qt.Assert(t, qt.Equals(string(res), string(gld)))
100-
101-
// TODO(mvdan): check that all files format in an idempotent way,
102-
// i.e. that formatting a golden file results in no changes.
103-
})
104-
}
105-
if cuetest.UpdateGoldenFiles {
106-
err = os.WriteFile(txtarFile, txtar.Format(ar), 0o666)
58+
// make sure formatted output is syntactically correct
59+
_, err = parser.ParseFile("", res, parser.AllErrors)
10760
qt.Assert(t, qt.IsNil(err))
61+
62+
goldenFile := strings.TrimSuffix(f.Name, ".input") + ".golden"
63+
t.Writer(goldenFile).Write(res)
64+
65+
// TODO(mvdan): check that all files format in an idempotent way,
66+
// i.e. that formatting a golden file results in no changes.
10867
}
109-
}
68+
})
11069
}
11170

11271
// Verify that the printer can be invoked during initialization.
11372
func init() {
11473
const name = "foobar"
115-
b, err := Fprint(&ast.Ident{Name: name})
74+
b, err := format.Node(&ast.Ident{Name: name})
11675
if err != nil {
11776
panic(err) // error in test
11877
}
@@ -163,7 +122,7 @@ func TestNodes(t *testing.T) {
163122
}}
164123
for _, tc := range testCases {
165124
t.Run(tc.name, func(t *testing.T) {
166-
b, err := Node(tc.in, Simplify())
125+
b, err := format.Node(tc.in, format.Simplify())
167126
if err != nil {
168127
t.Fatal(err)
169128
}
@@ -183,7 +142,7 @@ func TestBadNodes(t *testing.T) {
183142
if err == nil {
184143
t.Error("expected illegal program") // error in test
185144
}
186-
b, _ := Fprint(f)
145+
b, _ := format.Node(f)
187146
if string(b) != res {
188147
t.Errorf("got %q, expected %q", string(b), res)
189148
}
@@ -201,7 +160,7 @@ func TestPackage(t *testing.T) {
201160
},
202161
},
203162
}
204-
b, err := Node(f)
163+
b, err := format.Node(f)
205164
if err != nil {
206165
t.Fatal(err)
207166
}
@@ -270,7 +229,7 @@ e2: c*t.z
270229
}
271230

272231
// pretty-print original
273-
b, err := (&config{UseSpaces: true, Tabwidth: 8}).fprint(f1)
232+
b, err := format.Node(f1, format.UseSpaces(8))
274233
if err != nil {
275234
t.Fatal(err)
276235
}
@@ -331,12 +290,12 @@ func TestDeclLists(t *testing.T) {
331290
panic(err) // error in test
332291
}
333292

334-
b, err := Fprint(file.Decls) // only print declarations
293+
b, err := format.Node(file) // only print declarations
335294
if err != nil {
336295
panic(err) // error in test
337296
}
338297

339-
out := string(b)
298+
out := strings.TrimSpace(string(b))
340299

341300
if out != src {
342301
t.Errorf("\ngot : %q\nwant: %q\n", out, src)
@@ -355,7 +314,7 @@ func TestIncorrectIdent(t *testing.T) {
355314
}
356315
for _, tc := range testCases {
357316
t.Run(tc.ident, func(t *testing.T) {
358-
b, _ := Node(&ast.Field{Label: ast.NewIdent(tc.ident), Value: ast.NewIdent("A")})
317+
b, _ := format.Node(&ast.Field{Label: ast.NewIdent(tc.ident), Value: ast.NewIdent("A")})
359318
if got, want := string(b), tc.out+`: A`; got != want {
360319
t.Errorf("got %q; want %q", got, want)
361320
}
@@ -370,7 +329,7 @@ func TestX(t *testing.T) {
370329
const src = `
371330
372331
`
373-
b, err := Source([]byte(src), Simplify())
332+
b, err := format.Source([]byte(src), format.Simplify())
374333
if err != nil {
375334
t.Error(err)
376335
}

cue/format/testdata/comments.txtar

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
simplify
1+
#simplify
22

33
-- empty.input --
4-
-- empty.golden --
4+
-- out/format/empty.golden --
55

66
-- comments.input --
77
// Package line 1 group 1
@@ -173,7 +173,7 @@ foo: [
173173
[string]: _ // comment
174174
}
175175
}
176-
-- comments.golden --
176+
-- out/format/comments.golden --
177177
// Package line 1 group 1
178178
// Package line 2 group 1
179179

@@ -334,25 +334,25 @@ foo: [
334334
}
335335
-- comment_alone.input --
336336
// Just one comment on its own.
337-
-- comment_alone.golden --
337+
-- out/format/comment_alone.golden --
338338
// Just one comment on its own.
339339
-- comment_field.input --
340340
// Just one comment on a field.
341341
foo: string
342-
-- comment_field.golden --
342+
-- out/format/comment_field.golden --
343343
// Just one comment on a field.
344344
foo: string
345345
-- comments_alone.input --
346346
// Just a few comments
347347
// on their own.
348-
-- comments_alone.golden --
348+
-- out/format/comments_alone.golden --
349349
// Just a few comments
350350
// on their own.
351351
-- comments_field.input --
352352
// Just a few comments
353353
// on a field.
354354
foo: string
355-
-- comments_field.golden --
355+
-- out/format/comments_field.golden --
356356
// Just a few comments
357357
// on a field.
358358
foo: string

cue/format/testdata/expressions.txtar

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -283,7 +283,7 @@ import "list"
283283
s: [string]: [string]: a
284284
s: [string]: {s: string}
285285
}
286-
-- expressions.golden --
286+
-- out/format/expressions.golden --
287287
package expressions
288288

289289
import "list"
@@ -587,7 +587,7 @@ _foo: {
587587

588588
// skip_create_image: true
589589
}
590-
-- issue2496.golden --
590+
-- out/format/issue2496.golden --
591591
machine_type: [
592592
if arch == "amd" {"n2-standard-2"},
593593
if arch == "arm" {"t2a-standard-2"},

cue/format/testdata/imports.txtar

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
1-
sort-imports
1+
#sort-imports
22

33
-- pkgonly.input --
44
package foo
5-
-- pkgonly.golden --
5+
-- out/format/pkgonly.golden --
66
package foo
77
-- attronly.input --
88
@attr(foo)
9-
-- attronly.golden --
9+
-- out/format/attronly.golden --
1010
@attr(foo)
1111
-- importonly.input --
1212
import "foo"
13-
-- importonly.golden --
13+
-- out/format/importonly.golden --
1414
import "foo"
1515
-- imports.input --
1616
package foo
@@ -40,7 +40,7 @@ import (
4040
a: time.time
4141
b: foo.foo
4242
c: bar.Bar
43-
-- imports.golden --
43+
-- out/format/imports.golden --
4444
package foo
4545

4646
import (

cue/format/testdata/issue1544.txtar

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,21 +2,21 @@
22
if true {
33
x: 5
44
}
5-
-- if.golden --
5+
-- out/format/if.golden --
66
if true {
77
x: 5
88
}
99
-- let.input --
1010
let x = 5
1111
y: x
12-
-- let.golden --
12+
-- out/format/let.golden --
1313
let x = 5
1414
y: x
1515
-- for.input --
1616
for v in ["a", "b"] {
1717
(v): v
1818
}
19-
-- for.golden --
19+
-- out/format/for.golden --
2020
for v in ["a", "b"] {
2121
(v): v
2222
}

cue/format/testdata/issue3291.txtar

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ B: [
88
},
99
4, // this comment is fine
1010
]
11-
-- file.golden --
11+
-- out/format/file.golden --
1212
A: true
1313
B: [
1414
1, // this comment is fine

cue/format/testdata/simplify.txtar

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
simplify
1+
#simplify
22

33
-- simplify.input --
44
import "time"
@@ -89,7 +89,7 @@ y: {
8989
foo: {}
9090
bar: "foo": foo // removing the quotes would cause a cyclic reference
9191
}
92-
-- simplify.golden --
92+
-- out/format/simplify.golden --
9393
import "time"
9494

9595
foo: bar: "str"

cue/format/testdata/values.txtar

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ a: 3.e100
1515
s: """
1616
x\"\"\"
1717
"""
18-
-- values.golden --
18+
-- out/format/values.golden --
1919
a: 0e+1
2020
a: 0e1
2121
a: 0e+1

0 commit comments

Comments
 (0)