Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cleaned up tests #5

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 10 additions & 8 deletions is.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,29 +67,30 @@ 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()
}
}
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()
Expand Down Expand Up @@ -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
Expand Down
43 changes: 27 additions & 16 deletions is_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)",
Expand Down Expand Up @@ -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
{
Expand Down Expand Up @@ -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
{
Expand All @@ -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
Expand Down Expand Up @@ -359,6 +372,7 @@ func TestIs(t *testing.T) {

tt := new(mockT)
is := New(tt)
printerOutput = make([]string, 0)

func() {
defer func() {
Expand All @@ -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])
}
}

}

}
Expand Down