Skip to content

Conversation

@hcmlinj
Copy link

@hcmlinj hcmlinj commented Aug 20, 2020

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 hcmlinj force-pushed the colorize-expected-and-actual branch from 630cc70 to 2480372 Compare August 20, 2020 14:59
@hcmlinj
Copy link
Author

hcmlinj commented Sep 10, 2020

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

@alxn
Copy link
Contributor

alxn commented Jan 16, 2021

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

@hcmlinj
Copy link
Author

hcmlinj commented Jan 30, 2021

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

@alxn
Copy link
Contributor

alxn commented Jan 30, 2021

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.

@walles walles mentioned this pull request Nov 23, 2021
@dolmen dolmen added enhancement: colored output About adding colored output pkg-assert Change related to package testify/assert labels Mar 6, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement: colored output About adding colored output pkg-assert Change related to package testify/assert

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Colorize "expected" and "actual"

3 participants