Skip to content

Commit ce10c91

Browse files
committed
encoding/jsonschema: add debug test for external tests
This mimics the test in external_test, but allows debugging individual cases more easily. Signed-off-by: Marcel van Lohuizen <[email protected]> Change-Id: I673ff53e72d4515f8ac3df1e68a4122d41bbbced Reviewed-on: https://review.gerrithub.io/c/cue-lang/cue/+/1212024 Reviewed-by: Matthew Sackman <[email protected]> Unity-Result: CUE porcuepine <[email protected]> TryBot-Result: CUEcueckoo <[email protected]>
1 parent fc4172f commit ce10c91

File tree

1 file changed

+70
-0
lines changed

1 file changed

+70
-0
lines changed

Diff for: encoding/jsonschema/debug_test.go

+70
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,18 @@ package jsonschema_test
1616

1717
import (
1818
"path"
19+
"strings"
1920
"testing"
2021

2122
"cuelang.org/go/cue"
2223
"cuelang.org/go/cue/cuecontext"
24+
"cuelang.org/go/cue/errors"
25+
"cuelang.org/go/cue/format"
2326
"cuelang.org/go/encoding/json"
2427
"cuelang.org/go/encoding/jsonschema"
2528
"cuelang.org/go/encoding/yaml"
2629
"cuelang.org/go/internal/astinternal"
30+
"github.com/go-quicktest/qt"
2731
"golang.org/x/tools/txtar"
2832
)
2933

@@ -63,3 +67,69 @@ func TestX(t *testing.T) {
6367

6468
t.Fatal(astinternal.DebugStr(expr))
6569
}
70+
71+
// This is a test for debugging external tests.
72+
func TestExtX(t *testing.T) {
73+
t.Skip()
74+
75+
version := cuecontext.EvalDefault
76+
// version = cuecontext.EvalExperiment
77+
ctx := cuecontext.New(cuecontext.EvaluatorVersion(version))
78+
79+
const filename = `tests/draft4/optional/ecmascript-regexp.json`
80+
const jsonSchema = `
81+
{
82+
"type": "object",
83+
"patternProperties": {
84+
"\\wcole": {}
85+
},
86+
"additionalProperties": false
87+
}
88+
`
89+
const testData = `
90+
{
91+
"l'école": "pas de vraie vie"
92+
}
93+
`
94+
95+
jsonAST, err := json.Extract("schema.json", []byte(jsonSchema))
96+
qt.Assert(t, qt.IsNil(err))
97+
jsonValue := ctx.BuildExpr(jsonAST)
98+
qt.Assert(t, qt.IsNil(jsonValue.Err()))
99+
versStr, _, _ := strings.Cut(strings.TrimPrefix(filename, "tests/"), "/")
100+
vers, ok := extVersionToVersion[versStr]
101+
if !ok {
102+
t.Fatalf("unknown JSON schema version for file %q", filename)
103+
}
104+
if vers == jsonschema.VersionUnknown {
105+
t.Skipf("skipping test for unknown schema version %v", versStr)
106+
}
107+
schemaAST, extractErr := jsonschema.Extract(jsonValue, &jsonschema.Config{
108+
StrictFeatures: true,
109+
DefaultVersion: vers,
110+
})
111+
qt.Assert(t, qt.IsNil(extractErr))
112+
b, err := format.Node(schemaAST, format.Simplify())
113+
qt.Assert(t, qt.IsNil(err))
114+
t.Logf("extracted schema: %v", string(b))
115+
schema := string(b)
116+
schemaValue := ctx.CompileBytes(b, cue.Filename("generated.cue"))
117+
if err := schemaValue.Err(); err != nil {
118+
t.Fatalf("cannot compile resulting schema: %v", errors.Details(err, nil))
119+
}
120+
121+
instAST, err := json.Extract("instance.json", []byte(testData))
122+
if err != nil {
123+
t.Fatal(err)
124+
}
125+
126+
qt.Assert(t, qt.IsNil(err), qt.Commentf("test data: %q; details: %v", testData, errors.Details(err, nil)))
127+
128+
instValue := ctx.BuildExpr(instAST)
129+
qt.Assert(t, qt.IsNil(instValue.Err()))
130+
err = instValue.Unify(schemaValue).Validate(cue.Concrete(true))
131+
132+
t.Error(err)
133+
t.Log("VALUE", instValue)
134+
t.Log("SCHEMA", schema)
135+
}

0 commit comments

Comments
 (0)