github.com/unionj-cloud/go-doudou/v2@v2.3.5/toolkit/openapi/v3/helper_test.go (about) 1 package v3 2 3 import ( 4 . "github.com/smartystreets/goconvey/convey" 5 "github.com/stretchr/testify/require" 6 "github.com/unionj-cloud/go-doudou/v2/toolkit/astutils" 7 "github.com/unionj-cloud/go-doudou/v2/toolkit/pathutils" 8 "go/ast" 9 "go/parser" 10 "go/token" 11 "strings" 12 "testing" 13 ) 14 15 func Test_isSupport(t *testing.T) { 16 type args struct { 17 t string 18 } 19 tests := []struct { 20 name string 21 args args 22 want bool 23 }{ 24 { 25 name: "1", 26 args: args{ 27 t: "float32", 28 }, 29 want: true, 30 }, 31 { 32 name: "2", 33 args: args{ 34 t: "[]int64", 35 }, 36 want: true, 37 }, 38 } 39 for _, tt := range tests { 40 t.Run(tt.name, func(t *testing.T) { 41 if got := IsSupport(tt.args.t); got != tt.want { 42 t.Errorf("isSupport() = %v, want %v", got, tt.want) 43 } 44 }) 45 } 46 } 47 48 func Test_castFunc(t *testing.T) { 49 type args struct { 50 t string 51 } 52 tests := []struct { 53 name string 54 args args 55 want string 56 }{ 57 { 58 name: "1", 59 args: args{ 60 t: "uint64", 61 }, 62 want: "ToUint64", 63 }, 64 } 65 for _, tt := range tests { 66 t.Run(tt.name, func(t *testing.T) { 67 if got := CastFunc(tt.args.t); got != tt.want { 68 t.Errorf("castFunc() = %v, want %v", got, tt.want) 69 } 70 }) 71 } 72 } 73 74 func TestSchemaOf(t *testing.T) { 75 Convey("SchemaOf", t, func() { 76 So(SchemaOf(astutils.FieldMeta{ 77 Name: "avatar", 78 Type: "v3.FileModel", 79 IsExport: true, 80 }), ShouldEqual, File) 81 82 So(SchemaOf(astutils.FieldMeta{ 83 Name: "name", 84 Type: "string", 85 IsExport: true, 86 }), ShouldEqual, String) 87 88 So(SchemaOf(astutils.FieldMeta{ 89 Name: "age", 90 Type: "int", 91 IsExport: true, 92 }), ShouldEqual, Int) 93 94 So(SchemaOf(astutils.FieldMeta{ 95 Name: "id", 96 Type: "int64", 97 IsExport: true, 98 }), ShouldEqual, Int64) 99 100 So(SchemaOf(astutils.FieldMeta{ 101 Name: "married", 102 Type: "bool", 103 IsExport: true, 104 }), ShouldEqual, Bool) 105 106 So(SchemaOf(astutils.FieldMeta{ 107 Name: "score", 108 Type: "float32", 109 IsExport: true, 110 }), ShouldEqual, Float32) 111 112 So(SchemaOf(astutils.FieldMeta{ 113 Name: "average", 114 Type: "float64", 115 IsExport: true, 116 }), ShouldEqual, Float64) 117 118 So(SchemaOf(astutils.FieldMeta{ 119 Name: "params", 120 Type: "...int", 121 IsExport: true, 122 }).Type, ShouldEqual, ArrayT) 123 124 So(SchemaOf(astutils.FieldMeta{ 125 Name: "data", 126 Type: "map[string]string", 127 IsExport: true, 128 }).Type, ShouldEqual, ObjectT) 129 130 So(SchemaOf(astutils.FieldMeta{ 131 Name: "anony", 132 Type: "anonystruct«{\"Name\":\"\",\"Fields\":[{\"Name\":\"Name\",\"Type\":\"string\",\"Tag\":\"\",\"Comments\":null,\"IsExport\":true,\"DocName\":\"Name\"},{\"Name\":\"Addr\",\"Type\":\"anonystruct«{\\\"Name\\\":\\\"\\\",\\\"Fields\\\":[{\\\"Name\\\":\\\"Zip\\\",\\\"Type\\\":\\\"string\\\",\\\"Tag\\\":\\\"\\\",\\\"Comments\\\":null,\\\"IsExport\\\":true,\\\"DocName\\\":\\\"Zip\\\"},{\\\"Name\\\":\\\"Block\\\",\\\"Type\\\":\\\"string\\\",\\\"Tag\\\":\\\"\\\",\\\"Comments\\\":null,\\\"IsExport\\\":true,\\\"DocName\\\":\\\"Block\\\"},{\\\"Name\\\":\\\"Full\\\",\\\"Type\\\":\\\"string\\\",\\\"Tag\\\":\\\"\\\",\\\"Comments\\\":null,\\\"IsExport\\\":true,\\\"DocName\\\":\\\"Full\\\"}],\\\"Comments\\\":null,\\\"Methods\\\":null,\\\"IsExport\\\":false}»\",\"Tag\":\"\",\"Comments\":null,\"IsExport\":true,\"DocName\":\"Addr\"}],\"Comments\":null,\"Methods\":null,\"IsExport\":false}»", 133 IsExport: true, 134 }).Type, ShouldEqual, ObjectT) 135 136 SchemaNames = []string{"User"} 137 So(SchemaOf(astutils.FieldMeta{ 138 Name: "user", 139 Type: "User", 140 IsExport: true, 141 }).Ref, ShouldEqual, "#/components/schemas/User") 142 143 So(SchemaOf(astutils.FieldMeta{ 144 Name: "user", 145 Type: "vo.User", 146 IsExport: true, 147 }).Ref, ShouldEqual, "#/components/schemas/User") 148 149 Enums = map[string]astutils.EnumMeta{ 150 "KeyboardLayout": { 151 Name: "KeyboardLayout", 152 Values: []string{ 153 "UNKNOWN", 154 "QWERTZ", 155 }, 156 }, 157 } 158 So(SchemaOf(astutils.FieldMeta{ 159 Name: "layout", 160 Type: "KeyboardLayout", 161 IsExport: true, 162 }).Enum, ShouldResemble, []interface{}{ 163 "UNKNOWN", 164 "QWERTZ", 165 }) 166 167 So(SchemaOf(astutils.FieldMeta{ 168 Name: "any", 169 Type: "Any", 170 IsExport: true, 171 }), ShouldEqual, Any) 172 }) 173 } 174 175 func TestRefAddDoc(t *testing.T) { 176 Convey("Description should be equal to doc", t, func() { 177 SchemaNames = []string{"User"} 178 schema := SchemaOf(astutils.FieldMeta{ 179 Name: "user", 180 Type: "User", 181 IsExport: true, 182 }) 183 doc := "This is a struct for user field" 184 RefAddDoc(schema, doc) 185 So(strings.TrimSpace(Schemas["User"].Description), ShouldEqual, doc) 186 }) 187 } 188 189 func TestIsBuiltin(t *testing.T) { 190 Convey("Test IsBuiltIn", t, func() { 191 So(IsBuiltin(astutils.FieldMeta{ 192 Name: "age", 193 Type: "int", 194 IsExport: true, 195 }), ShouldBeTrue) 196 197 So(IsBuiltin(astutils.FieldMeta{ 198 Name: "books", 199 Type: "[]string", 200 IsExport: true, 201 }), ShouldBeTrue) 202 203 So(IsBuiltin(astutils.FieldMeta{ 204 Name: "data", 205 Type: "map[string]string", 206 IsExport: true, 207 }), ShouldBeFalse) 208 }) 209 } 210 211 func TestIsEnum(t *testing.T) { 212 Convey("Test IsEnum", t, func() { 213 So(IsEnum(astutils.FieldMeta{ 214 Name: "age", 215 Type: "int", 216 IsExport: true, 217 }), ShouldBeFalse) 218 219 Enums = map[string]astutils.EnumMeta{ 220 "KeyboardLayout": { 221 Name: "KeyboardLayout", 222 Values: []string{ 223 "UNKNOWN", 224 "QWERTZ", 225 }, 226 }, 227 } 228 So(IsEnum(astutils.FieldMeta{ 229 Name: "layout", 230 Type: "KeyboardLayout", 231 IsExport: true, 232 }), ShouldBeTrue) 233 }) 234 } 235 236 func TestElementType(t *testing.T) { 237 Convey("Test ElementType", t, func() { 238 So(ElementType("[]int"), ShouldEqual, "int") 239 So(ElementType("...int"), ShouldEqual, "int") 240 }) 241 } 242 243 func TestIsOptional(t *testing.T) { 244 Convey("Test IsOptional", t, func() { 245 So(IsOptional("*[]int"), ShouldBeTrue) 246 So(IsOptional("...int"), ShouldBeTrue) 247 So(IsOptional("int"), ShouldBeFalse) 248 }) 249 } 250 251 func TestIsSlice(t *testing.T) { 252 Convey("Test IsOptional", t, func() { 253 So(IsSlice("*[]int"), ShouldBeTrue) 254 So(IsSlice("...int"), ShouldBeTrue) 255 So(IsSlice("int"), ShouldBeFalse) 256 }) 257 } 258 259 func TestEnum(t *testing.T) { 260 file := pathutils.Abs("testdata/enum.go") 261 fset := token.NewFileSet() 262 root, err := parser.ParseFile(fset, file, nil, parser.ParseComments) 263 if err != nil { 264 panic(err) 265 } 266 sc := astutils.NewEnumCollector(astutils.ExprString) 267 ast.Walk(sc, root) 268 enumMap := make(map[string]astutils.EnumMeta) 269 for k, v := range sc.Methods { 270 if IsEnumType(v) { 271 em := astutils.EnumMeta{ 272 Name: k, 273 Values: sc.Consts[k], 274 } 275 enumMap[k] = em 276 } 277 } 278 require.Equal(t, 1, len(enumMap)) 279 }