-
Notifications
You must be signed in to change notification settings - Fork 26
Expand file tree
/
Copy pathcheckers_test.go
More file actions
146 lines (129 loc) · 3.35 KB
/
Copy pathcheckers_test.go
File metadata and controls
146 lines (129 loc) · 3.35 KB
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
package gomeassistant
import (
"errors"
"testing"
"github.com/stretchr/testify/assert"
"saml.dev/gome-assistant/internal"
)
type MockState struct {
EqualsReturn bool
EqualsError bool
GetReturn EntityState
GetError bool
}
func (s MockState) AfterSunrise(_ ...DurationString) bool {
return true
}
func (s MockState) BeforeSunrise(_ ...DurationString) bool {
return true
}
func (s MockState) AfterSunset(_ ...DurationString) bool {
return true
}
func (s MockState) BeforeSunset(_ ...DurationString) bool {
return true
}
func (s MockState) Get(eid string) (EntityState, error) {
if s.GetError {
return EntityState{}, errors.New("some error")
}
return s.GetReturn, nil
}
func (s MockState) GetStates(eids []string) ([]string, error) {
if s.GetError {
return nil, errors.New("some error")
}
states := make([]string, len(eids))
for i := range eids {
states[i] = s.GetReturn.State
}
return states, nil
}
func (s MockState) ListEntities() ([]EntityState, error) {
return []EntityState{}, nil
}
func (s MockState) Equals(eid, state string) (bool, error) {
if s.EqualsError {
return false, errors.New("some error")
}
return s.EqualsReturn, nil
}
var runOnError = internal.EnabledDisabledInfo{
Entity: "eid",
State: "state",
RunOnError: true,
}
var dontRunOnError = internal.EnabledDisabledInfo{
Entity: "eid",
State: "state",
RunOnError: false,
}
func list(infos ...internal.EnabledDisabledInfo) []internal.EnabledDisabledInfo {
ret := []internal.EnabledDisabledInfo{}
ret = append(ret, infos...)
return ret
}
func TestEnabledEntity_StateEqual_Passes(t *testing.T) {
state := MockState{
EqualsReturn: true,
}
c := checkEnabledEntity(state, list(runOnError))
assert.False(t, c.fail, "should pass")
}
func TestEnabledEntity_StateNotEqual_Fails(t *testing.T) {
state := MockState{
EqualsReturn: false,
}
c := checkEnabledEntity(state, list(runOnError))
assert.True(t, c.fail, "should fail")
}
func TestEnabledEntity_NetworkError_DontRun_Fails(t *testing.T) {
state := MockState{
EqualsError: true,
}
c := checkEnabledEntity(state, list(dontRunOnError))
assert.True(t, c.fail, "should fail")
}
func TestEnabledEntity_NetworkError_StillRun_Passes(t *testing.T) {
state := MockState{
EqualsError: true,
}
c := checkEnabledEntity(state, list(runOnError))
assert.False(t, c.fail, "should fail")
}
func TestDisabledEntity_StateEqual_Fails(t *testing.T) {
state := MockState{
EqualsReturn: true,
}
c := checkDisabledEntity(state, list(runOnError))
assert.True(t, c.fail, "should pass")
}
func TestDisabledEntity_StateNotEqual_Passes(t *testing.T) {
state := MockState{
EqualsReturn: false,
}
c := checkDisabledEntity(state, list(runOnError))
assert.False(t, c.fail, "should fail")
}
func TestDisabledEntity_NetworkError_DontRun_Fails(t *testing.T) {
state := MockState{
EqualsError: true,
}
c := checkDisabledEntity(state, list(dontRunOnError))
assert.True(t, c.fail, "should fail")
}
func TestDisabledEntity_NetworkError_StillRun_Passes(t *testing.T) {
state := MockState{
EqualsError: true,
}
c := checkDisabledEntity(state, list(runOnError))
assert.False(t, c.fail, "should fail")
}
func TestStatesMatch(t *testing.T) {
c := checkStatesMatch("hey", "hey")
assert.False(t, c.fail, "should pass")
}
func TestStatesDontMatch(t *testing.T) {
c := checkStatesMatch("hey", "bye")
assert.True(t, c.fail, "should fail")
}