From 1a91abaab3de51c71d4ac3c4d70b4d8557ff4a83 Mon Sep 17 00:00:00 2001 From: Rens Rikkerink Date: Tue, 5 Mar 2019 21:56:23 +0100 Subject: [PATCH] Improved failing test error messages --- errors_test.go | 44 +++++++++++++++++++++++--------------------- full_test.go | 23 ++++++++++------------- primitives_test.go | 13 +++++-------- 3 files changed, 38 insertions(+), 42 deletions(-) diff --git a/errors_test.go b/errors_test.go index d097b47..5b8569b 100644 --- a/errors_test.go +++ b/errors_test.go @@ -10,7 +10,7 @@ import ( func TestReadPointer(t *testing.T) { defer func() { if recover() == nil { - t.FailNow() + t.Error("TestReadPointer should have panicked due to an invalid argument, it didn't") } }() @@ -21,18 +21,18 @@ func TestReadPointer(t *testing.T) { func TestUseInt(t *testing.T) { defer func() { if recover() == nil { - t.FailNow() + t.Error("TestUseInt should have panicked due to an unsupported type, it didn't") } }() var i int - _ = Unpack(nil, &i) // int is not supported + _ = Unpack(nil, &i) } func TestUseUint(t *testing.T) { defer func() { if recover() == nil { - t.FailNow() + t.Error("TestUseUint should have panicked due to an unsupported type, it didn't") } }() @@ -43,7 +43,7 @@ func TestUseUint(t *testing.T) { func TestUnsupportedType(t *testing.T) { defer func() { if recover() == nil { - t.FailNow() + t.Error("TestUseUint should have panicked due to an unsupported type, it didn't") } }() @@ -58,28 +58,28 @@ func TestVariableLengthOverflow(t *testing.T) { Data struct{} `ikea:"compress:9"` } if err := Unpack(overflow, &t1); err == nil { - t.FailNow() + t.Error("TestVariableLengthOverflow: Data blobs may not have a length larger than math.MaxInt32") } overflow.Reset() _ = Pack(overflow, uint32(math.MaxInt32+1)) var t2 map[string]struct{} if err := Unpack(overflow, &t2); err == nil { - t.FailNow() + t.Error("TestVariableLengthOverflow: Data blobs may not have a length larger than math.MaxInt32") } overflow.Reset() _ = Pack(overflow, uint32(math.MaxInt32+1)) var t3 []struct{} if err := Unpack(overflow, &t3); err == nil { - t.FailNow() + t.Error("TestVariableLengthOverflow: Data blobs may not have a length larger than math.MaxInt32") } overflow.Reset() _ = Pack(overflow, uint32(math.MaxInt32+1)) var t4 string if err := Unpack(overflow, &t4); err == nil { - t.FailNow() + t.Error("TestVariableLengthOverflow: Data blobs may not have a length larger than math.MaxInt32") } } @@ -88,7 +88,7 @@ func TestCompressionInitError(t *testing.T) { Data []byte `ikea:"compress:10"` }{make([]byte, 10)} if err := Pack(new(bytes.Buffer), &s1); err == nil { - t.Fail() + t.Error("TestCompressionInitError should have failed because of an illegal compression level") } s2 := struct { @@ -96,7 +96,7 @@ func TestCompressionInitError(t *testing.T) { }{make([]byte, 10)} defer func() { if recover() == nil { - t.FailNow() + t.Error("TestCompressionInitError should have failed because of an non-numerical compression level") } }() _ = Pack(new(bytes.Buffer), &s2) @@ -106,14 +106,14 @@ func TestInvalidUTF8(t *testing.T) { var invalid string b := bytes.NewBuffer([]byte{0x00, 0x00, 0x00, 0x01, 0xF1}) if Unpack(b, &invalid) == nil { - t.FailNow() + t.Error("TestInvalidUTF8 should have failed because of an invalid utf-8 string") } } -func TestPackFixedNil(t *testing.T) { +func TestFixedNilPointerPacking(t *testing.T) { defer func() { if recover() == nil { - t.FailNow() + t.Error("TestFixedNilPointerPacking should panic because of an attempt to write an uninitialized variable, it didn't") } }() @@ -123,10 +123,10 @@ func TestPackFixedNil(t *testing.T) { _ = Pack(new(bytes.Buffer), &s) } -func TestPackVariableNil(t *testing.T) { +func TestVariableNilPointerPacking(t *testing.T) { defer func() { if recover() == nil { - t.FailNow() + t.Error("TestVariableNilPointerPacking should panic because of an attempt to write an uninitialized variable, it didn't") } }() @@ -136,17 +136,17 @@ func TestPackVariableNil(t *testing.T) { _ = Pack(new(bytes.Buffer), &s) } -func TestLenFixedNil(t *testing.T) { +func TestFixedNilPointerLength(t *testing.T) { s := struct { A *uint32 }{} Len(&s) // Unlike all other nil values, this should succeed } -func TestLenVariableNil(t *testing.T) { +func TestVariableNilPointerLength(t *testing.T) { defer func() { if recover() == nil { - t.FailNow() + t.Error("TestVariableNilPointerLength should panic because of an attempt to write an uninitialized variable, it didn't") } }() @@ -165,7 +165,8 @@ func TestReadErrors(t *testing.T) { for err != nil { err = Unpack(e, tst) if err == nil && e.pass != len(testData) { - t.FailNow() + t.Error("TestReadErrors should have failed because of simulated IO errors, it didn't") + return } e.Reset() } @@ -179,7 +180,8 @@ func TestWriteErrors(t *testing.T) { for err != nil { err = Pack(e, source) if err == nil && e.pass != len(testData) { - t.FailNow() + t.Error("TestWriteErrors should have failed because of simulated IO errors, it didn't") + return } e.Reset() } diff --git a/full_test.go b/full_test.go index 934aa6e..cab2a1b 100644 --- a/full_test.go +++ b/full_test.go @@ -3,7 +3,6 @@ package ikea import ( "bytes" "encoding/hex" - "fmt" "io" "reflect" "strconv" @@ -22,8 +21,8 @@ func TestOutput(t *testing.T) { result := buf.Bytes() if len(result) != len(testData) { - fmt.Printf("Failing TestWrite, result \"%s\" length (%d) does not match test data length (%d)\n", hex.EncodeToString(result), len(result), len(testData)) - t.FailNow() + t.Errorf("Failing TestWrite, result \"%s\" length (%d) does not match test data length (%d)\n", hex.EncodeToString(result), len(result), len(testData)) + return } // In struct creation we've added 0x4242 padding to we can split out the map. @@ -33,29 +32,28 @@ func TestOutput(t *testing.T) { resultParts := strings.Split(hex.EncodeToString(result), "4242") if originalParts[0] != resultParts[0] { - fmt.Printf("Failing TestWrite, hex output \"%s\" does not match test data slice\n", hex.EncodeToString(result)) - t.FailNow() + t.Errorf("Failing TestWrite, hex output \"%s\" does not match test data slice\n", hex.EncodeToString(result)) + return } // Instead, we treat the map a bit differently, we put it back into a buffer buf.Reset() if data, err := hex.DecodeString(resultParts[1]); err != nil { - fmt.Printf("Failing TestWrite, hex output \"%s\" is not a valid hex string: %s\n", resultParts[1], err.Error()) - t.FailNow() + t.Errorf("Failing TestWrite, hex output \"%s\" is not a valid hex string: %s\n", resultParts[1], err.Error()) + return } else { buf.Write(data) } // Unpack it var test map[string]string if err := Unpack(buf, &test); err != nil { - fmt.Printf("Failing TestWrite, could not unpack map: %s\n", err.Error()) - t.FailNow() + t.Errorf("Failing TestWrite, could not unpack map: %s\n", err.Error()) + return } // And then compare it using DeepEqual if !reflect.DeepEqual(source.TestMap, test) { - fmt.Printf("Failing TestWrite, resulting map is not equal\n") - t.FailNow() + t.Errorf("Failing TestWrite, resulting map is not equal\n") } } @@ -99,8 +97,7 @@ func TestCompleteRead(t *testing.T) { func TestLen(t *testing.T) { if l := Len(source); l != len(testData) { - fmt.Printf("Failing TestLen, Len reported an incorrect value %d, should be %d", l, len(testData)) - t.FailNow() + t.Errorf("Failing TestLen, Len reported an incorrect value %d, should be %d", l, len(testData)) } } diff --git a/primitives_test.go b/primitives_test.go index 065344d..ca05a03 100644 --- a/primitives_test.go +++ b/primitives_test.go @@ -2,9 +2,7 @@ package ikea import ( "bytes" - "fmt" "math/rand" - "os" "reflect" "testing" ) @@ -85,19 +83,18 @@ func typeTest(t *testing.T, typ string, value, compare interface{}) { var b bytes.Buffer if err := Pack(&b, value); err != nil { - _, _ = fmt.Fprintf(os.Stderr, "Failing %s, could not write value: %s\n", typ, err.Error()) - t.FailNow() + t.Errorf("Failing %s, could not write value: %s\n", typ, err.Error()) + return } target := reflect.New(reflect.TypeOf(value).Elem()) if err := Unpack(&b, target.Interface()); err != nil { - _, _ = fmt.Fprintf(os.Stderr, "Failing %s, could not read value: %s\n", typ, err.Error()) - t.FailNow() + t.Errorf("Failing %s, could not read value: %s\n", typ, err.Error()) + return } dereference := target.Elem().Interface() if dereference != compare { - _, _ = fmt.Fprintf(os.Stderr, "Failing %s, %T value %+v does not match original %T %+v\n", typ, dereference, dereference, compare, compare) - t.FailNow() + t.Errorf("Failing %s, %T value %+v does not match original %T %+v\n", typ, dereference, dereference, compare, compare) } }