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

102 lines
2.2 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
"testing"
2014-09-11 15:38:08 -05:00
"time"
2014-10-28 16:22:05 -07:00
"github.com/hybridgroup/gobot"
)
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-10-28 16:22:05 -07:00
sem := make(chan bool)
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
hmc.SetInterval(1 * time.Millisecond)
gobot.Assert(t, hmc.Start(), nil)
2014-10-28 16:22:05 -07:00
go func() {
for {
<-time.After(time.Duration(numberOfCyclesForEvery) * time.Millisecond)
2014-11-19 16:56:48 -08:00
heading, _ := hmc.Heading()
if heading == uint16(2534) {
2014-10-28 16:22:05 -07:00
sem <- true
}
}
}()
select {
case <-sem:
case <-time.After(100 * time.Millisecond):
t.Errorf("Heading not read correctly")
}
2014-09-11 15:38:08 -05:00
// 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(), nil)
2014-10-28 16:22:05 -07:00
go func() {
for {
<-time.After(time.Duration(numberOfCyclesForEvery) * time.Millisecond)
2014-11-19 16:56:48 -08:00
heading, _ := hmc.Heading()
if heading == uint16(0) {
2014-10-28 16:22:05 -07:00
sem <- true
}
}
}()
select {
case <-sem:
case <-time.After(100 * time.Millisecond):
t.Errorf("Heading not read correctly")
}
2014-09-11 15:38:08 -05:00
}
func TestHMC6352DriverHalt(t *testing.T) {
hmc := initTestHMC6352Driver()
gobot.Assert(t, hmc.Halt(), nil)
2014-06-13 12:48:00 -07:00
}