2023-10-30 02:35:26 +08:00
|
|
|
package i2c
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"strings"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
2023-11-12 21:17:02 +08:00
|
|
|
"github.com/stretchr/testify/require"
|
2023-11-16 03:51:52 +08:00
|
|
|
|
2023-10-30 02:35:26 +08:00
|
|
|
"gobot.io/x/gobot/v2"
|
|
|
|
)
|
|
|
|
|
|
|
|
// this ensures that the implementation implements the gobot.Driver interface
|
|
|
|
var _ gobot.Driver = (*Adafruit2327Driver)(nil)
|
|
|
|
|
|
|
|
func initTestAdafruit2327WithStubbedAdaptor() (*Adafruit2327Driver, *i2cTestAdaptor) {
|
|
|
|
a := newI2cTestAdaptor()
|
|
|
|
d := NewAdafruit2327Driver(a)
|
|
|
|
if err := d.Start(); err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
return d, a
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestNewAdafruit2327Driver(t *testing.T) {
|
|
|
|
// arrange & act
|
|
|
|
d := NewAdafruit2327Driver(newI2cTestAdaptor())
|
|
|
|
// assert
|
|
|
|
assert.IsType(t, &Adafruit2327Driver{}, d)
|
|
|
|
assert.True(t, strings.HasPrefix(d.Name(), "Adafruit2327ServoHat"))
|
|
|
|
assert.Equal(t, 0x40, d.defaultAddress)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestAdafruit2327Options(t *testing.T) {
|
|
|
|
// This is a general test, that options are applied in constructor by using the common WithBus() option and
|
|
|
|
// least one of this driver. Further tests for options can also be done by call of "WithOption(val)(d)".
|
|
|
|
// arrange & act
|
|
|
|
d := NewAdafruit2327Driver(newI2cTestAdaptor(), WithBus(2))
|
|
|
|
// assert
|
|
|
|
assert.Equal(t, 2, d.GetBusOrDefault(1))
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestAdafruit2327SetServoMotorFreq(t *testing.T) {
|
|
|
|
// arrange
|
|
|
|
d, a := initTestAdafruit2327WithStubbedAdaptor()
|
|
|
|
a.written = []byte{} // reset writes of former test
|
|
|
|
const freq = 60.0
|
|
|
|
// act
|
|
|
|
err := d.SetServoMotorFreq(freq)
|
|
|
|
// assert
|
2023-11-12 21:17:02 +08:00
|
|
|
require.NoError(t, err)
|
|
|
|
assert.Len(t, a.written, 9) // detailed test, see "TestPCA9685SetPWMFreq"
|
2023-10-30 02:35:26 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestAdafruit2327SetServoMotorFreqError(t *testing.T) {
|
|
|
|
// arrange
|
|
|
|
d, a := initTestAdafruit2327WithStubbedAdaptor()
|
|
|
|
a.i2cWriteImpl = func([]byte) (int, error) {
|
|
|
|
return 0, errors.New("write error")
|
|
|
|
}
|
|
|
|
const freq = 60.0
|
|
|
|
// act & assert
|
2023-11-12 21:17:02 +08:00
|
|
|
require.ErrorContains(t, d.SetServoMotorFreq(freq), "write error")
|
2023-10-30 02:35:26 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestAdafruit2327SetServoMotorPulse(t *testing.T) {
|
|
|
|
// arrange
|
|
|
|
d, a := initTestAdafruit2327WithStubbedAdaptor()
|
|
|
|
a.written = []byte{} // reset writes of former test
|
|
|
|
const (
|
|
|
|
channel byte = 7
|
|
|
|
on int32 = 1234
|
|
|
|
off int32 = 4321
|
|
|
|
)
|
|
|
|
// act
|
|
|
|
err := d.SetServoMotorPulse(channel, on, off)
|
|
|
|
// assert
|
2023-11-12 21:17:02 +08:00
|
|
|
require.NoError(t, err)
|
|
|
|
assert.Len(t, a.written, 8) // detailed test, see "TestPCA9685SetPWM"
|
2023-10-30 02:35:26 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestAdafruit2327SetServoMotorPulseError(t *testing.T) {
|
|
|
|
// arrange
|
|
|
|
d, a := initTestAdafruit2327WithStubbedAdaptor()
|
|
|
|
a.i2cWriteImpl = func([]byte) (int, error) {
|
|
|
|
return 0, errors.New("write error")
|
|
|
|
}
|
|
|
|
const (
|
|
|
|
channel byte = 7
|
|
|
|
on int32 = 1234
|
|
|
|
off int32 = 4321
|
|
|
|
)
|
|
|
|
// act & assert
|
2023-11-12 21:17:02 +08:00
|
|
|
require.ErrorContains(t, d.SetServoMotorPulse(channel, on, off), "write error")
|
2023-10-30 02:35:26 +08:00
|
|
|
}
|