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

Add test.Err and clean up tests #2

Merged
merged 1 commit into from
Aug 15, 2023
Merged
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
8 changes: 8 additions & 0 deletions test.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,14 @@ func Ok(t testing.TB, err error) {
}
}

// Err fails if err == nil.
func Err(t testing.TB, err error) {
t.Helper()
if err == nil {
t.Fatalf("Error was not nil:\t%v\n", err)
}
}

// True fails if v is false.
func True(t testing.TB, v bool) {
t.Helper()
Expand Down
198 changes: 94 additions & 104 deletions test_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,59 +45,52 @@ func TestPass(t *testing.T) {
}
}

shouldPass(func(tb testing.TB) { test.Equal(tb, "hello", "hello") })
shouldPass(func(tb testing.TB) { test.Equal(tb, 42, 42) })
shouldPass(func(tb testing.TB) { test.Equal(tb, true, true) })
shouldPass(func(tb testing.TB) { test.Equal(tb, 3.14, 3.14) })

shouldPass(func(tb testing.TB) { test.NotEqual(tb, "hello", "there") })
shouldPass(func(tb testing.TB) { test.NotEqual(tb, 42, 27) })
shouldPass(func(tb testing.TB) { test.NotEqual(tb, true, false) })
shouldPass(func(tb testing.TB) { test.NotEqual(tb, 3.14, 8.67) })

shouldPass(func(tb testing.TB) { test.Ok(tb, nil) })

shouldPass(func(tb testing.TB) { test.True(tb, true) })
shouldPass(func(tb testing.TB) { test.False(tb, false) })

shouldPass(func(tb testing.TB) {
test.EqualFunc(tb, "something", "equal", func(got, want string) bool { return true })
})

shouldPass(func(tb testing.TB) {
test.EqualFunc(tb, 42, 42, func(got, want int) bool { return true })
})

shouldPass(func(tb testing.TB) {
test.EqualFunc(tb, []string{"hello"}, []string{"hello"}, func(got, want []string) bool { return true })
})

shouldPass(func(tb testing.TB) {
test.NotEqualFunc(tb, "something", "different", func(got, want string) bool { return false })
})

shouldPass(func(tb testing.TB) {
test.NotEqualFunc(tb, 42, 12, func(got, want int) bool { return false })
})

shouldPass(func(tb testing.TB) {
test.NotEqualFunc(tb, []string{"hello"}, []string{"something", "else"}, func(got, want []string) bool { return false })
})

shouldPass(func(tb testing.TB) { test.Diff(tb, "hello", "hello") })
shouldPass(func(tb testing.TB) { test.Diff(tb, 42, 42) })
shouldPass(func(tb testing.TB) { test.Diff(tb, true, true) })
shouldPass(func(tb testing.TB) { test.Diff(tb, 3.14, 3.14) })
shouldPass(func(tb testing.TB) { test.Diff(tb, []string{"hello"}, []string{"hello"}) })

shouldPass(func(tb testing.TB) {
test.Diff(tb, struct{ name string }{name: "dave"}, struct{ name string }{name: "dave"})
})
shouldPass(func(tb testing.TB) {
test.Diff(tb, struct{ Name string }{Name: "dave"}, struct{ Name string }{Name: "dave"})
})

shouldPass(func(tb testing.TB) { test.DeepEqual(tb, []string{"hello"}, []string{"hello"}) })
// All functions that should not fail their test TB
passFns := []func(tb testing.TB){
func(tb testing.TB) { test.Equal(tb, "hello", "hello") },
func(tb testing.TB) { test.Equal(tb, "hello", "hello") },
func(tb testing.TB) { test.Equal(tb, 42, 42) },
func(tb testing.TB) { test.Equal(tb, true, true) },
func(tb testing.TB) { test.Equal(tb, 3.14, 3.14) },
func(tb testing.TB) { test.NotEqual(tb, "hello", "there") },
func(tb testing.TB) { test.NotEqual(tb, 42, 27) },
func(tb testing.TB) { test.NotEqual(tb, true, false) },
func(tb testing.TB) { test.NotEqual(tb, 3.14, 8.67) },
func(tb testing.TB) { test.Ok(tb, nil) },
func(tb testing.TB) { test.Err(tb, errors.New("uh oh")) },
func(tb testing.TB) { test.True(tb, true) },
func(tb testing.TB) { test.False(tb, false) },
func(tb testing.TB) { test.Diff(tb, 42, 42) },
func(tb testing.TB) { test.Diff(tb, true, true) },
func(tb testing.TB) { test.Diff(tb, "hello", "hello") },
func(tb testing.TB) { test.Diff(tb, 3.14, 3.14) },
func(tb testing.TB) { test.Diff(tb, []string{"hello"}, []string{"hello"}) },
func(tb testing.TB) {
test.EqualFunc(tb, "something", "equal", func(got, want string) bool { return true })
},
func(tb testing.TB) { test.EqualFunc(tb, 42, 42, func(got, want int) bool { return true }) },
func(tb testing.TB) {
test.EqualFunc(tb, []string{"hello"}, []string{"hello"}, func(got, want []string) bool { return true })
},
func(tb testing.TB) {
test.NotEqualFunc(tb, "something", "different", func(got, want string) bool { return false })
},
func(tb testing.TB) { test.NotEqualFunc(tb, 42, 12, func(got, want int) bool { return false }) },
func(tb testing.TB) {
test.NotEqualFunc(tb, []string{"hello"}, []string{"something", "else"}, func(got, want []string) bool { return false })
},
func(tb testing.TB) {
test.Diff(tb, struct{ name string }{name: "dave"}, struct{ name string }{name: "dave"})
},
func(tb testing.TB) {
test.Diff(tb, struct{ Name string }{Name: "dave"}, struct{ Name string }{Name: "dave"})
},
func(tb testing.TB) { test.DeepEqual(tb, []string{"hello"}, []string{"hello"}) },
}

