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-10-20 10:27:09 +02:00
|
|
|
"github.com/stretchr/testify/assert"
|
2023-05-20 14:25:21 +02:00
|
|
|
"gobot.io/x/gobot/v2"
|
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()
|
2023-10-20 10:27:09 +02:00
|
|
|
assert.Equal(t, "1", d.Pin())
|
|
|
|
assert.NotNil(t, d.Connection())
|
2014-12-16 13:42:48 -08:00
|
|
|
|
|
|
|
ret = d.Command("DigitalRead")(nil).(map[string]interface{})
|
|
|
|
|
2023-10-20 10:27:09 +02:00
|
|
|
assert.Equal(t, 1, ret["val"].(int))
|
|
|
|
assert.Nil(t, ret["err"])
|
2014-12-16 13:42:48 -08:00
|
|
|
|
|
|
|
err = d.Command("DigitalWrite")(map[string]interface{}{"level": "1"})
|
2023-10-20 10:27:09 +02:00
|
|
|
assert.Errorf(t, err.(error), "write error")
|
2014-12-16 13:42:48 -08:00
|
|
|
|
|
|
|
err = d.Command("PwmWrite")(map[string]interface{}{"level": "1"})
|
2023-10-20 10:27:09 +02:00
|
|
|
assert.Errorf(t, err.(error), "write error")
|
2014-12-16 13:42:48 -08:00
|
|
|
|
|
|
|
err = d.Command("ServoWrite")(map[string]interface{}{"level": "1"})
|
2023-10-20 10:27:09 +02:00
|
|
|
assert.Errorf(t, err.(error), "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()
|
2023-10-20 10:27:09 +02:00
|
|
|
assert.Nil(t, d.Start())
|
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()
|
2023-10-20 10:27:09 +02:00
|
|
|
assert.Nil(t, d.Halt())
|
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()
|
2023-10-20 10:27:09 +02:00
|
|
|
assert.NotNil(t, d.Off())
|
2016-06-14 08:42:50 -07:00
|
|
|
|
2017-04-02 17:15:45 +02:00
|
|
|
a := newGpioTestAdaptor()
|
|
|
|
d = NewDirectPinDriver(a, "1")
|
2023-10-20 10:27:09 +02:00
|
|
|
assert.Nil(t, d.Off())
|
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")
|
2023-10-20 10:27:09 +02:00
|
|
|
assert.Errorf(t, d.Off(), "DigitalWrite is not supported by this platform")
|
2017-04-12 11:17:44 +02:00
|
|
|
}
|
|
|
|
|
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")
|
2023-10-20 10:27:09 +02:00
|
|
|
assert.Nil(t, d.On())
|
2017-04-12 11:17:44 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestDirectPinDriverOnError(t *testing.T) {
|
2017-04-02 17:15:45 +02:00
|
|
|
d := initTestDirectPinDriver()
|
2023-10-20 10:27:09 +02:00
|
|
|
assert.NotNil(t, d.On())
|
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")
|
2023-10-20 10:27:09 +02:00
|
|
|
assert.Errorf(t, d.On(), "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")
|
2023-10-20 10:27:09 +02:00
|
|
|
assert.Nil(t, d.DigitalWrite(1))
|
2017-04-12 11:17:44 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestDirectPinDriverDigitalWriteNotSupported(t *testing.T) {
|
|
|
|
a := &gpioTestBareAdaptor{}
|
|
|
|
d := NewDirectPinDriver(a, "1")
|
2023-10-20 10:27:09 +02:00
|
|
|
assert.Errorf(t, d.DigitalWrite(1), "DigitalWrite is not supported by this platform")
|
2017-04-12 11:17:44 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestDirectPinDriverDigitalWriteError(t *testing.T) {
|
2017-04-02 17:15:45 +02:00
|
|
|
d := initTestDirectPinDriver()
|
2023-10-20 10:27:09 +02:00
|
|
|
assert.NotNil(t, d.DigitalWrite(1))
|
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()
|
2023-10-20 10:27:09 +02:00
|
|
|
assert.Equal(t, 1, ret)
|
|
|
|
assert.Nil(t, err)
|
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()
|
2023-10-20 10:27:09 +02:00
|
|
|
assert.Errorf(t, e, "DigitalRead is not supported by this platform")
|
2017-04-12 11:17:44 +02:00
|
|
|
}
|
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")
|
2023-10-20 10:27:09 +02:00
|
|
|
assert.Nil(t, d.PwmWrite(1))
|
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")
|
2023-10-20 10:27:09 +02:00
|
|
|
assert.Errorf(t, d.PwmWrite(1), "PwmWrite is not supported by this platform")
|
2017-04-12 11:17:44 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestDirectPinDriverPwmWriteError(t *testing.T) {
|
2017-04-02 17:15:45 +02:00
|
|
|
d := initTestDirectPinDriver()
|
2023-10-20 10:27:09 +02:00
|
|
|
assert.NotNil(t, d.PwmWrite(1))
|
2017-04-12 11:17:44 +02:00
|
|
|
}
|
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")
|
2023-10-20 10:27:09 +02:00
|
|
|
assert.Nil(t, d.ServoWrite(1))
|
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")
|
2023-10-20 10:27:09 +02:00
|
|
|
assert.Errorf(t, d.ServoWrite(1), "ServoWrite is not supported by this platform")
|
2017-04-12 11:17:44 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestDirectPinDriverServoWriteError(t *testing.T) {
|
|
|
|
d := initTestDirectPinDriver()
|
2023-10-20 10:27:09 +02:00
|
|
|
assert.NotNil(t, d.ServoWrite(1))
|
2017-04-12 11:17:44 +02:00
|
|
|
}
|
|
|
|
|
2017-03-21 11:55:46 +01:00
|
|
|
func TestDirectPinDriverDefaultName(t *testing.T) {
|
2017-04-02 17:15:45 +02:00
|
|
|
d := initTestDirectPinDriver()
|
2023-10-20 10:27:09 +02:00
|
|
|
assert.True(t, strings.HasPrefix(d.Name(), "Direct"))
|
2017-03-21 11:55:46 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
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")
|
2023-10-20 10:27:09 +02:00
|
|
|
assert.Equal(t, "mybot", d.Name())
|
2017-03-21 11:55:46 +01:00
|
|
|
}
|