-
-
Notifications
You must be signed in to change notification settings - Fork 630
/
Copy pathutils.go
60 lines (53 loc) · 1.69 KB
/
utils.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
package test
import (
"bufio"
"bytes"
"fmt"
"reflect"
"testing"
"github.com/pkg/diff"
"github.com/pkg/diff/write"
)
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())
}
}
func AssertResult(t *testing.T, expectedValue interface{}, actualValue interface{}) {
t.Helper()
if expectedValue != actualValue {
printDifference(t, expectedValue, actualValue)
}
}
func AssertResultComplex(t *testing.T, expectedValue interface{}, actualValue interface{}) {
t.Helper()
if !reflect.DeepEqual(expectedValue, actualValue) {
printDifference(t, expectedValue, actualValue)
}
}
func AssertResultComplexWithContext(t *testing.T, expectedValue interface{}, actualValue interface{}, context interface{}) {
t.Helper()
if !reflect.DeepEqual(expectedValue, actualValue) {
t.Error(context)
printDifference(t, expectedValue, actualValue)
}
}
func AssertResultWithContext(t *testing.T, expectedValue interface{}, actualValue interface{}, context interface{}) {
t.Helper()
opts := []write.Option{write.TerminalColor()}
if expectedValue != actualValue {
t.Error(context)
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())
}
}
}