Skip to content

Commit

Permalink
playground: add test cases to show an export bug
Browse files Browse the repository at this point in the history
In particular, exporting this input as CUE works correctly,
but exporting as JSON or YAML fails with an error.

For #2417.

Signed-off-by: Daniel Martí <[email protected]>
Change-Id: I20b3615b5e80d2bc27374661c143f0ce80228057
Dispatch-Trailer: {"type":"trybot","CL":1170813,"patchset":2,"ref":"refs/changes/13/1170813/2","targetBranch":"alpha"}
  • Loading branch information
mvdan authored and cueckoo committed Oct 16, 2023
1 parent 441d9e9 commit cc6e2a9
Showing 1 changed file with 64 additions and 14 deletions.
78 changes: 64 additions & 14 deletions playground/impl_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,26 +30,76 @@ var testTable = []struct {
{inputCUE, functionExport, outputCUE, "", "\n", ""},
{inputCUE, functionExport, outputCUE, "a: b: 5\na: c: 4", "a: {\n\tb: 5\n\tc: 4\n}\n", ""},
{inputCUE, functionExport, outputJSON, "test: 5", "{\n \"test\": 5\n}\n", ""},

// Incomplete values.
{inputCUE, functionExport, outputCUE, "foo: int", "foo: int\n", ""},
{inputCUE, functionExport, outputJSON, "foo: int", "", "foo: incomplete value int"},

// Selecting defaults.
{inputCUE, functionExport, outputCUE, "foo: int | *3", "foo: 3\n", ""},
{inputCUE, functionExport, outputJSON, "foo: int | *3", "{\n \"foo\": 3\n}\n", ""},

// Pattern constraints with different output formats; see https://cuelang.org/issue/2417
{
inputCUE, functionExport, outputCUE,
`
[ID=_]: x: y: ID
"foo": {}
`,
"foo: {\n\tx: {\n\t\ty: \"foo\"\n\t}\n}\n",
"",
},
{
inputCUE, functionExport, outputJSON,
`
[ID=_]: x: y: ID
"foo": {}
`,
"",
"failed to encode: foo.x.y: incomplete value string",
},

// Cases which one could argue should be errors, beyond just being incomplete.
{
inputCUE, functionExport, outputCUE,
`
x: string
y: (x): string
`,
"x: string\ny: {\n\t(x): string\n}\n",
"",
},
{
inputCUE, functionExport, outputCUE,
`
x: x2: "foo"
y: x.missing
`,
"x: {\n\tx2: \"foo\"\n}\ny: x.missing\n",
"",
},
}

func TestHandleCUECompile(t *testing.T) {
for _, tv := range testTable {
desc := fmt.Sprintf("handleCUECompile(%q, %q, %q, %q)", tv.In, tv.Fn, tv.Out, tv.InVal)
out, err := handleCUECompile(tv.In, tv.Fn, tv.Out, tv.InVal)
if tv.Err != "" {
if err != nil {
if err.Error() != tv.Err {
t.Fatalf("%v: expected error string %q; got %q", desc, tv.Err, err)
t.Run("", func(t *testing.T) {
desc := fmt.Sprintf("handleCUECompile(%q, %q, %q, %q)", tv.In, tv.Fn, tv.Out, tv.InVal)
out, err := handleCUECompile(tv.In, tv.Fn, tv.Out, tv.InVal)
if tv.Err != "" {
if err != nil {
if err.Error() != tv.Err {
t.Fatalf("%v: expected error string %q; got %q", desc, tv.Err, err)
}
} else {
t.Fatalf("%v: expected error, did not see one. Output was %q", desc, out)
}
} else {
t.Fatalf("%v: expected error, did not see one. Output was %q", desc, out)
}
} else {
if err != nil {
t.Fatalf("%v: got unexpected error: %v", desc, err)
} else if out != tv.OutVal {
t.Fatalf("%v: expected output %q: got %q", desc, tv.OutVal, out)
if err != nil {
t.Fatalf("%v: got unexpected error: %v", desc, err)
} else if out != tv.OutVal {
t.Fatalf("%v: expected output %q: got %q", desc, tv.OutVal, out)
}
}
}
})
}
}

0 comments on commit cc6e2a9

Please sign in to comment.