k8s.io/kube-openapi@v0.0.0-20240228011516-70dd3763d340/pkg/generators/enum_test.go (about) 1 /* 2 Copyright 2022 The Kubernetes Authors. 3 4 Licensed under the Apache License, Version 2.0 (the "License"); 5 you may not use this file except in compliance with the License. 6 You may obtain a copy of the License at 7 8 http://www.apache.org/licenses/LICENSE-2.0 9 10 Unless required by applicable law or agreed to in writing, software 11 distributed under the License is distributed on an "AS IS" BASIS, 12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 See the License for the specific language governing permissions and 14 limitations under the License. 15 */ 16 17 package generators 18 19 import ( 20 "reflect" 21 "sort" 22 "testing" 23 24 "k8s.io/gengo/v2/generator" 25 "k8s.io/gengo/v2/types" 26 ) 27 28 func TestParseEnums(t *testing.T) { 29 for _, tc := range []struct { 30 name string 31 universe types.Universe 32 expected map[string][]string 33 }{ 34 { 35 name: "value in different package", 36 universe: types.Universe{ 37 "foo": &types.Package{ 38 Name: "foo", 39 Types: map[string]*types.Type{ 40 "Foo": { 41 Name: types.Name{ 42 Package: "foo", 43 Name: "Foo", 44 }, 45 Kind: types.Alias, 46 Underlying: types.String, 47 CommentLines: []string{"+enum"}, 48 }, 49 }, 50 }, 51 "bar": &types.Package{ 52 Name: "bar", 53 Constants: map[string]*types.Type{ 54 "Bar": { 55 Name: types.Name{ 56 Package: "bar", 57 Name: "Bar", 58 }, 59 Kind: types.Alias, 60 Underlying: &types.Type{ 61 Name: types.Name{ 62 Package: "foo", 63 Name: "Foo", 64 }, 65 }, 66 ConstValue: &[]string{"bar"}[0], 67 }, 68 }, 69 }, 70 }, 71 expected: map[string][]string{ 72 "foo.Foo": {"bar"}, 73 }, 74 }, 75 { 76 name: "value in same package", 77 universe: types.Universe{ 78 "foo": &types.Package{ 79 Name: "foo", 80 Types: map[string]*types.Type{ 81 "Foo": { 82 Name: types.Name{ 83 Package: "foo", 84 Name: "Foo", 85 }, 86 Kind: types.Alias, 87 Underlying: types.String, 88 CommentLines: []string{"+enum"}, 89 }, 90 }, 91 Constants: map[string]*types.Type{ 92 "Bar": { 93 Name: types.Name{ 94 Package: "foo", 95 Name: "Bar", 96 }, 97 Kind: types.Alias, 98 Underlying: &types.Type{ 99 Name: types.Name{ 100 Package: "foo", 101 Name: "Foo", 102 }, 103 }, 104 ConstValue: &[]string{"bar"}[0], 105 }, 106 }, 107 }, 108 }, 109 expected: map[string][]string{ 110 "foo.Foo": {"bar"}, 111 }, 112 }, 113 { 114 name: "values in same and different packages", 115 universe: types.Universe{ 116 "foo": &types.Package{ 117 Name: "foo", 118 Types: map[string]*types.Type{ 119 "Foo": { 120 Name: types.Name{ 121 Package: "foo", 122 Name: "Foo", 123 }, 124 Kind: types.Alias, 125 Underlying: types.String, 126 CommentLines: []string{"+enum"}, 127 }, 128 }, 129 Constants: map[string]*types.Type{ 130 "FooSame": { 131 Name: types.Name{ 132 Package: "foo", 133 Name: "FooSame", 134 }, 135 Kind: types.Alias, 136 Underlying: &types.Type{ 137 Name: types.Name{ 138 Package: "foo", 139 Name: "Foo", 140 }, 141 }, 142 ConstValue: &[]string{"same"}[0], 143 }, 144 }, 145 }, 146 "bar": &types.Package{ 147 Name: "bar", 148 Constants: map[string]*types.Type{ 149 "FooDifferent": { 150 Name: types.Name{ 151 Package: "bar", 152 Name: "FooDifferent", 153 }, 154 Kind: types.Alias, 155 Underlying: &types.Type{ 156 Name: types.Name{ 157 Package: "foo", 158 Name: "Foo", 159 }, 160 }, 161 ConstValue: &[]string{"different"}[0], 162 }, 163 }, 164 }, 165 }, 166 expected: map[string][]string{ 167 "foo.Foo": {"different", "same"}, 168 }, 169 }, 170 { 171 name: "aliasing and re-exporting enum from different package", 172 universe: types.Universe{ 173 "foo": &types.Package{ 174 Name: "foo", 175 Types: map[string]*types.Type{ 176 "Foo": { 177 Name: types.Name{ 178 Package: "foo", 179 Name: "Foo", 180 }, 181 Kind: types.Alias, 182 Underlying: types.String, 183 CommentLines: []string{"+enum"}, 184 }, 185 }, 186 Constants: map[string]*types.Type{ 187 "FooCase1": { 188 Name: types.Name{ 189 Package: "foo", 190 Name: "FooCase1", 191 }, 192 Kind: types.DeclarationOf, 193 Underlying: &types.Type{ 194 Name: types.Name{ 195 Package: "foo", 196 Name: "Foo", 197 }, 198 }, 199 ConstValue: &[]string{"case1"}[0], 200 }, 201 "FooCase2": { 202 Name: types.Name{ 203 Package: "foo", 204 Name: "FooCase2", 205 }, 206 Kind: types.DeclarationOf, 207 Underlying: &types.Type{ 208 Name: types.Name{ 209 Package: "foo", 210 Name: "Foo", 211 }, 212 }, 213 ConstValue: &[]string{"case2"}[0], 214 }, 215 }, 216 }, 217 "bar": &types.Package{ 218 Name: "bar", 219 Constants: map[string]*types.Type{ 220 "FooCase1": { 221 Name: types.Name{ 222 Package: "foo", 223 Name: "FooCase1", 224 }, 225 Kind: types.DeclarationOf, 226 Underlying: &types.Type{ 227 Name: types.Name{ 228 Package: "foo", 229 Name: "Foo", 230 }, 231 }, 232 ConstValue: &[]string{"case1"}[0], 233 }, 234 "FooCase2": { 235 Name: types.Name{ 236 Package: "foo", 237 Name: "FooCase2", 238 }, 239 Kind: types.DeclarationOf, 240 Underlying: &types.Type{ 241 Name: types.Name{ 242 Package: "foo", 243 Name: "Foo", 244 }, 245 }, 246 ConstValue: &[]string{"case2"}[0], 247 }, 248 }, 249 }, 250 }, 251 expected: map[string][]string{ 252 "foo.Foo": {"case1", "case2"}, 253 }, 254 }, 255 } { 256 t.Run(tc.name, func(t *testing.T) { 257 enums := parseEnums(&generator.Context{Universe: tc.universe}) 258 259 actual := make(map[string][]string) 260 for _, enum := range enums { 261 values := make([]string, len(enum.Values)) 262 for i := range values { 263 values[i] = enum.Values[i].Value 264 } 265 sort.Strings(values) 266 actual[enum.Name.String()] = values 267 } 268 269 if !reflect.DeepEqual(tc.expected, actual) { 270 t.Errorf("expected: %#v, got %#v", tc.expected, actual) 271 } 272 }) 273 } 274 }