hybridgroup.gobot/drivers/gpio/led_driver_test.go

79 lines
1.8 KiB
Go
Raw Normal View History

2014-04-28 10:34:16 +08:00
package gpio
import (
2014-12-17 05:42:48 +08:00
"errors"
2014-06-13 11:08:06 +08:00
"testing"
2014-07-10 09:19:58 +08:00
"gobot.io/x/gobot/gobottest"
2014-04-28 10:34:16 +08:00
)
2014-12-17 05:42:48 +08:00
func initTestLedDriver(conn DigitalWriter) *LedDriver {
testAdaptorDigitalWrite = func() (err error) {
return nil
}
testAdaptorPwmWrite = func() (err error) {
return nil
}
return NewLedDriver(conn, "1")
2014-12-17 05:42:48 +08:00
}
func TestLedDriver(t *testing.T) {
var err interface{}
d := initTestLedDriver(newGpioTestAdaptor())
2014-12-17 05:42:48 +08:00
gobottest.Assert(t, d.Pin(), "1")
gobottest.Refute(t, d.Connection(), nil)
2014-12-17 05:42:48 +08:00
testAdaptorDigitalWrite = func() (err error) {
return errors.New("write error")
}
testAdaptorPwmWrite = func() (err error) {
return errors.New("pwm error")
}
err = d.Command("Toggle")(nil)
gobottest.Assert(t, err.(error), errors.New("write error"))
2014-12-17 05:42:48 +08:00
err = d.Command("On")(nil)
gobottest.Assert(t, err.(error), errors.New("write error"))
2014-12-17 05:42:48 +08:00
err = d.Command("Off")(nil)
gobottest.Assert(t, err.(error), errors.New("write error"))
2014-12-17 05:42:48 +08:00
err = d.Command("Brightness")(map[string]interface{}{"level": 100.0})
gobottest.Assert(t, err.(error), errors.New("pwm error"))
2014-12-17 05:42:48 +08:00
2014-06-13 11:08:06 +08:00
}
2014-06-14 07:01:39 +08:00
func TestLedDriverStart(t *testing.T) {
d := initTestLedDriver(newGpioTestAdaptor())
gobottest.Assert(t, d.Start(), nil)
2014-06-13 11:08:06 +08:00
}
2014-06-14 07:01:39 +08:00
func TestLedDriverHalt(t *testing.T) {
d := initTestLedDriver(newGpioTestAdaptor())
gobottest.Assert(t, d.Halt(), nil)
2014-06-13 11:08:06 +08:00
}
2014-06-14 07:01:39 +08:00
func TestLedDriverToggle(t *testing.T) {
d := initTestLedDriver(newGpioTestAdaptor())
2014-06-14 07:01:39 +08:00
d.Off()
d.Toggle()
gobottest.Assert(t, d.State(), true)
2014-06-14 07:01:39 +08:00
d.Toggle()
gobottest.Assert(t, d.State(), false)
2014-06-13 11:08:06 +08:00
}
2014-06-14 07:01:39 +08:00
func TestLedDriverBrightness(t *testing.T) {
2014-12-17 05:42:48 +08:00
d := initTestLedDriver(&gpioTestDigitalWriter{})
gobottest.Assert(t, d.Brightness(150), ErrPwmWriteUnsupported)
2014-12-17 05:42:48 +08:00
d = initTestLedDriver(newGpioTestAdaptor())
2014-12-17 05:42:48 +08:00
testAdaptorPwmWrite = func() (err error) {
err = errors.New("pwm error")
return
}
gobottest.Assert(t, d.Brightness(150), errors.New("pwm error"))
2014-06-13 11:08:06 +08:00
}