1
0
mirror of https://github.com/hybridgroup/gobot.git synced 2025-05-02 22:17:12 +08:00
hybridgroup.gobot/drivers/i2c/hmc6352_driver_test.go

95 lines
2.5 KiB
Go
Raw Normal View History

2014-04-27 18:54:41 -07:00
package i2c
import (
2014-12-22 13:36:52 -08:00
"errors"
"strings"
2014-06-13 12:48:00 -07:00
"testing"
2014-10-28 16:22:05 -07:00
"github.com/stretchr/testify/assert"
"gobot.io/x/gobot/v2"
)
// this ensures that the implementation is based on i2c.Driver, which implements the gobot.Driver
// and tests all implementations, so no further tests needed here for gobot.Driver interface
var _ gobot.Driver = (*HMC6352Driver)(nil)
2014-09-11 15:38:08 -05:00
func initTestHMC6352DriverWithStubbedAdaptor() (*HMC6352Driver, *i2cTestAdaptor) {
a := newI2cTestAdaptor()
d := NewHMC6352Driver(a)
if err := d.Start(); err != nil {
panic(err)
}
return d, a
2014-09-11 15:38:08 -05:00
}
func TestNewHMC6352Driver(t *testing.T) {
var di interface{} = NewHMC6352Driver(newI2cTestAdaptor())
d, ok := di.(*HMC6352Driver)
2014-09-11 15:38:08 -05:00
if !ok {
t.Errorf("NewHMC6352Driver() should have returned a *HMC6352Driver")
}
assert.NotNil(t, d.Driver)
assert.True(t, strings.HasPrefix(d.Name(), "HMC6352"))
assert.Equal(t, 0x21, d.defaultAddress)
2014-09-11 15:38:08 -05:00
}
func TestHMC6352Options(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)".
d := NewHMC6352Driver(newI2cTestAdaptor(), WithBus(2))
assert.Equal(t, 2, d.GetBusOrDefault(1))
2014-12-22 13:36:52 -08:00
}
func TestHMC6352Start(t *testing.T) {
d := NewHMC6352Driver(newI2cTestAdaptor())
assert.NoError(t, d.Start())
}
func TestHMC6352Halt(t *testing.T) {
d, _ := initTestHMC6352DriverWithStubbedAdaptor()
assert.NoError(t, d.Halt())
2014-12-22 13:36:52 -08:00
}
func TestHMC6352Heading(t *testing.T) {
2014-12-22 13:36:52 -08:00
// when len(data) is 2
d, a := initTestHMC6352DriverWithStubbedAdaptor()
a.i2cReadImpl = func(b []byte) (int, error) {
copy(b, []byte{99, 1})
return 2, nil
2014-10-28 16:22:05 -07:00
}
2014-09-11 15:38:08 -05:00
heading, _ := d.Heading()
assert.Equal(t, uint16(2534), heading)
2014-12-22 13:36:52 -08:00
2014-09-11 15:38:08 -05:00
// when len(data) is not 2
d, a = initTestHMC6352DriverWithStubbedAdaptor()
a.i2cReadImpl = func(b []byte) (int, error) {
copy(b, []byte{99})
return 1, nil
2014-09-11 15:38:08 -05:00
}
heading, err := d.Heading()
assert.Equal(t, uint16(0), heading)
assert.Equal(t, ErrNotEnoughBytes, err)
2014-12-22 13:36:52 -08:00
// when read error
d, a = initTestHMC6352DriverWithStubbedAdaptor()
a.i2cReadImpl = func([]byte) (int, error) {
return 0, errors.New("read error")
2014-10-28 16:22:05 -07:00
}
2014-09-11 15:38:08 -05:00
heading, err = d.Heading()
assert.Equal(t, uint16(0), heading)
assert.ErrorContains(t, err, "read error")
2014-09-11 15:38:08 -05:00
2014-12-22 13:36:52 -08:00
// when write error
d, a = initTestHMC6352DriverWithStubbedAdaptor()
a.i2cWriteImpl = func([]byte) (int, error) {
return 0, errors.New("write error")
2014-12-22 13:36:52 -08:00
}
heading, err = d.Heading()
assert.Equal(t, uint16(0), heading)
assert.ErrorContains(t, err, "write error")
2014-06-13 12:48:00 -07:00
}