forked from oapi-codegen/oapi-codegen-exp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathenum_oneof_test.go
More file actions
147 lines (129 loc) · 3.71 KB
/
enum_oneof_test.go
File metadata and controls
147 lines (129 loc) · 3.71 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
package codegen
import (
"testing"
"github.com/pb33f/libopenapi"
"github.com/pb33f/libopenapi/datamodel/high/base"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
// targetSchema parses a minimal OpenAPI 3.1 spec with a single component
// schema named "Target" and returns the resolved high-level schema.
// The input is the YAML body of the Target schema (indented by 6 spaces to
// sit under `components.schemas.Target`).
func targetSchema(t *testing.T, targetYAML string) *base.Schema {
t.Helper()
const preamble = "openapi: 3.1.0\n" +
"info:\n" +
" title: t\n" +
" version: \"1\"\n" +
"paths: {}\n" +
"components:\n" +
" schemas:\n" +
" Target:\n"
doc, err := libopenapi.NewDocument([]byte(preamble + targetYAML))
require.NoError(t, err)
model, errs := doc.BuildV3Model()
require.Empty(t, errs, "BuildV3Model errors")
require.NotNil(t, model)
proxy := model.Model.Components.Schemas.GetOrZero("Target")
require.NotNil(t, proxy)
sch := proxy.Schema()
require.NotNil(t, sch)
return sch
}
func TestIsConstOneOfEnum_Integer(t *testing.T) {
sch := targetSchema(t, ` type: integer
oneOf:
- title: HIGH
const: 2
description: An urgent problem
- title: MEDIUM
const: 1
- title: LOW
const: 0
description: Can wait forever
`)
items, ok := isConstOneOfEnum(sch)
require.True(t, ok)
require.Len(t, items, 3)
assert.Equal(t, "HIGH", items[0].Title)
assert.Equal(t, "2", items[0].Value)
assert.Equal(t, "An urgent problem", items[0].Doc)
assert.Equal(t, "MEDIUM", items[1].Title)
assert.Equal(t, "1", items[1].Value)
assert.Equal(t, "", items[1].Doc)
assert.Equal(t, "LOW", items[2].Title)
assert.Equal(t, "0", items[2].Value)
assert.Equal(t, "Can wait forever", items[2].Doc)
}
func TestIsConstOneOfEnum_String(t *testing.T) {
sch := targetSchema(t, ` type: string
oneOf:
- title: Red
const: r
- title: Green
const: g
- title: Blue
const: b
`)
items, ok := isConstOneOfEnum(sch)
require.True(t, ok)
require.Len(t, items, 3)
assert.Equal(t, "Red", items[0].Title)
assert.Equal(t, "r", items[0].Value)
}
func TestIsConstOneOfEnum_MissingTitle(t *testing.T) {
sch := targetSchema(t, ` type: integer
oneOf:
- title: HIGH
const: 2
- const: 1
`)
_, ok := isConstOneOfEnum(sch)
assert.False(t, ok, "missing title on one branch must disqualify the idiom")
}
func TestIsConstOneOfEnum_MissingConst(t *testing.T) {
sch := targetSchema(t, ` type: integer
oneOf:
- title: HIGH
const: 2
- title: MEDIUM
`)
_, ok := isConstOneOfEnum(sch)
assert.False(t, ok, "missing const on one branch must disqualify the idiom")
}
func TestIsConstOneOfEnum_NonScalarOuterType(t *testing.T) {
sch := targetSchema(t, ` type: object
oneOf:
- title: HIGH
const: 2
- title: LOW
const: 0
`)
_, ok := isConstOneOfEnum(sch)
assert.False(t, ok, "object outer type must disqualify the idiom")
}
func TestIsConstOneOfEnum_EmptyOneOf(t *testing.T) {
sch := targetSchema(t, ` type: integer
`)
_, ok := isConstOneOfEnum(sch)
assert.False(t, ok, "no oneOf means no idiom")
}
func TestIsConstOneOfEnum_NestedComposition(t *testing.T) {
sch := targetSchema(t, ` type: integer
oneOf:
- title: HIGH
const: 2
oneOf:
- const: 3
- const: 4
- title: LOW
const: 0
`)
_, ok := isConstOneOfEnum(sch)
assert.False(t, ok, "a branch with nested composition must disqualify the idiom")
}
func TestIsConstOneOfEnum_NilSchema(t *testing.T) {
_, ok := isConstOneOfEnum(nil)
assert.False(t, ok)
}