gobot.io/x/gobot@v1.16.0/drivers/i2c/bmp388_driver_test.go (about) 1 package i2c 2 3 import ( 4 "bytes" 5 "encoding/binary" 6 "errors" 7 "testing" 8 9 "gobot.io/x/gobot" 10 "gobot.io/x/gobot/gobottest" 11 ) 12 13 var _ gobot.Driver = (*BMP388Driver)(nil) 14 15 // --------- HELPERS 16 func initTestBMP388Driver() (driver *BMP388Driver) { 17 driver, _ = initTestBMP388DriverWithStubbedAdaptor() 18 return 19 } 20 21 func initTestBMP388DriverWithStubbedAdaptor() (*BMP388Driver, *i2cTestAdaptor) { 22 adaptor := newI2cTestAdaptor() 23 adaptor.i2cReadImpl = func(b []byte) (int, error) { 24 buf := new(bytes.Buffer) 25 // Simulate returning of 0x50 for the 26 // ReadByteData(bmp388RegisterChipID) call in initialisation() 27 binary.Write(buf, binary.LittleEndian, uint8(0x50)) 28 copy(b, buf.Bytes()) 29 return buf.Len(), nil 30 } 31 return NewBMP388Driver(adaptor), adaptor 32 } 33 34 // --------- TESTS 35 36 func TestNewBMP388Driver(t *testing.T) { 37 // Does it return a pointer to an instance of BMP388Driver? 38 var bmp388 interface{} = NewBMP388Driver(newI2cTestAdaptor()) 39 _, ok := bmp388.(*BMP388Driver) 40 if !ok { 41 t.Errorf("NewBMP388Driver() should have returned a *BMP388Driver") 42 } 43 } 44 45 func TestBMP388Driver(t *testing.T) { 46 bmp388 := initTestBMP388Driver() 47 gobottest.Refute(t, bmp388.Connection(), nil) 48 } 49 50 func TestBMP388DriverStart(t *testing.T) { 51 bmp388, _ := initTestBMP388DriverWithStubbedAdaptor() 52 gobottest.Assert(t, bmp388.Start(), nil) 53 } 54 55 func TestBMP388StartConnectError(t *testing.T) { 56 bmp388, adaptor := initTestBMP388DriverWithStubbedAdaptor() 57 adaptor.Testi2cConnectErr(true) 58 gobottest.Assert(t, bmp388.Start(), errors.New("Invalid i2c connection")) 59 } 60 61 func TestBMP388DriverStartWriteError(t *testing.T) { 62 bmp388, adaptor := initTestBMP388DriverWithStubbedAdaptor() 63 adaptor.i2cWriteImpl = func([]byte) (int, error) { 64 return 0, errors.New("write error") 65 } 66 gobottest.Assert(t, bmp388.Start(), errors.New("write error")) 67 } 68 69 func TestBMP388DriverStartReadError(t *testing.T) { 70 bmp388, adaptor := initTestBMP388DriverWithStubbedAdaptor() 71 adaptor.i2cReadImpl = func(b []byte) (int, error) { 72 return 0, errors.New("read error") 73 } 74 gobottest.Assert(t, bmp388.Start(), errors.New("read error")) 75 } 76 77 func TestBMP388DriverHalt(t *testing.T) { 78 bmp388 := initTestBMP388Driver() 79 80 gobottest.Assert(t, bmp388.Halt(), nil) 81 } 82 83 func TestBMP388DriverMeasurements(t *testing.T) { 84 bmp388, adaptor := initTestBMP388DriverWithStubbedAdaptor() 85 adaptor.i2cReadImpl = func(b []byte) (int, error) { 86 buf := new(bytes.Buffer) 87 lastWritten := adaptor.written[len(adaptor.written)-1] 88 switch lastWritten { 89 case bmp388RegisterChipID: 90 // Simulate returning of 0x50 for the 91 // ReadByteData(bmp388RegisterChipID) call in initialisation() 92 binary.Write(buf, binary.LittleEndian, uint8(0x50)) 93 case bmp388RegisterCalib00: 94 // Values produced by dumping data from actual sensor 95 buf.Write([]byte{36, 107, 156, 73, 246, 104, 255, 189, 245, 35, 0, 151, 101, 184, 122, 243, 246, 211, 64, 14, 196, 0, 0, 0}) 96 case bmp388RegisterTempData: 97 buf.Write([]byte{0, 28, 127}) 98 case bmp388RegisterPressureData: 99 buf.Write([]byte{0, 66, 113}) 100 } 101 102 copy(b, buf.Bytes()) 103 return buf.Len(), nil 104 } 105 bmp388.Start() 106 temp, err := bmp388.Temperature(2) 107 gobottest.Assert(t, err, nil) 108 gobottest.Assert(t, temp, float32(22.906143)) 109 pressure, err := bmp388.Pressure(2) 110 gobottest.Assert(t, err, nil) 111 gobottest.Assert(t, pressure, float32(98874.85)) 112 alt, err := bmp388.Altitude(2) 113 gobottest.Assert(t, err, nil) 114 gobottest.Assert(t, alt, float32(205.89395)) 115 } 116 117 func TestBMP388DriverTemperatureWriteError(t *testing.T) { 118 bmp388, adaptor := initTestBMP388DriverWithStubbedAdaptor() 119 bmp388.Start() 120 121 adaptor.i2cWriteImpl = func([]byte) (int, error) { 122 return 0, errors.New("write error") 123 } 124 temp, err := bmp388.Temperature(2) 125 gobottest.Assert(t, err, errors.New("write error")) 126 gobottest.Assert(t, temp, float32(0.0)) 127 } 128 129 func TestBMP388DriverTemperatureReadError(t *testing.T) { 130 bmp388, adaptor := initTestBMP388DriverWithStubbedAdaptor() 131 bmp388.Start() 132 133 adaptor.i2cReadImpl = func([]byte) (int, error) { 134 return 0, errors.New("read error") 135 } 136 temp, err := bmp388.Temperature(2) 137 gobottest.Assert(t, err, errors.New("read error")) 138 gobottest.Assert(t, temp, float32(0.0)) 139 } 140 141 func TestBMP388DriverPressureWriteError(t *testing.T) { 142 bmp388, adaptor := initTestBMP388DriverWithStubbedAdaptor() 143 bmp388.Start() 144 145 adaptor.i2cWriteImpl = func([]byte) (int, error) { 146 return 0, errors.New("write error") 147 } 148 press, err := bmp388.Pressure(2) 149 gobottest.Assert(t, err, errors.New("write error")) 150 gobottest.Assert(t, press, float32(0.0)) 151 } 152 153 func TestBMP388DriverPressureReadError(t *testing.T) { 154 bmp388, adaptor := initTestBMP388DriverWithStubbedAdaptor() 155 bmp388.Start() 156 157 adaptor.i2cReadImpl = func([]byte) (int, error) { 158 return 0, errors.New("read error") 159 } 160 press, err := bmp388.Pressure(2) 161 gobottest.Assert(t, err, errors.New("read error")) 162 gobottest.Assert(t, press, float32(0.0)) 163 } 164 165 func TestBMP388DriverSetName(t *testing.T) { 166 b := initTestBMP388Driver() 167 b.SetName("TESTME") 168 gobottest.Assert(t, b.Name(), "TESTME") 169 } 170 171 func TestBMP388DriverOptions(t *testing.T) { 172 b := NewBMP388Driver(newI2cTestAdaptor(), WithBus(2)) 173 gobottest.Assert(t, b.GetBusOrDefault(1), 2) 174 }