testify icon indicating copy to clipboard operation
testify copied to clipboard

Colorize 'expected' and 'actual'

Open hcmlinj opened this issue 5 years ago • 4 comments

Summary

Colorize "expected" and "actual"

Changes

Resolves #946

Motivation

  1. @boyan-soubachov says "It's an interesting idea."

  2. @mvdkleijn says "I like this idea."

package testify

import (
	"errors"
	"testing"

	"github.com/stretchr/testify/assert"
)

func TestExamples(t *testing.T) {
	wrapT := assert.WrapColorize(t)
	newAssert := assert.New(wrapT)

	assert.Equal(wrapT, 1, 2)
	assert.Equal(wrapT, "A", "b")
	newAssert.Equal(3, 4)

	assert.Same(wrapT, new(assert.Colorize), new(assert.Colorize))
	newAssert.Same(new(assert.Colorize), new(assert.Colorize))

	assert.EqualValues(wrapT, uint32(123), int32(124))
	newAssert.EqualValues(uint32(123), int32(124))

	assert.EqualError(wrapT, errors.New("foo"), "bar")
	newAssert.EqualError(errors.New("foo"), "bar")
}

Related issues

N/A

Screenshot

TestColorize

TestColorize

Equal

Equal

Same

Same

EqualValues

EqualValues

EqualError

EqualError

hcmlinj avatar Aug 20 '20 14:08 hcmlinj

Dear @boyan-soubachov , do you mind review this PR or give some suggestion?

hcmlinj avatar Sep 10 '20 08:09 hcmlinj

Can you retest by redirecting to a file and see if you see control characters ?

alxn avatar Jan 16 '21 04:01 alxn

I think yes.

Please refer this unit test: assert/colorize_test.go#L31

func TestColorize_Message(t *testing.T) {
	failureMessage := fmt.Sprintf("Not equal: \n"+
		"expected: %s\n"+
		"actual  : %s%s", "expected", "actual", "diff")
	fmt.Println(WrapColorize(t).Message(failureMessage))
}

output: Screen Shot 2021-01-30 at 10 59 43 AM

hcmlinj avatar Jan 30 '21 03:01 hcmlinj

I think yes.

Please refer this unit test: assert/colorize_test.go#L31

func TestColorize_Message(t *testing.T) {
	failureMessage := fmt.Sprintf("Not equal: \n"+
		"expected: %s\n"+
		"actual  : %s%s", "expected", "actual", "diff")
	fmt.Println(WrapColorize(t).Message(failureMessage))
}

output: Screen Shot 2021-01-30 at 10 59 43 AM

I think that proves it fails - you can see the terminal escapes e.g. [31m and [0m.

alxn avatar Jan 30 '21 04:01 alxn