1
0
mirror of https://github.com/hybridgroup/gobot.git synced 2025-04-29 13:49:14 +08:00
hybridgroup.gobot/platforms/gpio/direct_pin_driver_test.go

113 lines
3.3 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"
2014-06-12 20:08:06 -07:00
"testing"
2014-09-27 11:45:52 -07:00
"github.com/hybridgroup/gobot"
"github.com/hybridgroup/gobot/gobottest"
)
2014-12-16 13:42:48 -08:00
func initTestDirectPinDriver(conn gobot.Connection) *DirectPinDriver {
testAdaptorDigitalRead = func() (val int, err error) {
val = 1
return
}
testAdaptorDigitalWrite = func() (err error) {
return errors.New("write error")
}
testAdaptorAnalogRead = func() (val int, err error) {
val = 80
return
}
testAdaptorPwmWrite = func() (err error) {
return errors.New("write error")
}
testAdaptorServoWrite = func() (err error) {
return errors.New("write error")
}
return NewDirectPinDriver(conn, "bot", "1")
}
func TestDirectPinDriver(t *testing.T) {
var ret map[string]interface{}
var err interface{}
d := initTestDirectPinDriver(newGpioTestAdaptor("adaptor"))
gobottest.Assert(t, d.Name(), "bot")
gobottest.Assert(t, d.Pin(), "1")
gobottest.Assert(t, d.Connection().Name(), "adaptor")
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
ret = d.Command("AnalogRead")(nil).(map[string]interface{})
gobottest.Assert(t, ret["val"].(int), 80)
gobottest.Assert(t, ret["err"], nil)
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) {
2014-12-16 13:42:48 -08:00
d := initTestDirectPinDriver(newGpioTestAdaptor("adaptor"))
gobottest.Assert(t, len(d.Start()), 0)
2014-06-12 20:08:06 -07:00
}
2014-06-13 16:01:39 -07:00
func TestDirectPinDriverHalt(t *testing.T) {
2014-12-16 13:42:48 -08:00
d := initTestDirectPinDriver(newGpioTestAdaptor("adaptor"))
gobottest.Assert(t, len(d.Halt()), 0)
2014-06-12 20:08:06 -07:00
}
2014-12-16 13:42:48 -08:00
func TestDirectPinDriverDigitalWrite(t *testing.T) {
d := initTestDirectPinDriver(newGpioTestAdaptor("adaptor"))
gobottest.Refute(t, d.DigitalWrite(1), nil)
2014-12-16 13:42:48 -08:00
d = initTestDirectPinDriver(&gpioTestBareAdaptor{})
gobottest.Assert(t, d.DigitalWrite(1), ErrDigitalWriteUnsupported)
2014-06-12 20:08:06 -07:00
}
2014-12-16 13:42:48 -08:00
func TestDirectPinDriverDigitalRead(t *testing.T) {
d := initTestDirectPinDriver(newGpioTestAdaptor("adaptor"))
ret, err := d.DigitalRead()
gobottest.Assert(t, ret, 1)
2014-12-16 13:42:48 -08:00
d = initTestDirectPinDriver(&gpioTestBareAdaptor{})
ret, err = d.DigitalRead()
gobottest.Assert(t, err, ErrDigitalReadUnsupported)
2014-06-12 20:08:06 -07:00
}
2014-06-13 16:01:39 -07:00
func TestDirectPinDriverAnalogRead(t *testing.T) {
2014-12-16 13:42:48 -08:00
d := initTestDirectPinDriver(newGpioTestAdaptor("adaptor"))
ret, err := d.AnalogRead()
gobottest.Assert(t, ret, 80)
2014-12-16 13:42:48 -08:00
d = initTestDirectPinDriver(&gpioTestBareAdaptor{})
ret, err = d.AnalogRead()
gobottest.Assert(t, err, ErrAnalogReadUnsupported)
2014-06-12 20:08:06 -07:00
}
2014-06-13 16:01:39 -07:00
func TestDirectPinDriverPwmWrite(t *testing.T) {
2014-12-16 13:42:48 -08:00
d := initTestDirectPinDriver(newGpioTestAdaptor("adaptor"))
gobottest.Refute(t, d.PwmWrite(1), nil)
2014-12-16 13:42:48 -08:00
d = initTestDirectPinDriver(&gpioTestBareAdaptor{})
gobottest.Assert(t, d.PwmWrite(1), ErrPwmWriteUnsupported)
2014-06-12 20:08:06 -07:00
}
2014-12-16 13:42:48 -08:00
func TestDirectPinDriverDigitalWrie(t *testing.T) {
d := initTestDirectPinDriver(newGpioTestAdaptor("adaptor"))
gobottest.Refute(t, d.ServoWrite(1), nil)
2014-06-12 20:08:06 -07:00
2014-12-16 13:42:48 -08:00
d = initTestDirectPinDriver(&gpioTestBareAdaptor{})
gobottest.Assert(t, d.ServoWrite(1), ErrServoWriteUnsupported)
2014-06-12 20:08:06 -07:00
}