-
Notifications
You must be signed in to change notification settings - Fork 0
/
door_test.go
120 lines (97 loc) · 3.71 KB
/
door_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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
package main
import (
"testing"
)
func TestCan(t *testing.T) {
var testTable = []struct {
initial DoorState
attempt DoorState
expected bool
}{
{OpenDoorState{}, OpenDoorState{}, false},
{OpenDoorState{}, ClosedDoorState{}, true},
{OpenDoorState{}, LockedDoorState{}, false},
{ClosedDoorState{}, OpenDoorState{}, true},
{ClosedDoorState{}, ClosedDoorState{}, false},
{ClosedDoorState{}, LockedDoorState{}, true},
{LockedDoorState{}, OpenDoorState{}, false},
{LockedDoorState{}, ClosedDoorState{}, true},
{LockedDoorState{}, LockedDoorState{}, false},
}
for _, tt := range testTable {
d := New(tt.initial)
if d.Can(tt.attempt) != tt.expected {
t.Errorf("Guard failure. Expected: %t got: %t", tt.expected, d.Can(tt.attempt))
}
}
}
func TestIs(t *testing.T) {
var testTable = []struct {
initial DoorState
fnIs func(d *Door) bool
expected bool
}{
{OpenDoorState{}, func(d *Door) bool { return d.IsOpen() }, true},
{OpenDoorState{}, func(d *Door) bool { return d.IsClosed() }, false},
{OpenDoorState{}, func(d *Door) bool { return d.IsLocked() }, false},
{ClosedDoorState{}, func(d *Door) bool { return d.IsOpen() }, false},
{ClosedDoorState{}, func(d *Door) bool { return d.IsClosed() }, true},
{ClosedDoorState{}, func(d *Door) bool { return d.IsLocked() }, false},
{LockedDoorState{}, func(d *Door) bool { return d.IsOpen() }, false},
{LockedDoorState{}, func(d *Door) bool { return d.IsClosed() }, false},
{LockedDoorState{}, func(d *Door) bool { return d.IsLocked() }, true},
}
for _, tt := range testTable {
d := New(tt.initial)
if tt.fnIs(d) != tt.expected {
t.Errorf("Guard failure. Expected: %t got: %t", tt.expected, tt.fnIs(d))
}
}
}
func TestTransitions(t *testing.T) {
var testTable = []struct {
initial DoorState
fnTransition func(d *Door) error
fnExpected error
}{
{OpenDoorState{}, func(d *Door) error { return d.Open() }, ErrIllegalStateTransition},
{OpenDoorState{}, func(d *Door) error { return d.Close() }, nil},
{OpenDoorState{}, func(d *Door) error { return d.Lock() }, ErrIllegalStateTransition},
{OpenDoorState{}, func(d *Door) error { return d.Unlock() }, ErrIllegalStateTransition},
{ClosedDoorState{}, func(d *Door) error { return d.Open() }, nil},
{ClosedDoorState{}, func(d *Door) error { return d.Close() }, ErrIllegalStateTransition},
{ClosedDoorState{}, func(d *Door) error { return d.Lock() }, nil},
{ClosedDoorState{}, func(d *Door) error { return d.Unlock() }, ErrIllegalStateTransition},
{LockedDoorState{}, func(d *Door) error { return d.Open() }, ErrIllegalStateTransition},
{LockedDoorState{}, func(d *Door) error { return d.Close() }, ErrIllegalStateTransition},
{LockedDoorState{}, func(d *Door) error { return d.Unlock() }, nil},
{LockedDoorState{}, func(d *Door) error { return d.Lock() }, ErrIllegalStateTransition},
}
for _, tt := range testTable {
d := New(tt.initial)
if err := tt.fnTransition(d); err != tt.fnExpected {
t.Errorf("Transition failure. Expected: %v got: %v", tt.fnExpected, err.Error())
}
}
}
func TestContextModification(t *testing.T) {
var testTable = []struct {
fnTransition func(d *Door)
expectedVisitorCount int
expectedLockCount int
}{
{func(d *Door) {d.Open()}, 1, 0},
{func(d *Door) {d.Close()}, 1, 0},
{func(d *Door) {d.Lock()}, 1, 1},
}
d := New(ClosedDoorState{})
for _, tt := range testTable {
tt.fnTransition(d)
if d.VisitorCount != tt.expectedVisitorCount {
t.Errorf("Context modification of VisitorCount failure. Expected: %v got: %v", tt.expectedVisitorCount, d.VisitorCount)
}
if d.LockCount != tt.expectedLockCount {
t.Errorf("Context modification of LockCount failure. Expected: %v got: %v", tt.expectedLockCount, d.LockCount)
}
}
}