Skip to content

Commit ba1084a

Browse files
committed
encoding/openapi: convert remaining cue->json tests to txtar
These two tests are slightly more awkward because they require linking up with custom functions to influence the encoding. Consequently it is impossible to have them all of each test in a single file, but it's not worse than it was before, and it's preferable to completely remove the old test code, and ensure we use the eval matrix for all these tests. Signed-off-by: Matthew Sackman <[email protected]> Change-Id: Ifdda60d693939dd0ef993ab8a3ea4390b158d8e9 Reviewed-on: https://review.gerrithub.io/c/cue-lang/cue/+/1209363 Reviewed-by: Roger Peppe <[email protected]> Unity-Result: CUE porcuepine <[email protected]> TryBot-Result: CUEcueckoo <[email protected]>
1 parent 374c59e commit ba1084a

File tree

5 files changed

+154
-192
lines changed

5 files changed

+154
-192
lines changed

encoding/openapi/openapi_test.go

+42-92
Original file line numberDiff line numberDiff line change
@@ -17,20 +17,14 @@ package openapi_test
1717
import (
1818
"bytes"
1919
"encoding/json"
20-
"os"
21-
"path/filepath"
2220
"strings"
2321
"testing"
2422

25-
"github.com/google/go-cmp/cmp"
26-
2723
"cuelang.org/go/cue"
2824
"cuelang.org/go/cue/cuecontext"
2925
"cuelang.org/go/cue/errors"
30-
"cuelang.org/go/cue/load"
3126
"cuelang.org/go/encoding/openapi"
3227
"cuelang.org/go/internal/cuetdtest"
33-
"cuelang.org/go/internal/cuetest"
3428
"cuelang.org/go/internal/cuetxtar"
3529
)
3630

@@ -45,6 +39,34 @@ func TestGenerateOpenAPI(t *testing.T) {
4539
Matrix: matrix,
4640
}
4741

