gobot.io/x/gobot@v1.16.0/drivers/i2c/adxl345_driver_test.go (about) 1 package i2c 2 3 import ( 4 "bytes" 5 "errors" 6 "strings" 7 "testing" 8 9 "gobot.io/x/gobot" 10 "gobot.io/x/gobot/gobottest" 11 ) 12 13 var _ gobot.Driver = (*ADXL345Driver)(nil) 14 15 // --------- HELPERS 16 func initTestADXL345Driver() (driver *ADXL345Driver) { 17 driver, _ = initTestADXL345DriverWithStubbedAdaptor() 18 return 19 } 20 21 func initTestADXL345DriverWithStubbedAdaptor() (*ADXL345Driver, *i2cTestAdaptor) { 22 adaptor := newI2cTestAdaptor() 23 return NewADXL345Driver(adaptor), adaptor 24 } 25 26 // --------- TESTS 27 28 func TestNewADXL345Driver(t *testing.T) { 29 // Does it return a pointer to an instance of ADXL345Driver? 30 var mma interface{} = NewADXL345Driver(newI2cTestAdaptor()) 31 _, ok := mma.(*ADXL345Driver) 32 if !ok { 33 t.Errorf("NewADXL345Driver() should have returned a *ADXL345Driver") 34 } 35 } 36 37 // Methods 38 func TestADXL345Driver(t *testing.T) { 39 mma := initTestADXL345Driver() 40 41 gobottest.Refute(t, mma.Connection(), nil) 42 gobottest.Assert(t, strings.HasPrefix(mma.Name(), "ADXL345"), true) 43 } 44 45 func TestADXL345DriverSetName(t *testing.T) { 46 d := initTestADXL345Driver() 47 d.SetName("TESTME") 48 gobottest.Assert(t, d.Name(), "TESTME") 49 } 50 51 func TestADXL345DriverOptions(t *testing.T) { 52 d := NewADXL345Driver(newI2cTestAdaptor(), WithBus(2)) 53 gobottest.Assert(t, d.GetBusOrDefault(1), 2) 54 } 55 56 func TestADXL345DriverStart(t *testing.T) { 57 d := initTestADXL345Driver() 58 gobottest.Assert(t, d.Start(), nil) 59 } 60 61 func TestADXL345StartConnectError(t *testing.T) { 62 d, adaptor := initTestADXL345DriverWithStubbedAdaptor() 63 adaptor.Testi2cConnectErr(true) 64 gobottest.Assert(t, d.Start(), errors.New("Invalid i2c connection")) 65 } 66 67 func TestADXL345DriverStartWriteError(t *testing.T) { 68 mma, adaptor := initTestADXL345DriverWithStubbedAdaptor() 69 adaptor.i2cWriteImpl = func([]byte) (int, error) { 70 return 0, errors.New("write error") 71 } 72 gobottest.Assert(t, mma.Start(), errors.New("write error")) 73 } 74 75 func TestADXL345DriverHalt(t *testing.T) { 76 d := initTestADXL345Driver() 77 gobottest.Assert(t, d.Start(), nil) 78 gobottest.Assert(t, d.Halt(), nil) 79 } 80 81 func TestADXL345DriverNullXYZ(t *testing.T) { 82 d, _ := initTestADXL345DriverWithStubbedAdaptor() 83 d.Start() 84 x, y, z, _ := d.XYZ() 85 gobottest.Assert(t, x, 0.0) 86 gobottest.Assert(t, y, 0.0) 87 gobottest.Assert(t, z, 0.0) 88 } 89 90 func TestADXL345DriverXYZ(t *testing.T) { 91 d, adaptor := initTestADXL345DriverWithStubbedAdaptor() 92 d.Start() 93 94 adaptor.i2cReadImpl = func(b []byte) (int, error) { 95 buf := new(bytes.Buffer) 96 buf.Write([]byte{218, 0, 251, 255, 100, 0}) 97 copy(b, buf.Bytes()) 98 return buf.Len(), nil 99 } 100 101 x, y, z, _ := d.XYZ() 102 gobottest.Assert(t, x, 0.8515625) 103 gobottest.Assert(t, y, -0.01953125) 104 gobottest.Assert(t, z, 0.390625) 105 } 106 107 func TestADXL345DriverXYZError(t *testing.T) { 108 d, adaptor := initTestADXL345DriverWithStubbedAdaptor() 109 d.Start() 110 111 adaptor.i2cReadImpl = func(b []byte) (int, error) { 112 return 0, errors.New("read error") 113 } 114 115 _, _, _, err := d.XYZ() 116 gobottest.Assert(t, err, errors.New("read error")) 117 } 118 119 120 func TestADXL345DriverRawXYZ(t *testing.T) { 121 d, adaptor := initTestADXL345DriverWithStubbedAdaptor() 122 d.Start() 123 124 adaptor.i2cReadImpl = func(b []byte) (int, error) { 125 buf := new(bytes.Buffer) 126 buf.Write([]byte{218, 0, 251, 255, 100, 0}) 127 copy(b, buf.Bytes()) 128 return buf.Len(), nil 129 } 130 131 x, y, z, _ := d.RawXYZ() 132 gobottest.Assert(t, int(x), 218) 133 gobottest.Assert(t, int(y), -5) 134 gobottest.Assert(t, int(z), 100) 135 } 136 137 func TestADXL345DriverRawXYZError(t *testing.T) { 138 d, adaptor := initTestADXL345DriverWithStubbedAdaptor() 139 d.Start() 140 141 adaptor.i2cReadImpl = func(b []byte) (int, error) { 142 return 0, errors.New("read error") 143 } 144 145 _, _, _, err := d.RawXYZ() 146 gobottest.Assert(t, err, errors.New("read error")) 147 } 148 149 func TestADXL345DriverSetRange(t *testing.T) { 150 d := initTestADXL345Driver() 151 d.Start() 152 gobottest.Assert(t, d.SetRange(ADXL345_RANGE_16G), nil) 153 }