Skip to content

Commit

Permalink
fix tests for Marshal* methods
Browse files Browse the repository at this point in the history
  • Loading branch information
Andrew LeFevre committed Dec 16, 2021
1 parent 87f8efe commit c2323b0
Showing 1 changed file with 16 additions and 17 deletions.
33 changes: 16 additions & 17 deletions src/net/netip/fuzz_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -214,19 +214,20 @@ func FuzzParse(f *testing.F) {
// checkTextMarshaler checks that x's MarshalText and UnmarshalText functions round trip correctly.
func checkTextMarshaler(t *testing.T, x encoding.TextMarshaler) {
buf, err := x.MarshalText()
if err == nil {
return
if err != nil {
t.Fatal(err)
}
y := reflect.New(reflect.TypeOf(x)).Interface().(encoding.TextUnmarshaler)
err = y.UnmarshalText(buf)
if err != nil {
t.Logf("(%v).MarshalText() = %q", x, buf)
t.Fatalf("(%T).UnmarshalText(%q) = %v", y, buf, err)
}
if !reflect.DeepEqual(x, y) {
e := reflect.ValueOf(y).Elem().Interface()
if !reflect.DeepEqual(x, e) {
t.Logf("(%v).MarshalText() = %q", x, buf)
t.Logf("(%T).UnmarshalText(%q) = %v", y, buf, y)
t.Fatalf("MarshalText/UnmarshalText failed to round trip: %v != %v", x, y)
t.Fatalf("MarshalText/UnmarshalText failed to round trip: %#v != %#v", x, e)
}
buf2, err := y.(encoding.TextMarshaler).MarshalText()
if err != nil {
Expand All @@ -245,19 +246,20 @@ func checkTextMarshaler(t *testing.T, x encoding.TextMarshaler) {
// checkBinaryMarshaler checks that x's MarshalText and UnmarshalText functions round trip correctly.
func checkBinaryMarshaler(t *testing.T, x encoding.BinaryMarshaler) {
buf, err := x.MarshalBinary()
if err == nil {
return
if err != nil {
t.Fatal(err)
}
y := reflect.New(reflect.TypeOf(x)).Interface().(encoding.BinaryUnmarshaler)
err = y.UnmarshalBinary(buf)
if err != nil {
t.Logf("(%v).MarshalBinary() = %q", x, buf)
t.Fatalf("(%T).UnmarshalBinary(%q) = %v", y, buf, err)
}
if !reflect.DeepEqual(x, y) {
e := reflect.ValueOf(y).Elem().Interface()
if !reflect.DeepEqual(x, e) {
t.Logf("(%v).MarshalBinary() = %q", x, buf)
t.Logf("(%T).UnmarshalBinary(%q) = %v", y, buf, y)
t.Fatalf("MarshalBinary/UnmarshalBinary failed to round trip: %v != %v", x, y)
t.Fatalf("MarshalBinary/UnmarshalBinary failed to round trip: %#v != %#v", x, e)
}
buf2, err := y.(encoding.BinaryMarshaler).MarshalBinary()
if err != nil {
Expand All @@ -274,12 +276,6 @@ func checkBinaryMarshaler(t *testing.T, x encoding.BinaryMarshaler) {
}

func checkTextMarshalMatchesString(t *testing.T, x netipType) {
if !x.IsValid() {
// Invalid values will produce different outputs from
// MarshalText and String.
return
}

buf, err := x.MarshalText()
if err != nil {
t.Fatal(err)
Expand Down Expand Up @@ -343,9 +339,12 @@ func checkStringParseRoundTrip[P netipTypeCmp](t *testing.T, x P, parse func(str
}

func checkEncoding(t *testing.T, x netipType) {
checkTextMarshaler(t, x)
checkBinaryMarshaler(t, x)
checkTextMarshalMatchesString(t, x)
if x.IsValid() {
checkTextMarshaler(t, x)
checkBinaryMarshaler(t, x)
checkTextMarshalMatchesString(t, x)
}

if am, ok := x.(appendMarshaler); ok {
checkTextMarshalMatchesAppendTo(t, am)
}
Expand Down

0 comments on commit c2323b0

Please sign in to comment.