this repo has no description
0
fork

Configure Feed

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

encoding/jsonschema: support instance tests

Currently the encoding/jsonschema tests assert the CUE output, but
doesn't give full assurance that the generated CUE does actually behave
as expected.

This CL adds that capability, and adds a few such tests just to prove
the concept. Future CLs will add many more.

Also add some docs about the test file conventions for future reference.

Signed-off-by: Roger Peppe <rogpeppe@gmail.com>
Change-Id: I2fd8babf470dcf6162d9f82b5e0ebacf57f77c60
Reviewed-on: https://review.gerrithub.io/c/cue-lang/cue/+/1199743
TryBot-Result: CUEcueckoo <cueckoo@cuelang.org>
Unity-Result: CUE porcuepine <cue.porcuepine@gmail.com>
Reviewed-by: Daniel Martí <mvdan@mvdan.cc>

authored by

Roger Peppe and committed by
Daniel Martí
bbe2f81d cb1dc6ba

+88 -10
+58 -9
encoding/jsonschema/decode_test.go
··· 92 92 if err != nil { 93 93 t.Fatal(errors.Details(err, nil)) 94 94 } 95 - if !t.HasTag("noverify") { 96 - // Verify the generated CUE. 97 - v := ctx.CompileBytes(b, cue.Filename("generated.cue")) 95 + b = append(bytes.TrimSpace(b), '\n') 96 + w.Write(b) 97 + if t.HasTag("noverify") { 98 + return 99 + } 100 + // Verify that the generated CUE compiles. 101 + schemav := ctx.CompileBytes(b, cue.Filename("generated.cue")) 102 + if err := schemav.Err(); err != nil { 103 + t.Fatal(errors.Details(err, nil)) 104 + } 105 + testEntries, err := fs.ReadDir(fsys, "test") 106 + if err != nil { 107 + return 108 + } 109 + for _, e := range testEntries { 110 + file := path.Join("test", e.Name()) 111 + var v cue.Value 112 + base := "" 113 + testData, err := fs.ReadFile(fsys, file) 114 + if err != nil { 115 + t.Fatal(err) 116 + } 117 + switch { 118 + case strings.HasSuffix(file, ".json"): 119 + expr, err := json.Extract(file, testData) 120 + if err != nil { 121 + t.Fatal(err) 122 + } 123 + v = ctx.BuildExpr(expr) 124 + base = strings.TrimSuffix(e.Name(), ".json") 125 + 126 + case strings.HasSuffix(file, ".yaml"): 127 + file, err := yaml.Extract(file, testData) 128 + if err != nil { 129 + t.Fatal(err) 130 + } 131 + v = ctx.BuildFile(file) 132 + base = strings.TrimSuffix(e.Name(), ".yaml") 133 + default: 134 + t.Fatalf("unknown file encoding for test file %v", file) 135 + } 98 136 if err := v.Err(); err != nil { 99 - t.Fatal(errors.Details(err, nil)) 137 + t.Fatalf("error building expression for test %v: %v", file, err) 138 + } 139 + rv := schemav.Unify(v) 140 + if strings.HasPrefix(e.Name(), "err-") { 141 + err := rv.Err() 142 + if err == nil { 143 + t.Fatalf("test %v unexpectedly passes", file) 144 + } 145 + if t.M.IsDefault() { 146 + // The error results of the different evaluators can vary, 147 + // so only test the exact results for the default evaluator. 148 + t.Writer(path.Join("testerr", base)).Write([]byte(errors.Details(err, nil))) 149 + } 150 + } else { 151 + if err := rv.Err(); err != nil { 152 + t.Fatalf("test %v unexpectedly fails", file) 153 + } 100 154 } 101 - // TODO run some instance verification tests. 102 155 } 103 - 104 - b = append(bytes.TrimSpace(b), '\n') 105 - w.Write(b) 106 - 107 156 }) 108 157 } 109 158
+15 -1
encoding/jsonschema/testdata/constarray.txtar
··· 5 5 1 6 6 ] 7 7 } 8 - 9 8 -- out/decode/extract -- 10 9 [1] 10 + -- out/decode/testerr/err-1 -- 11 + conflicting values 1 and [1] (mismatched types int and list): 12 + generated.cue:1:1 13 + test/err-1.json:1:1 14 + -- out/decode/testerr/err-2 -- 15 + 0: conflicting values 2 and 1: 16 + generated.cue:1:1 17 + generated.cue:1:2 18 + test/err-2.json:1:2 19 + -- test/exact.json -- 20 + [1] 21 + -- test/err-1.json -- 22 + 1 23 + -- test/err-2.json -- 24 + [2]
+15
encoding/jsonschema/testdata/list.txtar
··· 43 43 has?: list.Contains(3) 44 44 size?: list.UniqueItems() & list.MaxItems(9) & [_, _, _, ...] 45 45 additional?: [int, int, ...string] 46 + -- out/decode/testerr/err-foo-not-string -- 47 + foo.0: conflicting values true and string (mismatched types bool and string): 48 + generated.cue:3:8 49 + generated.cue:3:11 50 + test/err-foo-not-string.json:2:10 51 + -- test/empty.json -- 52 + {} 53 + -- test/foo-items.json -- 54 + { 55 + "foo": ["x", "y"] 56 + } 57 + -- test/err-foo-not-string.json -- 58 + { 59 + "foo": [true] 60 + }