Skip to content

Commit 2480372

Browse files
committed
Colorize 'expected' and 'actual'
1 parent cf221cc commit 2480372

File tree

4 files changed

+167
-11
lines changed

4 files changed

+167
-11
lines changed

assert/assertion_forward.go

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,9 +125,20 @@ func (a *Assertions) Emptyf(object interface{}, msg string, args ...interface{})
125125
// referenced values (as opposed to the memory addresses). Function equality
126126
// cannot be determined and will always fail.
127127
func (a *Assertions) Equal(expected interface{}, actual interface{}, msgAndArgs ...interface{}) bool {
128+
t, c := Retrieve(a.t)
129+
130+
if t != nil {
131+
a.t = t
132+
}
133+
128134
if h, ok := a.t.(tHelper); ok {
129135
h.Helper()
130136
}
137+
138+
if c != nil {
139+
a.t = WrapColorize(t)
140+
}
141+
131142
return Equal(a.t, expected, actual, msgAndArgs...)
132143
}
133144

@@ -137,9 +148,20 @@ func (a *Assertions) Equal(expected interface{}, actual interface{}, msgAndArgs
137148
// actualObj, err := SomeFunction()
138149
// a.EqualError(err, expectedErrorString)
139150
func (a *Assertions) EqualError(theError error, errString string, msgAndArgs ...interface{}) bool {
151+
t, c := Retrieve(a.t)
152+
153+
if t != nil {
154+
a.t = t
155+
}
156+
140157
if h, ok := a.t.(tHelper); ok {
141158
h.Helper()
142159
}
160+
161+
if c != nil {
162+
a.t = WrapColorize(t)
163+
}
164+
143165
return EqualError(a.t, theError, errString, msgAndArgs...)
144166
}
145167

@@ -160,9 +182,20 @@ func (a *Assertions) EqualErrorf(theError error, errString string, msg string, a
160182
//
161183
// a.EqualValues(uint32(123), int32(123))
162184
func (a *Assertions) EqualValues(expected interface{}, actual interface{}, msgAndArgs ...interface{}) bool {
185+
t, c := Retrieve(a.t)
186+
187+
if t != nil {
188+
a.t = t
189+
}
190+
163191
if h, ok := a.t.(tHelper); ok {
164192
h.Helper()
165193
}
194+
195+
if c != nil {
196+
a.t = WrapColorize(t)
197+
}
198+
166199
return EqualValues(a.t, expected, actual, msgAndArgs...)
167200
}
168201

@@ -1312,9 +1345,20 @@ func (a *Assertions) Regexpf(rx interface{}, str interface{}, msg string, args .
13121345
// Both arguments must be pointer variables. Pointer variable sameness is
13131346
// determined based on the equality of both type and value.
13141347
func (a *Assertions) Same(expected interface{}, actual interface{}, msgAndArgs ...interface{}) bool {
1348+
t, c := Retrieve(a.t)
1349+
1350+
if t != nil {
1351+
a.t = t
1352+
}
1353+
13151354
if h, ok := a.t.(tHelper); ok {
13161355
h.Helper()
13171356
}
1357+
1358+
if c != nil {
1359+
a.t = WrapColorize(t)
1360+
}
1361+
13181362
return Same(a.t, expected, actual, msgAndArgs...)
13191363
}
13201364

assert/assertions.go

Lines changed: 44 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -242,6 +242,7 @@ func Fail(t TestingT, failureMessage string, msgAndArgs ...interface{}) bool {
242242
if h, ok := t.(tHelper); ok {
243243
h.Helper()
244244
}
245+
245246
content := []labeledContent{
246247
{"Error Trace", strings.Join(CallerInfo(), "\n\t\t\t")},
247248
{"Error", failureMessage},
@@ -332,6 +333,8 @@ func IsType(t TestingT, expectedType interface{}, object interface{}, msgAndArgs
332333
// referenced values (as opposed to the memory addresses). Function equality
333334
// cannot be determined and will always fail.
334335
func Equal(t TestingT, expected, actual interface{}, msgAndArgs ...interface{}) bool {
336+
t, c := Retrieve(t)
337+
335338
if h, ok := t.(tHelper); ok {
336339
h.Helper()
337340
}
@@ -343,9 +346,15 @@ func Equal(t TestingT, expected, actual interface{}, msgAndArgs ...interface{})
343346
if !ObjectsAreEqual(expected, actual) {
344347
diff := diff(expected, actual)
345348
expected, actual = formatUnequalValues(expected, actual)
346-
return Fail(t, fmt.Sprintf("Not equal: \n"+
349+
failureMessage := fmt.Sprintf("Not equal: \n"+
347350
"expected: %s\n"+
348-
"actual : %s%s", expected, actual, diff), msgAndArgs...)
351+
"actual : %s%s", expected, actual, diff)
352+
353+
if c != nil {
354+
failureMessage = c.Message(failureMessage)
355+
}
356+
357+
return Fail(t, failureMessage, msgAndArgs...)
349358
}
350359

351360
return true
@@ -372,14 +381,22 @@ func validateEqualArgs(expected, actual interface{}) error {
372381
// Both arguments must be pointer variables. Pointer variable sameness is
373382
// determined based on the equality of both type and value.
374383
func Same(t TestingT, expected, actual interface{}, msgAndArgs ...interface{}) bool {
384+
t, c := Retrieve(t)
385+
375386
if h, ok := t.(tHelper); ok {
376387
h.Helper()
377388
}
378389

379390
if !samePointers(expected, actual) {
380-
return Fail(t, fmt.Sprintf("Not same: \n"+
381-
"expected: %p %#v\n"+
382-
"actual : %p %#v", expected, expected, actual, actual), msgAndArgs...)
391+
failureMessage := "Not same: \n" +
392+
"expected: %p %#v\n" +
393+
"actual : %p %#v"
394+
395+
if c != nil {
396+
failureMessage = c.Message(failureMessage)
397+
}
398+
399+
return Fail(t, fmt.Sprintf(failureMessage, expected, expected, actual, actual), msgAndArgs...)
383400
}
384401

385402
return true
@@ -457,16 +474,24 @@ func truncatingFormat(data interface{}) string {
457474
//
458475
// assert.EqualValues(t, uint32(123), int32(123))
459476
func EqualValues(t TestingT, expected, actual interface{}, msgAndArgs ...interface{}) bool {
477+
t, c := Retrieve(t)
478+
460479
if h, ok := t.(tHelper); ok {
461480
h.Helper()
462481
}
463482

464483
if !ObjectsAreEqualValues(expected, actual) {
465484
diff := diff(expected, actual)
466485
expected, actual = formatUnequalValues(expected, actual)
467-
return Fail(t, fmt.Sprintf("Not equal: \n"+
468-
"expected: %s\n"+
469-
"actual : %s%s", expected, actual, diff), msgAndArgs...)
486+
failureMessage := "Not equal: \n" +
487+
"expected: %s\n" +
488+
"actual : %s%s"
489+
490+
if c != nil {
491+
failureMessage = c.Message(failureMessage)
492+
}
493+
494+
return Fail(t, fmt.Sprintf(failureMessage, expected, actual, diff), msgAndArgs...)
470495
}
471496

472497
return true
@@ -1358,6 +1383,8 @@ func Error(t TestingT, err error, msgAndArgs ...interface{}) bool {
13581383
// actualObj, err := SomeFunction()
13591384
// assert.EqualError(t, err, expectedErrorString)
13601385
func EqualError(t TestingT, theError error, errString string, msgAndArgs ...interface{}) bool {
1386+
t, c := Retrieve(t)
1387+
13611388
if h, ok := t.(tHelper); ok {
13621389
h.Helper()
13631390
}
@@ -1368,9 +1395,15 @@ func EqualError(t TestingT, theError error, errString string, msgAndArgs ...inte
13681395
actual := theError.Error()
13691396
// don't need to use deep equals here, we know they are both strings
13701397
if expected != actual {
1371-
return Fail(t, fmt.Sprintf("Error message not equal:\n"+
1372-
"expected: %q\n"+
1373-
"actual : %q", expected, actual), msgAndArgs...)
1398+
failureMessage := "Error message not equal:\n" +
1399+
"expected: %q\n" +
1400+
"actual : %q"
1401+
1402+
if c != nil {
1403+
failureMessage = c.Message(failureMessage)
1404+
}
1405+
1406+
return Fail(t, fmt.Sprintf(failureMessage, expected, actual), msgAndArgs...)
13741407
}
13751408
return true
13761409
}

assert/colorize.go

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package assert
2+
3+
import (
4+
"regexp"
5+
)
6+
7+
// Colorize struct to colorize failure message
8+
type Colorize struct {
9+
t TestingT
10+
}
11+
12+
// Errorf implements for TestingT interface
13+
func (c Colorize) Errorf(format string, args ...interface{}) {
14+
c.t.Errorf(format, args...)
15+
}
16+
17+
// Message implements colourize actual and expected at the very least
18+
func (Colorize) Message(failureMessage string) string {
19+
re := regexp.MustCompile(`(.*)\n(expected.*)\n(actual.*)`)
20+
21+
return string(
22+
re.ReplaceAll(
23+
[]byte(failureMessage),
24+
[]byte("\033[33m${1}\033[0m\n\033[32m${2}\033[0m\n\033[31m${3}\033[0m"),
25+
),
26+
)
27+
}
28+
29+
// WrapColorize wrappers around TestingT to Colorize
30+
func WrapColorize(t TestingT) Colorize {
31+
return Colorize{
32+
t: t,
33+
}
34+
}
35+
36+
// Retrieve is a helper function to retrieve field of TestingT and Colorize or return self and nil
37+
func Retrieve(t TestingT) (TestingT, *Colorize) {
38+
if c, ok := t.(Colorize); ok {
39+
return c.t, &c
40+
}
41+
42+
return t, nil
43+
}

assert/colorize_test.go

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package assert
2+
3+
import (
4+
// "errors"
5+
"fmt"
6+
"testing"
7+
)
8+
9+
// func TestColorize(t *testing.T) {
10+
// wrapT := WrapColorize(t)
11+
// assert := New(wrapT)
12+
13+
// Equal(wrapT, 1, 2)
14+
// Equal(wrapT, "A", "b")
15+
// Equal(t, 1, 2)
16+
// assert.Equal(3, 4)
17+
18+
// Same(wrapT, new(Colorize), new(Colorize))
19+
// Same(t, new(Colorize), new(Colorize))
20+
// assert.Same(new(Colorize), new(Colorize))
21+
22+
// EqualValues(wrapT, uint32(123), int32(124))
23+
// EqualValues(t, uint32(123), int32(124))
24+
// assert.EqualValues(uint32(123), int32(124))
25+
26+
// EqualError(wrapT, errors.New("foo"), "bar")
27+
// EqualError(t, errors.New("foo"), "bar")
28+
// assert.EqualError(errors.New("foo"), "bar")
29+
// }
30+
31+
func TestColorize_Message(t *testing.T) {
32+
failureMessage := fmt.Sprintf("Not equal: \n"+
33+
"expected: %s\n"+
34+
"actual : %s%s", "expected", "actual", "diff")
35+
fmt.Println(WrapColorize(t).Message(failureMessage))
36+
}

0 commit comments

Comments
 (0)