1
0
mirror of https://github.com/hybridgroup/gobot.git synced 2025-05-06 19:29:15 +08:00
hybridgroup.gobot/drivers/i2c/hmc6352_driver_test.go

95 lines
2.6 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
"gobot.io/x/gobot"
"gobot.io/x/gobot/gobottest"
)
// 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")
}
gobottest.Refute(t, d.Driver, nil)
gobottest.Assert(t, strings.HasPrefix(d.Name(), "HMC6352"), true)
gobottest.Assert(t, d.defaultAddress, 0x21)
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))
gobottest.Assert(t, d.GetBusOrDefault(1), 2)
2014-12-22 13:36:52 -08:00
}
func TestHMC6352Start(t *testing.T) {
d := NewHMC6352Driver(newI2cTestAdaptor())
gobottest.Assert(t, d.Start(), nil)
}
func TestHMC6352Halt(t *testing.T) {
d, _ := initTestHMC6352DriverWithStubbedAdaptor()
gobottest.Assert(t, d.Halt(), nil)
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()
gobottest.Assert(t, heading, uint16(2534))
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()
gobottest.Assert(t, heading, uint16(0))
gobottest.Assert(t, err, ErrNotEnoughBytes)
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()
gobottest.Assert(t, heading, uint16(0))
gobottest.Assert(t, err, errors.New("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()
gobottest.Assert(t, heading, uint16(0))
gobottest.Assert(t, err, errors.New("write error"))
2014-06-13 12:48:00 -07:00
}