forked from monatheoctocat/gobot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrgb_led_driver_test.go
100 lines (81 loc) · 2.39 KB
/
rgb_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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
package gpio
import (
"errors"
"strings"
"testing"
"gobot.io/x/gobot"
"gobot.io/x/gobot/gobottest"
)
var _ gobot.Driver = (*RgbLedDriver)(nil)
func initTestRgbLedDriver() *RgbLedDriver {
a := newGpioTestAdaptor()
a.testAdaptorDigitalWrite = func() (err error) {
return nil
}
a.testAdaptorPwmWrite = func() (err error) {
return nil
}
return NewRgbLedDriver(a, "1", "2", "3")
}
func TestRgbLedDriver(t *testing.T) {
var err interface{}
a := newGpioTestAdaptor()
d := NewRgbLedDriver(a, "1", "2", "3")
gobottest.Assert(t, d.Pin(), "r=1, g=2, b=3")
gobottest.Assert(t, d.RedPin(), "1")
gobottest.Assert(t, d.GreenPin(), "2")
gobottest.Assert(t, d.BluePin(), "3")
gobottest.Refute(t, d.Connection(), nil)
a.testAdaptorDigitalWrite = func() (err error) {
return errors.New("write error")
}
a.testAdaptorPwmWrite = func() (err error) {
return errors.New("pwm error")
}
err = d.Command("Toggle")(nil)
gobottest.Assert(t, err.(error), errors.New("pwm error"))
err = d.Command("On")(nil)
gobottest.Assert(t, err.(error), errors.New("pwm error"))
err = d.Command("Off")(nil)
gobottest.Assert(t, err.(error), errors.New("pwm error"))
err = d.Command("SetRGB")(map[string]interface{}{"r": 0xff, "g": 0xff, "b": 0xff})
gobottest.Assert(t, err.(error), errors.New("pwm error"))
}
func TestRgbLedDriverStart(t *testing.T) {
d := initTestRgbLedDriver()
gobottest.Assert(t, d.Start(), nil)
}
func TestRgbLedDriverHalt(t *testing.T) {
d := initTestRgbLedDriver()
gobottest.Assert(t, d.Halt(), nil)
}
func TestRgbLedDriverToggle(t *testing.T) {
d := initTestRgbLedDriver()
d.Off()
d.Toggle()
gobottest.Assert(t, d.State(), true)
d.Toggle()
gobottest.Assert(t, d.State(), false)
}
func TestRgbLedDriverSetLevel(t *testing.T) {
a := newGpioTestAdaptor()
d := NewRgbLedDriver(a, "1", "2", "3")
gobottest.Assert(t, d.SetLevel("1", 150), nil)
d = NewRgbLedDriver(a, "1", "2", "3")
a.testAdaptorPwmWrite = func() (err error) {
err = errors.New("pwm error")
return
}
gobottest.Assert(t, d.SetLevel("1", 150), errors.New("pwm error"))
}
func TestRgbLedDriverDefaultName(t *testing.T) {
a := newGpioTestAdaptor()
d := NewRgbLedDriver(a, "1", "2", "3")
gobottest.Assert(t, strings.HasPrefix(d.Name(), "RGB"), true)
}
func TestRgbLedDriverSetName(t *testing.T) {
a := newGpioTestAdaptor()
d := NewRgbLedDriver(a, "1", "2", "3")
d.SetName("mybot")
gobottest.Assert(t, d.Name(), "mybot")
}