Skip to content

Commit 44bc1ab

Browse files
committed
encoding/jsonschema: port txtar tests to cuetxtar
This saves about 60 lines of code to deal with loading the txtar files as well as updating output files following CUE_UPDATE and so on, but most importantly, it unlocks the ability to test with evalv3 too. For #3351. Signed-off-by: Daniel Martí <[email protected]> Change-Id: I3f9a368391c4510d2f5a389da3ac7babb376e036 Reviewed-on: https://review.gerrithub.io/c/cue-lang/cue/+/1198734 Reviewed-by: Roger Peppe <[email protected]> TryBot-Result: CUEcueckoo <[email protected]> Unity-Result: CUE porcuepine <[email protected]>
1 parent 91929bd commit 44bc1ab

17 files changed

+68
-129
lines changed

encoding/jsonschema/decode_test.go

Lines changed: 49 additions & 109 deletions
Original file line numberDiff line numberDiff line change
@@ -12,19 +12,13 @@
1212
// See the License for the specific language governing permissions and
1313
// limitations under the License.
1414

15-
package jsonschema
15+
package jsonschema_test
1616

1717
import (
1818
"bytes"
19-
"io/fs"
20-
"os"
2119
"path"
22-
"path/filepath"
23-
"strings"
2420
"testing"
2521

26-
"github.com/go-quicktest/qt"
27-
"github.com/google/go-cmp/cmp"
2822
"golang.org/x/tools/txtar"
2923

3024
"cuelang.org/go/cue"
@@ -34,9 +28,10 @@ import (
3428
"cuelang.org/go/cue/format"
3529
"cuelang.org/go/cue/token"
3630
"cuelang.org/go/encoding/json"
31+
"cuelang.org/go/encoding/jsonschema"
3732
"cuelang.org/go/encoding/yaml"
3833
"cuelang.org/go/internal/astinternal"
39-
"cuelang.org/go/internal/cuetest"
34+
"cuelang.org/go/internal/cuetxtar"
4035
_ "cuelang.org/go/pkg"
4136
)
4237

@@ -45,121 +40,66 @@ import (
4540
//
4641
// Set CUE_UPDATE=1 to update test files with the corresponding output.
4742
func TestDecode(t *testing.T) {
48-
err := filepath.WalkDir("testdata", func(fullpath string, entry fs.DirEntry, err error) error {
49-
if err != nil {
50-
return err
51-
}
52-
if !strings.HasSuffix(fullpath, ".txtar") {
53-
return nil
54-
}
55-
56-
t.Run(fullpath, func(t *testing.T) {
57-
a, err := txtar.ParseFile(fullpath)
58-
if err != nil {
59-
t.Fatal(err)
43+
test := cuetxtar.TxTarTest{
44+
Root: "./testdata",
45+
Name: "decode",
46+
}
47+
test.Run(t, func(t *cuetxtar.Test) {
48+
cfg := &jsonschema.Config{}
49+
50+
if t.HasTag("openapi") {
51+
cfg.Root = "#/components/schemas/"
52+
cfg.Map = func(p token.Pos, a []string) ([]ast.Label, error) {
53+
// Just for testing: does not validate the path.
54+
return []ast.Label{ast.NewIdent("#" + a[len(a)-1])}, nil
6055
}
56+
}
6157

62-
cfg := &Config{ID: fullpath}
58+
ctx := t.Context()
59+
var v cue.Value
6360

64-
if bytes.Contains(a.Comment, []byte("openapi")) {
65-
cfg.Root = "#/components/schemas/"
66-
cfg.Map = func(p token.Pos, a []string) ([]ast.Label, error) {
67-
// Just for testing: does not validate the path.
68-
return []ast.Label{ast.NewIdent("#" + a[len(a)-1])}, nil
61+
for _, f := range t.Archive.Files {
62+
switch path.Ext(f.Name) {
63+
case ".json":
64+
expr, err := json.Extract(f.Name, f.Data)
65+
if err != nil {
66+
t.Fatal(err)
6967
}
70-
}
71-
72-
ctx := cuecontext.New()
73-
var v cue.Value
74-
var out, errout []byte
75-
outIndex := -1
76-
errIndex := -1
77-
78-
for i, f := range a.Files {
79-
switch path.Ext(f.Name) {
80-
case ".json":
81-
expr, err := json.Extract(f.Name, f.Data)
82-
if err != nil {
83-
t.Fatal(err)
84-
}
85-
v = ctx.BuildExpr(expr)
86-
case ".yaml":
87-
file, err := yaml.Extract(f.Name, f.Data)
88-
if err != nil {
89-
t.Fatal(err)
90-
}
91-
v = ctx.BuildFile(file)
92-
case ".cue":
93-
out = f.Data
94-
outIndex = i
95-
case ".err":
96-
errout = f.Data
97-
errIndex = i
68+
v = ctx.BuildExpr(expr)
69+
case ".yaml":
70+
file, err := yaml.Extract(f.Name, f.Data)
71+
if err != nil {
72+
t.Fatal(err)
9873
}
74+
v = ctx.BuildFile(file)
9975
}
100-
if err != nil {
101-
t.Fatal(err)
102-
}
76+
}
10377

104-
updated := false
78+
expr, err := jsonschema.Extract(v, cfg)
79+
if err != nil {
80+
got := []byte(errors.Details(err, nil))
81+
got = append(bytes.TrimSpace(got), '\n')
82+
t.Writer("err").Write(got)
83+
}
10584

106-
expr, err := Extract(v, cfg)
85+
if expr != nil {
86+
b, err := format.Node(expr, format.Simplify())
10787
if err != nil {
108-
got := []byte(errors.Details(err, nil))
109-
110-
got = bytes.TrimSpace(got)
111-
errout = bytes.TrimSpace(errout)
112-
113-
switch {
114-
case !cmp.Equal(errout, got):
115-
if cuetest.UpdateGoldenFiles {
116-
a.Files[errIndex].Data = got
117-
updated = true
118-
break
119-
}
120-
t.Error(cmp.Diff(string(got), string(errout)))
121-
}
88+
t.Fatal(errors.Details(err, nil))
12289
}
12390

124-
if expr != nil {
125-
b, err := format.Node(expr, format.Simplify())
126-
if err != nil {
91+
// verify the generated CUE.
92+
if !t.HasTag("noverify") {
93+
v := ctx.CompileBytes(b, cue.Filename("generated.cue"))
94+
if err := v.Err(); err != nil {
12795
t.Fatal(errors.Details(err, nil))
12896
}
129-
130-
// verify the generated CUE.
131-
if !bytes.Contains(a.Comment, []byte("#noverify")) {
132-
v := ctx.CompileBytes(b, cue.Filename(fullpath))
133-
if err := v.Err(); err != nil {
134-
t.Fatal(errors.Details(err, nil))
135-
}
136-
}
137-
138-
b = bytes.TrimSpace(b)
139-
out = bytes.TrimSpace(out)
140-
141-
switch {
142-
case !cmp.Equal(b, out):
143-
if cuetest.UpdateGoldenFiles {
144-
updated = true
145-
a.Files[outIndex].Data = b
146-
break
147-
}
148-
t.Error(cmp.Diff(string(out), string(b)))
149-
}
15097
}
15198

152-
if updated {
153-
b := txtar.Format(a)
154-
err = os.WriteFile(fullpath, b, 0644)
155-
if err != nil {
156-
t.Fatal(err)
157-
}
158-
}
159-
})
160-
return nil
99+
b = append(bytes.TrimSpace(b), '\n')
100+
t.Writer("cue").Write(b)
101+
}
161102
})
162-
qt.Assert(t, qt.IsNil(err))
163103
}
164104

165105
func TestX(t *testing.T) {
@@ -190,8 +130,8 @@ func TestX(t *testing.T) {
190130
}
191131
}
192132

193-
cfg := &Config{ID: "test"}
194-
expr, err := Extract(v, cfg)
133+
cfg := &jsonschema.Config{ID: "test"}
134+
expr, err := jsonschema.Extract(v, cfg)
195135
if err != nil {
196136
t.Fatal(err)
197137
}

encoding/jsonschema/testdata/basic.txtar

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@
3737
}
3838
}
3939

40-
-- out.cue --
40+
-- out/decode/cue --
4141
import "strings"
4242

4343
// Main schema

encoding/jsonschema/testdata/def.txtar

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@
3838
}
3939
}
4040

41-
-- out.cue --
41+
-- out/decode/cue --
4242
@jsonschema(schema="http://json-schema.org/draft-07/schema#")
4343
@jsonschema(id="http://cuelang.org/go/encoding/openapi/testdata/order.json")
4444
person?: #["per-son"]

encoding/jsonschema/testdata/emptyanyof.txtar

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
}
2121
}
2222

23-
-- out.cue --
23+
-- out/decode/cue --
2424
_
2525

2626
#shell: (string | ("bash" | "sh" | "cmd" | "powershell")) & string

encoding/jsonschema/testdata/emptyobj.txtar

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,7 @@
2222
}
2323
}
2424
}
25-
-- out.err --
26-
-- out.cue --
25+
-- out/decode/cue --
2726
@jsonschema(schema="http://json-schema.org/draft-07/schema")
2827
_
2928

encoding/jsonschema/testdata/err.txtar

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@
1313
"additionalProperties": false
1414
}
1515

