diff --git a/playground/impl_test.go b/playground/impl_test.go index 6d1fca6c6d..ad7c9e18ae 100644 --- a/playground/impl_test.go +++ b/playground/impl_test.go @@ -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) + } } - } + }) } }