12
12
// See the License for the specific language governing permissions and
13
13
// limitations under the License.
14
14
15
- package format
15
+ package format_test
16
16
17
17
// TODO: port more of the tests of go/printer
18
18
19
19
import (
20
- "io/fs"
21
- "os"
22
- "path"
23
- "path/filepath"
24
20
"strings"
25
21
"testing"
26
22
27
23
"github.com/go-quicktest/qt"
28
- "golang.org/x/tools/txtar"
29
24
30
25
"cuelang.org/go/cue/ast"
26
+ "cuelang.org/go/cue/format"
31
27
"cuelang.org/go/cue/parser"
32
28
"cuelang.org/go/cue/token"
33
29
"cuelang.org/go/internal"
34
- "cuelang.org/go/internal/cuetest "
30
+ "cuelang.org/go/internal/cuetxtar "
35
31
)
36
32
37
- var (
38
- defaultConfig = newConfig ([]Option {})
39
- Fprint = defaultConfig .fprint
40
- )
33
+ const debug = false
41
34
42
35
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 ())
57
44
}
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 ))
58
57
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 )
107
60
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.
108
67
}
109
- }
68
+ })
110
69
}
111
70
112
71
// Verify that the printer can be invoked during initialization.
113
72
func init () {
114
73
const name = "foobar"
115
- b , err := Fprint (& ast.Ident {Name : name })
74
+ b , err := format . Node (& ast.Ident {Name : name })
116
75
if err != nil {
117
76
panic (err ) // error in test
118
77
}
@@ -163,7 +122,7 @@ func TestNodes(t *testing.T) {
163
122
}}
164
123
for _ , tc := range testCases {
165
124
t .Run (tc .name , func (t * testing.T ) {
166
- b , err := Node (tc .in , Simplify ())
125
+ b , err := format . Node (tc .in , format . Simplify ())
167
126
if err != nil {
168
127
t .Fatal (err )
169
128
}
@@ -183,7 +142,7 @@ func TestBadNodes(t *testing.T) {
183
142
if err == nil {
184
143
t .Error ("expected illegal program" ) // error in test
185
144
}
186
- b , _ := Fprint (f )
145
+ b , _ := format . Node (f )
187
146
if string (b ) != res {
188
147
t .Errorf ("got %q, expected %q" , string (b ), res )
189
148
}
@@ -201,7 +160,7 @@ func TestPackage(t *testing.T) {
201
160
},
202
161
},
203
162
}
204
- b , err := Node (f )
163
+ b , err := format . Node (f )
205
164
if err != nil {
206
165
t .Fatal (err )
207
166
}
@@ -270,7 +229,7 @@ e2: c*t.z
270
229
}
271
230
272
231
// pretty-print original
273
- b , err := ( & config { UseSpaces : true , Tabwidth : 8 }). fprint ( f1 )
232
+ b , err := format . Node ( f1 , format . UseSpaces ( 8 ) )
274
233
if err != nil {
275
234
t .Fatal (err )
276
235
}
@@ -331,12 +290,12 @@ func TestDeclLists(t *testing.T) {
331
290
panic (err ) // error in test
332
291
}
333
292
334
- b , err := Fprint (file . Decls ) // only print declarations
293
+ b , err := format . Node (file ) // only print declarations
335
294
if err != nil {
336
295
panic (err ) // error in test
337
296
}
338
297
339
- out := string (b )
298
+ out := strings . TrimSpace ( string (b ) )
340
299
341
300
if out != src {
342
301
t .Errorf ("\n got : %q\n want: %q\n " , out , src )
@@ -355,7 +314,7 @@ func TestIncorrectIdent(t *testing.T) {
355
314
}
356
315
for _ , tc := range testCases {
357
316
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" )})
359
318
if got , want := string (b ), tc .out + `: A` ; got != want {
360
319
t .Errorf ("got %q; want %q" , got , want )
361
320
}
@@ -370,7 +329,7 @@ func TestX(t *testing.T) {
370
329
const src = `
371
330
372
331
`
373
- b , err := Source ([]byte (src ), Simplify ())
332
+ b , err := format . Source ([]byte (src ), format . Simplify ())
374
333
if err != nil {
375
334
t .Error (err )
376
335
}
0 commit comments