-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathlutron-spy_test.go
112 lines (96 loc) · 2.18 KB
/
lutron-spy_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
package main
import (
"bufio"
"fmt"
"github.com/stretchr/testify/assert"
"net/http"
"os"
"testing"
)
type MockTransport struct {
needFailNoResponder bool
}
func (m *MockTransport) RoundTrip(req *http.Request) (*http.Response, error) {
return &http.Response{}, nil
}
func TestMain(m *testing.M) {
cf, _ := os.Open("example-config.json")
parseConfig(cf)
client = &http.Client{Transport: &MockTransport{}}
os.Exit(m.Run())
}
// Test results with unpaired remote
func Example_spy_ur_on() {
tf, _ := os.Open("traces/FFA265_on.trace")
spy(tf)
// Output:
// serial: FFA265
// button: 2
//
// EOF reached.
}
func Example_spy_r1_on() {
tf, _ := os.Open("traces/AEB551_on.trace")
spy(tf)
// Output:
// serial: AEB551
// button: 2
// nickname: bedside
// PUT {"on":true} application/json http://192.168.1.1/api/blah/lights/2/state
//
// EOF reached.
}
func Example_spy_r1_up() {
tf, _ := os.Open("traces/AEB551_up.trace")
spy(tf)
// Output:
// serial: AEB551
// button: 5
// nickname: bedside
//
// EOF reached.
}
func Example_spy_r1_select() {
tf, _ := os.Open("traces/AEB551_select.trace")
spy(tf)
// Output:
// serial: AEB551
// button: 3
// nickname: bedside
//
// EOF reached.
}
func Test_parseLine_r1_on(t *testing.T) {
Helper_parseLine(t, "AEB551", "on")
}
func Test_parseLine_r2_on(t *testing.T) {
Helper_parseLine(t, "AFA592", "on")
}
func Test_parseLine_r1_off(t *testing.T) {
Helper_parseLine(t, "AEB551", "off")
}
func Test_parseLine_r1_up(t *testing.T) {
Helper_parseLine(t, "AEB551", "up")
}
func Test_parseLine_r1_down(t *testing.T) {
Helper_parseLine(t, "AEB551", "down")
}
func Test_parseLine_r1_select(t *testing.T) {
Helper_parseLine(t, "AEB551", "select")
}
func Helper_parseLine(t *testing.T, remote string, button string) {
fh, err := os.Open(fmt.Sprintf("traces/%s_%s.trace", remote, button))
if err != nil {
t.Error("Missing testing trace")
}
reader := bufio.NewReader(fh)
// First string is a button press
line, err := reader.ReadString('\n')
if err != nil {
t.Error("Corrupted testing trace")
}
serial, pressed, err := parseLine(line)
assert.Equal(t, remote, serial)
assert.Equal(t, button, BUTTONS[pressed])
assert.Nil(t, err)
}