2019-11-23 03:52:29 +00:00
|
|
|
package test
|
2015-10-10 08:18:54 +00:00
|
|
|
|
|
|
|
import (
|
2022-01-15 00:57:59 +00:00
|
|
|
"bufio"
|
2017-09-22 19:58:12 +00:00
|
|
|
"bytes"
|
2015-10-10 08:18:54 +00:00
|
|
|
"fmt"
|
2017-09-20 23:40:33 +00:00
|
|
|
"reflect"
|
2015-10-10 08:18:54 +00:00
|
|
|
"testing"
|
2017-09-20 23:40:33 +00:00
|
|
|
|
2022-01-15 00:57:59 +00:00
|
|
|
"github.com/pkg/diff"
|
|
|
|
"github.com/pkg/diff/write"
|
2015-10-10 08:18:54 +00:00
|
|
|
)
|
|
|
|
|
2022-10-25 02:30:38 +00:00
|
|
|
func printDifference(t *testing.T, expectedValue interface{}, actualValue interface{}) {
|
|
|
|
opts := []write.Option{write.TerminalColor()}
|
|
|
|
var differenceBuffer bytes.Buffer
|
|
|
|
expectedString := fmt.Sprintf("%v", expectedValue)
|
|
|
|
actualString := fmt.Sprintf("%v", actualValue)
|
|
|
|
if err := diff.Text("expected", "actual", expectedString, actualString, bufio.NewWriter(&differenceBuffer), opts...); err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
} else {
|
|
|
|
t.Error(differenceBuffer.String())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-23 03:52:29 +00:00
|
|
|
func AssertResult(t *testing.T, expectedValue interface{}, actualValue interface{}) {
|
2018-05-04 15:47:08 +00:00
|
|
|
t.Helper()
|
2015-10-10 08:18:54 +00:00
|
|
|
if expectedValue != actualValue {
|
2022-10-25 02:30:38 +00:00
|
|
|
printDifference(t, expectedValue, actualValue)
|
2015-10-10 08:18:54 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-23 03:52:29 +00:00
|
|
|
func AssertResultComplex(t *testing.T, expectedValue interface{}, actualValue interface{}) {
|
2018-05-04 15:47:08 +00:00
|
|
|
t.Helper()
|
2017-09-20 23:40:33 +00:00
|
|
|
if !reflect.DeepEqual(expectedValue, actualValue) {
|
2022-10-25 02:30:38 +00:00
|
|
|
printDifference(t, expectedValue, actualValue)
|
2017-09-20 23:40:33 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-16 01:29:26 +00:00
|
|
|
func AssertResultComplexWithContext(t *testing.T, expectedValue interface{}, actualValue interface{}, context interface{}) {
|
|
|
|
t.Helper()
|
|
|
|
if !reflect.DeepEqual(expectedValue, actualValue) {
|
|
|
|
t.Error(context)
|
2022-10-25 02:30:38 +00:00
|
|
|
printDifference(t, expectedValue, actualValue)
|
2020-10-16 01:29:26 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-23 03:52:29 +00:00
|
|
|
func AssertResultWithContext(t *testing.T, expectedValue interface{}, actualValue interface{}, context interface{}) {
|
2018-05-04 15:47:08 +00:00
|
|
|
t.Helper()
|
2022-01-15 00:57:59 +00:00
|
|
|
opts := []write.Option{write.TerminalColor()}
|
2015-10-10 08:18:54 +00:00
|
|
|
if expectedValue != actualValue {
|
|
|
|
t.Error(context)
|
2022-01-15 00:57:59 +00:00
|
|
|
var differenceBuffer bytes.Buffer
|
|
|
|
if err := diff.Text("expected", "actual", expectedValue, actualValue, bufio.NewWriter(&differenceBuffer), opts...); err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
} else {
|
|
|
|
t.Error(differenceBuffer.String())
|
|
|
|
}
|
2015-10-10 08:18:54 +00:00
|
|
|
}
|
|
|
|
}
|