16-
-- out.err --
16+
-- out/decode/err --
1717
constraint not allowed because type string is excluded:
1818
type.json:9:9
19-
-- out.cue --
19+
-- out/decode/cue --
2020
multi?: int & >=2 & <=3

encoding/jsonschema/testdata/list.txtar

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ properties:
3535

3636
additionalProperties: false
3737

38-
-- out.cue --
38+
-- out/decode/cue --
3939
import "list"
4040

4141
foo?: [...string]

encoding/jsonschema/testdata/num.txtar

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@
3838
"additionalProperties": false
3939
}
4040

41-
-- out.cue --
41+
-- out/decode/cue --
4242
import (
4343
"strings"
4444
"math"

encoding/jsonschema/testdata/object.txtar

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@
6767
"additionalProperties": false
6868
}
6969

70-
-- out.cue --
70+
-- out/decode/cue --
7171
import "struct"
7272

7373
// Main schema

encoding/jsonschema/testdata/openapi.txtar

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
openapi
1+
#openapi
22

33
-- type.yaml --
44
components:
@@ -18,7 +18,7 @@ components:
1818
description: "The number to dial."
1919
type: string
2020

21-
-- out.cue --
21+
-- out/decode/cue --
2222
// A User uses something.
2323
#User: {
2424
id?: int

0 commit comments

Comments
 (0)