Skip to content

Commit

Permalink
Add tests for DigitalWrite value
Browse files Browse the repository at this point in the history
  • Loading branch information
stuartleeks committed Mar 31, 2020
1 parent 66af258 commit 558e981
Showing 1 changed file with 46 additions and 10 deletions.
56 changes: 46 additions & 10 deletions drivers/gpio/relay_driver_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,84 +13,120 @@ var _ gobot.Driver = (*RelayDriver)(nil)
// Helper to return low/high value for testing
func (l *RelayDriver) High() bool { return l.high }

func initTestRelayDriver() *RelayDriver {
func initTestRelayDriver() (*RelayDriver, *gpioTestAdaptor) {
a := newGpioTestAdaptor()
a.testAdaptorDigitalWrite = func(string, byte) (err error) {
return nil
}
a.testAdaptorPwmWrite = func(string, byte) (err error) {
return nil
}
return NewRelayDriver(a, "1")
return NewRelayDriver(a, "1"), a
}

func TestRelayDriverDefaultName(t *testing.T) {
g := initTestRelayDriver()
g, _ := initTestRelayDriver()
gobottest.Refute(t, g.Connection(), nil)
gobottest.Assert(t, strings.HasPrefix(g.Name(), "Relay"), true)
}

func TestRelayDriverSetName(t *testing.T) {
g := initTestRelayDriver()
g, _ := initTestRelayDriver()
g.SetName("mybot")
gobottest.Assert(t, g.Name(), "mybot")
}

func TestRelayDriverStart(t *testing.T) {
d := initTestRelayDriver()
d, _ := initTestRelayDriver()
gobottest.Assert(t, d.Start(), nil)
}

func TestRelayDriverHalt(t *testing.T) {
d := initTestRelayDriver()
d, _ := initTestRelayDriver()
gobottest.Assert(t, d.Halt(), nil)
}

func TestRelayDriverToggle(t *testing.T) {
d := initTestRelayDriver()
d, a := initTestRelayDriver()
var lastVal byte
a.TestAdaptorDigitalWrite(func(pin string, val byte) error {
lastVal = val
return nil
})

d.Off()
gobottest.Assert(t, d.State(), false)
gobottest.Assert(t, lastVal, byte(0))
d.Toggle()
gobottest.Assert(t, d.State(), true)
gobottest.Assert(t, lastVal, byte(1))
d.Toggle()
gobottest.Assert(t, d.State(), false)
gobottest.Assert(t, lastVal, byte(0))
}

func TestRelayDriverToggleInverted(t *testing.T) {
d := initTestRelayDriver()
d, a := initTestRelayDriver()
var lastVal byte
a.TestAdaptorDigitalWrite(func(pin string, val byte) error {
lastVal = val
return nil
})

d.Inverted = true
d.Off()
gobottest.Assert(t, d.State(), false)
gobottest.Assert(t, lastVal, byte(1))
d.Toggle()
gobottest.Assert(t, d.State(), true)
gobottest.Assert(t, lastVal, byte(0))
d.Toggle()
gobottest.Assert(t, d.State(), false)
gobottest.Assert(t, lastVal, byte(1))
}

func TestRelayDriverCommands(t *testing.T) {
d := initTestRelayDriver()
d, a := initTestRelayDriver()
var lastVal byte
a.TestAdaptorDigitalWrite(func(pin string, val byte) error {
lastVal = val
return nil
})

gobottest.Assert(t, d.Command("Off")(nil), nil)
gobottest.Assert(t, d.State(), false)
gobottest.Assert(t, lastVal, byte(0))

gobottest.Assert(t, d.Command("On")(nil), nil)
gobottest.Assert(t, d.State(), true)
gobottest.Assert(t, lastVal, byte(1))

gobottest.Assert(t, d.Command("Toggle")(nil), nil)
gobottest.Assert(t, d.State(), false)
gobottest.Assert(t, lastVal, byte(0))
}

func TestRelayDriverCommandsInverted(t *testing.T) {
d := initTestRelayDriver()
d, a := initTestRelayDriver()
var lastVal byte
a.TestAdaptorDigitalWrite(func(pin string, val byte) error {
lastVal = val
return nil
})
d.Inverted = true

gobottest.Assert(t, d.Command("Off")(nil), nil)
gobottest.Assert(t, d.High(), true)
gobottest.Assert(t, d.State(), false)
gobottest.Assert(t, lastVal, byte(1))

gobottest.Assert(t, d.Command("On")(nil), nil)
gobottest.Assert(t, d.High(), false)
gobottest.Assert(t, d.State(), true)
gobottest.Assert(t, lastVal, byte(0))

gobottest.Assert(t, d.Command("Toggle")(nil), nil)
gobottest.Assert(t, d.High(), true)
gobottest.Assert(t, d.State(), false)
gobottest.Assert(t, lastVal, byte(1))
}

0 comments on commit 558e981

Please sign in to comment.