hybridgroup.gobot/drivers/gpio/servo_driver_test.go

78 lines
1.6 KiB
Go
Raw Normal View History

2014-04-28 10:34:16 +08:00
package gpio
import (
2014-12-17 05:42:48 +08:00
"errors"
2014-06-14 03:39:02 +08:00
"testing"
2014-09-28 02:45:52 +08:00
"gobot.io/x/gobot"
"gobot.io/x/gobot/gobottest"
)
var _ gobot.Driver = (*ServoDriver)(nil)
2014-06-14 07:01:39 +08:00
func initTestServoDriver() *ServoDriver {
return NewServoDriver(newGpioTestAdaptor(), "1")
2014-06-14 03:39:02 +08:00
}
2014-12-17 05:42:48 +08:00
func TestServoDriver(t *testing.T) {
var err interface{}
d := initTestServoDriver()
gobottest.Assert(t, d.Pin(), "1")
gobottest.Refute(t, d.Connection(), nil)
2014-12-17 05:42:48 +08:00
testAdaptorServoWrite = func() (err error) {
return errors.New("pwm error")
}
err = d.Command("Min")(nil)
gobottest.Assert(t, err.(error), errors.New("pwm error"))
2014-12-17 05:42:48 +08:00
err = d.Command("Center")(nil)
gobottest.Assert(t, err.(error), errors.New("pwm error"))
2014-12-17 05:42:48 +08:00
err = d.Command("Max")(nil)
gobottest.Assert(t, err.(error), errors.New("pwm error"))
2014-12-17 05:42:48 +08:00
err = d.Command("Move")(map[string]interface{}{"angle": 100.0})
gobottest.Assert(t, err.(error), errors.New("pwm error"))
2014-12-17 05:42:48 +08:00
}
2014-06-14 07:01:39 +08:00
func TestServoDriverStart(t *testing.T) {
d := initTestServoDriver()
gobottest.Assert(t, d.Start(), nil)
2014-06-14 03:39:02 +08:00
}
2014-06-14 07:01:39 +08:00
func TestServoDriverHalt(t *testing.T) {
d := initTestServoDriver()
gobottest.Assert(t, d.Halt(), nil)
2014-06-14 03:39:02 +08:00
}
2014-06-14 07:01:39 +08:00
func TestServoDriverMove(t *testing.T) {
d := initTestServoDriver()
d.Move(100)
gobottest.Assert(t, d.CurrentAngle, uint8(100))
2014-12-17 05:42:48 +08:00
err := d.Move(200)
gobottest.Assert(t, err, ErrServoOutOfRange)
2014-06-14 03:39:02 +08:00
}
2014-06-14 07:01:39 +08:00
func TestServoDriverMin(t *testing.T) {
d := initTestServoDriver()
d.Min()
gobottest.Assert(t, d.CurrentAngle, uint8(0))
2014-06-14 03:39:02 +08:00
}
2014-06-14 07:01:39 +08:00
func TestServoDriverMax(t *testing.T) {
d := initTestServoDriver()
d.Max()
gobottest.Assert(t, d.CurrentAngle, uint8(180))
2014-06-14 03:39:02 +08:00
}
2014-06-14 07:01:39 +08:00
func TestServoDriverCenter(t *testing.T) {
d := initTestServoDriver()
d.Center()
gobottest.Assert(t, d.CurrentAngle, uint8(90))
2014-06-14 03:39:02 +08:00
}