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

110 lines
2.8 KiB
Go
Raw Normal View History

//nolint:forcetypeassert // ok here
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
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"gobot.io/x/gobot/v2"
"gobot.io/x/gobot/v2/drivers/aio"
)
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
}
func TestNewServoDriver(t *testing.T) {
// arrange
a := newGpioTestAdaptor()
// act
d := NewServoDriver(a, "10")
// assert
assert.IsType(t, &ServoDriver{}, d)
// assert: gpio.driver attributes
require.NotNil(t, d.driver)
assert.True(t, strings.HasPrefix(d.driverCfg.name, "Servo"))
assert.Equal(t, "10", d.driverCfg.pin)
assert.Equal(t, a, d.connection)
assert.NotNil(t, d.afterStart)
assert.NotNil(t, d.beforeHalt)
assert.NotNil(t, d.Commander)
assert.NotNil(t, d.mutex)
// assert: driver specific attributes
assert.Equal(t, uint8(0), d.currentAngle)
}
func TestNewServoDriver_options(t *testing.T) {
// This is a general test, that options are applied in constructor by using the common WithName() option, least one
// option of this driver and one of another driver (which should lead to panic). Further tests for options can also
// be done by call of "WithOption(val).apply(cfg)".
// arrange
const (
myName = "left wheel"
)
panicFunc := func() {
NewServoDriver(newGpioTestAdaptor(), "1", WithName("crazy"),
aio.WithActuatorScaler(func(float64) int { return 0 }))
}
// act
d := NewServoDriver(newGpioTestAdaptor(), "1", WithName(myName))
// assert
assert.Equal(t, myName, d.Name())
assert.PanicsWithValue(t, "'scaler option for analog actuators' can not be applied on 'crazy'", panicFunc)
}
func TestServo_Commands(t *testing.T) {
2014-12-16 13:42:48 -08:00
var err interface{}
a := newGpioTestAdaptor()
d := NewServoDriver(a, "1")
2014-12-16 13:42:48 -08:00
a.servoWriteFunc = func(string, byte) error {
2014-12-16 13:42:48 -08:00
return errors.New("pwm error")
}
err = d.Command("ToMin")(nil)
require.EqualError(t, err.(error), "pwm error")
2014-12-16 13:42:48 -08:00
err = d.Command("ToCenter")(nil)
require.EqualError(t, err.(error), "pwm error")
2014-12-16 13:42:48 -08:00
err = d.Command("ToMax")(nil)
require.EqualError(t, err.(error), "pwm error")
2014-12-16 13:42:48 -08:00
err = d.Command("Move")(map[string]interface{}{"angle": 100.0})
require.EqualError(t, err.(error), "pwm error")
2014-12-16 13:42:48 -08:00
}
func TestServoMove(t *testing.T) {
2014-06-13 16:01:39 -07:00
d := initTestServoDriver()
_ = d.Move(100)
assert.Equal(t, uint8(100), d.currentAngle)
2014-12-16 13:42:48 -08:00
err := d.Move(200)
require.EqualError(t, err, "servo angle (200) must be between 0-180")
2014-06-13 12:39:02 -07:00
}
func TestServoMin(t *testing.T) {
2014-06-13 16:01:39 -07:00
d := initTestServoDriver()
_ = d.ToMin()
assert.Equal(t, uint8(0), d.currentAngle)
assert.Equal(t, d.currentAngle, d.Angle())
2014-06-13 12:39:02 -07:00
}
func TestServoMax(t *testing.T) {
d := initTestServoDriver()
_ = d.ToMax()
assert.Equal(t, uint8(180), d.currentAngle)
}
func TestServoCenter(t *testing.T) {
d := initTestServoDriver()
_ = d.ToCenter()
assert.Equal(t, uint8(90), d.currentAngle)
}