Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fixed serialization unit tests to actually check equality #217

Merged
merged 1 commit into from
Aug 24, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
126 changes: 67 additions & 59 deletions pkg/serialize/encode_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,89 +30,101 @@ type StructUnexportedField struct {

func Test_Encoding(t *testing.T) {
type testCase struct {
name string
input any
output any
encodingErr bool
name string
testable
}

testCases := []testCase{
{
name: "valid struct",
input: TestStruct1{
Val1: 1,
Val3: []byte("test"),
Val2: "test",
Val4: true,
},
output: TestStruct1{
Val1: 1,
Val2: "test",
Val3: []byte("test"),
Val4: true,
testable: genericTestCase[TestStruct1]{
input: TestStruct1{
Val1: 1,
Val3: []byte("test"),
Val2: "test",
Val4: true,
},
output: TestStruct1{
Val1: 1,
Val2: "test",
Val3: []byte("test"),
Val4: true,
},
},
},
{
name: "unexported field",
input: StructUnexportedField{
val1: 1,
Val2: "test",
},
output: StructUnexportedField{
Val2: "test",
testable: genericTestCase[StructUnexportedField]{
input: StructUnexportedField{
val1: 1,
Val2: "test",
},
output: StructUnexportedField{
Val2: "test",
},
},
},
{
name: "invalid struct - signed int",
input: InvalidStructSignedInt{
Val1: -1,
testable: genericTestCase[InvalidStructSignedInt]{
input: InvalidStructSignedInt{
Val1: -1,
},
encodingErr: true,
},
encodingErr: true,
},
{
name: "invalid struct - map",
input: InvalidStructMap{
Val1: map[string]string{
"test": "test",
testable: genericTestCase[InvalidStructMap]{
input: InvalidStructMap{
Val1: map[string]string{
"test": "test",
},
},
encodingErr: true,
},
encodingErr: true,
},
}

for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
output, err := serialize.Encode(tc.input)
if tc.encodingErr && err == nil {
t.Errorf("Expected error, got nil")
}
if !tc.encodingErr && err != nil {
t.Errorf("Expected no error, got %v", err)
}
if tc.encodingErr {
return
}

_, err = serialize.Decode[any](output)
if err != nil {
t.Errorf("Expected no error, got %v", err)
}
if err != nil {
t.Errorf("Expected no error, got %v", err)
}

var v any
err = serialize.DecodeInto(output, &v)
if err != nil {
t.Errorf("Expected no error, got %v", err)
}
if err != nil {
return
}
tc.testable.runTest(t)
})
}
}

type testable interface {
runTest(t *testing.T)
}

type genericTestCase[T any] struct {
input T
output T
encodingErr bool
}

func (g genericTestCase[T]) runTest(t *testing.T) {
output, err := serialize.Encode(g.input)
if g.encodingErr && err == nil {
t.Errorf("Expected error, got nil")
}
if !g.encodingErr && err != nil {
t.Errorf("Expected no error, got %v", err)
}
if g.encodingErr {
return
}

result := new(T)
err = serialize.DecodeInto(output, result)
if err != nil {
t.Errorf("Expected no error, got %v", err)
}

assert.EqualValuesf(t, g.output, *result, "Expected result to be %v, got %v", g.output, *result)

}

func Test_EncodeSlice(t *testing.T) {

// this is an atypical way of testing, made to work with generics
Expand Down Expand Up @@ -196,10 +208,6 @@ func Test_EncodeSlice(t *testing.T) {
}
}

type testable interface {
runTest(t *testing.T)
}

type genericSliceTestCase[T any] struct {
input []*T
output []*T
Expand Down