2016-09-01 18:17:43 +08:00
|
|
|
package gpio
|
|
|
|
|
2017-03-21 18:38:18 +08:00
|
|
|
import (
|
|
|
|
"strings"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"gobot.io/x/gobot"
|
|
|
|
"gobot.io/x/gobot/gobottest"
|
|
|
|
)
|
2016-09-01 18:17:43 +08:00
|
|
|
|
|
|
|
var _ gobot.Driver = (*RelayDriver)(nil)
|
2017-03-21 18:38:18 +08:00
|
|
|
|
2017-04-02 23:15:45 +08:00
|
|
|
func initTestRelayDriver() *RelayDriver {
|
|
|
|
a := newGpioTestAdaptor()
|
|
|
|
a.testAdaptorDigitalWrite = func() (err error) {
|
2017-03-21 18:38:18 +08:00
|
|
|
return nil
|
|
|
|
}
|
2017-04-02 23:15:45 +08:00
|
|
|
a.testAdaptorPwmWrite = func() (err error) {
|
2017-03-21 18:38:18 +08:00
|
|
|
return nil
|
|
|
|
}
|
2017-04-02 23:15:45 +08:00
|
|
|
return NewRelayDriver(a, "1")
|
2017-03-21 18:38:18 +08:00
|
|
|
}
|
|
|
|
|
2017-03-21 19:15:54 +08:00
|
|
|
func TestRelayDriverDefaultName(t *testing.T) {
|
2017-04-02 23:15:45 +08:00
|
|
|
g := initTestRelayDriver()
|
2017-03-21 18:38:18 +08:00
|
|
|
gobottest.Refute(t, g.Connection(), nil)
|
|
|
|
gobottest.Assert(t, strings.HasPrefix(g.Name(), "Relay"), true)
|
|
|
|
}
|
|
|
|
|
2017-03-21 19:15:54 +08:00
|
|
|
func TestRelayDriverSetName(t *testing.T) {
|
2017-04-02 23:15:45 +08:00
|
|
|
g := initTestRelayDriver()
|
2017-03-21 19:15:54 +08:00
|
|
|
g.SetName("mybot")
|
|
|
|
gobottest.Assert(t, g.Name(), "mybot")
|
|
|
|
}
|
|
|
|
|
2017-03-21 18:38:18 +08:00
|
|
|
func TestRelayDriverStart(t *testing.T) {
|
2017-04-02 23:15:45 +08:00
|
|
|
d := initTestRelayDriver()
|
2017-03-21 18:38:18 +08:00
|
|
|
gobottest.Assert(t, d.Start(), nil)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestRelayDriverHalt(t *testing.T) {
|
2017-04-02 23:15:45 +08:00
|
|
|
d := initTestRelayDriver()
|
2017-03-21 18:38:18 +08:00
|
|
|
gobottest.Assert(t, d.Halt(), nil)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestRelayDriverToggle(t *testing.T) {
|
2017-04-02 23:15:45 +08:00
|
|
|
d := initTestRelayDriver()
|
2017-03-21 18:38:18 +08:00
|
|
|
d.Off()
|
|
|
|
d.Toggle()
|
|
|
|
gobottest.Assert(t, d.State(), true)
|
|
|
|
d.Toggle()
|
|
|
|
gobottest.Assert(t, d.State(), false)
|
|
|
|
}
|
2017-03-21 19:14:38 +08:00
|
|
|
|
|
|
|
func TestRelayDriverCommands(t *testing.T) {
|
2017-04-02 23:15:45 +08:00
|
|
|
d := initTestRelayDriver()
|
2017-03-21 19:14:38 +08:00
|
|
|
gobottest.Assert(t, d.Command("Off")(nil), nil)
|
|
|
|
gobottest.Assert(t, d.State(), false)
|
|
|
|
|
|
|
|
gobottest.Assert(t, d.Command("On")(nil), nil)
|
|
|
|
gobottest.Assert(t, d.State(), true)
|
|
|
|
|
|
|
|
gobottest.Assert(t, d.Command("Toggle")(nil), nil)
|
|
|
|
gobottest.Assert(t, d.State(), false)
|
|
|
|
}
|