this repo has no description
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

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í <mvdan@mvdan.cc>
Change-Id: Iabf75ae4a32983703ec1b245a78ce7747210abae
Reviewed-on: https://review.gerrithub.io/c/cue-lang/cue/+/1215471
Reviewed-by: Matthew Sackman <matthew@cue.works>
TryBot-Result: CUEcueckoo <cueckoo@cuelang.org>
Unity-Result: CUE porcuepine <cue.porcuepine@gmail.com>

+61 -102
+40 -81
cue/format/format_test.go
··· 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)) 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()) 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 + // } 48 50 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()) 51 + for _, f := range t.Archive.Files { 52 + if !strings.HasSuffix(f.Name, ".input") { 53 + continue 56 54 } 57 - } 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)) 58 + // make sure formatted output is syntactically correct 59 + _, err = parser.ParseFile("", res, parser.AllErrors) 60 + qt.Assert(t, qt.IsNil(err)) 63 61 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)) 62 + goldenFile := strings.TrimSuffix(f.Name, ".input") + ".golden" 63 + t.Writer(goldenFile).Write(res) 69 64 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) 107 - qt.Assert(t, qt.IsNil(err)) 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 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 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 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 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 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("\ngot : %q\nwant: %q\n", out, src) ··· 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 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 }
+7 -7
cue/format/testdata/comments.txtar
··· 1 - simplify 1 + #simplify 2 2 3 3 -- empty.input -- 4 - -- empty.golden -- 4 + -- out/format/empty.golden -- 5 5 6 6 -- comments.input -- 7 7 // Package line 1 group 1 ··· 173 173 [string]: _ // comment 174 174 } 175 175 } 176 - -- comments.golden -- 176 + -- out/format/comments.golden -- 177 177 // Package line 1 group 1 178 178 // Package line 2 group 1 179 179 ··· 334 334 } 335 335 -- comment_alone.input -- 336 336 // Just one comment on its own. 337 - -- comment_alone.golden -- 337 + -- out/format/comment_alone.golden -- 338 338 // Just one comment on its own. 339 339 -- comment_field.input -- 340 340 // Just one comment on a field. 341 341 foo: string 342 - -- comment_field.golden -- 342 + -- out/format/comment_field.golden -- 343 343 // Just one comment on a field. 344 344 foo: string 345 345 -- comments_alone.input -- 346 346 // Just a few comments 347 347 // on their own. 348 - -- comments_alone.golden -- 348 + -- out/format/comments_alone.golden -- 349 349 // Just a few comments 350 350 // on their own. 351 351 -- comments_field.input -- 352 352 // Just a few comments 353 353 // on a field. 354 354 foo: string 355 - -- comments_field.golden -- 355 + -- out/format/comments_field.golden -- 356 356 // Just a few comments 357 357 // on a field. 358 358 foo: string
+2 -2
cue/format/testdata/expressions.txtar
··· 283 283 s: [string]: [string]: a 284 284 s: [string]: {s: string} 285 285 } 286 - -- expressions.golden -- 286 + -- out/format/expressions.golden -- 287 287 package expressions 288 288 289 289 import "list" ··· 587 587 588 588 // skip_create_image: true 589 589 } 590 - -- issue2496.golden -- 590 + -- out/format/issue2496.golden -- 591 591 machine_type: [ 592 592 if arch == "amd" {"n2-standard-2"}, 593 593 if arch == "arm" {"t2a-standard-2"},
+5 -5
cue/format/testdata/imports.txtar
··· 1 - sort-imports 1 + #sort-imports 2 2 3 3 -- pkgonly.input -- 4 4 package foo 5 - -- pkgonly.golden -- 5 + -- out/format/pkgonly.golden -- 6 6 package foo 7 7 -- attronly.input -- 8 8 @attr(foo) 9 - -- attronly.golden -- 9 + -- out/format/attronly.golden -- 10 10 @attr(foo) 11 11 -- importonly.input -- 12 12 import "foo" 13 - -- importonly.golden -- 13 + -- out/format/importonly.golden -- 14 14 import "foo" 15 15 -- imports.input -- 16 16 package foo ··· 40 40 a: time.time 41 41 b: foo.foo 42 42 c: bar.Bar 43 - -- imports.golden -- 43 + -- out/format/imports.golden -- 44 44 package foo 45 45 46 46 import (
+3 -3
cue/format/testdata/issue1544.txtar
··· 2 2 if true { 3 3 x: 5 4 4 } 5 - -- if.golden -- 5 + -- out/format/if.golden -- 6 6 if true { 7 7 x: 5 8 8 } 9 9 -- let.input -- 10 10 let x = 5 11 11 y: x 12 - -- let.golden -- 12 + -- out/format/let.golden -- 13 13 let x = 5 14 14 y: x 15 15 -- for.input -- 16 16 for v in ["a", "b"] { 17 17 (v): v 18 18 } 19 - -- for.golden -- 19 + -- out/format/for.golden -- 20 20 for v in ["a", "b"] { 21 21 (v): v 22 22 }
+1 -1
cue/format/testdata/issue3291.txtar
··· 8 8 }, 9 9 4, // this comment is fine 10 10 ] 11 - -- file.golden -- 11 + -- out/format/file.golden -- 12 12 A: true 13 13 B: [ 14 14 1, // this comment is fine
+2 -2
cue/format/testdata/simplify.txtar
··· 1 - simplify 1 + #simplify 2 2 3 3 -- simplify.input -- 4 4 import "time" ··· 89 89 foo: {} 90 90 bar: "foo": foo // removing the quotes would cause a cyclic reference 91 91 } 92 - -- simplify.golden -- 92 + -- out/format/simplify.golden -- 93 93 import "time" 94 94 95 95 foo: bar: "str"
+1 -1
cue/format/testdata/values.txtar
··· 15 15 s: """ 16 16 x\"\"\" 17 17 """ 18 - -- values.golden -- 18 + -- out/format/values.golden -- 19 19 a: 0e+1 20 20 a: 0e1 21 21 a: 0e+1