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.

While here, add more test cases to cover other scenarios,
and use subtests so we can run one test case with -run.

For #2417.

Signed-off-by: Daniel Martí <[email protected]>
Change-Id: I20b3615b5e80d2bc27374661c143f0ce80228057
  • Loading branch information
mvdan committed Oct 19, 2023
1 parent 441d9e9 commit 4ad18b4
Showing 1 changed file with 72 additions and 14 deletions.
86 changes: 72 additions & 14 deletions playground/impl_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,29 +27,87 @@ var testTable = []struct {
OutVal string
Err string
}{
{inputCUE, functionDef, outputCUE, "", "\n", ""},
{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, functionDef, outputCUE, "foo: int", "foo: int\n", ""},
{inputCUE, functionExport, outputCUE, "foo: int", "foo: int\n", ""},
{inputCUE, functionExport, outputJSON, "foo: int", "", "foo: incomplete value int"},

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

// Simplifying values; def does less.
{inputCUE, functionDef, outputCUE, "foo: [1, 2, 3][1]", "foo: [1, 2, 3][1]\n", ""},
{inputCUE, functionExport, outputCUE, "foo: [1, 2, 3][1]", "foo: 2\n", ""},
{inputCUE, functionExport, outputJSON, "foo: [1, 2, 3][1]", "{\n \"foo\": 2\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 4ad18b4

Please sign in to comment.