Add tests

This commit is contained in:
Nise Void 2020-05-12 18:01:11 +02:00
parent 5f6f4dd047
commit 0cc5d27277
Signed by: NiseVoid
GPG Key ID: FBA14AC83EA602F3
2 changed files with 203 additions and 0 deletions

179
assert_test.go Normal file
View File

@ -0,0 +1,179 @@
package assert
import (
"testing"
"time"
)
// -----------------------------------------
// Slices
// -----------------------------------------
func TestSameElementsSlices(t *testing.T) {
assert := New(t)
ft, fa := newFakeT()
a, b := []int{1, 2}, []int{1, 2}
fa.SameElements(a, a)
assert.False(ft.GotError())
fa.SameElements(a, b)
assert.False(ft.GotError())
b = []int{2, 1}
fa.SameElements(b, a) // SameElements ignores order
assert.False(ft.GotError())
b = []int{1, 2, 3}
fa.SameElements(b, a)
assert.True(ft.GotError())
b = []int{2, 3}
fa.SameElements(b, a)
assert.True(ft.GotError())
}
func TestCmpSlices(t *testing.T) {
assert := New(t)
ft, fa := newFakeT()
a, b := []int{1, 2}, []int{1, 2}
fa.Cmp(a, a)
assert.False(ft.GotError())
fa.Cmp(a, b)
assert.False(ft.GotError())
b = []int{2, 1}
fa.Cmp(a, b) // Cmp does not accept different order
assert.True(ft.GotError())
b = []int{1, 2, 3}
fa.Cmp(a, b)
assert.True(ft.GotError())
b = []int{2, 3}
fa.Cmp(a, b)
assert.True(ft.GotError())
}
// -----------------------------------------
// Maps
// -----------------------------------------
func TestCmpMaps(t *testing.T) {
assert := New(t)
ft, fa := newFakeT()
a, b := map[int]int{1: 2, 3: 4}, map[int]int{1: 2, 3: 4}
fa.Cmp(a, a)
assert.False(ft.GotError())
fa.Cmp(a, b)
assert.False(ft.GotError())
b = map[int]int{1: 2, 3: 5}
fa.Cmp(a, b)
assert.True(ft.GotError())
b = map[int]int{1: 2, 3: 4, 5: 6}
fa.Cmp(a, b)
assert.True(ft.GotError())
b = map[int]int{1: 2}
fa.Cmp(a, b)
assert.True(ft.GotError())
}
// -----------------------------------------
// Pointers
// -----------------------------------------
func TestEqPointers(t *testing.T) {
assert := New(t)
ft, fa := newFakeT()
var a, b int
fa.Eq(&a, &a)
assert.False(ft.GotError())
fa.Eq(&a, &b)
assert.True(ft.GotError())
fa.Eq(&b, &a)
assert.True(ft.GotError())
}
func TestCmpPointers(t *testing.T) {
assert := New(t)
ft, fa := newFakeT()
type B struct {
Value int
}
type A struct {
B *B
}
fa.Cmp(A{B: &B{1}}, A{&B{1}})
assert.False(ft.GotError())
fa.Cmp(A{B: &B{1}}, A{&B{2}})
assert.True(ft.GotError())
fa.Cmp(A{B: &B{1}}, A{nil})
assert.True(ft.GotError())
}
// -----------------------------------------
// Timezones
// -----------------------------------------
func mustLoadLocation(zone string) *time.Location {
loc, err := time.LoadLocation(zone)
if err != nil {
panic(err)
}
return loc
}
var locAmsterdam = mustLoadLocation(`Europe/Amsterdam`)
var locTokyo = mustLoadLocation(`Asia/Tokyo`)
func TestEqTimezones(t *testing.T) {
assert := New(t)
ft, fa := newFakeT()
ti := time.Now()
ti, ti2 := ti.In(locAmsterdam), ti.In(locTokyo)
fa.Eq(ti, ti2)
assert.True(ft.GotError())
}
func TestCmpTimezones(t *testing.T) {
assert := New(t)
ft, fa := newFakeT()
type A struct {
Time time.Time
}
ti := time.Now()
ti2 := ti.In(time.UTC)
fa.Cmp(&A{ti}, &A{ti2})
assert.False(ft.GotError())
ti2 = ti.In(locAmsterdam)
fa.Cmp(&A{ti}, &A{ti2})
assert.False(ft.GotError())
ti = ti.In(locTokyo)
fa.Cmp(&A{ti}, &A{ti2})
assert.False(ft.GotError())
ti2 = ti2.Add(time.Second)
fa.Cmp(&A{ti}, &A{ti2})
assert.True(ft.GotError())
}

24
faket_test.go Normal file
View File

@ -0,0 +1,24 @@
package assert
type fakeT struct {
gotError bool
}
func newFakeT() (*fakeT, Assert) {
var ft fakeT
return &ft, New(&ft)
}
func (t *fakeT) Error(_ ...interface{}) {
t.gotError = true
}
func (_ *fakeT) Helper() {}
func (_ *fakeT) Cleanup(_ func()) {}
func (t *fakeT) GotError() bool {
r := t.gotError
t.gotError = false
return r
}