Skip to content

Commit

Permalink
encoding/jsonschema: more consistent test data
Browse files Browse the repository at this point in the history
Currently the tests in `encoding/jsonschema` can have any name for the
schema file, and the output file is ignored if there's an error.

In preparation for adding some actual verification tests, change the
convention so the schema file is always named `schema.json` or
`schema.yaml` and the output is always placed into `out/decode/extract`,
so there's no chance of confusion as to what the actual test result is.

Signed-off-by: Roger Peppe <[email protected]>
Change-Id: Ie49110a0e27bf0792162791a0da39b7081952f81
Reviewed-on: https://review.gerrithub.io/c/cue-lang/cue/+/1199737
Reviewed-by: Daniel Martí <[email protected]>
TryBot-Result: CUEcueckoo <[email protected]>
Unity-Result: CUE porcuepine <[email protected]>
  • Loading branch information
rogpeppe authored and mvdan committed Aug 21, 2024
1 parent 43d722b commit cb1dc6b
Show file tree
Hide file tree
Showing 32 changed files with 124 additions and 98 deletions.
87 changes: 54 additions & 33 deletions encoding/jsonschema/decode_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,10 @@ package jsonschema_test
import (
"bytes"
"fmt"
"io/fs"
"net/url"
"path"
"strings"
"testing"

"github.com/go-quicktest/qt"
Expand Down Expand Up @@ -62,52 +64,71 @@ func TestDecode(t *testing.T) {
cfg.Strict = t.HasTag("strict")

ctx := t.Context()
var v cue.Value

for _, f := range t.Archive.Files {
switch path.Ext(f.Name) {
case ".json":
expr, err := json.Extract(f.Name, f.Data)
if err != nil {
t.Fatal(err)
}
v = ctx.BuildExpr(expr)
case ".yaml":
file, err := yaml.Extract(f.Name, f.Data)
if err != nil {
t.Fatal(err)
}
v = ctx.BuildFile(file)
}

fsys, err := txtar.FS(t.Archive)
if err != nil {
t.Fatal(err)
}
v, err := readSchema(ctx, fsys)
if err != nil {
t.Fatal(err)
}
if err := v.Err(); err != nil {
t.Fatal(err)
}

w := t.Writer("extract")
expr, err := jsonschema.Extract(v, cfg)
if err != nil {
got := []byte(errors.Details(err, nil))
got = append(bytes.TrimSpace(got), '\n')
t.Writer("err").Write(got)
got := "ERROR:\n" + errors.Details(err, nil)
w.Write([]byte(strings.TrimSpace(got) + "\n"))
return
}
if expr == nil {
t.Fatal("no expression was extracted")
}

if expr != nil {
b, err := format.Node(expr, format.Simplify())
if err != nil {
b, err := format.Node(expr, format.Simplify())
if err != nil {
t.Fatal(errors.Details(err, nil))
}
if !t.HasTag("noverify") {
// Verify the generated CUE.
v := ctx.CompileBytes(b, cue.Filename("generated.cue"))
if err := v.Err(); err != nil {
t.Fatal(errors.Details(err, nil))
}
// TODO run some instance verification tests.
}

// verify the generated CUE.
if !t.HasTag("noverify") {
v := ctx.CompileBytes(b, cue.Filename("generated.cue"))
if err := v.Err(); err != nil {
t.Fatal(errors.Details(err, nil))
}
}
b = append(bytes.TrimSpace(b), '\n')
w.Write(b)

b = append(bytes.TrimSpace(b), '\n')
t.Writer("cue").Write(b)
}
})
}

func readSchema(ctx *cue.Context, fsys fs.FS) (cue.Value, error) {
jsonData, jsonErr := fs.ReadFile(fsys, "schema.json")
yamlData, yamlErr := fs.ReadFile(fsys, "schema.yaml")
switch {
case jsonErr == nil && yamlErr == nil:
return cue.Value{}, fmt.Errorf("cannot define both schema.json and schema.yaml")
case jsonErr == nil:
expr, err := json.Extract("schema.json", jsonData)
if err != nil {
return cue.Value{}, err
}
return ctx.BuildExpr(expr), nil
case yamlErr == nil:
file, err := yaml.Extract("schema.yaml", yamlData)
if err != nil {
return cue.Value{}, err
}
return ctx.BuildFile(file), nil
}
return cue.Value{}, fmt.Errorf("no schema.yaml or schema.json file found for test")
}

func TestMapURL(t *testing.T) {
v := cuecontext.New().CompileString(`
type: "object"
Expand Down
4 changes: 2 additions & 2 deletions encoding/jsonschema/testdata/basic.txtar
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
-- basic.json --
-- schema.json --
{
"$schema": "http://json-schema.org/draft-07/schema#",

Expand Down Expand Up @@ -37,7 +37,7 @@
}
}

-- out/decode/cue --
-- out/decode/extract --
import "strings"

// Main schema
Expand Down
4 changes: 2 additions & 2 deletions encoding/jsonschema/testdata/boolschema.txtar
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
-- type.json --
-- schema.json --
{
"anyOf": [
true,
false
]
}

-- out/decode/cue --
-- out/decode/extract --
_ | _|_
7 changes: 4 additions & 3 deletions encoding/jsonschema/testdata/boolschema_oldversion.txtar
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
-- type.json --
-- schema.json --
{
"$schema": "http://json-schema.org/draft-04/schema#",
"anyOf": [true]
}

-- out/decode/err --
-- out/decode/extract --
ERROR:
boolean schemas not supported in http://json-schema.org/draft-04/schema#:
type.json:3:12
schema.json:3:12
4 changes: 2 additions & 2 deletions encoding/jsonschema/testdata/boolschemaembed.txtar
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
-- type.json --
-- schema.json --
{
"$defs": {
"foo": true
},
"$ref": "#/$defs/foo",
"type": "string"
}
-- out/decode/cue --
-- out/decode/extract --
#foo & string

#foo: _
4 changes: 2 additions & 2 deletions encoding/jsonschema/testdata/constarray.txtar
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
-- type.json --
-- schema.json --
{
"type": "array",
"const": [
1
]
}

-- out/decode/cue --
-- out/decode/extract --
[1]
4 changes: 2 additions & 2 deletions encoding/jsonschema/testdata/constobj.txtar
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@

-- type.json --
-- schema.json --
{
"type": "object",
"const": {"a": 1}
}

-- out/decode/cue --
-- out/decode/extract --
a: 1
...
4 changes: 2 additions & 2 deletions encoding/jsonschema/testdata/def.txtar
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// This test tests the conversion and ordering of definitions.

-- definition.json --
-- schema.json --
{
"$schema": "http://json-schema.org/draft-07/schema#",

Expand Down Expand Up @@ -38,7 +38,7 @@
}
}

-- out/decode/cue --
-- out/decode/extract --
@jsonschema(schema="http://json-schema.org/draft-07/schema#")
@jsonschema(id="http://cuelang.org/go/encoding/openapi/testdata/order.json")
person?: #["per-son"]
Expand Down
4 changes: 2 additions & 2 deletions encoding/jsonschema/testdata/emptyobj.txtar
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Objects without properties should convert correctly.
//
// Issue #734
-- github-workflow.json --
-- schema.json --
{
"$schema": "http://json-schema.org/draft-07/schema#",
"definitions": {
Expand All @@ -22,7 +22,7 @@
}
}
}
-- out/decode/cue --
-- out/decode/extract --
@jsonschema(schema="http://json-schema.org/draft-07/schema#")
_

Expand Down
4 changes: 2 additions & 2 deletions encoding/jsonschema/testdata/emptyobjinanyof.txtar
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
-- emptyanyof.json --
-- schema.json --
{
"$defs": {
"shell": {
Expand All @@ -20,7 +20,7 @@
}
}

-- out/decode/cue --
-- out/decode/extract --
_

#shell: string | ("bash" | "sh" | "cmd" | "powershell")
7 changes: 4 additions & 3 deletions encoding/jsonschema/testdata/enumexcluded.txtar
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
-- type.json --
-- schema.json --
{
"type": "object",
"enum": ["x"]
}
-- out/decode/err --
-- out/decode/extract --
ERROR:
constraints are not possible to satisfy:
type.json:1:1
schema.json:1:1
2 changes: 1 addition & 1 deletion encoding/jsonschema/testdata/id_in_oneOf.txtar
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
]
}

-- out/decode/cue --
-- out/decode/extract --
@jsonschema(schema="http://json-schema.org/draft-07/schema#")
@jsonschema(id="https://test.example/foo")
{
Expand Down
2 changes: 1 addition & 1 deletion encoding/jsonschema/testdata/id_scalar.txtar
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"type": "string"
}

-- out/decode/cue --
-- out/decode/extract --
@jsonschema(schema="http://json-schema.org/draft-07/schema#")
@jsonschema(id="http://cuelang.org/go/encoding/openapi/testdata/id_scalar.json")
string
9 changes: 5 additions & 4 deletions encoding/jsonschema/testdata/impossibleallof.txtar
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
-- type.json --
-- schema.json --
{
"allOf": [
{ "type": "object" },
{ "type": "string" }
]
}
-- out/decode/err --
-- out/decode/extract --
ERROR:
constraints are not possible to satisfy:
type.json:1:1
schema.json:1:1
constraints are not possible to satisfy:
type.json:4:9
schema.json:4:9
4 changes: 2 additions & 2 deletions encoding/jsonschema/testdata/issue3176.txtar
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
-- definition.json --
-- schema.json --
{
"oneOf": [
{
Expand All @@ -15,7 +15,7 @@
}
}
}
-- out/decode/cue --
-- out/decode/extract --
import "strings"

({
Expand Down
4 changes: 2 additions & 2 deletions encoding/jsonschema/testdata/issue3351.txtar
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
-- json-e.json --
-- schema.json --
{
"$schema": "https://json-schema.org/draft/2019-09/schema",
"$id": "https://www.sourcemeta.com/schemas/vendor/[email protected]",
Expand Down Expand Up @@ -62,7 +62,7 @@
"title": "JSON-e templates",
"type": "object"
}
-- out/decode/cue --
-- out/decode/extract --
_schema
_schema: {
// JSON-e templates
Expand Down
4 changes: 2 additions & 2 deletions encoding/jsonschema/testdata/list.txtar
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
-- list.yaml --
-- schema.yaml --
type: object

properties:
Expand Down Expand Up @@ -35,7 +35,7 @@ properties:

additionalProperties: false

-- out/decode/cue --
-- out/decode/extract --
import "list"

foo?: [...string]
Expand Down
7 changes: 4 additions & 3 deletions encoding/jsonschema/testdata/newid_oldversion.txtar
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
#strict
-- definition.json --
-- schema.json --
{
"$schema": "http://json-schema.org/draft-04/schema#",
"$id": "http://example.test",
"type": "string"
}
-- out/decode/err --
-- out/decode/extract --
ERROR:
constraint "$id" is not supported in JSON schema version http://json-schema.org/draft-04/schema#:
definition.json:3:3
schema.json:3:3
4 changes: 2 additions & 2 deletions encoding/jsonschema/testdata/num.txtar
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
-- type.json --
-- schema.json --
{
"type": "object",

Expand Down Expand Up @@ -37,7 +37,7 @@
},
"additionalProperties": false
}
-- out/decode/cue --
-- out/decode/extract --
import (
"strings"
"math"
Expand Down
4 changes: 2 additions & 2 deletions encoding/jsonschema/testdata/numericenum.txtar
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@

# TODO fix this so it works.

-- type.json --
-- schema.json --
{
"type": "number",
"enum": [ 1, 2, 3]
}
-- out/decode/cue --
-- out/decode/extract --
1 | 2 | 3
Loading

0 comments on commit cb1dc6b

Please sign in to comment.