From 09b5d291b5d12a305cefe263b6eb7d469840b532 Mon Sep 17 00:00:00 2001 From: LAD Date: Tue, 31 Dec 2019 14:54:14 -0500 Subject: [PATCH] add AssertException --- README.md | 12 +++- assertions/general_assert.go | 17 ++++++ assertions/general_assert_test.go | 61 ++++++++++++++----- assertions/slices_assert.go | 16 ++--- assertions/slices_assert_test.go | 98 +++++++++---------------------- 5 files changed, 110 insertions(+), 94 deletions(-) diff --git a/README.md b/README.md index c8d2907..3e923f1 100644 --- a/README.md +++ b/README.md @@ -1,3 +1,13 @@ [![Build Status](https://api.travis-ci.org/LuigiAndrea/test-helper.png?branch=master)](https://travis-ci.org/LuigiAndrea/test-helper) [![Build status](https://ci.appveyor.com/api/projects/status/cmqqw3f6374gr0wh?svg=true)](https://ci.appveyor.com/project/LuigiAndrea/test-helper) -[![Go Report Card](https://goreportcard.com/badge/github.com/LuigiAndrea/test-helper)](https://goreportcard.com/report/github.com/LuigiAndrea/test-helper) \ No newline at end of file +[![Go Report Card](https://goreportcard.com/badge/github.com/LuigiAndrea/test-helper)](https://goreportcard.com/report/github.com/LuigiAndrea/test-helper) + +# Go Test Helper + +## Install + +`go get github.com/LuigiAndrea/test-helper` + +## Usage + +See the [GO documentation](https://godoc.org/github.com/LuigiAndrea/test-helper) diff --git a/assertions/general_assert.go b/assertions/general_assert.go index 561dc21..618e97f 100644 --- a/assertions/general_assert.go +++ b/assertions/general_assert.go @@ -15,3 +15,20 @@ func AssertDeepEqual(expected interface{}, actual interface{}) error { return nil } + +//ExceptionError records an error when two exceptions are of different type +type ExceptionError struct { + ExpectedException, CurrentException interface{} +} + +func (e *ExceptionError) Error() string { + return m.ErrorMessage(e.ExpectedException, e.CurrentException) +} + +//AssertException Check if the exception fired is right +func AssertException(expectedException interface{}, currentException interface{}) error { + if res := reflect.TypeOf(expectedException) != reflect.TypeOf(currentException); res { + return &ExceptionError{ExpectedException: expectedException, CurrentException: currentException} + } + return nil +} diff --git a/assertions/general_assert_test.go b/assertions/general_assert_test.go index d195ad1..d2484f1 100644 --- a/assertions/general_assert_test.go +++ b/assertions/general_assert_test.go @@ -2,40 +2,73 @@ package assertions import ( "testing" + + m "github.com/LuigiAndrea/test-helper/messages" ) -type testDeepEqual struct { +type testEqual struct { expected, actual interface{} } func TestAssertDeepEqual(t *testing.T) { - tests := []testDeepEqual{ - testDeepEqual{expected: []int{1, 2}, actual: []int{1, 2}}, - testDeepEqual{expected: [][]int{{1, 2}, {3, 3, 2}}, actual: [][]int{{1, 2}, {3, 3, 2}}}, - testDeepEqual{expected: []string{"house", "apartment"}, actual: []string{"house", "apartment"}}, - testDeepEqual{expected: 4.3, actual: 4.3}, + tests := []testEqual{ + testEqual{expected: []int{1, 2}, actual: []int{1, 2}}, + testEqual{expected: [][]int{{1, 2}, {3, 3, 2}}, actual: [][]int{{1, 2}, {3, 3, 2}}}, + testEqual{expected: []string{"house", "apartment"}, actual: []string{"house", "apartment"}}, + testEqual{expected: 4.3, actual: 4.3}, } for i, test := range tests { if err := AssertDeepEqual(test.expected, test.actual); err != nil { - t.Errorf("%d - %s", i+1, err.Error()) + t.Errorf("Test %d - %s", i+1, err.Error()) } } } func TestAssertDeepEqualDifferentObjects(t *testing.T) { - tests := []testDeepEqual{ - testDeepEqual{expected: []int{1}, actual: []int{1, 2}}, - testDeepEqual{expected: [][]int{{1, 2}, {3, 3, 2}}, actual: [][]int{{1, 2}, {3, 1, 2}}}, - testDeepEqual{expected: []int{1, 2}, actual: []string{"1", "2"}}, - testDeepEqual{expected: 4.3, actual: 4.31}, + tests := []testEqual{ + testEqual{expected: []int{1}, actual: []int{1, 2}}, + testEqual{expected: [][]int{{1, 2}, {3, 3, 2}}, actual: [][]int{{1, 2}, {3, 1, 2}}}, + testEqual{expected: []int{1, 2}, actual: []string{"1", "2"}}, + testEqual{expected: 4.3, actual: 4.31}, } for i, test := range tests { if err := AssertDeepEqual(test.expected, test.actual); err == nil { - t.Errorf("%d - Expected Exception!", i+1) + t.Errorf("Test %d - Expected Exception!", i+1) + } + } +} + +func TestAssertException(t *testing.T) { + tests := []testEqual{ + testEqual{expected: &ValueError{}, actual: &ValueError{}}, + testEqual{expected: &LengthError{}, actual: &LengthError{}}, + } + + for i, test := range tests { + if err := AssertException(test.expected, test.actual); err != nil { + t.Errorf("Test %d - %s", i+1, err.Error()) + } + } +} + +func TestAssertDfferentException(t *testing.T) { + tests := []testEqual{ + testEqual{expected: &ValueError{}, actual: nil}, + testEqual{expected: &ValueError{}, actual: &LengthError{}}, + } + + for i, test := range tests { + if err := AssertException(test.expected, test.actual); err == nil { + t.Errorf("Test %d - Expected Exception!", i+1) + } else if err.Error() != m.ErrorMessage(test.expected, test.actual) { + t.Errorf("Test %d - Expected a different error message!", i+1) } else { - t.Log(err.Error()) + var ee *ExceptionError + if _, ok := err.(*ExceptionError); !ok { + t.Errorf("Test %d - %s", i+1, m.ErrorMessage(ee, err)) + } } } } diff --git a/assertions/slices_assert.go b/assertions/slices_assert.go index 3d2a62f..e3a12d1 100644 --- a/assertions/slices_assert.go +++ b/assertions/slices_assert.go @@ -41,12 +41,12 @@ func (e *LengthError) Error() string { //ValueError records an error when two elements have different values type ValueError struct { - x, y interface{} - pos int + X, Y interface{} + Pos int } func (e *ValueError) Error() string { - return fmt.Sprintf("%s at position %d", m.ErrorMessage(e.x, e.y), e.pos) + return fmt.Sprintf("%s at position %d", m.ErrorMessage(e.X, e.Y), e.Pos) } // StringSlicesMatch attaches the methods of CheckSlices to struct StringSlicesMatch @@ -66,7 +66,7 @@ func (s StringSlicesMatch) Size() int { return len(s.Expected) } // GetError displays an error message when the values at position i are different func (s StringSlicesMatch) GetError(i int) error { - return &ValueError{x: s.Expected[i], y: s.Actual[i], pos: i} + return &ValueError{X: s.Expected[i], Y: s.Actual[i], Pos: i} } // IntSlicesMatch attaches the methods of CheckSlices to struct IntSlicesMatch @@ -86,7 +86,7 @@ func (islice IntSlicesMatch) Size() int { return len(islice.Expected) } // GetError displays an error message when the values at position i are different func (islice IntSlicesMatch) GetError(i int) error { - return &ValueError{x: islice.Expected[i], y: islice.Actual[i], pos: i} + return &ValueError{X: islice.Expected[i], Y: islice.Actual[i], Pos: i} } //Float64SlicesMatch attaches the methods of CheckSlices to struct Float64SlicesMatch @@ -106,7 +106,7 @@ func (f Float64SlicesMatch) Size() int { return len(f.Expected) } // GetError displays an error message when the values at position i are different func (f Float64SlicesMatch) GetError(i int) error { - return &ValueError{x: f.Expected[i], y: f.Actual[i], pos: i} + return &ValueError{X: f.Expected[i], Y: f.Actual[i], Pos: i} } //ByteSlicesMatch attaches the methods of CheckSlices to struct ByteSlicesMatch @@ -126,7 +126,7 @@ func (b ByteSlicesMatch) Size() int { return len(b.Expected) } // GetError displays an error message when the values at position i are different func (b ByteSlicesMatch) GetError(i int) error { - return &ValueError{x: b.Expected[i], y: b.Actual[i], pos: i} + return &ValueError{X: b.Expected[i], Y: b.Actual[i], Pos: i} } //DataSlicesMatch attaches the methods of CheckSlices to struct DataSlicesMatch @@ -146,5 +146,5 @@ func (d DataSlicesMatch) Size() int { return len(d.Expected) } // GetError displays an error message when the values at position i are different func (d DataSlicesMatch) GetError(i int) error { - return &ValueError{x: d.Expected[i], y: d.Actual[i], pos: i} + return &ValueError{X: d.Expected[i], Y: d.Actual[i], Pos: i} } diff --git a/assertions/slices_assert_test.go b/assertions/slices_assert_test.go index a13c50c..73af631 100644 --- a/assertions/slices_assert_test.go +++ b/assertions/slices_assert_test.go @@ -38,12 +38,13 @@ func TestHelperStringSlices(t *testing.T) { for i, test := range tests { if err := AssertSlicesEqual(StringSlicesMatch{Expected: test.input1, Actual: test.input2}); err != nil { - t.Errorf("%d - %v", i+1, err.Error()) + t.Errorf("Test %d - %v", i+1, err.Error()) } } } func TestHelperDifferentElementsStringSlices(t *testing.T) { + var ve *ValueError tests := []testStringData{ testStringData{input1: []string{"-1", "1"}, input2: []string{"-1", "13"}}, testStringData{input1: []string{"1", "5", "7", "8"}, input2: []string{"1", "5", "3", "8"}}, @@ -52,17 +53,8 @@ func TestHelperDifferentElementsStringSlices(t *testing.T) { for i, test := range tests { err := AssertSlicesEqual(StringSlicesMatch{Expected: test.input1, Actual: test.input2}) - - if err == nil { - t.Errorf("%d - Expected Exception! Slices with different values", i+1) - } else { - t.Log(err.Error()) - } - - _, isValueError := err.(*ValueError) - - if !isValueError { - t.Errorf("%d - Expected a ValueError: %T", i+1, err) + if errExc := AssertException(ve, err); errExc != nil { + t.Errorf("Test %d - %v", i+1, errExc.Error()) } } } @@ -78,12 +70,13 @@ func TestHelperIntSlices(t *testing.T) { for i, test := range tests { if err := AssertSlicesEqual(IntSlicesMatch{Expected: test.input1, Actual: test.input2}); err != nil { - t.Errorf("%d - %v", i+1, err.Error()) + t.Errorf("Test %d - %v", i+1, err.Error()) } } } func TestHelperDifferentElementsIntSlices(t *testing.T) { + var ve *ValueError tests := []testIntData{ testIntData{input1: []int{-1, 3}, input2: []int{-1, 33}}, testIntData{input1: []int{1, 5, 7, 8}, input2: []int{1, 5, 3, 8}}, @@ -91,17 +84,8 @@ func TestHelperDifferentElementsIntSlices(t *testing.T) { for i, test := range tests { err := AssertSlicesEqual(IntSlicesMatch{Expected: test.input1, Actual: test.input2}) - - if err == nil { - t.Errorf("%d - Expected Exception! Slices with different values", i+1) - } else { - t.Log(err.Error()) - } - - _, isValueError := err.(*ValueError) - - if !isValueError { - t.Errorf("%d - Expected a ValueError: %T", i+1, err) + if errExc := AssertException(ve, err); errExc != nil { + t.Errorf("Test %d - %v", i+1, errExc.Error()) } } } @@ -119,13 +103,13 @@ func TestHelperFloat64Slices(t *testing.T) { for i, test := range tests { if err := AssertSlicesEqual(Float64SlicesMatch{Expected: test.input1, Actual: test.input2}); err != nil { - t.Errorf("%d - %v", i+1, err.Error()) + t.Errorf("Test %d - %v", i+1, err.Error()) } } } func TestHelperDifferentElementsFloatSlices(t *testing.T) { - + var ve *ValueError tests := []testFloatData{ testFloatData{input1: []float64{-1, 3}, input2: []float64{-1, 33}}, testFloatData{input1: []float64{1, 5, 7, 8}, input2: []float64{1, 5, 3, 8}}, @@ -133,19 +117,9 @@ func TestHelperDifferentElementsFloatSlices(t *testing.T) { for i, test := range tests { err := AssertSlicesEqual(Float64SlicesMatch{Expected: test.input1, Actual: test.input2}) - - if err == nil { - t.Errorf("%d - Expected Exception! Slices with different values", i+1) - } else { - t.Log(err.Error()) - } - - _, isValueError := err.(*ValueError) - - if !isValueError { - t.Errorf("%d - Expected a ValueError: %T", i+1, err) + if errExc := AssertException(ve, err); errExc != nil { + t.Errorf("Test %d - %v", i+1, errExc.Error()) } - } } @@ -162,12 +136,13 @@ func TestHelperByteSlices(t *testing.T) { for i, test := range tests { if err := AssertSlicesEqual(ByteSlicesMatch{Expected: test.input1, Actual: test.input2}); err != nil { - t.Errorf("%d - %v", i+1, err.Error()) + t.Errorf("Test %d - %v", i+1, err.Error()) } } } func TestHelperDifferentElementsByteSlices(t *testing.T) { + var ve *ValueError tests := []testByteData{ testByteData{input1: []byte{1, 3}, input2: []byte{1, 33}}, testByteData{input1: []byte{1, 5, 7, 8}, input2: []byte{1, 5, 3, 8}}, @@ -176,16 +151,8 @@ func TestHelperDifferentElementsByteSlices(t *testing.T) { for i, test := range tests { err := AssertSlicesEqual(ByteSlicesMatch{Expected: test.input1, Actual: test.input2}) - if err == nil { - t.Errorf("%d - Expected Exception! Slices with different values", i+1) - } else { - t.Log(err.Error()) - } - - _, isValueError := err.(*ValueError) - - if !isValueError { - t.Errorf("%d - Expected a ValueError: %T", i+1, err) + if errExc := AssertException(ve, err); errExc != nil { + t.Errorf("Test %d - %v", i+1, errExc.Error()) } } } @@ -200,13 +167,13 @@ func TestHelperDataSlices(t *testing.T) { for i, test := range tests { if err := AssertSlicesEqual(DataSlicesMatch{Expected: test.input1, Actual: test.input2}); err != nil { - t.Errorf("%d - %v", i+1, err.Error()) + t.Errorf("Test %d - %v", i+1, err.Error()) } } } func TestHelperDifferentElementsDataSlices(t *testing.T) { - + var ve *ValueError tests := []testData{ testData{input1: []interface{}{1, 3}, input2: []interface{}{1, 33}}, testData{input1: []interface{}{1, "5", 7, false}, input2: []interface{}{1, "5", 3, true}}, @@ -216,22 +183,16 @@ func TestHelperDifferentElementsDataSlices(t *testing.T) { for i, test := range tests { err := AssertSlicesEqual(DataSlicesMatch{Expected: test.input1, Actual: test.input2}) - if err == nil { - t.Errorf("%d - Expected Exception! Slices with different values", i+1) - } else { - t.Log(err.Error()) - } - - _, isValueError := err.(*ValueError) - - if !isValueError { - t.Errorf("%d - Expected a ValueError: %T", i+1, err) + if errExc := AssertException(ve, err); errExc != nil { + t.Errorf("Test %d - %v", i+1, errExc.Error()) + } else if len(err.Error()) == 0 { + t.Errorf("Test %d - Expected to receive an error message", i+1) } } } func TestHelperDifferentLengthSlices(t *testing.T) { - + var le *LengthError tests := []testData{ testData{input1: []interface{}{-1, 3}, input2: []interface{}{1, 3, 4}}, testData{input1: []interface{}{1, "5", 7, false}, input2: []interface{}{1, "5"}}, @@ -240,15 +201,10 @@ func TestHelperDifferentLengthSlices(t *testing.T) { for i, test := range tests { err := AssertSlicesEqual(DataSlicesMatch{Expected: test.input1, Actual: test.input2}) - if err == nil { - t.Errorf("%d - Expected Exception! Slices with different values", i+1) - } else { - t.Log(err.Error()) - } - _, isLengthError := err.(*LengthError) - - if !isLengthError { - t.Errorf("%d - Expected a LengthError: %T", i+1, err) + if errExc := AssertException(le, err); errExc != nil { + t.Errorf("Test %d - %v", i+1, errExc.Error()) + } else if len(err.Error()) == 0 { + t.Errorf("Test %d - Expected to receive an error message", i+1) } } }