1
0
mirror of https://github.com/hybridgroup/gobot.git synced 2025-05-02 22:17:12 +08:00
hybridgroup.gobot/drivers/gpio/direct_pin_driver_test.go

172 lines
4.7 KiB
Go
Raw Normal View History

2014-04-27 19:34:16 -07:00
package gpio
import (
2014-12-16 13:42:48 -08:00
"errors"
"strings"
2014-06-12 20:08:06 -07:00
"testing"
2014-09-27 11:45:52 -07:00
"gobot.io/x/gobot/v2"
"gobot.io/x/gobot/v2/gobottest"
)
var _ gobot.Driver = (*DirectPinDriver)(nil)
func initTestDirectPinDriver() *DirectPinDriver {
a := newGpioTestAdaptor()
a.testAdaptorDigitalRead = func(string) (val int, err error) {
2014-12-16 13:42:48 -08:00
val = 1
return
}
a.testAdaptorDigitalWrite = func(string, byte) (err error) {
2014-12-16 13:42:48 -08:00
return errors.New("write error")
}
a.testAdaptorPwmWrite = func(string, byte) (err error) {
2014-12-16 13:42:48 -08:00
return errors.New("write error")
}
a.testAdaptorServoWrite = func(string, byte) (err error) {
2014-12-16 13:42:48 -08:00
return errors.New("write error")
}
return NewDirectPinDriver(a, "1")
2014-12-16 13:42:48 -08:00
}
func TestDirectPinDriver(t *testing.T) {
var ret map[string]interface{}
var err interface{}
d := initTestDirectPinDriver()
gobottest.Assert(t, d.Pin(), "1")
gobottest.Refute(t, d.Connection(), nil)
2014-12-16 13:42:48 -08:00
ret = d.Command("DigitalRead")(nil).(map[string]interface{})
gobottest.Assert(t, ret["val"].(int), 1)
gobottest.Assert(t, ret["err"], nil)
2014-12-16 13:42:48 -08:00
err = d.Command("DigitalWrite")(map[string]interface{}{"level": "1"})
gobottest.Assert(t, err.(error), errors.New("write error"))
2014-12-16 13:42:48 -08:00
err = d.Command("PwmWrite")(map[string]interface{}{"level": "1"})
gobottest.Assert(t, err.(error), errors.New("write error"))
2014-12-16 13:42:48 -08:00
err = d.Command("ServoWrite")(map[string]interface{}{"level": "1"})
gobottest.Assert(t, err.(error), errors.New("write error"))
2014-06-12 20:08:06 -07:00
}
2014-06-13 16:01:39 -07:00
func TestDirectPinDriverStart(t *testing.T) {
d := initTestDirectPinDriver()
gobottest.Assert(t, d.Start(), nil)
2014-06-12 20:08:06 -07:00
}
2014-06-13 16:01:39 -07:00
func TestDirectPinDriverHalt(t *testing.T) {
d := initTestDirectPinDriver()
gobottest.Assert(t, d.Halt(), nil)
2014-06-12 20:08:06 -07:00
}
2016-06-14 08:42:50 -07:00
func TestDirectPinDriverOff(t *testing.T) {
d := initTestDirectPinDriver()
gobottest.Refute(t, d.Off(), nil)
2016-06-14 08:42:50 -07:00
a := newGpioTestAdaptor()
d = NewDirectPinDriver(a, "1")
gobottest.Assert(t, d.Off(), nil)
2016-06-14 08:42:50 -07:00
}
func TestDirectPinDriverOffNotSupported(t *testing.T) {
a := &gpioTestBareAdaptor{}
d := NewDirectPinDriver(a, "1")
gobottest.Assert(t, d.Off(), errors.New("DigitalWrite is not supported by this platform"))
}
2016-06-14 08:42:50 -07:00
func TestDirectPinDriverOn(t *testing.T) {
a := newGpioTestAdaptor()
d := NewDirectPinDriver(a, "1")
gobottest.Assert(t, d.On(), nil)
}
func TestDirectPinDriverOnError(t *testing.T) {
d := initTestDirectPinDriver()
gobottest.Refute(t, d.On(), nil)
}
2016-06-14 08:42:50 -07:00
func TestDirectPinDriverOnNotSupported(t *testing.T) {
a := &gpioTestBareAdaptor{}
d := NewDirectPinDriver(a, "1")
gobottest.Assert(t, d.On(), errors.New("DigitalWrite is not supported by this platform"))
2016-06-14 08:42:50 -07:00
}
2014-12-16 13:42:48 -08:00
func TestDirectPinDriverDigitalWrite(t *testing.T) {
adaptor := newGpioTestAdaptor()
d := NewDirectPinDriver(adaptor, "1")
gobottest.Assert(t, d.DigitalWrite(1), nil)
}
func TestDirectPinDriverDigitalWriteNotSupported(t *testing.T) {
a := &gpioTestBareAdaptor{}
d := NewDirectPinDriver(a, "1")
gobottest.Assert(t, d.DigitalWrite(1), errors.New("DigitalWrite is not supported by this platform"))
}
func TestDirectPinDriverDigitalWriteError(t *testing.T) {
d := initTestDirectPinDriver()
gobottest.Refute(t, d.DigitalWrite(1), nil)
2014-06-12 20:08:06 -07:00
}
2014-12-16 13:42:48 -08:00
func TestDirectPinDriverDigitalRead(t *testing.T) {
d := initTestDirectPinDriver()
2014-12-16 13:42:48 -08:00
ret, err := d.DigitalRead()
gobottest.Assert(t, ret, 1)
gobottest.Assert(t, err, nil)
2014-06-12 20:08:06 -07:00
}
func TestDirectPinDriverDigitalReadNotSupported(t *testing.T) {
a := &gpioTestBareAdaptor{}
d := NewDirectPinDriver(a, "1")
_, e := d.DigitalRead()
gobottest.Assert(t, e, errors.New("DigitalRead is not supported by this platform"))
}
2014-12-16 13:42:48 -08:00
func TestDirectPinDriverPwmWrite(t *testing.T) {
a := newGpioTestAdaptor()
d := NewDirectPinDriver(a, "1")
gobottest.Assert(t, d.PwmWrite(1), nil)
2014-06-12 20:08:06 -07:00
}
func TestDirectPinDriverPwmWriteNotSupported(t *testing.T) {
a := &gpioTestBareAdaptor{}
d := NewDirectPinDriver(a, "1")
gobottest.Assert(t, d.PwmWrite(1), errors.New("PwmWrite is not supported by this platform"))
}
func TestDirectPinDriverPwmWriteError(t *testing.T) {
d := initTestDirectPinDriver()
gobottest.Refute(t, d.PwmWrite(1), nil)
}
2014-06-12 20:08:06 -07:00
func TestDirectPinDriverServoWrite(t *testing.T) {
a := newGpioTestAdaptor()
d := NewDirectPinDriver(a, "1")
gobottest.Assert(t, d.ServoWrite(1), nil)
2014-06-12 20:08:06 -07:00
}
func TestDirectPinDriverServoWriteNotSupported(t *testing.T) {
a := &gpioTestBareAdaptor{}
d := NewDirectPinDriver(a, "1")
gobottest.Assert(t, d.ServoWrite(1), errors.New("ServoWrite is not supported by this platform"))
}
func TestDirectPinDriverServoWriteError(t *testing.T) {
d := initTestDirectPinDriver()
gobottest.Refute(t, d.ServoWrite(1), nil)
}
func TestDirectPinDriverDefaultName(t *testing.T) {
d := initTestDirectPinDriver()
gobottest.Assert(t, strings.HasPrefix(d.Name(), "Direct"), true)
}
func TestDirectPinDriverSetName(t *testing.T) {
d := initTestDirectPinDriver()
d.SetName("mybot")
gobottest.Assert(t, d.Name(), "mybot")
}