forked from hybridgroup/gobot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathled_driver_test.go
79 lines (62 loc) · 1.88 KB
/
led_driver_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
package gpio
import (
"errors"
"testing"
"github.com/hybridgroup/gobot"
)
func initTestLedDriver(conn DigitalWriter) *LedDriver {
testAdaptorDigitalWrite = func() (err error) {
return nil
}
testAdaptorPwmWrite = func() (err error) {
return nil
}
return NewLedDriver(conn, "bot", "1")
}
func TestLedDriver(t *testing.T) {
var err interface{}
d := initTestLedDriver(newGpioTestAdaptor("adaptor"))
gobot.Assert(t, d.Name(), "bot")
gobot.Assert(t, d.Pin(), "1")
gobot.Assert(t, d.Connection().Name(), "adaptor")
testAdaptorDigitalWrite = func() (err error) {
return errors.New("write error")
}
testAdaptorPwmWrite = func() (err error) {
return errors.New("pwm error")
}
err = d.Command("Toggle")(nil)
gobot.Assert(t, err.(error), errors.New("write error"))
err = d.Command("On")(nil)
gobot.Assert(t, err.(error), errors.New("write error"))
err = d.Command("Off")(nil)
gobot.Assert(t, err.(error), errors.New("write error"))
err = d.Command("Brightness")(map[string]interface{}{"level": 100.0})
gobot.Assert(t, err.(error), errors.New("pwm error"))
}
func TestLedDriverStart(t *testing.T) {
d := initTestLedDriver(newGpioTestAdaptor("adaptor"))
gobot.Assert(t, len(d.Start()), 0)
}
func TestLedDriverHalt(t *testing.T) {
d := initTestLedDriver(newGpioTestAdaptor("adaptor"))
gobot.Assert(t, len(d.Halt()), 0)
}
func TestLedDriverToggle(t *testing.T) {
d := initTestLedDriver(newGpioTestAdaptor("adaptor"))
d.Off()
d.Toggle()
gobot.Assert(t, d.State(), true)
d.Toggle()
gobot.Assert(t, d.State(), false)
}
func TestLedDriverBrightness(t *testing.T) {
d := initTestLedDriver(&gpioTestDigitalWriter{})
gobot.Assert(t, d.Brightness(150), ErrPwmWriteUnsupported)
d = initTestLedDriver(newGpioTestAdaptor("adaptor"))
testAdaptorPwmWrite = func() (err error) {
err = errors.New("pwm error")
return
}
gobot.Assert(t, d.Brightness(150), errors.New("pwm error"))
}