Repository: gavv/httpexpect
Pull request #218 (2022-12-31 21:10:01)

Update formatXXX to be methods of DefaultFormatter
Issue: (part of) https://github.com/gavv/httpexpect/issues/190

Tasks:

- [X] Update formatXXX to be methods of `DefaultFormatter`

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

Coverage: 95.43%. Remained the same when pulling **8016193661e6d65a48aa601efa7bf2ec6427e581 on neverbeenthisweeb:190-disable-scientific-fmt** into **fea393ee2257bb6e31c6f9c96839e5408201d426 on gavv:master**.

gavv: I think it makes sense to split refactoring (making format funcs to be methods) and adding new flag into two prs. It would be easie to review them separately. If you don't mind this approach, let me know when the first part is ready for review. 
gavv: Let's rename formatTemplate to something without format, to make it distinct from formatXxx methods. E.g. applyTemplate.
neverbeenthisweeb: got it, will do the change
neverbeenthisweeb: @gavv ready for review

Patch:
diff --git a/formatter.go b/formatter.go
index 838905c..e139816 100644
--- a/formatter.go
+++ b/formatter.go
@@ -63,10 +63,10 @@ type DefaultFormatter struct {
 // FormatSuccess implements Formatter.FormatSuccess.
 func (f *DefaultFormatter) FormatSuccess(ctx *AssertionContext) string {
 	if f.SuccessTemplate != "" {
-		return f.formatTemplate("SuccessTemplate",
+		return f.applyTemplate("SuccessTemplate",
 			f.SuccessTemplate, f.TemplateFuncs, ctx, nil)
 	} else {
-		return f.formatTemplate("SuccessTemplate",
+		return f.applyTemplate("SuccessTemplate",
 			defaultSuccessTemplate, defaultTemplateFuncs, ctx, nil)
 	}
 }
@@ -76,10 +76,10 @@ func (f *DefaultFormatter) FormatFailure(
 	ctx *AssertionContext, failure *AssertionFailure,
 ) string {
 	if f.FailureTemplate != "" {
-		return f.formatTemplate("FailureTemplate",
+		return f.applyTemplate("FailureTemplate",
 			f.FailureTemplate, f.TemplateFuncs, ctx, failure)
 	} else {
-		return f.formatTemplate("FailureTemplate",
+		return f.applyTemplate("FailureTemplate",
 			defaultFailureTemplate, defaultTemplateFuncs, ctx, failure)
 	}
 }
@@ -131,7 +131,7 @@ const (
 	kindValueList  = "values"
 )
 
-func (f *DefaultFormatter) formatTemplate(
+func (f *DefaultFormatter) applyTemplate(
 	templateName string,
 	templateString string,
 	templateFuncs template.FuncMap,
@@ -229,11 +229,11 @@ func (f *DefaultFormatter) fillActual(
 
 	case AssertType, AssertNotType:
 		data.HaveActual = true
-		data.Actual = formatTyped(failure.Actual.Value)
+		data.Actual = f.formatTyped(failure.Actual.Value)
 
 	default:
 		data.HaveActual = true
-		data.Actual = formatValue(failure.Actual.Value)
+		data.Actual = f.formatValue(failure.Actual.Value)
 	}
 }
 
@@ -253,11 +253,11 @@ func (f *DefaultFormatter) fillExpected(
 		data.HaveExpected = true
 		data.ExpectedKind = kindValue
 		data.Expected = []string{
-			formatValue(failure.Expected.Value),
+			f.formatValue(failure.Expected.Value),
 		}
 
 		if !f.DisableDiffs && failure.Actual != nil && failure.Expected != nil {
-			data.Diff, data.HaveDiff = formatDiff(
+			data.Diff, data.HaveDiff = f.formatDiff(
 				failure.Expected.Value, failure.Actual.Value)
 		}
 
@@ -265,33 +265,33 @@ func (f *DefaultFormatter) fillExpected(
 		data.HaveExpected = true
 		data.ExpectedKind = kindValue
 		data.Expected = []string{
-			formatValue(failure.Expected.Value),
+			f.formatValue(failure.Expected.Value),
 		}
 
 	case AssertInRange, AssertNotInRange:
 		data.HaveExpected = true
 		data.ExpectedKind = kindRange
-		data.Expected = formatRange(failure.Expected.Value)
+		data.Expected = f.formatRange(failure.Expected.Value)
 
 	case AssertMatchSchema, AssertNotMatchSchema:
 		data.HaveExpected = true
 		data.ExpectedKind = kindSchema
 		data.Expected = []string{
-			formatBareString(failure.Expected.Value),
+			f.formatBareString(failure.Expected.Value),
 		}
 
 	case AssertMatchPath, AssertNotMatchPath:
 		data.HaveExpected = true
 		data.ExpectedKind = kindPath
 		data.Expected = []string{
-			formatBareString(failure.Expected.Value),
+			f.formatBareString(failure.Expected.Value),
 		}
 
 	case AssertMatchRegexp, AssertNotMatchRegexp:
 		data.HaveExpected = true
 		data.ExpectedKind = kindRegexp
 		data.Expected = []string{
-			formatBareString(failure.Expected.Value),
+			f.formatBareString(failure.Expected.Value),
 		}
 
 	case AssertMatchFormat, AssertNotMatchFormat:
@@ -301,33 +301,33 @@ func (f *DefaultFormatter) fillExpected(
 		} else {
 			data.ExpectedKind = kindFormat
 		}
-		data.Expected = formatList(failure.Expected.Value)
+		data.Expected = f.formatList(failure.Expected.Value)
 
 	case AssertContainsKey, AssertNotContainsKey:
 		data.HaveExpected = true
 		data.ExpectedKind = kindKey
 		data.Expected = []string{
-			formatValue(failure.Expected.Value),
+			f.formatValue(failure.Expected.Value),
 		}
 
 	case AssertContainsElement, AssertNotContainsElement:
 		data.HaveExpected = true
 		data.ExpectedKind = kindElement
 		data.Expected = []string{
-			formatValue(failure.Expected.Value),
+			f.formatValue(failure.Expected.Value),
 		}
 
 	case AssertContainsSubset, AssertNotContainsSubset:
 		data.HaveExpected = true
 		data.ExpectedKind = kindSubset
 		data.Expected = []string{
-			formatValue(failure.Expected.Value),
+			f.formatValue(failure.Expected.Value),
 		}
 
 	case AssertBelongs, AssertNotBelongs:
 		data.HaveExpected = true
 		data.ExpectedKind = kindValueList
-		data.Expected = formatList(failure.Expected.Value)
+		data.Expected = f.formatList(failure.Expected.Value)
 	}
 }
 
@@ -384,25 +384,25 @@ func (f *DefaultFormatter) fillReference(
 	data *FormatData, ctx *AssertionContext, failure *AssertionFailure,
 ) {
 	data.HaveReference = true
-	data.Reference = formatValue(failure.Reference.Value)
+	data.Reference = f.formatValue(failure.Reference.Value)
 }
 
 func (f *DefaultFormatter) fillDelta(
 	data *FormatData, ctx *AssertionContext, failure *AssertionFailure,
 ) {
 	data.HaveDelta = true
-	data.Delta = formatValue(failure.Delta.Value)
+	data.Delta = f.formatValue(failure.Delta.Value)
 }
 
-func formatTyped(value interface{}) string {
+func (f *DefaultFormatter) formatTyped(value interface{}) string {
 	if isNumber(value) {
-		return fmt.Sprintf("%T(%v)", value, formatValue(value))
+		return fmt.Sprintf("%T(%v)", value, f.formatValue(value))
 	}
 
 	return fmt.Sprintf("%T(%#v)", value, value)
 }
 
-func formatValue(value interface{}) string {
+func (f *DefaultFormatter) formatValue(value interface{}) string {
 	if isNumber(value) {
 		return fmt.Sprintf("%v", value)
 	}
@@ -425,20 +425,20 @@ func formatValue(value interface{}) string {
 	return sq.Sdump(value)
 }
 
-func formatBareString(value interface{}) string {
+func (f *DefaultFormatter) formatBareString(value interface{}) string {
 	switch v := value.(type) {
 	case string:
 		return v
 	}
 
-	return formatValue(value)
+	return f.formatValue(value)
 }
 
-func formatRange(value interface{}) []string {
+func (f *DefaultFormatter) formatRange(value interface{}) []string {
 	if rng := exctractRange(value); rng != nil {
 		if isNumber(rng.Min) && isNumber(rng.Max) {
 			return []string{
-				fmt.Sprintf("[%v; %v]", formatValue(rng.Min), formatValue(rng.Max)),
+				fmt.Sprintf("[%v; %v]", f.formatValue(rng.Min), f.formatValue(rng.Max)),
 			}
 		} else {
 			return []string{
@@ -448,26 +448,26 @@ func formatRange(value interface{}) []string {
 		}
 	} else {
 		return []string{
-			formatValue(value),
+			f.formatValue(value),
 		}
 	}
 }
 
-func formatList(value interface{}) []string {
+func (f *DefaultFormatter) formatList(value interface{}) []string {
 	if lst := extractList(value); lst != nil {
 		s := make([]string, 0, len(*lst))
 		for _, e := range *lst {
-			s = append(s, formatValue(e))
+			s = append(s, f.formatValue(e))
 		}
 		return s
 	} else {
 		return []string{
-			formatValue(value),
+			f.formatValue(value),
 		}
 	}
 }
 
-func formatDiff(expected, actual interface{}) (string, bool) {
+func (f *DefaultFormatter) formatDiff(expected, actual interface{}) (string, bool) {
 	differ := gojsondiff.New()
 
 	var diff gojsondiff.Diff
@@ -495,9 +495,9 @@ func formatDiff(expected, actual interface{}) (string, bool) {
 	config := formatter.AsciiFormatterConfig{
 		ShowArrayIndex: true,
 	}
-	f := formatter.NewAsciiFormatter(expected, config)
+	fa := formatter.NewAsciiFormatter(expected, config)
 
-	str, err := f.Format(diff)
+	str, err := fa.Format(diff)
 	if err != nil {
 		return "", false
 	}


Test patch:
diff --git a/formatter_test.go b/formatter_test.go
index feaf7f8..8955649 100644
--- a/formatter_test.go
+++ b/formatter_test.go
@@ -9,6 +9,8 @@ import (
 	"github.com/stretchr/testify/assert"
 )
 
+var mockDefaultFormatter = &DefaultFormatter{}
+
 type typedNil int
 
 func (*typedNil) String() string {
@@ -45,39 +47,39 @@ func TestFormat_Values(t *testing.T) {
 	}
 
 	t.Run("formatTypes", func(t *testing.T) {
-		checkAll(t, formatTyped)
+		checkAll(t, mockDefaultFormatter.formatTyped)
 	})
 
 	t.Run("formatValue", func(t *testing.T) {
-		checkAll(t, formatValue)
+		checkAll(t, mockDefaultFormatter.formatValue)
 	})
 
 	t.Run("formatBareString", func(t *testing.T) {
-		checkAll(t, formatBareString)
+		checkAll(t, mockDefaultFormatter.formatBareString)
 	})
 
 	t.Run("formatRange", func(t *testing.T) {
 		checkAll(t, func(v interface{}) string {
-			return strings.Join(formatRange(v), "")
+			return strings.Join(mockDefaultFormatter.formatRange(v), "")
 		})
 	})
 
 	t.Run("formatList", func(t *testing.T) {
 		checkAll(t, func(v interface{}) string {
-			return strings.Join(formatList(v), "")
+			return strings.Join(mockDefaultFormatter.formatList(v), "")
 		})
 	})
 }
 
 func TestFormat_Diff(t *testing.T) {
 	checkOK := func(a, b interface{}) {
-		s, ok := formatDiff(a, b)
+		s, ok := mockDefaultFormatter.formatDiff(a, b)
 		assert.True(t, ok)
 		assert.NotEqual(t, "", s)
 	}
 
 	checkNotOK := func(a, b interface{}) {
-		s, ok := formatDiff(a, b)
+		s, ok := mockDefaultFormatter.formatDiff(a, b)
 		assert.False(t, ok)
 		assert.Equal(t, "", s)
 	}