2014-11-10 07:37:12 +08:00
|
|
|
package i2c
|
|
|
|
|
|
|
|
import (
|
2017-04-04 17:50:31 +08:00
|
|
|
"errors"
|
2017-02-09 20:26:53 +08:00
|
|
|
"strings"
|
2014-11-10 07:37:12 +08:00
|
|
|
"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"
|
2014-11-10 07:37:12 +08:00
|
|
|
)
|
|
|
|
|
2017-02-11 18:53:19 +08:00
|
|
|
// ensure that MPU6050Driver fulfills Gobot Driver interface
|
2016-12-21 01:59:26 +08:00
|
|
|
var _ gobot.Driver = (*MPU6050Driver)(nil)
|
|
|
|
|
2014-11-10 07:37:12 +08:00
|
|
|
// --------- HELPERS
|
|
|
|
func initTestMPU6050Driver() (driver *MPU6050Driver) {
|
|
|
|
driver, _ = initTestMPU6050DriverWithStubbedAdaptor()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func initTestMPU6050DriverWithStubbedAdaptor() (*MPU6050Driver, *i2cTestAdaptor) {
|
2016-09-25 20:08:18 +08:00
|
|
|
adaptor := newI2cTestAdaptor()
|
|
|
|
return NewMPU6050Driver(adaptor), adaptor
|
2014-11-10 07:37:12 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// --------- TESTS
|
|
|
|
|
|
|
|
func TestNewMPU6050Driver(t *testing.T) {
|
|
|
|
// Does it return a pointer to an instance of MPU6050Driver?
|
2016-09-25 20:08:18 +08:00
|
|
|
var bm interface{} = NewMPU6050Driver(newI2cTestAdaptor())
|
2014-11-10 07:37:12 +08:00
|
|
|
_, ok := bm.(*MPU6050Driver)
|
|
|
|
if !ok {
|
|
|
|
t.Errorf("NewMPU6050Driver() should have returned a *MPU6050Driver")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-02-11 18:53:19 +08:00
|
|
|
func TestMPU6050DriverName(t *testing.T) {
|
2014-12-23 05:36:52 +08:00
|
|
|
mpu := initTestMPU6050Driver()
|
2016-09-25 20:08:18 +08:00
|
|
|
gobottest.Refute(t, mpu.Connection(), nil)
|
2017-02-11 18:53:19 +08:00
|
|
|
gobottest.Assert(t, strings.HasPrefix(mpu.Name(), "MPU6050"), true)
|
|
|
|
}
|
2014-12-23 05:36:52 +08:00
|
|
|
|
2017-02-11 18:53:19 +08:00
|
|
|
func TestMPU6050DriverOptions(t *testing.T) {
|
|
|
|
mpu := NewMPU6050Driver(newI2cTestAdaptor(), WithBus(2))
|
2017-02-10 18:08:32 +08:00
|
|
|
gobottest.Assert(t, mpu.GetBusOrDefault(1), 2)
|
2014-12-23 05:36:52 +08:00
|
|
|
}
|
|
|
|
|
2014-11-10 07:37:12 +08:00
|
|
|
// Methods
|
|
|
|
func TestMPU6050DriverStart(t *testing.T) {
|
|
|
|
mpu := initTestMPU6050Driver()
|
|
|
|
|
2016-11-08 00:21:43 +08:00
|
|
|
gobottest.Assert(t, mpu.Start(), nil)
|
2014-11-10 07:37:12 +08:00
|
|
|
}
|
|
|
|
|
2017-04-12 17:47:42 +08:00
|
|
|
func TestMPU6050StartConnectError(t *testing.T) {
|
|
|
|
d, adaptor := initTestMPU6050DriverWithStubbedAdaptor()
|
|
|
|
adaptor.Testi2cConnectErr(true)
|
|
|
|
gobottest.Assert(t, d.Start(), errors.New("Invalid i2c connection"))
|
|
|
|
}
|
|
|
|
|
2017-04-04 17:50:31 +08:00
|
|
|
func TestMPU6050DriverStartWriteError(t *testing.T) {
|
|
|
|
mpu, adaptor := initTestMPU6050DriverWithStubbedAdaptor()
|
|
|
|
adaptor.i2cWriteImpl = func([]byte) (int, error) {
|
|
|
|
return 0, errors.New("write error")
|
|
|
|
}
|
|
|
|
gobottest.Assert(t, mpu.Start(), errors.New("write error"))
|
|
|
|
}
|
|
|
|
|
2014-11-10 07:37:12 +08:00
|
|
|
func TestMPU6050DriverHalt(t *testing.T) {
|
|
|
|
mpu := initTestMPU6050Driver()
|
|
|
|
|
2016-11-08 00:21:43 +08:00
|
|
|
gobottest.Assert(t, mpu.Halt(), nil)
|
2014-11-10 07:37:12 +08:00
|
|
|
}
|
2017-02-11 17:37:34 +08:00
|
|
|
|
|
|
|
func TestMPU6050DriverReadData(t *testing.T) {
|
|
|
|
mpu, adaptor := initTestMPU6050DriverWithStubbedAdaptor()
|
|
|
|
|
|
|
|
adaptor.i2cReadImpl = func(b []byte) (int, error) {
|
|
|
|
copy(b, []byte{0x00, 0x01, 0x02, 0x04})
|
|
|
|
return 4, nil
|
|
|
|
}
|
|
|
|
mpu.Start()
|
|
|
|
mpu.GetData()
|
|
|
|
gobottest.Assert(t, mpu.Temperature, int16(36))
|
|
|
|
}
|
2017-02-11 18:53:19 +08:00
|
|
|
|
2017-04-12 22:36:27 +08:00
|
|
|
func TestMPU6050DriverGetDataReadError(t *testing.T) {
|
2017-04-04 17:50:31 +08:00
|
|
|
mpu, adaptor := initTestMPU6050DriverWithStubbedAdaptor()
|
|
|
|
mpu.Start()
|
|
|
|
|
|
|
|
adaptor.i2cReadImpl = func(b []byte) (int, error) {
|
2017-04-12 22:36:27 +08:00
|
|
|
return 0, errors.New("read error")
|
|
|
|
}
|
|
|
|
|
|
|
|
gobottest.Assert(t, mpu.GetData(), errors.New("read error"))
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestMPU6050DriverGetDataWriteError(t *testing.T) {
|
|
|
|
mpu, adaptor := initTestMPU6050DriverWithStubbedAdaptor()
|
|
|
|
mpu.Start()
|
|
|
|
|
|
|
|
adaptor.i2cWriteImpl = func(b []byte) (int, error) {
|
2017-04-04 17:50:31 +08:00
|
|
|
return 0, errors.New("write error")
|
|
|
|
}
|
|
|
|
|
|
|
|
gobottest.Assert(t, mpu.GetData(), errors.New("write error"))
|
|
|
|
}
|
|
|
|
|
2017-02-11 18:53:19 +08:00
|
|
|
func TestMPU6050DriverSetName(t *testing.T) {
|
|
|
|
mpu := initTestMPU6050Driver()
|
|
|
|
mpu.SetName("TESTME")
|
|
|
|
gobottest.Assert(t, mpu.Name(), "TESTME")
|
|
|
|
}
|