Merge pull request #74 from obeattie/master

Print the expected and actual items of an Equal call on separate lines
pull/78/head
Mat Ryer 2014-08-17 13:22:06 -06:00
commit 653e88ba55
2 changed files with 48 additions and 5 deletions

View File

@ -1,6 +1,8 @@
package assert
import (
"bufio"
"bytes"
"fmt"
"reflect"
"runtime"
@ -109,15 +111,48 @@ func messageFromMsgAndArgs(msgAndArgs ...interface{}) string {
return ""
}
// Indents all lines of the message by appending a number of tabs to each line, in an output format compatible with Go's
// test printing (see inner comment for specifics)
func indentMessageLines(message string, tabs int) string {
outBuf := new(bytes.Buffer)
for i, scanner := 0, bufio.NewScanner(strings.NewReader(message)); scanner.Scan(); i++ {
if i != 0 {
outBuf.WriteRune('\n')
}
for ii := 0; ii < tabs; ii++ {
outBuf.WriteRune('\t')
// Bizarrely, all lines except the first need one fewer tabs prepended, so deliberately advance the counter
// by 1 prematurely.
if ii == 0 && i > 0 {
ii++
}
}
outBuf.WriteString(scanner.Text())
}
return outBuf.String()
}
// Fail reports a failure through
func Fail(t TestingT, failureMessage string, msgAndArgs ...interface{}) bool {
message := messageFromMsgAndArgs(msgAndArgs...)
if len(message) > 0 {
t.Errorf("\r%s\r\tLocation:\t%s\n\r\tError:\t\t%s\n\r\tMessages:\t%s\n\r", getWhitespaceString(), CallerInfo(), failureMessage, message)
t.Errorf("\r%s\r\tLocation:\t%s\n"+
"\r\tError:%s\n"+
"\r\tMessages:\t%s\n\r",
getWhitespaceString(),
CallerInfo(),
indentMessageLines(failureMessage, 2),
message)
} else {
t.Errorf("\r%s\r\tLocation:\t%s\n\r\tError:\t\t%s\n\r", getWhitespaceString(), CallerInfo(), failureMessage)
t.Errorf("\r%s\r\tLocation:\t%s\n"+
"\r\tError:%s\n\r",
getWhitespaceString(),
CallerInfo(),
indentMessageLines(failureMessage, 2))
}
return false
@ -156,7 +191,8 @@ func IsType(t TestingT, expectedType interface{}, object interface{}, msgAndArgs
func Equal(t TestingT, expected, actual interface{}, msgAndArgs ...interface{}) bool {
if !ObjectsAreEqual(expected, actual) {
return Fail(t, fmt.Sprintf("Not equal: %#v != %#v", expected, actual), msgAndArgs...)
return Fail(t, fmt.Sprintf("Not equal: %#v (expected)\n"+
" != %#v (actual)", expected, actual), msgAndArgs...)
}
return true

View File

@ -116,7 +116,7 @@ func (s *SuiteLoggingTester) TestLoggingPass() {
func (s *SuiteLoggingTester) TestLoggingFail() {
s.T().Log("TESTLOGFAIL")
assert.NotNil(s.T(), nil) //expected to fail
assert.NotNil(s.T(), nil) // expected to fail
}
type StdoutCapture struct {
@ -154,6 +154,13 @@ func TestSuiteLogging(t *testing.T) {
assert.Nil(t, err, "Got an error trying to capture stdout!")
// Failed tests' output is always printed
assert.Contains(t, output, "TESTLOGFAIL")
assert.NotContains(t, output, "TESTLOGPASS")
if testing.Verbose() {
// In verbose mode, output from successful tests is also printed
assert.Contains(t, output, "TESTLOGPASS")
} else {
assert.NotContains(t, output, "TESTLOGPASS")
}
}