MCPcopy Index your code
hub / github.com/oapi-codegen/oapi-codegen / TestEnumDefaultPointers

Function TestEnumDefaultPointers

pkg/codegen/server_urls_test.go:269–348  ·  view source on GitHub ↗

TestEnumDefaultPointers verifies the asymmetric default-pointer naming and the dedup-aware target reference — the two behaviours raised in the PR #2358 review.

(t *testing.T)

Source from the content-addressed store, hash-verified

267// naming and the dedup-aware target reference — the two behaviours
268// raised in the PR #2358 review.
269func TestEnumDefaultPointers(t *testing.T) {
270 t.Run("happy-path enum keeps the historical `…Default` name", func(t *testing.T) {
271 // No enum value folds to the literal "Default", so there's no
272 // collision and the default-pointer keeps the pre-fix name.
273 // This is the asymmetric-rename criterion: only specs that
274 // would have collided under the old codegen see the rename.
275 srv := ServerObjectDefinition{
276 GoName: "ServerUrlExample",
277 OAPISchema: &openapi3.Server{
278 URL: "https://api.example.com:{port}",
279 Variables: map[string]*openapi3.ServerVariable{
280 "port": {Default: "8443", Enum: []string{"443", "8443"}},
281 },
282 },
283 }
284 ptrs := srv.EnumDefaultPointers()
285 require.Len(t, ptrs, 1)
286 assert.Equal(t, "ServerUrlExamplePortVariableDefault", ptrs[0].PointerName)
287 assert.Equal(t, "ServerUrlExamplePortVariable8443", ptrs[0].TargetName)
288 })
289
290 t.Run("colliding enum value triggers `…DefaultValue` rename (#2003)", func(t *testing.T) {
291 srv := ServerObjectDefinition{
292 GoName: "ServerUrlExample",
293 OAPISchema: &openapi3.Server{
294 URL: "https://api.example.com/{port}",
295 Variables: map[string]*openapi3.ServerVariable{
296 "port": {Default: "default", Enum: []string{"default", "443"}},
297 },
298 },
299 }
300 ptrs := srv.EnumDefaultPointers()
301 require.Len(t, ptrs, 1)
302 assert.Equal(t, "ServerUrlExamplePortVariableDefaultValue", ptrs[0].PointerName)
303 assert.Equal(t, "ServerUrlExamplePortVariableDefault", ptrs[0].TargetName,
304 "the target const for value \"default\" is …VariableDefault; the pointer is …VariableDefaultValue and references it")
305 })
306
307 t.Run("dedup-suffix value is referenced by the right name", func(t *testing.T) {
308 // `enum: [foo, Foo]` both `ucFirst`-fold to `Foo`; the second
309 // becomes `Foo1`. With `default: "Foo"` the pointer must
310 // reference …VariableFoo1, not …VariableFoo (which holds "foo").
311 // Greptile flagged this in PR #2358 review.
312 srv := ServerObjectDefinition{
313 GoName: "ServerUrlExample",
314 OAPISchema: &openapi3.Server{
315 URL: "https://api.example.com/{mode}",
316 Variables: map[string]*openapi3.ServerVariable{
317 "mode": {Default: "Foo", Enum: []string{"foo", "Foo"}},
318 },
319 },
320 }
321 ptrs := srv.EnumDefaultPointers()
322 require.Len(t, ptrs, 1)
323 assert.Equal(t, "ServerUrlExampleModeVariableDefault", ptrs[0].PointerName,
324 "no enum value folds to `Default`, so no rename")
325 assert.Equal(t, "ServerUrlExampleModeVariableFoo1", ptrs[0].TargetName,
326 "target must be the post-suffix const for `Foo`, not the unsuffixed `Foo` which holds `foo`")

Callers

nothing calls this directly

Calls 3

EnumDefaultPointersMethod · 0.95
RunMethod · 0.80
LenMethod · 0.45

Tested by

no test coverage detected