Skip to content

Commit

Permalink
add AssertException
Browse files Browse the repository at this point in the history
  • Loading branch information
LuigiAndrea committed Dec 31, 2019
1 parent 4899a4e commit 09b5d29
Show file tree
Hide file tree
Showing 5 changed files with 110 additions and 94 deletions.
12 changes: 11 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -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)
[![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)
17 changes: 17 additions & 0 deletions assertions/general_assert.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
61 changes: 47 additions & 14 deletions assertions/general_assert_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
}
}
}
}
16 changes: 8 additions & 8 deletions assertions/slices_assert.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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}
}
98 changes: 27 additions & 71 deletions assertions/slices_assert_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"}},
Expand All @@ -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())
}
}
}
Expand All @@ -78,30 +70,22 @@ 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}},
}

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())
}
}
}
Expand All @@ -119,33 +103,23 @@ 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}},
}

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())
}

}
}

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

0 comments on commit 09b5d29

Please sign in to comment.