2015-02-18 04:11:02 +08:00
|
|
|
package i2c
|
|
|
|
|
|
|
|
import (
|
2017-04-04 20:34:06 +08:00
|
|
|
"bytes"
|
2015-02-18 04:11:02 +08:00
|
|
|
"errors"
|
|
|
|
"testing"
|
|
|
|
|
2016-12-21 01:59:26 +08:00
|
|
|
"gobot.io/x/gobot"
|
2016-12-08 20:24:03 +08:00
|
|
|
"gobot.io/x/gobot/gobottest"
|
2015-02-18 04:11:02 +08:00
|
|
|
)
|
|
|
|
|
2016-12-21 01:59:26 +08:00
|
|
|
var _ gobot.Driver = (*LIDARLiteDriver)(nil)
|
|
|
|
|
2015-02-18 04:11:02 +08:00
|
|
|
// --------- HELPERS
|
|
|
|
func initTestLIDARLiteDriver() (driver *LIDARLiteDriver) {
|
|
|
|
driver, _ = initTestLIDARLiteDriverWithStubbedAdaptor()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func initTestLIDARLiteDriverWithStubbedAdaptor() (*LIDARLiteDriver, *i2cTestAdaptor) {
|
2016-09-25 20:08:18 +08:00
|
|
|
adaptor := newI2cTestAdaptor()
|
|
|
|
return NewLIDARLiteDriver(adaptor), adaptor
|
2015-02-18 04:11:02 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// --------- TESTS
|
|
|
|
|
|
|
|
func TestNewLIDARLiteDriver(t *testing.T) {
|
|
|
|
// Does it return a pointer to an instance of LIDARLiteDriver?
|
2016-09-25 20:08:18 +08:00
|
|
|
var bm interface{} = NewLIDARLiteDriver(newI2cTestAdaptor())
|
2015-02-18 04:11:02 +08:00
|
|
|
_, ok := bm.(*LIDARLiteDriver)
|
|
|
|
if !ok {
|
|
|
|
t.Errorf("NewLIDARLiteDriver() should have returned a *LIDARLiteDriver")
|
|
|
|
}
|
|
|
|
|
2016-09-25 20:08:18 +08:00
|
|
|
b := NewLIDARLiteDriver(newI2cTestAdaptor())
|
|
|
|
gobottest.Refute(t, b.Connection(), nil)
|
2015-02-18 04:11:02 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Methods
|
|
|
|
func TestLIDARLiteDriverStart(t *testing.T) {
|
2017-02-06 07:19:42 +08:00
|
|
|
hmc, _ := initTestLIDARLiteDriverWithStubbedAdaptor()
|
2015-02-18 04:11:02 +08:00
|
|
|
|
2016-11-08 00:21:43 +08:00
|
|
|
gobottest.Assert(t, hmc.Start(), nil)
|
2015-02-18 04:11:02 +08:00
|
|
|
}
|
|
|
|
|
2017-04-12 17:47:42 +08:00
|
|
|
func TestLIDARLiteStartConnectError(t *testing.T) {
|
|
|
|
d, adaptor := initTestLIDARLiteDriverWithStubbedAdaptor()
|
|
|
|
adaptor.Testi2cConnectErr(true)
|
|
|
|
gobottest.Assert(t, d.Start(), errors.New("Invalid i2c connection"))
|
|
|
|
}
|
|
|
|
|
2015-02-18 04:11:02 +08:00
|
|
|
func TestLIDARLiteDriverHalt(t *testing.T) {
|
|
|
|
hmc := initTestLIDARLiteDriver()
|
|
|
|
|
2016-11-08 00:21:43 +08:00
|
|
|
gobottest.Assert(t, hmc.Halt(), nil)
|
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()
|
|
|
|
|
2017-02-06 07:19:42 +08:00
|
|
|
gobottest.Assert(t, hmc.Start(), nil)
|
|
|
|
|
2015-07-13 06:22:23 +08:00
|
|
|
first := true
|
2017-02-06 07:19:42 +08:00
|
|
|
adaptor.i2cReadImpl = func(b []byte) (int, error) {
|
2015-07-13 06:22:23 +08:00
|
|
|
if first {
|
|
|
|
first = false
|
2017-02-06 07:19:42 +08:00
|
|
|
copy(b, []byte{99})
|
|
|
|
return 1, nil
|
2015-07-13 06:22:23 +08:00
|
|
|
}
|
2017-02-06 07:19:42 +08:00
|
|
|
copy(b, []byte{1})
|
|
|
|
return 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()
|
|
|
|
|
2017-02-06 07:19:42 +08:00
|
|
|
gobottest.Assert(t, hmc.Start(), nil)
|
|
|
|
|
|
|
|
adaptor.i2cReadImpl = func([]byte) (int, error) {
|
|
|
|
return 0, 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()
|
|
|
|
|
2017-02-06 07:19:42 +08:00
|
|
|
gobottest.Assert(t, hmc.Start(), nil)
|
|
|
|
|
|
|
|
adaptor.i2cReadImpl = func([]byte) (int, error) {
|
|
|
|
return 0, errors.New("read error")
|
2015-02-18 04:11:02 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
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"))
|
2017-04-04 20:34:06 +08:00
|
|
|
}
|
2015-02-18 04:11:02 +08:00
|
|
|
|
2017-04-04 20:34:06 +08:00
|
|
|
func TestLIDARLiteDriverDistanceError1(t *testing.T) {
|
|
|
|
hmc, adaptor := initTestLIDARLiteDriverWithStubbedAdaptor()
|
2017-02-06 07:19:42 +08:00
|
|
|
gobottest.Assert(t, hmc.Start(), nil)
|
|
|
|
|
|
|
|
adaptor.i2cWriteImpl = func([]byte) (int, error) {
|
|
|
|
return 0, errors.New("write error")
|
2015-02-18 04:11:02 +08:00
|
|
|
}
|
|
|
|
|
2017-04-04 20:34:06 +08:00
|
|
|
distance, err := hmc.Distance()
|
|
|
|
gobottest.Assert(t, distance, int(0))
|
|
|
|
gobottest.Assert(t, err, errors.New("write error"))
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestLIDARLiteDriverDistanceError2(t *testing.T) {
|
|
|
|
hmc, adaptor := initTestLIDARLiteDriverWithStubbedAdaptor()
|
|
|
|
gobottest.Assert(t, hmc.Start(), nil)
|
|
|
|
|
|
|
|
adaptor.i2cWriteImpl = func(b []byte) (int, error) {
|
|
|
|
if b[0] == 0x0f {
|
|
|
|
return 0, errors.New("write error")
|
|
|
|
}
|
|
|
|
return len(b), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
distance, err := hmc.Distance()
|
|
|
|
gobottest.Assert(t, distance, int(0))
|
|
|
|
gobottest.Assert(t, err, errors.New("write error"))
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestLIDARLiteDriverDistanceError3(t *testing.T) {
|
|
|
|
hmc, adaptor := initTestLIDARLiteDriverWithStubbedAdaptor()
|
|
|
|
gobottest.Assert(t, hmc.Start(), nil)
|
|
|
|
|
|
|
|
adaptor.i2cWriteImpl = func(b []byte) (int, error) {
|
|
|
|
if b[0] == 0x10 {
|
|
|
|
return 0, errors.New("write error")
|
|
|
|
}
|
|
|
|
return len(b), nil
|
|
|
|
}
|
|
|
|
adaptor.i2cReadImpl = func(b []byte) (int, error) {
|
|
|
|
buf := new(bytes.Buffer)
|
|
|
|
buf.Write([]byte{0x03})
|
|
|
|
copy(b, buf.Bytes())
|
|
|
|
return buf.Len(), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
}
|
2017-02-11 18:53:19 +08:00
|
|
|
|
|
|
|
func TestLIDARLiteDriverSetName(t *testing.T) {
|
|
|
|
l := initTestLIDARLiteDriver()
|
|
|
|
l.SetName("TESTME")
|
|
|
|
gobottest.Assert(t, l.Name(), "TESTME")
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestLIDARLiteDriverOptions(t *testing.T) {
|
|
|
|
l := NewLIDARLiteDriver(newI2cTestAdaptor(), WithBus(2))
|
|
|
|
gobottest.Assert(t, l.GetBusOrDefault(1), 2)
|
|
|
|
}
|