diff --git a/cue/format/format_test.go b/cue/format/format_test.go index 7b3d4a74a94..a1c92754b86 100644 --- a/cue/format/format_test.go +++ b/cue/format/format_test.go @@ -17,19 +17,22 @@ package format // TODO: port more of the tests of go/printer import ( - "bytes" - "fmt" + "io/fs" "os" + "path" "path/filepath" + "strings" "testing" - "time" + + "github.com/go-quicktest/qt" + "golang.org/x/tools/txtar" "cuelang.org/go/cue/ast" - "cuelang.org/go/cue/errors" "cuelang.org/go/cue/parser" "cuelang.org/go/cue/token" "cuelang.org/go/internal" "cuelang.org/go/internal/cuetest" + "cuelang.org/go/internal/txtarfs" ) var ( @@ -37,184 +40,75 @@ var ( Fprint = defaultConfig.fprint ) -const ( - dataDir = "testdata" -) - -type checkMode uint - -const ( - _ checkMode = 1 << iota - idempotent - simplify - sortImps -) - -// format parses src, prints the corresponding AST, verifies the resulting -// src is syntactically correct, and returns the resulting src or an error -// if any. -func format(src []byte, mode checkMode) ([]byte, error) { - // parse src - opts := []Option{TabIndent(true)} - if mode&simplify != 0 { - opts = append(opts, Simplify()) - } - if mode&sortImps != 0 { - opts = append(opts, sortImportsOption()) - } - - res, err := Source(src, opts...) - if err != nil { - return nil, err - } - - // make sure formatted output is syntactically correct - if _, err := parser.ParseFile("", res, parser.AllErrors); err != nil { - return nil, errors.Append(err.(errors.Error), - errors.Newf(token.NoPos, "re-parse failed: %s", res)) - } - - return res, nil -} - -// lineAt returns the line in text starting at offset offs. -func lineAt(text []byte, offs int) []byte { - i := offs - for i < len(text) && text[i] != '\n' { - i++ - } - return text[offs:i] -} - -// diff compares a and b. -func diff(aname, bname string, a, b []byte) error { - var buf bytes.Buffer // holding long error message - - // compare lengths - if len(a) != len(b) { - fmt.Fprintf(&buf, "\nlength changed: len(%s) = %d, len(%s) = %d", aname, len(a), bname, len(b)) - } - - // compare contents - line := 1 - offs := 1 - for i := 0; i < len(a) && i < len(b); i++ { - ch := a[i] - if ch != b[i] { - fmt.Fprintf(&buf, "\n%s:%d:%d: %s", aname, line, i-offs+1, lineAt(a, offs)) - fmt.Fprintf(&buf, "\n%s:%d:%d: %s", bname, line, i-offs+1, lineAt(b, offs)) - fmt.Fprintf(&buf, "\n\n") - break - } - if ch == '\n' { - line++ - offs = i + 1 - } - } - - if buf.Len() > 0 { - return errors.New(buf.String()) - } - return nil -} - -func runcheck(t *testing.T, source, golden string, mode checkMode) { - src, err := os.ReadFile(source) - if err != nil { - t.Error(err) - return - } - - res, err := format(src, mode) - if err != nil { - b := &bytes.Buffer{} - errors.Print(b, err, nil) - t.Error(b.String()) - return - } - - // update golden files if necessary - if cuetest.UpdateGoldenFiles { - if err := os.WriteFile(golden, res, 0644); err != nil { - t.Error(err) +func TestFiles(t *testing.T) { + txtarFiles, err := filepath.Glob("testdata/*.txtar") + qt.Assert(t, qt.IsNil(err)) + for _, txtarFile := range txtarFiles { + ar, err := txtar.ParseFile(txtarFile) + qt.Assert(t, qt.IsNil(err)) + + opts := []Option{TabIndent(true)} + for _, word := range strings.Fields(string(ar.Comment)) { + switch word { + case "simplify": + opts = append(opts, Simplify()) + case "sort-imports": + opts = append(opts, sortImportsOption()) + } } - return - } - // get golden - gld, err := os.ReadFile(golden) - if err != nil { - t.Error(err) - return - } - - // formatted source and golden must be the same - if err := diff(source, golden, res, gld); err != nil { - t.Error(err) - return - } - - if mode&idempotent != 0 { - // formatting golden must be idempotent - // (This is very difficult to achieve in general and for now - // it is only checked for files explicitly marked as such.) - res, err = format(gld, mode) - if err != nil { - t.Fatal(err) + tfs := txtarfs.FS(ar) + inputFiles, err := fs.Glob(tfs, "*.input") + qt.Assert(t, qt.IsNil(err)) + + for _, inputFile := range inputFiles { + goldenFile := strings.TrimSuffix(inputFile, ".input") + ".golden" + t.Run(path.Join(txtarFile, inputFile), func(t *testing.T) { + src, err := fs.ReadFile(tfs, inputFile) + qt.Assert(t, qt.IsNil(err)) + + res, err := Source(src, opts...) + qt.Assert(t, qt.IsNil(err)) + + // make sure formatted output is syntactically correct + _, err = parser.ParseFile("", res, parser.AllErrors) + qt.Assert(t, qt.IsNil(err)) + + // update golden files if necessary + // TODO(mvdan): deduplicate this code with UpdateGoldenFiles on txtar files? + if cuetest.UpdateGoldenFiles { + for i := range ar.Files { + file := &ar.Files[i] + if file.Name == goldenFile { + file.Data = res + return + } + } + ar.Files = append(ar.Files, txtar.File{ + Name: goldenFile, + Data: res, + }) + return + } + + // get golden + gld, err := fs.ReadFile(tfs, goldenFile) + qt.Assert(t, qt.IsNil(err)) + + // formatted source and golden must be the same + qt.Assert(t, qt.Equals(string(res), string(gld))) + + // TODO(mvdan): check that all files format in an idempotent way, + // i.e. that formatting a golden file results in no changes. + }) } - if err := diff(golden, fmt.Sprintf("format(%s)", golden), gld, res); err != nil { - t.Errorf("golden is not idempotent: %s", err) + if cuetest.UpdateGoldenFiles { + err = os.WriteFile(txtarFile, txtar.Format(ar), 0o666) + qt.Assert(t, qt.IsNil(err)) } } } -func check(t *testing.T, source, golden string, mode checkMode) { - // run the test - cc := make(chan int) - go func() { - runcheck(t, source, golden, mode) - cc <- 0 - }() - - // wait with timeout - select { - case <-time.After(100000 * time.Second): // plenty of a safety margin, even for very slow machines - // test running past time out - t.Errorf("%s: running too slowly", source) - case <-cc: - // test finished within allotted time margin - } -} - -type entry struct { - source, golden string - mode checkMode -} - -// Set CUE_UPDATE=1 to create/update the respective golden files. -var data = []entry{ - {"comments.input", "comments.golden", simplify}, - {"simplify.input", "simplify.golden", simplify}, - {"expressions.input", "expressions.golden", 0}, - {"values.input", "values.golden", 0}, - {"imports.input", "imports.golden", sortImps}, -} - -func TestFiles(t *testing.T) { - t.Parallel() - for _, e := range data { - source := filepath.Join(dataDir, e.source) - golden := filepath.Join(dataDir, e.golden) - mode := e.mode - t.Run(e.source, func(t *testing.T) { - t.Parallel() - check(t, source, golden, mode) - // TODO(gri) check that golden is idempotent - //check(t, golden, golden, e.mode) - }) - } -} - // Verify that the printer can be invoked during initialization. func init() { const name = "foobar" @@ -476,7 +370,7 @@ func TestX(t *testing.T) { const src = ` ` - b, err := format([]byte(src), simplify) + b, err := Source([]byte(src), Simplify()) if err != nil { t.Error(err) } diff --git a/cue/format/testdata/comments.golden b/cue/format/testdata/comments.golden deleted file mode 100644 index 35c33ea9c75..00000000000 --- a/cue/format/testdata/comments.golden +++ /dev/null @@ -1,158 +0,0 @@ -// Package line 1 group 1 -// Package line 2 group 1 - -// Package line 1 - group 2 -// Package line 2 - group 2 -package comments - -// Emit line 1 group 1 - -// Emit line 1 group 2 -// Emit line 2 group 2 -{ - // Inside Emit -} - -a: 3 // a line comment - -b: 4 // line comment that is last in the file. -cc: 555 // align comments - -// don't simplify -a: { - b: 2 -} - -// simplify -a: { - b: c: d: 1 -} - -// unchanged -a: b: [1] - -// don't simplify -a: b: c: 2 - -// rewrite okay -a: b: c: d: 2 - -m: {} -m: { - // empty with comment -} - -// Issue #478 -struct1: { - // This is a comment - - // This is a comment - - // Another comment - - something: {} - // extra comment -} - -struct2: { - // This is a comment - - // This is a comment - - // Another comment - something: {} - - // extra comment -} - -list1: [ - // Comment1 - - // Comment2 - - // Another comment - {}, - - // Comment 3 -] - -list2: [ - // foo -] - -list3: [ - // foo - - // bar -] - -list4: [ - 1, // number - a.b, // selector - (a), // paren - +1, // unary - a[0], // index - a[2:3], // slice - strings.Sort([1, 2]), // call - - a.b, - // under selector - - a.b, - // multiple - // under - - // selector -] - -funcArg1: foo( - // Comment1 - - // Comment2 - 3, - - // Comment3 - ) - -funcArg2: foo( - // Comment1 - - // Comment2 - - 3, - // Comment3 - ) - -funcArg3: foo( - 2, - - // Comment1 - - // Comment2 - 3, - - // Comment3 - ) - -// comment including some tabs - -// issue #2567 -foo: [ - bar["baz"], //some comment -] - -[ - if true {}, // inline comment -] - -{ - - foo: { - // some - ... // comment - } - // comments - ... // surrounding - - // an ellipsis -} diff --git a/cue/format/testdata/comments.input b/cue/format/testdata/comments.txtar similarity index 50% rename from cue/format/testdata/comments.input rename to cue/format/testdata/comments.txtar index 85257ac7497..87c2bd4bbb6 100644 --- a/cue/format/testdata/comments.input +++ b/cue/format/testdata/comments.txtar @@ -1,3 +1,6 @@ +simplify + +-- comments.input -- // Package line 1 group 1 // Package line 2 group 1 @@ -166,4 +169,163 @@ foo: [ // some [string]: _ // comment } -} \ No newline at end of file +} +-- comments.golden -- +// Package line 1 group 1 +// Package line 2 group 1 + +// Package line 1 - group 2 +// Package line 2 - group 2 +package comments + +// Emit line 1 group 1 + +// Emit line 1 group 2 +// Emit line 2 group 2 +{ + // Inside Emit +} + +a: 3 // a line comment + +b: 4 // line comment that is last in the file. +cc: 555 // align comments + +// don't simplify +a: { + b: 2 +} + +// simplify +a: { + b: c: d: 1 +} + +// unchanged +a: b: [1] + +// don't simplify +a: b: c: 2 + +// rewrite okay +a: b: c: d: 2 + +m: {} +m: { + // empty with comment +} + +// Issue #478 +struct1: { + // This is a comment + + // This is a comment + + // Another comment + + something: {} + // extra comment +} + +struct2: { + // This is a comment + + // This is a comment + + // Another comment + something: {} + + // extra comment +} + +list1: [ + // Comment1 + + // Comment2 + + // Another comment + {}, + + // Comment 3 +] + +list2: [ + // foo +] + +list3: [ + // foo + + // bar +] + +list4: [ + 1, // number + a.b, // selector + (a), // paren + +1, // unary + a[0], // index + a[2:3], // slice + strings.Sort([1, 2]), // call + + a.b, + // under selector + + a.b, + // multiple + // under + + // selector +] + +funcArg1: foo( + // Comment1 + + // Comment2 + 3, + + // Comment3 + ) + +funcArg2: foo( + // Comment1 + + // Comment2 + + 3, + // Comment3 + ) + +funcArg3: foo( + 2, + + // Comment1 + + // Comment2 + 3, + + // Comment3 + ) + +// comment including some tabs + +// issue #2567 +foo: [ + bar["baz"], //some comment +] + +[ + if true {}, // inline comment +] + +{ + + foo: { + // some + ... // comment + } + // comments + ... // surrounding + + // an ellipsis +} diff --git a/cue/format/testdata/expressions.golden b/cue/format/testdata/expressions.golden deleted file mode 100644 index aff4893a42c..00000000000 --- a/cue/format/testdata/expressions.golden +++ /dev/null @@ -1,281 +0,0 @@ -package expressions - -import "list" - -{ - a: 1 // comment - aaa: 22 // comment - - "": 3 - - b: 3 - - c: b: a: 4 - c?: bb?: aaa?: 5 - c: b: [Name=string]: a: int - let alias = 3.14 - "g\("en")"?: 4 - - let alias2 = foo // with comment - let aaalias = foo - b: bar - - bottom: _|_ - - a: - b: - c: 2 - - req!: int - - a: bbbb: c: 3 - a: b: 3 - a: bb: cc: 3 - - empty: {} - emptyNewLine: {} - someObject: { - a: 8 - aa: 9 - aaa: 10 - } - - #someDefinition: { - embedding - - field: 2 - } - - #openDef: { - a: int - ... - } - - attrs: { - a: 8 @go(A) // comment - aa: 8 @go(A) // comment - bb: 9 - bbb: 10 @go(Bbb) @xml(,attr) // comment - bbbb: 100 @go(Bbbb) @xml(,attr) // comment - } - - foo: { - bar: string @go(-) - } - - e: 1 + 2*3 - e: 1 * 2 * 3 // error - e: >=2 & <=3 - e: >2 & <=(3 + 4) - ex: >2 & <=(3 + 4*5) - e: >2 & <=3 & <=4 - e: 1 + 2 + 3 // error - - e: s[1+2] - e: s[1:2] - e: s[1+2 : 2+4] - e: s[2] - e: s[2*3] - e: s[1+2*3] - - e: a | - b | - c - - e: - a | - b | c | - d - - e: f(3 + 4 + 5) - e: f(3 * 4 * 5) - e: f(3 + 4*5) - - e: f(3 + 4 div 5) - - e: 3 < 4 && 5 > 4 - e: a || b && c || d - - e: a + +b*3 - e: -a - -b - - e: b + c - e: b*c + d - e: a*b + c - e: a - b - c - e: a - (b - c) - e: a - b*c - e: a - (b * c) - e: a * b / c - e: a div b + 5 - e: a / b - e: x[a | b] - e: x[a/b] - e: a & b - e: a + +b - e: a - -b - e: a div -b - e: x[a*-b] - e: x[a + +b] - e: len(longVariableName) * 2 - - e: "\(a)" - e: 'aa \(aaa) aa' - e: "aa \(aaa)" - - e: [] - e: [] - e: [1, 2, - ] - e: [1, 2] - e: [1, 2, 3, 4, - 5, 6, 7, 8] - e: [1, 2, 3, 4, - 5, 6, 7, 8, // maybe force additional comma - ] - e: [...] - e: [ - ...] - e: [..., - ] - e: [1, 2, ...] - e: [1, 2, - ...] - e: [...int] - e: [...int] - e: [...int | float] - e: [for x in someObject if x > 9 { - x - }] - e: [for x in someObject if x > 9 {x}] - e: [ - for x in someObject - if x > 9 {x}] - e: [ - for x in someObject - if x > 9 {x}] - - e: [ - if x > 1 {}, - if x > 1 {}, - for x in src {}, - ] - - for k, v in someObject { - "\(k)": v - } - for k, v in someObject { - "\(k)": v - } - - e: { - for k, v in someObject - if k > "a" { - "\(k)": v - } - } - - e: {for k, v in someObject if k > "a" {"\(k)": v}} - e: {for k, v in someObject if k > "a" { - "\(k)": v - }} - - e: { - for k, v in someObject - let x = v - if k > "a" { - "\(k)": x - }} - - if a | - b { - c: d - } - - e: [{ - a: 1, b: 2 - }] - - e: [{ - a: 1, b: 2 - }, - ] - - e: [{ - a: 1, b: 2 - }, { - c: 1, d: 2 - }] - - e: [{ - a: 1, b: 2 - }, - 3, - 4, - ] - - e: e.f(1, 2) - - e: (3 + 4) - - // field before list - f: 3 - a: [1, 2, // add comma - ] - - foo: bar - - a: "foo-bar": 3 - b: a."foo-bar" - c: a."foo-bar".b - d: a. - "foo-bar" - e: a. - "foo-bar". - b - f: 2 - - "contains tabs": 123 - @jsonschema(foo="contains tabs") - - j: cueckoo: _ | [ - 1, - - 2, - ] - k: cueckoo: *[ - 1, - - 2, - ] - l: cueckoo: list.Concat([ - 1, - - 2, - ]) - - m: [1, 2, 3] - m: [1, 2, 3] - m: [1, 2, 3] - m: [1, 2, 3] - m: [1, 2, 3] - m: [1, 2, 3] - m: [1, 2, 3] - m: [1, 2, 3] - m: [1, 2, 3] - m: [if true {1}, 2, 3] - n: [1] - o: [{}] - o: [{}] - o: [{}] - o: [{}] - - p: 1 - p: p & {p: 2} - q: 1 - q: q | {q: 2} - r: 1 - r: b & [1, 2, {a: 4}] - s: [string]: [string]: a - s: [string]: {s: string} -} diff --git a/cue/format/testdata/expressions.input b/cue/format/testdata/expressions.txtar similarity index 55% rename from cue/format/testdata/expressions.input rename to cue/format/testdata/expressions.txtar index c63a0bcc6e2..fd41f684c07 100644 --- a/cue/format/testdata/expressions.input +++ b/cue/format/testdata/expressions.txtar @@ -1,3 +1,5 @@ + +-- expressions.input -- package expressions import "list" @@ -281,3 +283,285 @@ import "list" s: [string]: [string]: a s: [string]: {s: string} } +-- expressions.golden -- +package expressions + +import "list" + +{ + a: 1 // comment + aaa: 22 // comment + + "": 3 + + b: 3 + + c: b: a: 4 + c?: bb?: aaa?: 5 + c: b: [Name=string]: a: int + let alias = 3.14 + "g\("en")"?: 4 + + let alias2 = foo // with comment + let aaalias = foo + b: bar + + bottom: _|_ + + a: + b: + c: 2 + + req!: int + + a: bbbb: c: 3 + a: b: 3 + a: bb: cc: 3 + + empty: {} + emptyNewLine: {} + someObject: { + a: 8 + aa: 9 + aaa: 10 + } + + #someDefinition: { + embedding + + field: 2 + } + + #openDef: { + a: int + ... + } + + attrs: { + a: 8 @go(A) // comment + aa: 8 @go(A) // comment + bb: 9 + bbb: 10 @go(Bbb) @xml(,attr) // comment + bbbb: 100 @go(Bbbb) @xml(,attr) // comment + } + + foo: { + bar: string @go(-) + } + + e: 1 + 2*3 + e: 1 * 2 * 3 // error + e: >=2 & <=3 + e: >2 & <=(3 + 4) + ex: >2 & <=(3 + 4*5) + e: >2 & <=3 & <=4 + e: 1 + 2 + 3 // error + + e: s[1+2] + e: s[1:2] + e: s[1+2 : 2+4] + e: s[2] + e: s[2*3] + e: s[1+2*3] + + e: a | + b | + c + + e: + a | + b | c | + d + + e: f(3 + 4 + 5) + e: f(3 * 4 * 5) + e: f(3 + 4*5) + + e: f(3 + 4 div 5) + + e: 3 < 4 && 5 > 4 + e: a || b && c || d + + e: a + +b*3 + e: -a - -b + + e: b + c + e: b*c + d + e: a*b + c + e: a - b - c + e: a - (b - c) + e: a - b*c + e: a - (b * c) + e: a * b / c + e: a div b + 5 + e: a / b + e: x[a | b] + e: x[a/b] + e: a & b + e: a + +b + e: a - -b + e: a div -b + e: x[a*-b] + e: x[a + +b] + e: len(longVariableName) * 2 + + e: "\(a)" + e: 'aa \(aaa) aa' + e: "aa \(aaa)" + + e: [] + e: [] + e: [1, 2, + ] + e: [1, 2] + e: [1, 2, 3, 4, + 5, 6, 7, 8] + e: [1, 2, 3, 4, + 5, 6, 7, 8, // maybe force additional comma + ] + e: [...] + e: [ + ...] + e: [..., + ] + e: [1, 2, ...] + e: [1, 2, + ...] + e: [...int] + e: [...int] + e: [...int | float] + e: [for x in someObject if x > 9 { + x + }] + e: [for x in someObject if x > 9 {x}] + e: [ + for x in someObject + if x > 9 {x}] + e: [ + for x in someObject + if x > 9 {x}] + + e: [ + if x > 1 {}, + if x > 1 {}, + for x in src {}, + ] + + for k, v in someObject { + "\(k)": v + } + for k, v in someObject { + "\(k)": v + } + + e: { + for k, v in someObject + if k > "a" { + "\(k)": v + } + } + + e: {for k, v in someObject if k > "a" {"\(k)": v}} + e: {for k, v in someObject if k > "a" { + "\(k)": v + }} + + e: { + for k, v in someObject + let x = v + if k > "a" { + "\(k)": x + }} + + if a | + b { + c: d + } + + e: [{ + a: 1, b: 2 + }] + + e: [{ + a: 1, b: 2 + }, + ] + + e: [{ + a: 1, b: 2 + }, { + c: 1, d: 2 + }] + + e: [{ + a: 1, b: 2 + }, + 3, + 4, + ] + + e: e.f(1, 2) + + e: (3 + 4) + + // field before list + f: 3 + a: [1, 2, // add comma + ] + + foo: bar + + a: "foo-bar": 3 + b: a."foo-bar" + c: a."foo-bar".b + d: a. + "foo-bar" + e: a. + "foo-bar". + b + f: 2 + + "contains tabs": 123 + @jsonschema(foo="contains tabs") + + j: cueckoo: _ | [ + 1, + + 2, + ] + k: cueckoo: *[ + 1, + + 2, + ] + l: cueckoo: list.Concat([ + 1, + + 2, + ]) + + m: [1, 2, 3] + m: [1, 2, 3] + m: [1, 2, 3] + m: [1, 2, 3] + m: [1, 2, 3] + m: [1, 2, 3] + m: [1, 2, 3] + m: [1, 2, 3] + m: [1, 2, 3] + m: [if true {1}, 2, 3] + n: [1] + o: [{}] + o: [{}] + o: [{}] + o: [{}] + + p: 1 + p: p & {p: 2} + q: 1 + q: q | {q: 2} + r: 1 + r: b & [1, 2, {a: 4}] + s: [string]: [string]: a + s: [string]: {s: string} +} diff --git a/cue/format/testdata/imports.golden b/cue/format/testdata/imports.golden deleted file mode 100644 index 5d670859cda..00000000000 --- a/cue/format/testdata/imports.golden +++ /dev/null @@ -1,26 +0,0 @@ -package foo - -import ( - "cuelang.org/go/foo" - "cuelang.org/go/bar" - "time" -) - -import ( - time1 "time" - - // comment f2 - f2 "cuelang.org/go/foo" - f1 "cuelang.org/go/foo" -) - -import ( - time2 "time" - - same "cuelang.org/go/foo" // comment 1 - same2 "cuelang.org/go/foo" // comment 2 -) - -a: time.time -b: foo.foo -c: bar.Bar diff --git a/cue/format/testdata/imports.input b/cue/format/testdata/imports.input deleted file mode 100644 index a6177961103..00000000000 --- a/cue/format/testdata/imports.input +++ /dev/null @@ -1,27 +0,0 @@ -package foo - -import ( - "cuelang.org/go/foo" - "cuelang.org/go/bar" - "time" -) - -import ( - time1 "time" - - // comment f2 - f2 "cuelang.org/go/foo" - f1 "cuelang.org/go/foo" -) - -import ( - time2 "time" - - same "cuelang.org/go/foo" // comment 1 - same2 "cuelang.org/go/foo" // comment 2 -) - - -a: time.time -b: foo.foo -c: bar.Bar \ No newline at end of file diff --git a/cue/format/testdata/imports.txtar b/cue/format/testdata/imports.txtar new file mode 100644 index 00000000000..e26dd148b08 --- /dev/null +++ b/cue/format/testdata/imports.txtar @@ -0,0 +1,57 @@ +sort-imports + +-- imports.input -- +package foo + +import ( + "cuelang.org/go/foo" + "cuelang.org/go/bar" + "time" +) + +import ( + time1 "time" + + // comment f2 + f2 "cuelang.org/go/foo" + f1 "cuelang.org/go/foo" +) + +import ( + time2 "time" + + same "cuelang.org/go/foo" // comment 1 + same2 "cuelang.org/go/foo" // comment 2 +) + + +a: time.time +b: foo.foo +c: bar.Bar +-- imports.golden -- +package foo + +import ( + "cuelang.org/go/foo" + "cuelang.org/go/bar" + "time" +) + +import ( + time1 "time" + + // comment f2 + f2 "cuelang.org/go/foo" + f1 "cuelang.org/go/foo" +) + +import ( + time2 "time" + + same "cuelang.org/go/foo" // comment 1 + same2 "cuelang.org/go/foo" // comment 2 +) + +a: time.time +b: foo.foo +c: bar.Bar diff --git a/cue/format/testdata/simplify.golden b/cue/format/testdata/simplify.golden deleted file mode 100644 index c49c832b184..00000000000 --- a/cue/format/testdata/simplify.golden +++ /dev/null @@ -1,84 +0,0 @@ -import "time" - -foo: bar: "str" - -a: B: 42 - -"a.b": "foo-": cc_dd: x - -@attr(3) - -a: b: c: 3 - -// references to bar are all shadowed and this can be safely turned into -// an identifier. -bar: "str" - -// These references would be directly referred to if turned into an identifier. -// The quotes should therefore not be removed. -"baz1": 3 -"baz2": 3 -"baz3": 3 // TODO: could be simplified. - -// These string labels may be turned into an identifier. -qux: 4 -quux: 5 - -// TODO(legacy): Don't simplify "hidden" fields for now. -"_foo": 3 - -// Issue #294 -"\("x")": "x" - -(x): "foo" -(x)?: "foo" -(x)!: "foo" - -a: { - foo: 2 - ... -} - -"#A": dontSimplify - -x: { - @tag0(foo) - r1: baz1 - bar: r2: bar - r3: bar - E=quux: 3 - - @tag1(bar) - r4: quux - [baz2="str"]: 4 - r5: baz2 - [baz3="bar"]: name: baz3 - Time: time.Time -} - -y: { - a: { - bar: "a-value" - [!="bar"]: {} - } - b: { - x: { - bar: "bar" - baz: "baz" - [!={ - "a": "baz" - }.a & !="bar"]: {} - } - y: { - bar: "a-value" - ({ - "a": "bar" - }.a): {} - } - } -} - -{ - foo: {} - bar: "foo": foo // removing the quotes would cause a cyclic reference -} diff --git a/cue/format/testdata/simplify.input b/cue/format/testdata/simplify.txtar similarity index 53% rename from cue/format/testdata/simplify.input rename to cue/format/testdata/simplify.txtar index 479e5938079..c7f553b5b57 100644 --- a/cue/format/testdata/simplify.input +++ b/cue/format/testdata/simplify.txtar @@ -1,3 +1,6 @@ +simplify + +-- simplify.input -- import "time" "foo": "bar": "str" @@ -86,3 +89,88 @@ y: { foo: {} bar: "foo": foo // removing the quotes would cause a cyclic reference } +-- simplify.golden -- +import "time" + +foo: bar: "str" + +a: B: 42 + +"a.b": "foo-": cc_dd: x + +@attr(3) + +a: b: c: 3 + +// references to bar are all shadowed and this can be safely turned into +// an identifier. +bar: "str" + +// These references would be directly referred to if turned into an identifier. +// The quotes should therefore not be removed. +"baz1": 3 +"baz2": 3 +"baz3": 3 // TODO: could be simplified. + +// These string labels may be turned into an identifier. +qux: 4 +quux: 5 + +// TODO(legacy): Don't simplify "hidden" fields for now. +"_foo": 3 + +// Issue #294 +"\("x")": "x" + +(x): "foo" +(x)?: "foo" +(x)!: "foo" + +a: { + foo: 2 + ... +} + +"#A": dontSimplify + +x: { + @tag0(foo) + r1: baz1 + bar: r2: bar + r3: bar + E=quux: 3 + + @tag1(bar) + r4: quux + [baz2="str"]: 4 + r5: baz2 + [baz3="bar"]: name: baz3 + Time: time.Time +} + +y: { + a: { + bar: "a-value" + [!="bar"]: {} + } + b: { + x: { + bar: "bar" + baz: "baz" + [!={ + "a": "baz" + }.a & !="bar"]: {} + } + y: { + bar: "a-value" + ({ + "a": "bar" + }.a): {} + } + } +} + +{ + foo: {} + bar: "foo": foo // removing the quotes would cause a cyclic reference +} diff --git a/cue/format/testdata/values.golden b/cue/format/testdata/values.golden deleted file mode 100644 index c8968aa110e..00000000000 --- a/cue/format/testdata/values.golden +++ /dev/null @@ -1,14 +0,0 @@ -a: 0e+1 -a: 0e1 -a: 0e+1 -a: 0e1 -a: 0.3e+1 -a: 0.3e+1 -a: 0.3 -a: 3.0 -a: 3.0T -a: 3.0e100 - -s: """ - x\"\"\" - """ diff --git a/cue/format/testdata/values.input b/cue/format/testdata/values.input deleted file mode 100644 index b657973e674..00000000000 --- a/cue/format/testdata/values.input +++ /dev/null @@ -1,15 +0,0 @@ -a: 0e+1 -a: 0e1 -a: 0E+1 -a: 0E1 -a: .3e+1 -a: .3E+1 -a: .3 -a: 3. -a: 3.T -a: 3.e100 - - -s: """ - x\"\"\" - """ diff --git a/cue/format/testdata/values.txtar b/cue/format/testdata/values.txtar new file mode 100644 index 00000000000..becaa39a718 --- /dev/null +++ b/cue/format/testdata/values.txtar @@ -0,0 +1,32 @@ + +-- values.input -- +a: 0e+1 +a: 0e1 +a: 0E+1 +a: 0E1 +a: .3e+1 +a: .3E+1 +a: .3 +a: 3. +a: 3.T +a: 3.e100 + + +s: """ + x\"\"\" + """ +-- values.golden -- +a: 0e+1 +a: 0e1 +a: 0e+1 +a: 0e1 +a: 0.3e+1 +a: 0.3e+1 +a: 0.3 +a: 3.0 +a: 3.0T +a: 3.0e100 + +s: """ + x\"\"\" + """