-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathassert_test.go
69 lines (57 loc) · 1.29 KB
/
assert_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
package assert
import (
"errors"
"math"
"testing"
)
type Position struct {
X, Y, Theta float64
}
func (p Position) Move(distance float64) Position {
return Position{
X: p.X + math.Cos(p.Theta)*distance,
Y: p.Y + math.Sin(p.Theta)*distance,
Theta: p.Theta,
}
}
type Cat struct {
Position Position
isFed bool
}
func (cat *Cat) Jump(distance float64) {
cat.Position = cat.Position.Move(distance)
}
func (cat *Cat) Feed() {
cat.isFed = true
}
func (cat *Cat) Pet() error {
if !cat.isFed {
return errors.New("CLAW!")
}
return nil
}
func TestAssert(t *testing.T) {
assert := Assert(t)
cat1 := &Cat{}
cat2 := &Cat{}
// Cat1 and Cat2 start out as identical cats (same position, etc.)
assert.Equal(cat1, cat2)
// But they are not the SAME cat
assert.NotSame(cat1, cat2)
// The identity should hold
assert.Same(cat1, cat1)
// Cat1 gets bored.
cat1.Jump(3)
// Now they are in different positions, and therefore not equal
assert.Equal(cat1.Position, Position{3, 0, 0})
assert.NotEqual(cat1, cat2)
// Cats can only be pet after they have been fed
cat1.Feed()
assert.True(cat1.isFed, "Cat %v not fed", 1)
assert.False(cat2.isFed, "Cat %v is fed", 2)
// Cats can only be pet if they have been fed
err1 := cat1.Pet()
err2 := cat2.Pet()
assert.Nil(err1)
assert.NotNil(err2)
}