-
Notifications
You must be signed in to change notification settings - Fork 0
/
thermostat_test.go
150 lines (137 loc) · 4.21 KB
/
thermostat_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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
package example
import "testing"
func TestThermostat(t *testing.T) {
cases := map[string][]TestThermostatOption{
"at startup all off": {
AssertAllOff()},
"when too cold, blow and heat": {
MakeItTooCold(), AssertHeating(),
},
"when too hot, blow and cool": {
MakeItTooHot(), AssertCooling()},
"when comfy, all off": {
MakeItComfy(), AssertAllOff(),
},
"when too cold then too hot, blow and cool": {
MakeItTooCold(), MakeItTooHot(), AssertCooling(),
},
"when too hot then too cold, blow and heat": {
MakeItTooHot(), MakeItTooCold(), AssertHeating(),
},
"when too hot then comfy, all off": {
MakeItTooHot(), MakeItComfy(), AssertAllOff(),
},
"when too cold then comfy, heater disengages, blower remains on five cycles": {
MakeItTooCold(), AssertHeating(),
MakeItComfy(), AssertBlowing(), // 1
MakeItComfy(), AssertBlowing(), // 2
MakeItComfy(), AssertBlowing(), // 3
MakeItComfy(), AssertBlowing(), // 4
MakeItComfy(), AssertBlowing(), // 5
MakeItComfy(), AssertAllOff(),
},
"when too hot then comfy then too hot, cooler rests three cycles in between": {
MakeItTooHot(), AssertCooling(),
MakeItComfy(), AssertAllOff(), // 1
MakeItTooHot(), AssertBlowing(), // 2
MakeItTooHot(), AssertBlowing(), // 3
MakeItTooHot(), AssertCooling(),
},
"cooler stays off if not needed after delay": {
MakeItTooHot(), AssertCooling(),
MakeItComfy(), AssertAllOff(),
MakeItTooHot(), AssertBlowing(),
MakeItTooHot(), AssertBlowing(),
MakeItComfy(), AssertAllOff(),
},
}
for name, options := range cases {
t.Run(name, func(t *testing.T) { _TestThermostat(t, options...) })
}
}
func MakeItComfy() TestThermostatOption { return func(f *ThermostatFixture) { f.RegulateAt(70) } }
func MakeItTooHot() TestThermostatOption { return func(f *ThermostatFixture) { f.RegulateAt(76) } }
func MakeItTooCold() TestThermostatOption { return func(f *ThermostatFixture) { f.RegulateAt(64) } }
func (this *ThermostatFixture) RegulateAt(temp int) {
this.gauge.temperature = temp
this.thermostat.Regulate()
}
func AssertBlowing() TestThermostatOption { return AssertHVAC("Bch") }
func AssertCooling() TestThermostatOption { return AssertHVAC("BCh") }
func AssertHeating() TestThermostatOption { return AssertHVAC("BcH") }
func AssertAllOff() TestThermostatOption { return AssertHVAC("bch") }
func AssertHVAC(expected string) TestThermostatOption {
return func(this *ThermostatFixture) {
actual := this.hvac.String()
if actual == expected {
return
}
this.Helper()
this.Errorf("\n"+
"Expected: %s\n"+
"Actual: %s",
expected, actual)
}
}
func _TestThermostat(t *testing.T, options ...TestThermostatOption) {
t.Helper()
gauge := NewFakeGauge()
hvac := NewFakeHVAC()
fixture := &ThermostatFixture{
T: t,
hvac: hvac,
gauge: gauge,
thermostat: NewThermostat(hvac, gauge),
}
for _, option := range options {
option(fixture)
}
}
type TestThermostatOption func(this *ThermostatFixture)
type ThermostatFixture struct {
*testing.T
hvac *FakeHVAC
gauge *FakeGauge
thermostat *Thermostat
}
/***********************************************************************/
type FakeGauge struct {
temperature int
}
func NewFakeGauge() *FakeGauge {
return &FakeGauge{}
}
func (this *FakeGauge) CurrentTemperature() int {
return this.temperature
}
/***********************************************************************/
type FakeHVAC struct {
blowing bool
cooling bool
heating bool
}
func NewFakeHVAC() *FakeHVAC {
return &FakeHVAC{
blowing: true,
cooling: true,
heating: true,
}
}
func (this *FakeHVAC) SetBlower(state bool) { this.blowing = state }
func (this *FakeHVAC) SetCooler(state bool) { this.cooling = state }
func (this *FakeHVAC) SetHeater(state bool) { this.heating = state }
func (this *FakeHVAC) IsBlowing() bool { return this.blowing }
func (this *FakeHVAC) IsCooling() bool { return this.cooling }
func (this *FakeHVAC) IsHeating() bool { return this.heating }
func (this *FakeHVAC) String() string {
return boolean(this.blowing, 'B') +
boolean(this.cooling, 'C') +
boolean(this.heating, 'H')
}
func boolean(value bool, upper rune) string {
if value {
return string(upper)
} else {
return string(upper + 'a' - 'A')
}
}