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 12:27:12 +01:00
|
|
|
"strings"
|
2014-06-13 12:39:02 -07:00
|
|
|
"testing"
|
2014-09-27 11:45:52 -07:00
|
|
|
|
2016-12-08 13:24:03 +01:00
|
|
|
"gobot.io/x/gobot"
|
|
|
|
"gobot.io/x/gobot/gobottest"
|
2014-04-26 03:11:51 -07:00
|
|
|
)
|
|
|
|
|
2016-09-01 12:17:43 +02:00
|
|
|
var _ gobot.Driver = (*ServoDriver)(nil)
|
|
|
|
|
2014-06-13 16:01:39 -07:00
|
|
|
func initTestServoDriver() *ServoDriver {
|
2016-09-25 13:36:18 +02:00
|
|
|
return NewServoDriver(newGpioTestAdaptor(), "1")
|
2014-06-13 12:39:02 -07:00
|
|
|
}
|
|
|
|
|
2014-12-16 13:42:48 -08:00
|
|
|
func TestServoDriver(t *testing.T) {
|
|
|
|
var err interface{}
|
|
|
|
|
2017-04-02 17:15:45 +02:00
|
|
|
a := newGpioTestAdaptor()
|
|
|
|
d := NewServoDriver(a, "1")
|
2014-12-16 13:42:48 -08:00
|
|
|
|
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
|
|
|
|
2017-04-02 17:15:45 +02:00
|
|
|
a.testAdaptorServoWrite = func() (err error) {
|
2014-12-16 13:42:48 -08:00
|
|
|
return errors.New("pwm error")
|
|
|
|
}
|
|
|
|
|
|
|
|
err = d.Command("Min")(nil)
|
2016-02-22 00:21:24 -05:00
|
|
|
gobottest.Assert(t, err.(error), errors.New("pwm error"))
|
2014-12-16 13:42:48 -08:00
|
|
|
|
|
|
|
err = d.Command("Center")(nil)
|
2016-02-22 00:21:24 -05:00
|
|
|
gobottest.Assert(t, err.(error), errors.New("pwm error"))
|
2014-12-16 13:42:48 -08:00
|
|
|
|
|
|
|
err = d.Command("Max")(nil)
|
2016-02-22 00:21:24 -05:00
|
|
|
gobottest.Assert(t, err.(error), errors.New("pwm error"))
|
2014-12-16 13:42:48 -08:00
|
|
|
|
|
|
|
err = d.Command("Move")(map[string]interface{}{"angle": 100.0})
|
2016-02-22 00:21:24 -05:00
|
|
|
gobottest.Assert(t, err.(error), errors.New("pwm error"))
|
2014-12-16 13:42:48 -08:00
|
|
|
}
|
|
|
|
|
2014-06-13 16:01:39 -07:00
|
|
|
func TestServoDriverStart(t *testing.T) {
|
|
|
|
d := initTestServoDriver()
|
2016-11-07 17:12:14 +01:00
|
|
|
gobottest.Assert(t, d.Start(), nil)
|
2014-06-13 12:39:02 -07:00
|
|
|
}
|
|
|
|
|
2014-06-13 16:01:39 -07:00
|
|
|
func TestServoDriverHalt(t *testing.T) {
|
|
|
|
d := initTestServoDriver()
|
2016-11-07 17:12:14 +01:00
|
|
|
gobottest.Assert(t, d.Halt(), nil)
|
2014-06-13 12:39:02 -07:00
|
|
|
}
|
|
|
|
|
2014-06-13 16:01:39 -07:00
|
|
|
func TestServoDriverMove(t *testing.T) {
|
|
|
|
d := initTestServoDriver()
|
|
|
|
d.Move(100)
|
2016-02-22 00:21:24 -05:00
|
|
|
gobottest.Assert(t, d.CurrentAngle, uint8(100))
|
2014-12-16 13:42:48 -08:00
|
|
|
err := d.Move(200)
|
2016-02-22 00:21:24 -05:00
|
|
|
gobottest.Assert(t, err, ErrServoOutOfRange)
|
2014-06-13 12:39:02 -07:00
|
|
|
}
|
|
|
|
|
2014-06-13 16:01:39 -07:00
|
|
|
func TestServoDriverMin(t *testing.T) {
|
|
|
|
d := initTestServoDriver()
|
|
|
|
d.Min()
|
2016-02-22 00:21:24 -05:00
|
|
|
gobottest.Assert(t, d.CurrentAngle, uint8(0))
|
2014-06-13 12:39:02 -07:00
|
|
|
}
|
|
|
|
|
2014-06-13 16:01:39 -07:00
|
|
|
func TestServoDriverMax(t *testing.T) {
|
|
|
|
d := initTestServoDriver()
|
|
|
|
d.Max()
|
2016-02-22 00:21:24 -05:00
|
|
|
gobottest.Assert(t, d.CurrentAngle, uint8(180))
|
2014-06-13 12:39:02 -07:00
|
|
|
}
|
|
|
|
|
2014-06-13 16:01:39 -07:00
|
|
|
func TestServoDriverCenter(t *testing.T) {
|
|
|
|
d := initTestServoDriver()
|
|
|
|
d.Center()
|
2016-02-22 00:21:24 -05:00
|
|
|
gobottest.Assert(t, d.CurrentAngle, uint8(90))
|
2014-06-13 12:39:02 -07:00
|
|
|
}
|
2017-03-21 12:27:12 +01:00
|
|
|
|
|
|
|
func TestServoDriverDefaultName(t *testing.T) {
|
|
|
|
d := initTestServoDriver()
|
|
|
|
gobottest.Assert(t, strings.HasPrefix(d.Name(), "Servo"), true)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestServoDriverSetName(t *testing.T) {
|
|
|
|
d := initTestServoDriver()
|
|
|
|
d.SetName("mybot")
|
|
|
|
gobottest.Assert(t, d.Name(), "mybot")
|
|
|
|
}
|