for _, fn := range passFns {
shouldPass(fn)
}
}

func TestFail(t *testing.T) {
Expand All @@ -122,57 +115,54 @@ func TestFail(t *testing.T) {
}
}

shouldFail(func(tb testing.TB) { test.Equal(tb, "something", "else") })
shouldFail(func(tb testing.TB) { test.Equal(tb, 42, 27) })
shouldFail(func(tb testing.TB) { test.Equal(tb, true, false) })
shouldFail(func(tb testing.TB) { test.Equal(tb, 3.14, 8.96) })

shouldFail(func(tb testing.TB) { test.NotEqual(tb, "something", "something") })
shouldFail(func(tb testing.TB) { test.NotEqual(tb, 42, 42) })
shouldFail(func(tb testing.TB) { test.NotEqual(tb, true, true) })
shouldFail(func(tb testing.TB) { test.NotEqual(tb, 3.14, 3.14) })

shouldFail(func(tb testing.TB) { test.Ok(tb, errors.New("uh oh")) })

shouldFail(func(tb testing.TB) { test.True(tb, false) })
shouldFail(func(tb testing.TB) { test.False(tb, true) })

shouldFail(func(tb testing.TB) {
test.EqualFunc(tb, "something", "different", func(got, want string) bool { return false })
})

shouldFail(func(tb testing.TB) {
test.EqualFunc(tb, 42, 127, func(got, want int) bool { return false })
})

shouldFail(func(tb testing.TB) {
test.EqualFunc(tb, []int{42}, []int{27}, func(got, want []int) bool { return false })
})

shouldFail(func(tb testing.TB) {
test.NotEqualFunc(tb, "something", "something", func(got, want string) bool { return true })
})

shouldFail(func(tb testing.TB) {
test.NotEqualFunc(tb, 42, 42, func(got, want int) bool { return true })
})

shouldFail(func(tb testing.TB) {
test.NotEqualFunc(tb, []int{42}, []int{42}, func(got, want []int) bool { return true })
})

shouldFail(func(tb testing.TB) { test.Diff(tb, "hello", "there") })
shouldFail(func(tb testing.TB) { test.Diff(tb, 42, 27) })
shouldFail(func(tb testing.TB) { test.Diff(tb, true, false) })
shouldFail(func(tb testing.TB) { test.Diff(tb, 3.14, 8.69) })
shouldFail(func(tb testing.TB) { test.Diff(tb, []string{"hello"}, []string{"there"}) })
failFns := []func(tb testing.TB){
func(tb testing.TB) { test.Equal(tb, "something", "else") },
func(tb testing.TB) { test.Equal(tb, 42, 27) },
func(tb testing.TB) { test.Equal(tb, true, false) },
func(tb testing.TB) { test.Equal(tb, 3.14, 8.96) },
func(tb testing.TB) { test.NotEqual(tb, "something", "something") },
func(tb testing.TB) { test.NotEqual(tb, 42, 42) },
func(tb testing.TB) { test.NotEqual(tb, true, true) },
func(tb testing.TB) { test.NotEqual(tb, 3.14, 3.14) },
func(tb testing.TB) { test.Ok(tb, errors.New("uh oh")) },
func(tb testing.TB) { test.Err(tb, nilErr()) },
func(tb testing.TB) { test.True(tb, false) },
func(tb testing.TB) { test.False(tb, true) },
func(tb testing.TB) { test.Diff(tb, "hello", "there") },
func(tb testing.TB) { test.Diff(tb, 42, 27) },
func(tb testing.TB) { test.Diff(tb, true, false) },
func(tb testing.TB) { test.Diff(tb, 3.14, 8.69) },
func(tb testing.TB) { test.Diff(tb, []string{"hello"}, []string{"there"}) },
func(tb testing.TB) {
test.EqualFunc(tb, "something", "different", func(got, want string) bool { return false })
},
func(tb testing.TB) { test.EqualFunc(tb, 42, 127, func(got, want int) bool { return false }) },
func(tb testing.TB) {
test.EqualFunc(tb, []int{42}, []int{27}, func(got, want []int) bool { return false })
},
func(tb testing.TB) {
test.NotEqualFunc(tb, "something", "something", func(got, want string) bool { return true })
},
func(tb testing.TB) { test.NotEqualFunc(tb, 42, 42, func(got, want int) bool { return true }) },
func(tb testing.TB) {
test.NotEqualFunc(tb, []int{42}, []int{42}, func(got, want []int) bool { return true })
},
func(tb testing.TB) {
test.Diff(tb, struct{ name string }{name: "dave"}, struct{ name string }{name: "john"})
},
func(tb testing.TB) {
test.Diff(tb, struct{ Name string }{Name: "dave"}, struct{ Name string }{Name: "john"})
},
func(tb testing.TB) { test.DeepEqual(tb, []string{"hello"}, []string{"world"}) },
}

shouldFail(func(tb testing.TB) {
test.Diff(tb, struct{ name string }{name: "dave"}, struct{ name string }{name: "john"})
})
shouldFail(func(tb testing.TB) {
test.Diff(tb, struct{ Name string }{Name: "dave"}, struct{ Name string }{Name: "john"})
})
for _, fn := range failFns {
shouldFail(fn)
}
}

shouldFail(func(tb testing.TB) { test.DeepEqual(tb, []string{"hello"}, []string{"world"}) })
// Always returns a nil error, needed because manually constructing
// nil means it's not an error type but here it is.
func nilErr() error {
return nil
}