diff --git a/is.go b/is.go index 0c4f7a5..dd13b15 100644 --- a/is.go +++ b/is.go @@ -67,19 +67,22 @@ type T interface { FailNow() } +// printer allows output to be supressed during testing +var printer = (func(string))( + func(s string) { + fmt.Print(decorate(s)) + }) + // i represents an implementation of interface I. type i struct { t T - fails []string l sync.Mutex relaxed bool } func (i *i) Log(args ...interface{}) { i.l.Lock() - fail := fmt.Sprint(args...) - i.fails = append(i.fails, fail) - fmt.Print(decorate(fail)) + printer(decorate(fmt.Sprint(args...))) i.l.Unlock() if !i.relaxed { i.t.FailNow() @@ -87,9 +90,7 @@ func (i *i) Log(args ...interface{}) { } func (i *i) Logf(format string, args ...interface{}) { i.l.Lock() - fail := fmt.Sprintf(format, args...) - i.fails = append(i.fails, fail) - fmt.Print(decorate(fail)) + printer(decorate(fmt.Sprintf(format, args...))) i.l.Unlock() if !i.relaxed { i.t.FailNow() @@ -239,11 +240,12 @@ func (i *i) NotEqual(a, b interface{}) { } func (i *i) Fail(args ...interface{}) { + args = append([]interface{}{"failed: "}, args...) i.Log(args...) } func (i *i) Failf(format string, args ...interface{}) { - i.Logf(format, args...) + i.Logf("failed: "+format, args...) } // Panic asserts that the specified function diff --git a/is_test.go b/is_test.go index c1a9f85..f8a9fcf 100644 --- a/is_test.go +++ b/is_test.go @@ -23,7 +23,20 @@ func (m *mockT) Failed() bool { return m.failed } +type fooStringer struct{} + +func (fooStringer) String() string { + return "foo" +} + +var printerOutput []string + +func voidPrinter(s string) { + printerOutput = append(printerOutput, strings.TrimSpace(s)) +} + func TestIs(t *testing.T) { + printer = voidPrinter for _, test := range []struct { N string @@ -138,7 +151,7 @@ func TestIs(t *testing.T) { F: func(is I) { is.NoErr(&customErr{}, &customErr{}, &customErr{}) }, - Fails: []string{"unexpected error: Oops"}, + Fails: []string{"unexpected error (0): Oops", "unexpected error (1): Oops", "unexpected error (2): Oops"}, }, { N: "NoErr(err1, err2, err3)", @@ -182,7 +195,7 @@ func TestIs(t *testing.T) { var err3 error is.Err(err1, err2, err3) }, - Fails: []string{"error expected"}, + Fails: []string{"error expected (0)", "error expected (1)", "error expected (2)"}, }, // OK { @@ -303,7 +316,7 @@ func TestIs(t *testing.T) { F: func(is I) { is.True(false, false) }, - Fails: []string{"true!=false"}, + Fails: []string{"expected true (0): false", "expected true (1): false"}, }, // is.False { @@ -316,7 +329,7 @@ func TestIs(t *testing.T) { F: func(is I) { is.False(true, true) }, - Fails: []string{"false!=true"}, + Fails: []string{"expected false (0): true", "expected false (1): true"}, }, // is.NotEqual @@ -359,6 +372,7 @@ func TestIs(t *testing.T) { tt := new(mockT) is := New(tt) + printerOutput = make([]string, 0) func() { defer func() { @@ -367,21 +381,18 @@ func TestIs(t *testing.T) { test.F(is) }() - if len(test.Fails) > 0 { - for n, fail := range test.Fails { - if !tt.Failed() { - t.Errorf("%s should fail", test.N) - } - if test.Fails[n] != fail { - t.Errorf("expected fail \"%s\" but was \"%s\".", test.Fails[n], fail) - } + for n, fail := range test.Fails { + if n >= len(printerOutput) { + t.Errorf("(%d) expected fail \"%s\"", n, fail) + } else if !strings.Contains(printerOutput[n], fail) { + t.Errorf("(%d) expected fail \"%s\" but was \"%s\"", n, fail, printerOutput[n]) } - } else { - if tt.Failed() { - t.Errorf("%s shouldn't fail but: %s", test.N, strings.Join(test.Fails, ", ")) + } + if n, l := len(test.Fails), len(printerOutput); n < l { + for ; n < l; n++ { + t.Errorf("(%d) unexpected fail \"%s\"", n, printerOutput[n]) } } - } }