2014-04-27 19:34:16 -07:00
|
|
|
package gpio
|
2014-04-26 03:11:51 -07:00
|
|
|
|
|
|
|
import (
|
2014-12-16 13:42:48 -08:00
|
|
|
"errors"
|
2017-03-21 11:55:46 +01:00
|
|
|
"strings"
|
2014-06-12 20:08:06 -07:00
|
|
|
"testing"
|
2014-09-27 11:45:52 -07:00
|
|
|
|
2023-05-20 14:25:21 +02:00
|
|
|
"gobot.io/x/gobot/v2"
|
|
|
|
"gobot.io/x/gobot/v2/gobottest"
|
2014-04-26 03:11:51 -07:00
|
|
|
)
|
|
|
|
|
2016-09-01 12:17:43 +02:00
|
|
|
var _ gobot.Driver = (*DirectPinDriver)(nil)
|
|
|
|
|
2017-04-02 17:15:45 +02:00
|
|
|
func initTestDirectPinDriver() *DirectPinDriver {
|
|
|
|
a := newGpioTestAdaptor()
|
2020-03-30 21:36:32 +01:00
|
|
|
a.testAdaptorDigitalRead = func(string) (val int, err error) {
|
2014-12-16 13:42:48 -08:00
|
|
|
val = 1
|
|
|
|
return
|
|
|
|
}
|
2020-03-30 21:36:32 +01:00
|
|
|
a.testAdaptorDigitalWrite = func(string, byte) (err error) {
|
2014-12-16 13:42:48 -08:00
|
|
|
return errors.New("write error")
|
|
|
|
}
|
2020-03-30 21:36:32 +01:00
|
|
|
a.testAdaptorPwmWrite = func(string, byte) (err error) {
|
2014-12-16 13:42:48 -08:00
|
|
|
return errors.New("write error")
|
|
|
|
}
|
2020-03-30 21:36:32 +01:00
|
|
|
a.testAdaptorServoWrite = func(string, byte) (err error) {
|
2014-12-16 13:42:48 -08:00
|
|
|
return errors.New("write error")
|
|
|
|
}
|
2017-04-02 17:15:45 +02:00
|
|
|
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{}
|
|
|
|
|
2017-04-02 17:15:45 +02:00
|
|
|
d := initTestDirectPinDriver()
|
2016-02-22 00:21:24 -05:00
|
|
|
gobottest.Assert(t, d.Pin(), "1")
|
2016-09-25 13:36:18 +02:00
|
|
|
gobottest.Refute(t, d.Connection(), nil)
|
2014-12-16 13:42:48 -08:00
|
|
|
|
|
|
|
ret = d.Command("DigitalRead")(nil).(map[string]interface{})
|
|
|
|
|
2016-02-22 00:21:24 -05:00
|
|
|
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"})
|
2016-02-22 00:21:24 -05:00
|
|
|
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"})
|
2016-02-22 00:21:24 -05:00
|
|
|
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"})
|
2016-02-22 00:21:24 -05:00
|
|
|
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) {
|
2017-04-02 17:15:45 +02:00
|
|
|
d := initTestDirectPinDriver()
|
2016-11-07 17:12:14 +01:00
|
|
|
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) {
|
2017-04-02 17:15:45 +02:00
|
|
|
d := initTestDirectPinDriver()
|
2016-11-07 17:12:14 +01:00
|
|
|
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) {
|
2017-04-02 17:15:45 +02:00
|
|
|
d := initTestDirectPinDriver()
|
2017-04-04 15:36:10 +02:00
|
|
|
gobottest.Refute(t, d.Off(), nil)
|
2016-06-14 08:42:50 -07:00
|
|
|
|
2017-04-02 17:15:45 +02:00
|
|
|
a := newGpioTestAdaptor()
|
|
|
|
d = NewDirectPinDriver(a, "1")
|
2017-04-04 15:36:10 +02:00
|
|
|
gobottest.Assert(t, d.Off(), nil)
|
2016-06-14 08:42:50 -07:00
|
|
|
}
|
|
|
|
|
2017-04-12 11:17:44 +02: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) {
|
2017-04-12 11:17:44 +02:00
|
|
|
a := newGpioTestAdaptor()
|
|
|
|
d := NewDirectPinDriver(a, "1")
|
|
|
|
gobottest.Assert(t, d.On(), nil)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestDirectPinDriverOnError(t *testing.T) {
|
2017-04-02 17:15:45 +02:00
|
|
|
d := initTestDirectPinDriver()
|
2017-04-04 15:36:10 +02:00
|
|
|
gobottest.Refute(t, d.On(), nil)
|
2017-04-12 11:17:44 +02:00
|
|
|
}
|
2016-06-14 08:42:50 -07:00
|
|
|
|
2017-04-12 11:17:44 +02: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) {
|
2017-04-12 11:17:44 +02:00
|
|
|
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) {
|
2017-04-02 17:15:45 +02:00
|
|
|
d := initTestDirectPinDriver()
|
2016-02-22 00:21:24 -05:00
|
|
|
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) {
|
2017-04-02 17:15:45 +02:00
|
|
|
d := initTestDirectPinDriver()
|
2014-12-16 13:42:48 -08:00
|
|
|
ret, err := d.DigitalRead()
|
2016-02-22 00:21:24 -05:00
|
|
|
gobottest.Assert(t, ret, 1)
|
2017-04-02 17:15:45 +02:00
|
|
|
gobottest.Assert(t, err, nil)
|
2014-06-12 20:08:06 -07:00
|
|
|
}
|
|
|
|
|
2017-04-12 11:17:44 +02: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
|
|
|
|
2017-04-12 11:17:44 +02:00
|
|
|
func TestDirectPinDriverPwmWrite(t *testing.T) {
|
2017-04-02 17:15:45 +02:00
|
|
|
a := newGpioTestAdaptor()
|
2017-04-12 11:17:44 +02:00
|
|
|
d := NewDirectPinDriver(a, "1")
|
2017-04-02 17:15:45 +02:00
|
|
|
gobottest.Assert(t, d.PwmWrite(1), nil)
|
2014-06-12 20:08:06 -07:00
|
|
|
}
|
2016-12-20 13:25:22 +01:00
|
|
|
|
2017-04-12 11:17:44 +02: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) {
|
2017-04-02 17:15:45 +02:00
|
|
|
d := initTestDirectPinDriver()
|
2017-04-12 11:17:44 +02:00
|
|
|
gobottest.Refute(t, d.PwmWrite(1), nil)
|
|
|
|
}
|
2014-06-12 20:08:06 -07:00
|
|
|
|
2017-04-12 11:17:44 +02:00
|
|
|
func TestDirectPinDriverServoWrite(t *testing.T) {
|
2017-04-02 17:15:45 +02:00
|
|
|
a := newGpioTestAdaptor()
|
2017-04-12 11:17:44 +02:00
|
|
|
d := NewDirectPinDriver(a, "1")
|
2017-04-02 17:15:45 +02:00
|
|
|
gobottest.Assert(t, d.ServoWrite(1), nil)
|
2014-06-12 20:08:06 -07:00
|
|
|
}
|
2017-03-21 11:55:46 +01:00
|
|
|
|
2017-04-12 11:17:44 +02: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)
|
|
|
|
}
|
|
|
|
|
2017-03-21 11:55:46 +01:00
|
|
|
func TestDirectPinDriverDefaultName(t *testing.T) {
|
2017-04-02 17:15:45 +02:00
|
|
|
d := initTestDirectPinDriver()
|
2017-03-21 11:55:46 +01:00
|
|
|
gobottest.Assert(t, strings.HasPrefix(d.Name(), "Direct"), true)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestDirectPinDriverSetName(t *testing.T) {
|
2017-04-02 17:15:45 +02:00
|
|
|
d := initTestDirectPinDriver()
|
2017-03-21 11:55:46 +01:00
|
|
|
d.SetName("mybot")
|
|
|
|
gobottest.Assert(t, d.Name(), "mybot")
|
|
|
|
}
|