42+
nameFuncs := map[string]func(v cue.Value, path cue.Path) string{
43+
"toUpper": func(v cue.Value, path cue.Path) string {
44+
var buf strings.Builder
45+
for i, sel := range path.Selectors() {
46+
if i > 0 {
47+
buf.WriteByte('_')
48+
}
49+
s := sel.String()
50+
s = strings.TrimPrefix(s, "#")
51+
buf.WriteString(strings.ToUpper(s))
52+
}
53+
return buf.String()
54+
},
55+
"excludeExcluded": func(v cue.Value, path cue.Path) string {
56+
switch {
57+
case strings.HasPrefix(path.Selectors()[0].String(), "#Excluded"):
58+
return ""
59+
}
60+
return strings.TrimPrefix(path.String(), "#")
61+
},
62+
}
63+
64+
descFuncs := map[string]func(v cue.Value) string{
65+
"randomish": func(v cue.Value) string {
66+
return "Randomly picked description from a set of size one."
67+
},
68+
}
69+
4870
test.Run(t, func(t *cuetxtar.Test) {
4971
if t.HasTag("skip-" + t.Name()) {
5072
t.Skip()
@@ -68,6 +90,20 @@ func TestGenerateOpenAPI(t *testing.T) {
6890
if version, ok := t.Value("Version"); ok {
6991
config.Version = version
7092
}
93+
if name, ok := t.Value("NameFunc"); ok {
94+
if fun, found := nameFuncs[name]; found {
95+
config.NameFunc = fun
96+
} else {
97+
t.Fatal("Unknown NameFunc", name)
98+
}
99+
}
100+
if desc, ok := t.Value("DescriptionFunc"); ok {
101+
if fun, found := descFuncs[desc]; found {
102+
config.DescriptionFunc = fun
103+
} else {
104+
t.Fatal("Unknown DescriptionFunc", desc)
105+
}
106+
}
71107

72108
expectedErr, shouldErr := t.Value("ExpectError")
73109
b, err := openapi.Gen(v, &config)
@@ -104,92 +140,6 @@ func TestGenerateOpenAPI(t *testing.T) {
104140
})
105141
}
106142

107-
func TestParseDefinitions(t *testing.T) {
108-
info := struct {
109-
Title string `json:"title"`
110-
Version string `json:"version"`
111-
}{"test", "v1"}
112-
testCases := []struct {
113-
in, out string
114-
config *openapi.Config
115-
}{{
116-
in: "oneof.cue",
117-
out: "oneof-funcs.json",
118-
config: &openapi.Config{
119-
Info: info,
120-
NameFunc: func(v cue.Value, path cue.Path) string {
121-
var buf strings.Builder
122-
for i, sel := range path.Selectors() {
123-
if i > 0 {
124-
buf.WriteByte('_')
125-
}
126-
s := sel.String()
127-
s = strings.TrimPrefix(s, "#")
128-
buf.WriteString(strings.ToUpper(s))
129-
}
130-
return buf.String()
131-
},
132-
DescriptionFunc: func(v cue.Value) string {
133-
return "Randomly picked description from a set of size one."
134-
},
135-
},
136-
}, {
137-
in: "refs.cue",
138-
out: "refs.json",
139-
config: &openapi.Config{
140-
Info: info,
141-
NameFunc: func(v cue.Value, path cue.Path) string {
142-
switch {
143-
case strings.HasPrefix(path.Selectors()[0].String(), "#Excluded"):
144-
return ""
145-
}
146-
return strings.TrimPrefix(path.String(), "#")
147-
},
148-
},
149-
}}
150-
for _, tc := range testCases {
151-
t.Run(tc.out, func(t *testing.T) {
152-
filename := filepath.FromSlash(tc.in)
153-
inst := load.Instances([]string{filename}, &load.Config{
154-
Dir: "./testdata",
155-
})[0]
156-
ctx := cuecontext.New()
157-
v := ctx.BuildInstance(inst)
158-
if err := v.Err(); err != nil {
159-
t.Fatal(errors.Details(err, nil))
160-
}
161-
162-
b, err := openapi.Gen(v, tc.config)
163-
if err != nil {
164-
t.Fatal("unexpected error:", errors.Details(err, nil))
165-
}
166-
167-
_, err = openapi.Generate(v, tc.config)
168-
if err != nil {
169-
t.Fatal(err)
170-
}
171-
172-
var out = &bytes.Buffer{}
173-
_ = json.Indent(out, b, "", " ")
174-
175-
wantFile := filepath.Join("testdata", tc.out)
176-
if cuetest.UpdateGoldenFiles {
177-
_ = os.WriteFile(wantFile, out.Bytes(), 0666)
178-
return
179-
}
180-
181-
b, err = os.ReadFile(wantFile)
182-
if err != nil {
183-
t.Fatal(err)
184-
}
185-
186-
if d := cmp.Diff(string(b), out.String()); d != "" {
187-
t.Errorf("files differ:\n%v", d)
188-
}
189-
})
190-
}
191-
}
192-
193143
// TODO: move OpenAPI testing to txtar and allow errors.
194144
func TestIssue1234(t *testing.T) {
195145
val := cuecontext.New().CompileString(`

encoding/openapi/testdata/oneof-funcs.json renamed to encoding/openapi/testdata/oneof-funcs.txtar

+83-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,89 @@
1+
#NameFunc: toUpper
2+
#DescriptionFunc: randomish
3+
#skip-v3
4+
#skip-v3-noshare
5+
-- in.cue --
6+
// OpenAPI title.
7+
8+
$version: "v1alpha1"
9+
10+
#T: {
11+
shared: int
12+
}
13+
#T: {} | {
14+
exact: string
15+
} | {
16+
regex: string
17+
}
18+
#T: {} | {
19+
count: int
20+
} | {
21+
amount: int
22+
}
23+
#T: {
24+
shared2: int
25+
}
26+
27+
// This should be dedupped.
28+
#T: {} | {
29+
count: int
30+
} | {
31+
amount: int
32+
}
33+
34+
#MyInt: int
35+
36+
#Foo: {
37+
include: #T
38+
exclude: [...#T]
39+
count: #MyInt
40+
}
41+
42+
#Incompatible: {
43+
shared: int
44+
} | {
45+
shared: int
46+
extra1: int
47+
} | {
48+
shared: int
49+
extra2: int
50+
}
51+
52+
#WithMap: {
53+
shared: [string]: int
54+
} | {
55+
shared: [string]: int
56+
extra: int
57+
} | {
58+
shared: string // incompatible
59+
extra: int
60+
}
61+
62+
#Embed: {
63+
a?: int
64+
65+
close({}) |
66+
close({b: #T}) |
67+
close({c: int})
68+
69+
#T: {b?: int}
70+
71+
close({}) |
72+
close({d: #T}) |
73+
close({e: int})
74+
75+
// TODO: maybe support builtin to write this as
76+
// oneof({},
77+
// {b: int},
78+
// {c: int})
79+
}
80+
81+
-- out/TestGenerateOpenAPI/out.json --
182
{
283
"openapi": "3.0.0",
384
"info": {
4-
"title": "test",
5-
"version": "v1"
85+
"title": "OpenAPI title.",
86+
"version": "v1alpha1"
687
},
788
"paths": {},
889
"components": {

encoding/openapi/testdata/oneof.cue

-74
This file was deleted.

encoding/openapi/testdata/refs.cue

-21
This file was deleted.

0 commit comments

Comments
 (0)