Repository: gavv/httpexpect
Pull request #317 (2023-02-28 22:23:53)

Unit test TestFormatter_FailureErrors
PR for #264 

TestFormatter FailureErrors cases
- [x] nil Errors slice
- [x] non-nil empty Errors slice
- [x] Errors slice with nil error
- [x] Errors slice with typed nil error
- [x] Errors slice with one error
- [x] Errors slice with multiple errors

coveralls: 
[![Coverage Status](https://coveralls.io/builds/57393540/badge)](https://coveralls.io/builds/57393540)

Coverage: 95.181%. Remained the same when pulling **74511238aff7130c2a733a3f803bac903f6e9df6 on Darrellbor:264-test_formatter_unit_test** into **9e6c89d0652998d2535a1614b26d7ffde823fa53 on gavv:master**.

Darrellbor: @gavv ready for review
gavv: Minor fix: https://github.com/gavv/httpexpect/pull/317/commits/fdbca5563f05a4e4f4d3d754da4a590d85e2f75c

Patch:
diff --git a/formatter.go b/formatter.go
index 8584bc7..aaf53dc 100644
--- a/formatter.go
+++ b/formatter.go
@@ -244,6 +244,8 @@ func (f *DefaultFormatter) fillDescription(
 func (f *DefaultFormatter) fillErrors(
 	data *FormatData, ctx *AssertionContext, failure *AssertionFailure,
 ) {
+	data.Errors = []string{}
+
 	for _, err := range failure.Errors {
 		if refIsNil(err) {
 			continue


Test patch:
diff --git a/assertion_test.go b/assertion_test.go
index b8c7957..24920e6 100644
--- a/assertion_test.go
+++ b/assertion_test.go
@@ -8,12 +8,6 @@ import (
 	"github.com/stretchr/testify/require"
 )
 
-type typedErrorNil int
-
-func (*typedErrorNil) Error() string {
-	return ""
-}
-
 func TestAssertion_Handler(t *testing.T) {
 	type test struct {
 		formatter *mockFormatter
@@ -376,11 +370,11 @@ func TestAssertion_ValidateTraits(t *testing.T) {
 }
 
 func TestAssertion_ValidateAssertion(t *testing.T) {
-	var tnil *typedErrorNil
-	var tnilPtr error = tnil
+	var mErr *mockError
+	var mErrPtr error = mErr
 
-	assert.Nil(t, tnilPtr)
-	assert.NotEqual(t, nil, tnilPtr)
+	assert.Nil(t, mErrPtr)
+	assert.NotEqual(t, nil, mErrPtr)
 
 	tests := []struct {
 		testName          string
@@ -426,7 +420,7 @@ func TestAssertion_ValidateAssertion(t *testing.T) {
 			errorContainsText: "Errors",
 			input: AssertionFailure{
 				Type:   AssertOperation,
-				Errors: []error{tnilPtr},
+				Errors: []error{mErrPtr},
 			},
 		},
 		{
diff --git a/formatter_test.go b/formatter_test.go
index 5f83540..46df0b3 100644
--- a/formatter_test.go
+++ b/formatter_test.go
@@ -472,6 +472,70 @@ func TestFormatter_FailureDelta(t *testing.T) {
 	}
 }
 
+func TestFormatter_FailureErrors(t *testing.T) {
+	var mErr *mockError
+	var mErrPtr error = mErr
+
+	assert.Nil(t, mErrPtr)
+	assert.NotEqual(t, nil, mErrPtr)
+
+	cases := []struct {
+		name     string
+		errors   []error
+		expected []string
+	}{
+		{
+			name:     "nil errors slice",
+			errors:   nil,
+			expected: []string{},
+		},
+		{
+			name:     "empty errors slice",
+			errors:   []error{},
+			expected: []string{},
+		},
+		{
+			name:     "errors slice with nil error",
+			errors:   []error{nil},
+			expected: []string{},
+		},
+		{
+			name:     "errors slice with typed nil error",
+			errors:   []error{mErrPtr},
+			expected: []string{},
+		},
+		{
+			name:     "errors slice with one error",
+			errors:   []error{fmt.Errorf("error message")},
+			expected: []string{"error message"},
+		},
+		{
+			name: "errors slice with multiple errors",
+			errors: []error{
+				fmt.Errorf("error message 1"),
+				fmt.Errorf("error message 2"),
+			},
+			expected: []string{
+				"error message 1",
+				"error message 2",
+			},
+		},
+	}
+
+	df := &DefaultFormatter{}
+	ctx := &AssertionContext{}
+
+	for _, tc := range cases {
+		t.Run(tc.name, func(t *testing.T) {
+			fl := &AssertionFailure{
+				Errors: tc.errors,
+			}
+			fd := df.buildFormatData(ctx, fl)
+			assert.Equal(t, tc.expected, fd.Errors)
+		})
+	}
+}
+
 func TestFormatter_FloatFormat(t *testing.T) {
 	type testCase struct {
 		name     string
diff --git a/mocks_test.go b/mocks_test.go
index 2a2b4f8..0231cd6 100644
--- a/mocks_test.go
+++ b/mocks_test.go
@@ -403,3 +403,9 @@ func (e *mockNetError) Timeout() bool {
 func (e *mockNetError) Temporary() bool {
 	return e.isTemporary
 }
+
+type mockError struct{}
+
+func (e *mockError) Error() string {
+	return ""
+}