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

86 lines
2.1 KiB
Go
Raw Normal View History

2014-04-27 18:54:41 -07:00
package i2c
import (
2014-06-13 12:48:00 -07:00
"github.com/hybridgroup/gobot"
"testing"
2014-09-11 15:38:08 -05:00
"time"
)
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("adaptor")
return NewHMC6352Driver(adaptor, "bot"), adaptor
}
// --------- TESTS
func TestHMC6352Driver(t *testing.T) {
// Does it implement gobot.DriverInterface?
var _ gobot.DriverInterface = (*HMC6352Driver)(nil)
// Does its adaptor implements the I2cInterface?
driver := initTestHMC6352Driver()
var _ I2cInterface = driver.adaptor()
}
func TestNewHMC6352Driver(t *testing.T) {
// Does it return a pointer to an instance of HMC6352Driver?
var bm interface{} = NewHMC6352Driver(newI2cTestAdaptor("adaptor"), "bot")
_, ok := bm.(*HMC6352Driver)
if !ok {
t.Errorf("NewHMC6352Driver() should have returned a *HMC6352Driver")
}
}
// Methods
2014-06-13 16:01:39 -07:00
func TestHMC6352DriverStart(t *testing.T) {
2014-09-11 15:38:08 -05:00
// when len(data) is 2
hmc, adaptor := initTestHMC6352DriverWithStubbedAdaptor()
adaptor.i2cReadImpl = func() []byte {
return []byte{99, 1}
}
numberOfCyclesForEvery := 3
// Make sure "Heading" is set to 0 so that we assert
// its new value after executing "Start()"
gobot.Assert(t, hmc.Heading, uint16(0))
hmc.SetInterval(1 * time.Millisecond)
gobot.Assert(t, hmc.Start(), true)
<-time.After(time.Duration(numberOfCyclesForEvery) * time.Millisecond)
gobot.Assert(t, hmc.Heading, uint16(2534))
// when len(data) is not 2
hmc, adaptor = initTestHMC6352DriverWithStubbedAdaptor()
adaptor.i2cReadImpl = func() []byte {
return []byte{99}
}
hmc.SetInterval(1 * time.Millisecond)
gobot.Assert(t, hmc.Start(), true)
<-time.After(time.Duration(numberOfCyclesForEvery) * time.Millisecond)
gobot.Assert(t, hmc.Heading, uint16(0))
}
func TestHMC6352DriverInit(t *testing.T) {
hmc := initTestHMC6352Driver()
gobot.Assert(t, hmc.Init(), true)
}
func TestHMC6352DriverHalt(t *testing.T) {
hmc := initTestHMC6352Driver()
gobot.Assert(t, hmc.Halt(), true)
2014-06-13 12:48:00 -07:00
}