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

90 lines
2.0 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"
"strings"
2014-06-13 12:39:02 -07:00
"testing"
2014-09-27 11:45:52 -07:00
"gobot.io/x/gobot/v2"
"gobot.io/x/gobot/v2/gobottest"
)
var _ gobot.Driver = (*ServoDriver)(nil)
2014-06-13 16:01:39 -07:00
func initTestServoDriver() *ServoDriver {
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{}
a := newGpioTestAdaptor()
d := NewServoDriver(a, "1")
2014-12-16 13:42:48 -08:00
gobottest.Assert(t, d.Pin(), "1")
gobottest.Refute(t, d.Connection(), nil)
2014-12-16 13:42:48 -08:00
a.testAdaptorServoWrite = func(string, byte) (err error) {
2014-12-16 13:42:48 -08:00
return errors.New("pwm error")
}
err = d.Command("Min")(nil)
gobottest.Assert(t, err.(error), errors.New("pwm error"))
2014-12-16 13:42:48 -08:00
err = d.Command("Center")(nil)
gobottest.Assert(t, err.(error), errors.New("pwm error"))
2014-12-16 13:42:48 -08:00
err = d.Command("Max")(nil)
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})
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()
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()
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)
gobottest.Assert(t, d.CurrentAngle, uint8(100))
2014-12-16 13:42:48 -08:00
err := d.Move(200)
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()
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()
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()
gobottest.Assert(t, d.CurrentAngle, uint8(90))
2014-06-13 12:39:02 -07: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")
}