this repo has no description
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

encoding/jsonschema: more consistent test data

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 <rogpeppe@gmail.com>
Change-Id: Ie49110a0e27bf0792162791a0da39b7081952f81
Reviewed-on: https://review.gerrithub.io/c/cue-lang/cue/+/1199737
Reviewed-by: Daniel Martí <mvdan@mvdan.cc>
TryBot-Result: CUEcueckoo <cueckoo@cuelang.org>
Unity-Result: CUE porcuepine <cue.porcuepine@gmail.com>

authored by

Roger Peppe and committed by
Daniel Martí
cb1dc6ba 43d722b3

+123 -97
+53 -32
encoding/jsonschema/decode_test.go
··· 17 17 import ( 18 18 "bytes" 19 19 "fmt" 20 + "io/fs" 20 21 "net/url" 21 22 "path" 23 + "strings" 22 24 "testing" 23 25 24 26 "github.com/go-quicktest/qt" ··· 62 64 cfg.Strict = t.HasTag("strict") 63 65 64 66 ctx := t.Context() 65 - var v cue.Value 66 67 67 - for _, f := range t.Archive.Files { 68 - switch path.Ext(f.Name) { 69 - case ".json": 70 - expr, err := json.Extract(f.Name, f.Data) 71 - if err != nil { 72 - t.Fatal(err) 73 - } 74 - v = ctx.BuildExpr(expr) 75 - case ".yaml": 76 - file, err := yaml.Extract(f.Name, f.Data) 77 - if err != nil { 78 - t.Fatal(err) 79 - } 80 - v = ctx.BuildFile(file) 81 - } 68 + fsys, err := txtar.FS(t.Archive) 69 + if err != nil { 70 + t.Fatal(err) 71 + } 72 + v, err := readSchema(ctx, fsys) 73 + if err != nil { 74 + t.Fatal(err) 75 + } 76 + if err := v.Err(); err != nil { 77 + t.Fatal(err) 82 78 } 83 79 80 + w := t.Writer("extract") 84 81 expr, err := jsonschema.Extract(v, cfg) 85 82 if err != nil { 86 - got := []byte(errors.Details(err, nil)) 87 - got = append(bytes.TrimSpace(got), '\n') 88 - t.Writer("err").Write(got) 83 + got := "ERROR:\n" + errors.Details(err, nil) 84 + w.Write([]byte(strings.TrimSpace(got) + "\n")) 85 + return 86 + } 87 + if expr == nil { 88 + t.Fatal("no expression was extracted") 89 89 } 90 90 91 - if expr != nil { 92 - b, err := format.Node(expr, format.Simplify()) 93 - if err != nil { 91 + b, err := format.Node(expr, format.Simplify()) 92 + if err != nil { 93 + t.Fatal(errors.Details(err, nil)) 94 + } 95 + if !t.HasTag("noverify") { 96 + // Verify the generated CUE. 97 + v := ctx.CompileBytes(b, cue.Filename("generated.cue")) 98 + if err := v.Err(); err != nil { 94 99 t.Fatal(errors.Details(err, nil)) 95 100 } 101 + // TODO run some instance verification tests. 102 + } 96 103 97 - // verify the generated CUE. 98 - if !t.HasTag("noverify") { 99 - v := ctx.CompileBytes(b, cue.Filename("generated.cue")) 100 - if err := v.Err(); err != nil { 101 - t.Fatal(errors.Details(err, nil)) 102 - } 103 - } 104 + b = append(bytes.TrimSpace(b), '\n') 105 + w.Write(b) 104 106 105 - b = append(bytes.TrimSpace(b), '\n') 106 - t.Writer("cue").Write(b) 107 - } 108 107 }) 108 + } 109 + 110 + func readSchema(ctx *cue.Context, fsys fs.FS) (cue.Value, error) { 111 + jsonData, jsonErr := fs.ReadFile(fsys, "schema.json") 112 + yamlData, yamlErr := fs.ReadFile(fsys, "schema.yaml") 113 + switch { 114 + case jsonErr == nil && yamlErr == nil: 115 + return cue.Value{}, fmt.Errorf("cannot define both schema.json and schema.yaml") 116 + case jsonErr == nil: 117 + expr, err := json.Extract("schema.json", jsonData) 118 + if err != nil { 119 + return cue.Value{}, err 120 + } 121 + return ctx.BuildExpr(expr), nil 122 + case yamlErr == nil: 123 + file, err := yaml.Extract("schema.yaml", yamlData) 124 + if err != nil { 125 + return cue.Value{}, err 126 + } 127 + return ctx.BuildFile(file), nil 128 + } 129 + return cue.Value{}, fmt.Errorf("no schema.yaml or schema.json file found for test") 109 130 } 110 131 111 132 func TestMapURL(t *testing.T) {
+2 -2
encoding/jsonschema/testdata/basic.txtar
··· 1 - -- basic.json -- 1 + -- schema.json -- 2 2 { 3 3 "$schema": "http://json-schema.org/draft-07/schema#", 4 4 ··· 37 37 } 38 38 } 39 39 40 - -- out/decode/cue -- 40 + -- out/decode/extract -- 41 41 import "strings" 42 42 43 43 // Main schema
+2 -2
encoding/jsonschema/testdata/boolschema.txtar
··· 1 - -- type.json -- 1 + -- schema.json -- 2 2 { 3 3 "anyOf": [ 4 4 true, ··· 6 6 ] 7 7 } 8 8 9 - -- out/decode/cue -- 9 + -- out/decode/extract -- 10 10 _ | _|_
+4 -3
encoding/jsonschema/testdata/boolschema_oldversion.txtar
··· 1 - -- type.json -- 1 + -- schema.json -- 2 2 { 3 3 "$schema": "http://json-schema.org/draft-04/schema#", 4 4 "anyOf": [true] 5 5 } 6 6 7 - -- out/decode/err -- 7 + -- out/decode/extract -- 8 + ERROR: 8 9 boolean schemas not supported in http://json-schema.org/draft-04/schema#: 9 - type.json:3:12 10 + schema.json:3:12
+2 -2
encoding/jsonschema/testdata/boolschemaembed.txtar
··· 1 - -- type.json -- 1 + -- schema.json -- 2 2 { 3 3 "$defs": { 4 4 "foo": true ··· 6 6 "$ref": "#/$defs/foo", 7 7 "type": "string" 8 8 } 9 - -- out/decode/cue -- 9 + -- out/decode/extract -- 10 10 #foo & string 11 11 12 12 #foo: _
+2 -2
encoding/jsonschema/testdata/constarray.txtar
··· 1 - -- type.json -- 1 + -- schema.json -- 2 2 { 3 3 "type": "array", 4 4 "const": [ ··· 6 6 ] 7 7 } 8 8 9 - -- out/decode/cue -- 9 + -- out/decode/extract -- 10 10 [1]
+2 -2
encoding/jsonschema/testdata/constobj.txtar
··· 1 1 2 - -- type.json -- 2 + -- schema.json -- 3 3 { 4 4 "type": "object", 5 5 "const": {"a": 1} 6 6 } 7 7 8 - -- out/decode/cue -- 8 + -- out/decode/extract -- 9 9 a: 1 10 10 ...
+2 -2
encoding/jsonschema/testdata/def.txtar
··· 1 1 // This test tests the conversion and ordering of definitions. 2 2 3 - -- definition.json -- 3 + -- schema.json -- 4 4 { 5 5 "$schema": "http://json-schema.org/draft-07/schema#", 6 6 ··· 38 38 } 39 39 } 40 40 41 - -- out/decode/cue -- 41 + -- out/decode/extract -- 42 42 @jsonschema(schema="http://json-schema.org/draft-07/schema#") 43 43 @jsonschema(id="http://cuelang.org/go/encoding/openapi/testdata/order.json") 44 44 person?: #["per-son"]
+2 -2
encoding/jsonschema/testdata/emptyobj.txtar
··· 1 1 // Objects without properties should convert correctly. 2 2 // 3 3 // Issue #734 4 - -- github-workflow.json -- 4 + -- schema.json -- 5 5 { 6 6 "$schema": "http://json-schema.org/draft-07/schema#", 7 7 "definitions": { ··· 22 22 } 23 23 } 24 24 } 25 - -- out/decode/cue -- 25 + -- out/decode/extract -- 26 26 @jsonschema(schema="http://json-schema.org/draft-07/schema#") 27 27 _ 28 28
+2 -2
encoding/jsonschema/testdata/emptyobjinanyof.txtar
··· 1 - -- emptyanyof.json -- 1 + -- schema.json -- 2 2 { 3 3 "$defs": { 4 4 "shell": { ··· 20 20 } 21 21 } 22 22 23 - -- out/decode/cue -- 23 + -- out/decode/extract -- 24 24 _ 25 25 26 26 #shell: string | ("bash" | "sh" | "cmd" | "powershell")
+4 -3
encoding/jsonschema/testdata/enumexcluded.txtar
··· 1 - -- type.json -- 1 + -- schema.json -- 2 2 { 3 3 "type": "object", 4 4 "enum": ["x"] 5 5 } 6 - -- out/decode/err -- 6 + -- out/decode/extract -- 7 + ERROR: 7 8 constraints are not possible to satisfy: 8 - type.json:1:1 9 + schema.json:1:1
+1 -1
encoding/jsonschema/testdata/id_in_oneOf.txtar
··· 16 16 ] 17 17 } 18 18 19 - -- out/decode/cue -- 19 + -- out/decode/extract -- 20 20 @jsonschema(schema="http://json-schema.org/draft-07/schema#") 21 21 @jsonschema(id="https://test.example/foo") 22 22 {
+1 -1
encoding/jsonschema/testdata/id_scalar.txtar
··· 6 6 "type": "string" 7 7 } 8 8 9 - -- out/decode/cue -- 9 + -- out/decode/extract -- 10 10 @jsonschema(schema="http://json-schema.org/draft-07/schema#") 11 11 @jsonschema(id="http://cuelang.org/go/encoding/openapi/testdata/id_scalar.json") 12 12 string
+5 -4
encoding/jsonschema/testdata/impossibleallof.txtar
··· 1 - -- type.json -- 1 + -- schema.json -- 2 2 { 3 3 "allOf": [ 4 4 { "type": "object" }, 5 5 { "type": "string" } 6 6 ] 7 7 } 8 - -- out/decode/err -- 8 + -- out/decode/extract -- 9 + ERROR: 9 10 constraints are not possible to satisfy: 10 - type.json:1:1 11 + schema.json:1:1 11 12 constraints are not possible to satisfy: 12 - type.json:4:9 13 + schema.json:4:9
+2 -2
encoding/jsonschema/testdata/issue3176.txtar
··· 1 - -- definition.json -- 1 + -- schema.json -- 2 2 { 3 3 "oneOf": [ 4 4 { ··· 15 15 } 16 16 } 17 17 } 18 - -- out/decode/cue -- 18 + -- out/decode/extract -- 19 19 import "strings" 20 20 21 21 ({
+2 -2
encoding/jsonschema/testdata/issue3351.txtar
··· 1 - -- json-e.json -- 1 + -- schema.json -- 2 2 { 3 3 "$schema": "https://json-schema.org/draft/2019-09/schema", 4 4 "$id": "https://www.sourcemeta.com/schemas/vendor/json-e@1.json", ··· 62 62 "title": "JSON-e templates", 63 63 "type": "object" 64 64 } 65 - -- out/decode/cue -- 65 + -- out/decode/extract -- 66 66 _schema 67 67 _schema: { 68 68 // JSON-e templates
+2 -2
encoding/jsonschema/testdata/list.txtar
··· 1 - -- list.yaml -- 1 + -- schema.yaml -- 2 2 type: object 3 3 4 4 properties: ··· 35 35 36 36 additionalProperties: false 37 37 38 - -- out/decode/cue -- 38 + -- out/decode/extract -- 39 39 import "list" 40 40 41 41 foo?: [...string]
+4 -3
encoding/jsonschema/testdata/newid_oldversion.txtar
··· 1 1 #strict 2 - -- definition.json -- 2 + -- schema.json -- 3 3 { 4 4 "$schema": "http://json-schema.org/draft-04/schema#", 5 5 "$id": "http://example.test", 6 6 "type": "string" 7 7 } 8 - -- out/decode/err -- 8 + -- out/decode/extract -- 9 + ERROR: 9 10 constraint "$id" is not supported in JSON schema version http://json-schema.org/draft-04/schema#: 10 - definition.json:3:3 11 + schema.json:3:3
+2 -2
encoding/jsonschema/testdata/num.txtar
··· 1 - -- type.json -- 1 + -- schema.json -- 2 2 { 3 3 "type": "object", 4 4 ··· 37 37 }, 38 38 "additionalProperties": false 39 39 } 40 - -- out/decode/cue -- 40 + -- out/decode/extract -- 41 41 import ( 42 42 "strings" 43 43 "math"
+2 -2
encoding/jsonschema/testdata/numericenum.txtar
··· 3 3 4 4 # TODO fix this so it works. 5 5 6 - -- type.json -- 6 + -- schema.json -- 7 7 { 8 8 "type": "number", 9 9 "enum": [ 1, 2, 3] 10 10 } 11 - -- out/decode/cue -- 11 + -- out/decode/extract -- 12 12 1 | 2 | 3
+2 -2
encoding/jsonschema/testdata/object.txtar
··· 1 - -- type.json -- 1 + -- schema.json -- 2 2 { 3 3 "type": "object", 4 4 "title": "Main schema", ··· 67 67 "additionalProperties": false 68 68 } 69 69 70 - -- out/decode/cue -- 70 + -- out/decode/extract -- 71 71 import "struct" 72 72 73 73 // Main schema
+4 -3
encoding/jsonschema/testdata/oldid_newversion.txtar
··· 1 1 #strict 2 - -- definition.json -- 2 + -- schema.json -- 3 3 { 4 4 "$schema": "http://json-schema.org/draft-07/schema#", 5 5 "id": "http://example.test", 6 6 "type": "string" 7 7 } 8 - -- out/decode/err -- 8 + -- out/decode/extract -- 9 + ERROR: 9 10 constraint "id" is not supported in JSON schema version http://json-schema.org/draft-07/schema#: 10 - definition.json:3:3 11 + schema.json:3:3
+2 -2
encoding/jsonschema/testdata/openapi.txtar
··· 1 1 #openapi 2 2 3 - -- type.yaml -- 3 + -- schema.yaml -- 4 4 components: 5 5 schemas: 6 6 User: ··· 18 18 description: "The number to dial." 19 19 type: string 20 20 21 - -- out/decode/cue -- 21 + -- out/decode/extract -- 22 22 // A User uses something. 23 23 #User: { 24 24 id?: int
+2 -2
encoding/jsonschema/testdata/ref.txtar
··· 2 2 3 3 #noverify 4 4 5 - -- definition.json -- 5 + -- schema.json -- 6 6 { 7 7 "$schema": "http://json-schema.org/draft-07/schema#", 8 8 ··· 63 63 } 64 64 } 65 65 66 - -- out/decode/cue -- 66 + -- out/decode/extract -- 67 67 import ( 68 68 "acme.com/external.json:external" 69 69 "acme.com/external-foo.json:schema"
+2 -2
encoding/jsonschema/testdata/refroot.txtar
··· 1 1 // This test tests the conversion and ordering of $defs. 2 2 3 - -- definition.json -- 3 + -- schema.json -- 4 4 { 5 5 "$schema": "http://json-schema.org/draft-07/schema#", 6 6 ··· 12 12 } 13 13 } 14 14 15 - -- out/decode/cue -- 15 + -- out/decode/extract -- 16 16 _schema 17 17 _schema: { 18 18 @jsonschema(schema="http://json-schema.org/draft-07/schema#")
+2 -2
encoding/jsonschema/testdata/refroot2.txtar
··· 1 1 // This test tests the conversion and ordering of $defs. 2 2 3 - -- definition.json -- 3 + -- schema.json -- 4 4 { 5 5 "$schema": "http://json-schema.org/draft-07/schema#", 6 6 ··· 10 10 } 11 11 } 12 12 13 - -- out/decode/cue -- 13 + -- out/decode/extract -- 14 14 _schema 15 15 _schema: { 16 16 @jsonschema(schema="http://json-schema.org/draft-07/schema#")
+1 -1
encoding/jsonschema/testdata/required.txtar
··· 18 18 } 19 19 } 20 20 21 - -- out/decode/cue -- 21 + -- out/decode/extract -- 22 22 // example jsonschema 23 23 @jsonschema(schema="http://json-schema.org/draft-04/schema#") 24 24 @jsonschema(id="https://example.test/example")
+2 -2
encoding/jsonschema/testdata/type.txtar
··· 1 - -- type.json -- 1 + -- schema.json -- 2 2 { 3 3 "type": "object", 4 4 "title": "Main schema", ··· 29 29 "additionalProperties": false 30 30 } 31 31 32 - -- out/decode/cue -- 32 + -- out/decode/extract -- 33 33 // Main schema 34 34 intString?: null | bool | int | string | [...] 35 35 object?: {
+2 -2
encoding/jsonschema/testdata/typedis.txtar
··· 1 - -- type.json -- 1 + -- schema.json -- 2 2 { 3 3 "type": "object", 4 4 "title": "Main schema", ··· 36 36 } 37 37 } 38 38 } 39 - -- out/decode/cue -- 39 + -- out/decode/extract -- 40 40 // Main schema 41 41 intOrString1?: int | string 42 42 intOrString2?: int | string
+2 -2
encoding/jsonschema/testdata/typeexcluded.txtar
··· 1 - -- type.json -- 1 + -- schema.json -- 2 2 { 3 3 "type": "object", 4 4 "properties": { ··· 103 103 } 104 104 } 105 105 } 106 - -- out/decode/cue -- 106 + -- out/decode/extract -- 107 107 import ( 108 108 "list" 109 109 "strings"
+2 -2
encoding/jsonschema/testdata/unsupported.txtar
··· 1 - -- unsupported.json -- 1 + -- schema.json -- 2 2 { 3 3 "$schema": "http://json-schema.org/draft-07/schema#", 4 4 "definitions": { ··· 34 34 } 35 35 36 36 37 - -- out/decode/cue -- 37 + -- out/decode/extract -- 38 38 @jsonschema(schema="http://json-schema.org/draft-07/schema#") 39 39 _ 40 40
+2 -2
encoding/jsonschema/testdata/used.txtar
··· 1 - -- used.json -- 1 + -- schema.json -- 2 2 { 3 3 "$defs": { 4 4 "enum": { ··· 40 40 } 41 41 } 42 42 } 43 - -- out/decode/cue -- 43 + -- out/decode/extract -- 44 44 _ 45 45 46 46 #enum: "a" | "b" | "c"