github.com/artisanhe/tools@v1.0.1-0.20210607022958-19a8fef2eb04/courier/swagger/gen/definition_scanner_test.go (about) 1 package gen 2 3 import ( 4 "testing" 5 6 "github.com/stretchr/testify/assert" 7 8 "github.com/artisanhe/tools/codegen/loaderx" 9 ) 10 11 func TestDefinitionScanner(t *testing.T) { 12 tt := assert.New(t) 13 14 pkgImportPath, program := loaderx.NewTestProgram(` 15 package main 16 17 import ( 18 "time" 19 "github.com/artisanhe/tools/courier/enumeration" 20 ) 21 22 type String string 23 type Int int 24 type Bool bool 25 type SomeBool = enumeration.Bool 26 27 type SliceString []string 28 type SliceNamed []String 29 30 type Struct struct { 31 // name 32 Name *string ^^json:"name" validate:"@string[0,)"^^ 33 Id **string ^^json:"id,omitempty"^^ 34 Enum Enum ^^json:"enum"^^ 35 } 36 37 // swagger:strfmt date-time 38 // 日期 39 type Time time.Time 40 41 // swagger:enum 42 type Enum int 43 44 const ( 45 ENUM__ONE Enum = iota + 1 // one 46 ENUM__TWO // two 47 ) 48 `) 49 50 scanner := NewDefinitionScanner(program) 51 52 cases := []struct { 53 typeName string 54 schema interface{} 55 }{ 56 { 57 typeName: "String", 58 schema: map[string]interface{}{ 59 "type": "string", 60 }, 61 }, 62 { 63 typeName: "Int", 64 schema: map[string]interface{}{ 65 "type": "integer", 66 "format": "int64", 67 }, 68 }, 69 { 70 typeName: "Bool", 71 schema: map[string]interface{}{ 72 "type": "boolean", 73 }, 74 }, 75 { 76 typeName: "SomeBool", 77 schema: map[string]interface{}{ 78 "type": "boolean", 79 }, 80 }, 81 { 82 typeName: "SliceNamed", 83 schema: map[string]interface{}{ 84 "type": "array", 85 "items": map[string]interface{}{ 86 "$ref": "#/components/schemas/String", 87 }, 88 }, 89 }, 90 { 91 typeName: "SliceString", 92 schema: map[string]interface{}{ 93 "type": "array", 94 "items": map[string]interface{}{ 95 "type": "string", 96 }, 97 }, 98 }, 99 { 100 typeName: "Struct", 101 schema: map[string]interface{}{ 102 "type": "object", 103 "required": []string{"name", "enum"}, 104 "properties": map[string]interface{}{ 105 "name": map[string]interface{}{ 106 XField: "Name", 107 XTagJSON: "name", 108 "description": "name", 109 "minLength": 0, 110 "type": "string", 111 XPointer: 1, 112 XTagValidate: "@string[0,)", 113 }, 114 "id": map[string]interface{}{ 115 XField: "Id", 116 XTagJSON: "id,omitempty", 117 XPointer: 2, 118 "type": "string", 119 }, 120 "enum": map[string]interface{}{ 121 "allOf": []map[string]interface{}{ 122 { 123 "$ref": "#/components/schemas/Enum", 124 }, 125 {}, 126 }, 127 XField: "Enum", 128 XTagJSON: "enum", 129 }, 130 }, 131 }, 132 }, 133 { 134 typeName: "Time", 135 schema: map[string]interface{}{ 136 "description": "日期", 137 "type": "string", 138 "format": "date-time", 139 }, 140 }, 141 { 142 typeName: "Enum", 143 schema: map[string]interface{}{ 144 "type": "string", 145 "enum": []interface{}{"ONE", "TWO"}, 146 XEnumVals: []interface{}{1, 2}, 147 XEnumLabels: []string{"one", "two"}, 148 XEnumValues: []string{"ONE", "TWO"}, 149 }, 150 }, 151 } 152 153 query := loaderx.NewQuery(program, pkgImportPath) 154 155 for _, c := range cases { 156 schema := scanner.Def(query.TypeName(c.typeName)) 157 tt.Equal(ToMap(c.schema), ToMap(schema)) 158 } 159 } 160 161 func TestDefinitionScannerWithError(t *testing.T) { 162 pkgImportPath, program := loaderx.NewTestProgram(` 163 package main 164 165 type PartA struct { 166 Name string ^^json:"name" validate:"@string[0,)"^^ 167 } 168 169 type PartB struct { 170 Name string ^^json:"name" validate:"@string[0,)"^^ 171 } 172 173 type Composed struct { 174 PartA 175 PartB 176 } 177 `) 178 179 scanner := NewDefinitionScanner(program) 180 181 query := loaderx.NewQuery(program, pkgImportPath) 182 183 defer func() { 184 if e := recover(); e != nil { 185 t.Log(e) 186 } 187 }() 188 189 scanner.Def(query.TypeName("Composed")) 190 }