Skip to content

Commit

Permalink
WIP type condition
Browse files Browse the repository at this point in the history
  • Loading branch information
JeromeJu committed Aug 16, 2022
1 parent b7767a4 commit 1284453
Showing 1 changed file with 44 additions and 43 deletions.
87 changes: 44 additions & 43 deletions pkg/apis/version/conversion_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ limitations under the License.
package version_test

import (
"reflect"
"testing"

"github.com/google/go-cmp/cmp"
Expand All @@ -29,51 +30,51 @@ type testStruct struct {
}

func TestSerializationRoundTrip(t *testing.T) {
meta := metav1.ObjectMeta{}
source := testStruct{Field: "foo"}
key := "my-key"
err := version.SerializeToMetadata(&meta, source, key)
if err != nil {
t.Fatalf("Serialization error: %s", err)
}

sink := testStruct{}
err = version.DeserializeFromMetadata(&meta, &sink, key)
if err != nil {
t.Fatalf("Deserialization error: %s", err)
}

_, ok := meta.Annotations[key]
if ok {
t.Errorf("Expected key %s not to be present in annotations but it was", key)
}

if d := cmp.Diff(source, sink); d != "" {
t.Errorf("Unexpected diff after serialization/deserialization round trip: %s", d)
}
}

func TestArraySerializationRoundTrip(t *testing.T) {
meta := metav1.ObjectMeta{}
source := []testStruct{{Field: "foo"}, {Field: "bar"}}
key := "my-key"
err := version.SerializeToMetadata(&meta, source, key)
if err != nil {
t.Fatalf("Serialization error: %s", err)
}
var arraySourceType = reflect.TypeOf([]testStruct{{Field: ""}})

sink := []testStruct{}
err = version.DeserializeFromMetadata(&meta, &sink, key)
if err != nil {
t.Fatalf("Deserialization error: %s", err)
}
testcases := []struct {
name string
meta metav1.ObjectMeta
source interface{}
key string
}{{
name: "serialization roundtrip",
meta: metav1.ObjectMeta{},
source: testStruct{Field: "foo"},
key: "my-key",
}, {
name: "array serialization roundtrip",
meta: metav1.ObjectMeta{},
source: []testStruct{{Field: "foo"}, {Field: "bar"}},
key: "my-key",
}}
for _, tc := range testcases {
t.Run(tc.name, func(t *testing.T) {
err := version.SerializeToMetadata(&tc.meta, tc.source, tc.key)
if err != nil {
t.Fatalf("Serialization error: %s", err)
}
var sink interface{}
if reflect.TypeOf(tc.source) == arraySourceType {
sink = &[]testStruct{}
t.Log("arr")
} else {
sink = &testStruct{}
}
err = version.DeserializeFromMetadata(&tc.meta, &sink, tc.key)
if err != nil {
t.Fatalf("Deserialization error: %s", err)
}

_, ok := meta.Annotations[key]
if ok {
t.Errorf("Expected key %s not to be present in annotations but it was", key)
}
_, ok := tc.meta.Annotations[tc.key]
if ok {
t.Errorf("Expected key %s not to be present in annotations but it was", tc.key)
}

if d := cmp.Diff(source, sink); d != "" {
t.Errorf("Unexpected diff after serialization/deserialization round trip: %s", d)
// if d := cmp.Diff(tc.source, sink); d != "" {
if d := cmp.Diff(&tc.source, sink); d != "" {
t.Errorf("Unexpected diff after serialization/deserialization round trip: %s", d)
}
})
}
}

0 comments on commit 1284453

Please sign in to comment.