-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlight_test.go
More file actions
93 lines (78 loc) · 1.61 KB
/
Copy pathlight_test.go
File metadata and controls
93 lines (78 loc) · 1.61 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
package light
import (
"bytes"
"io"
"os"
"testing"
"time"
"go.bug.st/serial"
)
func TestInfer(t *testing.T) {
cmd, err := Infer("green", "on")
if err != nil {
t.Fatal(err.Error())
}
if cmd != GREEN_ON {
t.Fatal("was not GREEN_ON")
}
}
func TestSend(t *testing.T) {
s := &stub{}
open = s.testOpen
err := Send("portName", GREEN_OFF, GREEN_ON)
if err != nil {
t.Fatal("expected err to be nil")
}
data := s.data.Bytes()
if command(data[0]) != GREEN_OFF {
t.Fatal("expected first command to be green off")
}
if command(data[1]) != GREEN_ON {
t.Fatal("expected second command to be green on")
}
}
func TestOff(t *testing.T) {
s := &stub{}
open = s.testOpen
err := Off("port")
if err != nil {
t.Fatal("expected err to be nil")
}
data := s.data.Bytes()
if len(data) != 4 {
t.Fatal("expected 4 elements")
}
needle := make([]byte, 1)
for _, cmd := range []command{GREEN_OFF, YELLOW_OFF, RED_OFF, BUZZ_OFF} {
needle[0] = byte(cmd)
if !bytes.Contains(data, needle) {
t.Fatal("expected to contain: ", needle)
}
}
}
func TestLight(t *testing.T) {
port := os.Getenv("GO_LIGHT_SERIAL_PORT")
if port == "" {
t.Skip("integ test missing GO_LIGHT_SERIAL_PORT envvar")
}
open = internalOpen
if err := Send(port, GREEN_ON); err != nil {
t.Fatal(err)
}
time.Sleep(1 * time.Second)
if err := Off(port); err != nil {
t.Fatal(err)
}
}
type stub struct {
data bytes.Buffer
}
func (s *stub) testOpen(_ string, _ *serial.Mode) (io.WriteCloser, error) {
return s, nil
}
func (s *stub) Write(p []byte) (int, error) {
return s.data.Write(p)
}
func (s *stub) Close() error {
return nil
}