Skip to content

Commit

Permalink
require: codec_test.go
Browse files Browse the repository at this point in the history
  • Loading branch information
magiconair committed Dec 16, 2024
1 parent baab51d commit e616d78
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 27 deletions.
15 changes: 6 additions & 9 deletions ua/codec_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,26 +38,23 @@ func RunCodecTest(t *testing.T, cases []CodecTestCase) {
case reflect.Slice:
v = reflect.New(typ) // typ: []x, v: *[]x
default:
t.Fatalf("%T is not a pointer or a slice", c.Struct)
require.Fail(t, "%T is not a pointer or a slice", c.Struct)
}

if _, err := Decode(c.Bytes, v.Interface()); err != nil {
t.Fatal(err)
}
_, err := Decode(c.Bytes, v.Interface())
require.NoError(t, err, "Decode failed")

// if v is a *[]x we need to dereference it before comparing it.
if typ.Kind() == reflect.Slice {
v = v.Elem()
}
require.Equal(t, c.Struct, v.Interface())
require.Equal(t, c.Struct, v.Interface(), "Decoded payload not equal")
})

t.Run("encode", func(t *testing.T) {
b, err := Encode(c.Struct)
if err != nil {
t.Fatal(err)
}
require.Equal(t, c.Bytes, b)
require.NoError(t, err, "Encode failed")
require.Equal(t, c.Bytes, b, "Encoded payload not equal")
})
})
}
Expand Down
15 changes: 6 additions & 9 deletions uacp/codec_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,26 +39,23 @@ func RunCodecTest(t *testing.T, cases []CodecTestCase) {
case reflect.Slice:
v = reflect.New(typ) // typ: []x, v: *[]x
default:
t.Fatalf("%T is not a pointer or a slice", c.Struct)
require.Fail(t, "%T is not a pointer or a slice", c.Struct)
}

if _, err := ua.Decode(c.Bytes, v.Interface()); err != nil {
t.Fatal(err)
}
_, err := ua.Decode(c.Bytes, v.Interface())
require.NoError(t, err, "Decode failed")

// if v is a *[]x we need to dereference it before comparing it.
if typ.Kind() == reflect.Slice {
v = v.Elem()
}
require.Equal(t, c.Struct, v.Interface())
require.Equal(t, c.Struct, v.Interface(), "Decoded payload not equal")
})

t.Run("encode", func(t *testing.T) {
b, err := ua.Encode(c.Struct)
if err != nil {
t.Fatal(err)
}
require.Equal(t, c.Bytes, b)
require.NoError(t, err, "Encode failed")
require.Equal(t, c.Bytes, b, "Encoded payload not equal")
})
})
}
Expand Down
15 changes: 6 additions & 9 deletions uasc/codec_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,26 +39,23 @@ func RunCodecTest(t *testing.T, cases []CodecTestCase) {
case reflect.Slice:
v = reflect.New(typ) // typ: []x, v: *[]x
default:
t.Fatalf("%T is not a pointer or a slice", c.Struct)
require.Fail(t, "%T is not a pointer or a slice", c.Struct)
}

if _, err := ua.Decode(c.Bytes, v.Interface()); err != nil {
t.Fatal(err)
}
_, err := ua.Decode(c.Bytes, v.Interface())
require.NoError(t, err, "Decode failed")

// if v is a *[]x we need to dereference it before comparing it.
if typ.Kind() == reflect.Slice {
v = v.Elem()
}
require.Equal(t, c.Struct, v.Interface())
require.Equal(t, c.Struct, v.Interface(), "Decoded payload not equal")
})

t.Run("encode", func(t *testing.T) {
b, err := ua.Encode(c.Struct)
if err != nil {
t.Fatal(err)
}
require.Equal(t, c.Bytes, b)
require.NoError(t, err, "Encode failed")
require.Equal(t, c.Bytes, b, "Encoded payload not equal")
})
})
}
Expand Down

0 comments on commit e616d78

Please sign in to comment.