-
Notifications
You must be signed in to change notification settings - Fork 7
/
reflect_test.go
149 lines (130 loc) · 4.23 KB
/
reflect_test.go
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
148
149
// Copyright © 2022 zc2638 <[email protected]>.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package swag
import (
"bytes"
"encoding/json"
"fmt"
"os"
"reflect"
"testing"
"github.com/stretchr/testify/assert"
)
type Person struct {
First string
}
type Anonymous struct {
AnyOne string
}
type Pet struct {
Friend Person `json:"friend" desc:"description short expression"`
Friends []Person `json:"friends" description:"long desc"`
Pointer *Person `json:"pointer" required:"true"`
Pointers []*Person `json:"pointers"`
Int int
IntArray []int
Int64Array []int64
String string
StringSecondWay string `json:"StringSecondWay,string"`
StringArray []string
Float float32
FloatArray []float32
Double float64
DoubleArray []float64
Bool bool
Enum string `json:"enum" enum:"a,b,c" example:"b"`
Anonymous
MapSlicePtr map[string][]*string
MapSlice map[string][]string
MapSliceStructPtr map[string][]*Person
MapSliceStruct map[string][]Person
SliceStructPtr []*Person
SliceStruct *[]Person
SliceStringPtr []*string
SliceString *[]string
MapNestOptions *MapObj `json:"map_nest_options,omitempty"`
}
type MapObj struct {
RuleOptions map[string]*MapOption `json:"rule_options"`
}
type MapOption struct {
Name string `json:"name"`
SubOptions map[string]*MapOption `json:"sub_options,omitempty"`
}
type Empty struct {
Nope int `json:"-"`
}
func TestDefine(t *testing.T) {
v := define(Pet{})
obj, ok := v["github.com_zc2638_swag.Pet"]
assert.True(t, ok)
assert.Equal(t, 26, len(obj.Properties))
content := make(map[string]Object)
data, err := os.ReadFile("testdata/pet.json")
assert.Nil(t, err)
err = json.NewDecoder(bytes.NewReader(data)).Decode(&content)
assert.Nil(t, err)
expected := content["github.com_zc2638_swag.Pet"]
assert.Equal(t, expected.Type, obj.Type, "expected Type to match")
assert.Equal(t, expected.Required, obj.Required, "expected Required to match")
assert.Equal(t, len(expected.Properties), len(obj.Properties), "expected same number of properties")
for k, v := range obj.Properties {
e := expected.Properties[k]
assert.Equal(t, e.Type, v.Type, "expected %v.Type to match", k)
assert.Equal(t, e.Description, v.Description, "expected %v.Description to match", k)
assert.Equal(t, e.Enum, v.Enum, "expected %v.Enum to match", k)
assert.Equal(t, e.Format, v.Format, "expected %v.Format to match", k)
assert.Equal(t, e.Ref, v.Ref, "expected %v.Ref to match", k)
assert.Equal(t, e.Example, v.Example, "expected %v.Example to match", k)
}
}
func TestNotStructDefine(t *testing.T) {
v := define(int32(1))
obj, ok := v["int32"]
assert.True(t, ok)
assert.Equal(t, "integer", obj.Type)
assert.Equal(t, "int32", obj.Format)
v = define(uint64(1))
obj, ok = v["uint64"]
assert.True(t, ok)
assert.Equal(t, "integer", obj.Type)
assert.Equal(t, "int64", obj.Format)
v = define("")
obj, ok = v["string"]
assert.True(t, ok)
assert.Equal(t, "string", obj.Type)
assert.Equal(t, "", obj.Format)
v = define(byte(1))
obj, ok = v["uint8"]
if !assert.True(t, ok) {
fmt.Printf("%v", v)
}
assert.Equal(t, "integer", obj.Type)
assert.Equal(t, "int32", obj.Format)
bs := []byte{1, 2}
v = define(bs)
bsName := makeName(reflect.TypeOf(bs))
obj, ok = v[bsName]
assert.Equal(t, true, ok)
assert.Equal(t, "array", obj.Type)
assert.NotEqual(t, nil, obj.Items)
assert.Equal(t, "integer", obj.Items.Type)
assert.Equal(t, "int32", obj.Items.Format)
}
func TestHonorJsonIgnore(t *testing.T) {
v := define(Empty{})
obj, ok := v["github.com_zc2638_swag.Empty"]
assert.True(t, ok)
assert.Equal(t, 0, len(obj.Properties), "expected zero exposed properties")
}