2014-04-27 19:34:16 -07:00
|
|
|
package gpio
|
|
|
|
|
|
|
|
import (
|
2014-12-16 13:42:48 -08:00
|
|
|
"errors"
|
2014-06-12 20:08:06 -07:00
|
|
|
"testing"
|
2014-07-09 18:19:58 -07:00
|
|
|
|
|
|
|
"github.com/hybridgroup/gobot"
|
2014-04-27 19:34:16 -07:00
|
|
|
)
|
|
|
|
|
2014-12-16 13:42:48 -08:00
|
|
|
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"))
|
|
|
|
|
2014-06-12 20:08:06 -07:00
|
|
|
}
|
|
|
|
|
2014-06-13 16:01:39 -07:00
|
|
|
func TestLedDriverStart(t *testing.T) {
|
2014-12-16 13:42:48 -08:00
|
|
|
d := initTestLedDriver(newGpioTestAdaptor("adaptor"))
|
2014-11-19 23:21:19 -08:00
|
|
|
gobot.Assert(t, len(d.Start()), 0)
|
2014-06-12 20:08:06 -07:00
|
|
|
}
|
|
|
|
|
2014-06-13 16:01:39 -07:00
|
|
|
func TestLedDriverHalt(t *testing.T) {
|
2014-12-16 13:42:48 -08:00
|
|
|
d := initTestLedDriver(newGpioTestAdaptor("adaptor"))
|
2014-11-19 23:21:19 -08:00
|
|
|
gobot.Assert(t, len(d.Halt()), 0)
|
2014-06-12 20:08:06 -07:00
|
|
|
}
|
|
|
|
|
2014-06-13 16:01:39 -07:00
|
|
|
func TestLedDriverToggle(t *testing.T) {
|
2014-12-16 13:42:48 -08:00
|
|
|
d := initTestLedDriver(newGpioTestAdaptor("adaptor"))
|
2014-06-13 16:01:39 -07:00
|
|
|
d.Off()
|
|
|
|
d.Toggle()
|
2014-09-27 11:45:52 -07:00
|
|
|
gobot.Assert(t, d.State(), true)
|
2014-06-13 16:01:39 -07:00
|
|
|
d.Toggle()
|
2014-09-27 11:45:52 -07:00
|
|
|
gobot.Assert(t, d.State(), false)
|
2014-06-12 20:08:06 -07:00
|
|
|
}
|
|
|
|
|
2014-06-13 16:01:39 -07:00
|
|
|
func TestLedDriverBrightness(t *testing.T) {
|
2014-12-16 13:42:48 -08:00
|
|
|
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"))
|
2014-06-12 20:08:06 -07:00
|
|
|
}
|