Skip to content

Commit

Permalink
Improved failing test error messages
Browse files Browse the repository at this point in the history
  • Loading branch information
Rens Rikkerink authored and ikkerens committed Mar 5, 2019
1 parent 97a1db0 commit 1a91aba
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 42 deletions.
44 changes: 23 additions & 21 deletions errors_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}
}()

Expand All @@ -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")
}
}()

Expand All @@ -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")
}
}()

Expand All @@ -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")
}
}

Expand All @@ -88,15 +88,15 @@ 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 {
Data []byte `ikea:"compress:a"`
}{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)
Expand All @@ -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")
}
}()

Expand All @@ -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")
}
}()

Expand All @@ -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")
}
}()

Expand All @@ -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()
}
Expand All @@ -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()
}
Expand Down
23 changes: 10 additions & 13 deletions full_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package ikea
import (
"bytes"
"encoding/hex"
"fmt"
"io"
"reflect"
"strconv"
Expand All @@ -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.
Expand All @@ -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")
}
}

Expand Down Expand Up @@ -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))
}
}

Expand Down
13 changes: 5 additions & 8 deletions primitives_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,7 @@ package ikea

import (
"bytes"
"fmt"
"math/rand"
"os"
"reflect"
"testing"
)
Expand Down Expand Up @@ -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)
}
}

0 comments on commit 1a91aba

Please sign in to comment.