2015-02-18 04:11:02 +08:00
|
|
|
package i2c
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"testing"
|
|
|
|
|
2016-02-22 13:21:24 +08:00
|
|
|
"github.com/hybridgroup/gobot/gobottest"
|
2015-02-18 04:11:02 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
// --------- HELPERS
|
|
|
|
func initTestLIDARLiteDriver() (driver *LIDARLiteDriver) {
|
|
|
|
driver, _ = initTestLIDARLiteDriverWithStubbedAdaptor()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func initTestLIDARLiteDriverWithStubbedAdaptor() (*LIDARLiteDriver, *i2cTestAdaptor) {
|
|
|
|
adaptor := newI2cTestAdaptor("adaptor")
|
|
|
|
return NewLIDARLiteDriver(adaptor, "bot"), adaptor
|
|
|
|
}
|
|
|
|
|
|
|
|
// --------- TESTS
|
|
|
|
|
|
|
|
func TestNewLIDARLiteDriver(t *testing.T) {
|
|
|
|
// Does it return a pointer to an instance of LIDARLiteDriver?
|
|
|
|
var bm interface{} = NewLIDARLiteDriver(newI2cTestAdaptor("adaptor"), "bot")
|
|
|
|
_, ok := bm.(*LIDARLiteDriver)
|
|
|
|
if !ok {
|
|
|
|
t.Errorf("NewLIDARLiteDriver() should have returned a *LIDARLiteDriver")
|
|
|
|
}
|
|
|
|
|
|
|
|
b := NewLIDARLiteDriver(newI2cTestAdaptor("adaptor"), "bot")
|
2016-02-22 13:21:24 +08:00
|
|
|
gobottest.Assert(t, b.Name(), "bot")
|
|
|
|
gobottest.Assert(t, b.Connection().Name(), "adaptor")
|
2015-02-18 04:11:02 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Methods
|
|
|
|
func TestLIDARLiteDriverStart(t *testing.T) {
|
|
|
|
hmc, adaptor := initTestLIDARLiteDriverWithStubbedAdaptor()
|
|
|
|
|
2016-02-22 13:21:24 +08:00
|
|
|
gobottest.Assert(t, len(hmc.Start()), 0)
|
2015-02-18 04:11:02 +08:00
|
|
|
|
|
|
|
adaptor.i2cStartImpl = func() error {
|
|
|
|
return errors.New("start error")
|
|
|
|
}
|
|
|
|
err := hmc.Start()
|
2016-02-22 13:21:24 +08:00
|
|
|
gobottest.Assert(t, err[0], errors.New("start error"))
|
2015-02-18 04:11:02 +08:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestLIDARLiteDriverHalt(t *testing.T) {
|
|
|
|
hmc := initTestLIDARLiteDriver()
|
|
|
|
|
2016-02-22 13:21:24 +08:00
|
|
|
gobottest.Assert(t, len(hmc.Halt()), 0)
|
2015-02-18 04:11:02 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestLIDARLiteDriverDistance(t *testing.T) {
|
2015-07-13 06:22:23 +08:00
|
|
|
// when everything is happy
|
2015-02-18 04:11:02 +08:00
|
|
|
hmc, adaptor := initTestLIDARLiteDriverWithStubbedAdaptor()
|
|
|
|
|
2015-07-13 06:22:23 +08:00
|
|
|
first := true
|
2015-02-18 04:11:02 +08:00
|
|
|
adaptor.i2cReadImpl = func() ([]byte, error) {
|
2015-07-13 06:22:23 +08:00
|
|
|
if first {
|
|
|
|
first = false
|
|
|
|
return []byte{99}, nil
|
|
|
|
}
|
|
|
|
return []byte{1}, nil
|
2015-02-18 04:11:02 +08:00
|
|
|
}
|
|
|
|
|
2015-07-13 06:22:23 +08:00
|
|
|
distance, err := hmc.Distance()
|
|
|
|
|
2016-02-22 13:21:24 +08:00
|
|
|
gobottest.Assert(t, err, nil)
|
|
|
|
gobottest.Assert(t, distance, int(25345))
|
2015-02-18 04:11:02 +08:00
|
|
|
|
2015-07-13 06:22:23 +08:00
|
|
|
// when insufficient bytes have been read
|
2015-02-18 04:11:02 +08:00
|
|
|
hmc, adaptor = initTestLIDARLiteDriverWithStubbedAdaptor()
|
|
|
|
|
|
|
|
adaptor.i2cReadImpl = func() ([]byte, error) {
|
2015-07-13 06:22:23 +08:00
|
|
|
return []byte{}, nil
|
2015-02-18 04:11:02 +08:00
|
|
|
}
|
|
|
|
|
2015-07-13 06:22:23 +08:00
|
|
|
distance, err = hmc.Distance()
|
2016-02-22 13:21:24 +08:00
|
|
|
gobottest.Assert(t, distance, int(0))
|
|
|
|
gobottest.Assert(t, err, ErrNotEnoughBytes)
|
2015-02-18 04:11:02 +08:00
|
|
|
|
|
|
|
// when read error
|
|
|
|
hmc, adaptor = initTestLIDARLiteDriverWithStubbedAdaptor()
|
|
|
|
|
|
|
|
adaptor.i2cReadImpl = func() ([]byte, error) {
|
|
|
|
return []byte{}, errors.New("read error")
|
|
|
|
}
|
|
|
|
|
|
|
|
distance, err = hmc.Distance()
|
2016-02-22 13:21:24 +08:00
|
|
|
gobottest.Assert(t, distance, int(0))
|
|
|
|
gobottest.Assert(t, err, errors.New("read error"))
|
2015-02-18 04:11:02 +08:00
|
|
|
|
|
|
|
// when write error
|
|
|
|
hmc, adaptor = initTestLIDARLiteDriverWithStubbedAdaptor()
|
|
|
|
|
|
|
|
adaptor.i2cWriteImpl = func() error {
|
|
|
|
return errors.New("write error")
|
|
|
|
}
|
|
|
|
|
|
|
|
distance, err = hmc.Distance()
|
2016-02-22 13:21:24 +08:00
|
|
|
gobottest.Assert(t, distance, int(0))
|
|
|
|
gobottest.Assert(t, err, errors.New("write error"))
|
2015-02-18 04:11:02 +08:00
|
|
|
}
|