-
Notifications
You must be signed in to change notification settings - Fork 10
/
coil_test.go
129 lines (122 loc) · 2.94 KB
/
coil_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
package modbridge
import (
"testing"
mqtt "github.com/eclipse/paho.mqtt.golang"
"github.com/mhemeryck/modbridge/mocks"
"github.com/stretchr/testify/mock"
)
func TestCoilRising(t *testing.T) {
cases := []struct {
coil Coil
expected bool
}{
{
coil: Coil{current: true, previous: false},
expected: true,
},
{
coil: Coil{current: false, previous: true},
expected: false,
},
}
for _, testCase := range cases {
if testCase.coil.rising() != testCase.expected {
t.Fail()
}
}
}
func TestCoilFalling(t *testing.T) {
cases := []struct {
coil Coil
expected bool
}{
{
coil: Coil{current: false, previous: true},
expected: true,
},
{
coil: Coil{current: true, previous: false},
expected: false,
},
}
for _, testCase := range cases {
if testCase.coil.falling() != testCase.expected {
t.Fail()
}
}
}
func TestCoilUpdate(t *testing.T) {
table := []struct {
value bool
initialPrevious bool
initialCurrent bool
expectedPrevious bool
expectedCurrent bool
shouldPublish bool
switchType SwitchType
}{
// Update in case of true value NO
{
value: true,
initialPrevious: false,
initialCurrent: false,
expectedPrevious: false,
expectedCurrent: true,
shouldPublish: true,
switchType: NO,
},
// Update in case of a false value NC
{
value: false,
initialPrevious: true,
initialCurrent: true,
expectedPrevious: true,
expectedCurrent: false,
shouldPublish: true,
switchType: NC,
},
// No update in case of false value NO
{
value: false,
initialPrevious: true,
initialCurrent: true,
expectedPrevious: true,
expectedCurrent: false,
shouldPublish: false,
switchType: NO,
},
// No update in case of true value NC
{
value: true,
initialPrevious: false,
initialCurrent: false,
expectedPrevious: false,
expectedCurrent: true,
shouldPublish: false,
switchType: NC,
},
}
for _, testCase := range table {
// Set up the coil
coil := Coil{previous: testCase.initialPrevious, current: testCase.initialCurrent, switchType: testCase.switchType}
// Set up mqtt client (conditionally)
mqttClient := &mocks.MQTTClient{}
if testCase.shouldPublish {
mqttClient.On("Publish", mock.AnythingOfType("string"), byte(0), false, "trigger").Return(&mqtt.PublishToken{})
}
// Do the actual call
coil.Update(testCase.value, mqttClient)
// Check: conditionally check for mqtt client called
if testCase.shouldPublish {
mqttClient.AssertExpectations(t)
}
// Check previous value
if coil.previous != testCase.expectedPrevious {
t.Errorf("Expected previous %v but got %v\n", testCase.expectedPrevious, coil.previous)
}
// Check current value
if coil.current != testCase.expectedCurrent {
t.Errorf("Expected current %v but got %v\n", testCase.expectedCurrent, coil.current)
}
}
}