@@ -17,20 +17,14 @@ package openapi_test
17
17
import (
18
18
"bytes"
19
19
"encoding/json"
20
- "os"
21
- "path/filepath"
22
20
"strings"
23
21
"testing"
24
22
25
- "github.com/google/go-cmp/cmp"
26
-
27
23
"cuelang.org/go/cue"
28
24
"cuelang.org/go/cue/cuecontext"
29
25
"cuelang.org/go/cue/errors"
30
- "cuelang.org/go/cue/load"
31
26
"cuelang.org/go/encoding/openapi"
32
27
"cuelang.org/go/internal/cuetdtest"
33
- "cuelang.org/go/internal/cuetest"
34
28
"cuelang.org/go/internal/cuetxtar"
35
29
)
36
30
@@ -45,6 +39,34 @@ func TestGenerateOpenAPI(t *testing.T) {
45
39
Matrix : matrix ,
46
40
}
47
41
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
+
48
70
test .Run (t , func (t * cuetxtar.Test ) {
49
71
if t .HasTag ("skip-" + t .Name ()) {
50
72
t .Skip ()
@@ -68,6 +90,20 @@ func TestGenerateOpenAPI(t *testing.T) {
68
90
if version , ok := t .Value ("Version" ); ok {
69
91
config .Version = version
70
92
}
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
+ }
71
107
72
108
expectedErr , shouldErr := t .Value ("ExpectError" )
73
109
b , err := openapi .Gen (v , & config )
@@ -104,92 +140,6 @@ func TestGenerateOpenAPI(t *testing.T) {
104
140
})
105
141
}
106
142
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
-
193
143
// TODO: move OpenAPI testing to txtar and allow errors.
194
144
func TestIssue1234 (t * testing.T ) {
195
145
val := cuecontext .New ().CompileString (`
0 commit comments