gobot.io/x/gobot@v1.16.0/drivers/i2c/mpu6050_driver_test.go (about) 1 package i2c 2 3 import ( 4 "errors" 5 "strings" 6 "testing" 7 8 "gobot.io/x/gobot" 9 "gobot.io/x/gobot/gobottest" 10 ) 11 12 // ensure that MPU6050Driver fulfills Gobot Driver interface 13 var _ gobot.Driver = (*MPU6050Driver)(nil) 14 15 // --------- HELPERS 16 func initTestMPU6050Driver() (driver *MPU6050Driver) { 17 driver, _ = initTestMPU6050DriverWithStubbedAdaptor() 18 return 19 } 20 21 func initTestMPU6050DriverWithStubbedAdaptor() (*MPU6050Driver, *i2cTestAdaptor) { 22 adaptor := newI2cTestAdaptor() 23 return NewMPU6050Driver(adaptor), adaptor 24 } 25 26 // --------- TESTS 27 28 func TestNewMPU6050Driver(t *testing.T) { 29 // Does it return a pointer to an instance of MPU6050Driver? 30 var bm interface{} = NewMPU6050Driver(newI2cTestAdaptor()) 31 _, ok := bm.(*MPU6050Driver) 32 if !ok { 33 t.Errorf("NewMPU6050Driver() should have returned a *MPU6050Driver") 34 } 35 } 36 37 func TestMPU6050DriverName(t *testing.T) { 38 mpu := initTestMPU6050Driver() 39 gobottest.Refute(t, mpu.Connection(), nil) 40 gobottest.Assert(t, strings.HasPrefix(mpu.Name(), "MPU6050"), true) 41 } 42 43 func TestMPU6050DriverOptions(t *testing.T) { 44 mpu := NewMPU6050Driver(newI2cTestAdaptor(), WithBus(2)) 45 gobottest.Assert(t, mpu.GetBusOrDefault(1), 2) 46 } 47 48 // Methods 49 func TestMPU6050DriverStart(t *testing.T) { 50 mpu := initTestMPU6050Driver() 51 52 gobottest.Assert(t, mpu.Start(), nil) 53 } 54 55 func TestMPU6050StartConnectError(t *testing.T) { 56 d, adaptor := initTestMPU6050DriverWithStubbedAdaptor() 57 adaptor.Testi2cConnectErr(true) 58 gobottest.Assert(t, d.Start(), errors.New("Invalid i2c connection")) 59 } 60 61 func TestMPU6050DriverStartWriteError(t *testing.T) { 62 mpu, adaptor := initTestMPU6050DriverWithStubbedAdaptor() 63 adaptor.i2cWriteImpl = func([]byte) (int, error) { 64 return 0, errors.New("write error") 65 } 66 gobottest.Assert(t, mpu.Start(), errors.New("write error")) 67 } 68 69 func TestMPU6050DriverHalt(t *testing.T) { 70 mpu := initTestMPU6050Driver() 71 72 gobottest.Assert(t, mpu.Halt(), nil) 73 } 74 75 func TestMPU6050DriverReadData(t *testing.T) { 76 mpu, adaptor := initTestMPU6050DriverWithStubbedAdaptor() 77 78 adaptor.i2cReadImpl = func(b []byte) (int, error) { 79 copy(b, []byte{0x00, 0x01, 0x02, 0x04}) 80 return 4, nil 81 } 82 mpu.Start() 83 mpu.GetData() 84 gobottest.Assert(t, mpu.Temperature, int16(36)) 85 } 86 87 func TestMPU6050DriverGetDataReadError(t *testing.T) { 88 mpu, adaptor := initTestMPU6050DriverWithStubbedAdaptor() 89 mpu.Start() 90 91 adaptor.i2cReadImpl = func(b []byte) (int, error) { 92 return 0, errors.New("read error") 93 } 94 95 gobottest.Assert(t, mpu.GetData(), errors.New("read error")) 96 } 97 98 func TestMPU6050DriverGetDataWriteError(t *testing.T) { 99 mpu, adaptor := initTestMPU6050DriverWithStubbedAdaptor() 100 mpu.Start() 101 102 adaptor.i2cWriteImpl = func(b []byte) (int, error) { 103 return 0, errors.New("write error") 104 } 105 106 gobottest.Assert(t, mpu.GetData(), errors.New("write error")) 107 } 108 109 func TestMPU6050DriverSetName(t *testing.T) { 110 mpu := initTestMPU6050Driver() 111 mpu.SetName("TESTME") 112 gobottest.Assert(t, mpu.Name(), "TESTME") 113 }