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

111 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"
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"
)
var _ gobot.Driver = (*HMC6352Driver)(nil)
2014-09-11 15:38:08 -05:00
// --------- HELPERS
func initTestHMC6352Driver() (driver *HMC6352Driver) {
driver, _ = initTestHMC6352DriverWithStubbedAdaptor()
return
2014-06-13 12:48:00 -07:00
}
2014-09-11 15:38:08 -05:00
func initTestHMC6352DriverWithStubbedAdaptor() (*HMC6352Driver, *i2cTestAdaptor) {
adaptor := newI2cTestAdaptor()
return NewHMC6352Driver(adaptor), adaptor
2014-09-11 15:38:08 -05:00
}
// --------- TESTS
func TestNewHMC6352Driver(t *testing.T) {
// Does it return a pointer to an instance of HMC6352Driver?
var bm interface{} = NewHMC6352Driver(newI2cTestAdaptor())
2014-09-11 15:38:08 -05:00
_, ok := bm.(*HMC6352Driver)
if !ok {
t.Errorf("NewHMC6352Driver() should have returned a *HMC6352Driver")
}
2014-12-22 13:36:52 -08:00
b := NewHMC6352Driver(newI2cTestAdaptor())
gobottest.Refute(t, b.Connection(), nil)
2014-09-11 15:38:08 -05:00
}
// Methods
2014-06-13 16:01:39 -07:00
func TestHMC6352DriverStart(t *testing.T) {
2014-09-11 15:38:08 -05:00
hmc, adaptor := initTestHMC6352DriverWithStubbedAdaptor()
gobottest.Assert(t, hmc.Start(), nil)
2014-12-22 13:36:52 -08:00
adaptor.i2cWriteImpl = func([]byte) (int, error) {
return 0, errors.New("write error")
2014-09-11 15:38:08 -05:00
}
2014-12-22 13:36:52 -08:00
err := hmc.Start()
gobottest.Assert(t, err, errors.New("write error"))
2014-12-22 13:36:52 -08:00
}
func TestHMC6352DriverHalt(t *testing.T) {
hmc := initTestHMC6352Driver()
gobottest.Assert(t, hmc.Halt(), nil)
2014-12-22 13:36:52 -08:00
}
func TestHMC6352DriverHeading(t *testing.T) {
// when len(data) is 2
hmc, adaptor := initTestHMC6352DriverWithStubbedAdaptor()
gobottest.Assert(t, hmc.Start(), nil)
adaptor.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
2014-12-22 13:36:52 -08:00
heading, _ := hmc.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
hmc, adaptor = initTestHMC6352DriverWithStubbedAdaptor()
gobottest.Assert(t, hmc.Start(), nil)
adaptor.i2cReadImpl = func(b []byte) (int, error) {
copy(b, []byte{99})
return 1, nil
2014-09-11 15:38:08 -05:00
}
2014-12-22 13:36:52 -08:00
heading, err := hmc.Heading()
gobottest.Assert(t, heading, uint16(0))
gobottest.Assert(t, err, ErrNotEnoughBytes)
2014-12-22 13:36:52 -08:00
// when read error
hmc, adaptor = initTestHMC6352DriverWithStubbedAdaptor()
gobottest.Assert(t, hmc.Start(), nil)
adaptor.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
2014-12-22 13:36:52 -08:00
heading, err = hmc.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
hmc, adaptor = initTestHMC6352DriverWithStubbedAdaptor()
gobottest.Assert(t, hmc.Start(), nil)
adaptor.i2cWriteImpl = func([]byte) (int, error) {
return 0, errors.New("write error")
2014-12-22 13:36:52 -08:00
}
heading, err = hmc.Heading()
gobottest.Assert(t, heading, uint16(0))
gobottest.Assert(t, err, errors.New("write error"))
2014-06-13 12:48:00 -07:00
}