Skip to content

Commit 02f8b04

Browse files
Added tests for ReferencesATypeWithAnExtension: generated client composition
1 parent 3912499 commit 02f8b04

2 files changed

Lines changed: 33 additions & 19 deletions

File tree

examples/output-options/preferskipoptionalpointer/gen.go

Lines changed: 2 additions & 6 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

examples/output-options/preferskipoptionalpointer/gen_test.go

Lines changed: 31 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -81,20 +81,38 @@ func TestNestedType(t *testing.T) {
8181
}
8282

8383
func TestReferencesATypeWithAnExtension(t *testing.T) {
84-
obj := ReferencesATypeWithAnExtension{
85-
NoExtension: ReferencedWithoutExtension{
86-
// this is an optional field, hence no pointer
87-
Foo: "",
88-
},
89-
// this is a map
90-
NoExtensionMap: map[string]any{},
91-
// we're referencing a type, which has `x-go-type-skip-optional-pointer`, so should not have a pointer type
92-
WithExtension: ReferencedWithExtension{
93-
Foo: "this is set",
94-
},
95-
}
84+
t.Run("zero value", func(t *testing.T) {
85+
typeWithExt := ReferencesATypeWithAnExtension{}
86+
assert.Zero(t, typeWithExt)
87+
assert.Zero(t, typeWithExt.NoExtension)
88+
assert.Nil(t, typeWithExt.WithExtensionPointer)
89+
})
90+
91+
t.Run("value on noExtension", func(t *testing.T) {
92+
typeWithExt := ReferencesATypeWithAnExtension{
93+
NoExtension: ReferencedWithoutExtension{"value"},
94+
WithExtensionPointer: nil, // &ReferencedWithExtension{"ptrValue"},
95+
}
9696

97-
assert.NotZero(t, obj)
97+
b, err := json.Marshal(typeWithExt)
98+
require.NoError(t, err)
99+
100+
assert.True(t, jsonContainsKey(b, "noExtension"))
101+
assert.False(t, jsonContainsKey(b, "withExtensionPointer"))
102+
})
103+
104+
t.Run("value on noExtension and withExtensionPointer", func(t *testing.T) {
105+
typeWithExt := ReferencesATypeWithAnExtension{
106+
NoExtension: ReferencedWithoutExtension{"value"},
107+
WithExtensionPointer: &ReferencedWithExtension{"ptrValue"},
108+
}
109+
110+
b, err := json.Marshal(typeWithExt)
111+
require.NoError(t, err)
112+
113+
assert.True(t, jsonContainsKey(b, "noExtension"))
114+
assert.True(t, jsonContainsKey(b, "withExtensionPointer"))
115+
})
98116
}
99117

100118
// jsonContainsKey checks if the given JSON object contains the specified key at the top level.

0 commit comments

Comments
 (0)