gobot.io/x/gobot@v1.16.0/drivers/i2c/bmp280_driver_test.go (about) 1 package i2c 2 3 import ( 4 "bytes" 5 "errors" 6 "testing" 7 8 "gobot.io/x/gobot" 9 "gobot.io/x/gobot/gobottest" 10 ) 11 12 var _ gobot.Driver = (*BMP280Driver)(nil) 13 14 // --------- HELPERS 15 func initTestBMP280Driver() (driver *BMP280Driver) { 16 driver, _ = initTestBMP280DriverWithStubbedAdaptor() 17 return 18 } 19 20 func initTestBMP280DriverWithStubbedAdaptor() (*BMP280Driver, *i2cTestAdaptor) { 21 adaptor := newI2cTestAdaptor() 22 return NewBMP280Driver(adaptor), adaptor 23 } 24 25 // --------- TESTS 26 27 func TestNewBMP280Driver(t *testing.T) { 28 // Does it return a pointer to an instance of BME280Driver? 29 var bmp280 interface{} = NewBMP280Driver(newI2cTestAdaptor()) 30 _, ok := bmp280.(*BMP280Driver) 31 if !ok { 32 t.Errorf("NewBMP280Driver() should have returned a *BMP280Driver") 33 } 34 } 35 36 func TestBMP280Driver(t *testing.T) { 37 bmp280 := initTestBMP280Driver() 38 gobottest.Refute(t, bmp280.Connection(), nil) 39 } 40 41 func TestBMP280DriverStart(t *testing.T) { 42 bmp280, _ := initTestBMP280DriverWithStubbedAdaptor() 43 gobottest.Assert(t, bmp280.Start(), nil) 44 } 45 46 func TestBMP280StartConnectError(t *testing.T) { 47 d, adaptor := initTestBMP280DriverWithStubbedAdaptor() 48 adaptor.Testi2cConnectErr(true) 49 gobottest.Assert(t, d.Start(), errors.New("Invalid i2c connection")) 50 } 51 52 func TestBMP280DriverStartWriteError(t *testing.T) { 53 bmp280, adaptor := initTestBMP280DriverWithStubbedAdaptor() 54 adaptor.i2cWriteImpl = func([]byte) (int, error) { 55 return 0, errors.New("write error") 56 } 57 gobottest.Assert(t, bmp280.Start(), errors.New("write error")) 58 } 59 60 func TestBMP280DriverStartReadError(t *testing.T) { 61 bmp280, adaptor := initTestBMP280DriverWithStubbedAdaptor() 62 adaptor.i2cReadImpl = func(b []byte) (int, error) { 63 return 0, errors.New("read error") 64 } 65 gobottest.Assert(t, bmp280.Start(), errors.New("read error")) 66 } 67 68 func TestBMP280DriverHalt(t *testing.T) { 69 bmp280 := initTestBMP280Driver() 70 71 gobottest.Assert(t, bmp280.Halt(), nil) 72 } 73 74 func TestBMP280DriverMeasurements(t *testing.T) { 75 bmp280, adaptor := initTestBMP280DriverWithStubbedAdaptor() 76 adaptor.i2cReadImpl = func(b []byte) (int, error) { 77 buf := new(bytes.Buffer) 78 // Values produced by dumping data from actual sensor 79 if adaptor.written[len(adaptor.written)-1] == bmp280RegisterCalib00 { 80 buf.Write([]byte{126, 109, 214, 102, 50, 0, 54, 149, 220, 213, 208, 11, 64, 30, 166, 255, 249, 255, 172, 38, 10, 216, 189, 16}) 81 } else if adaptor.written[len(adaptor.written)-1] == bmp280RegisterTempData { 82 buf.Write([]byte{128, 243, 0}) 83 } else if adaptor.written[len(adaptor.written)-1] == bmp280RegisterPressureData { 84 buf.Write([]byte{77, 23, 48}) 85 } 86 copy(b, buf.Bytes()) 87 return buf.Len(), nil 88 } 89 bmp280.Start() 90 temp, err := bmp280.Temperature() 91 gobottest.Assert(t, err, nil) 92 gobottest.Assert(t, temp, float32(25.014637)) 93 pressure, err := bmp280.Pressure() 94 gobottest.Assert(t, err, nil) 95 gobottest.Assert(t, pressure, float32(99545.414)) 96 alt, err := bmp280.Altitude() 97 gobottest.Assert(t, err, nil) 98 gobottest.Assert(t, alt, float32(149.22713)) 99 } 100 101 func TestBMP280DriverTemperatureWriteError(t *testing.T) { 102 bmp280, adaptor := initTestBMP280DriverWithStubbedAdaptor() 103 bmp280.Start() 104 105 adaptor.i2cWriteImpl = func([]byte) (int, error) { 106 return 0, errors.New("write error") 107 } 108 temp, err := bmp280.Temperature() 109 gobottest.Assert(t, err, errors.New("write error")) 110 gobottest.Assert(t, temp, float32(0.0)) 111 } 112 113 func TestBMP280DriverTemperatureReadError(t *testing.T) { 114 bmp280, adaptor := initTestBMP280DriverWithStubbedAdaptor() 115 bmp280.Start() 116 117 adaptor.i2cReadImpl = func([]byte) (int, error) { 118 return 0, errors.New("read error") 119 } 120 temp, err := bmp280.Temperature() 121 gobottest.Assert(t, err, errors.New("read error")) 122 gobottest.Assert(t, temp, float32(0.0)) 123 } 124 125 func TestBMP280DriverPressureWriteError(t *testing.T) { 126 bmp280, adaptor := initTestBMP280DriverWithStubbedAdaptor() 127 bmp280.Start() 128 129 adaptor.i2cWriteImpl = func([]byte) (int, error) { 130 return 0, errors.New("write error") 131 } 132 press, err := bmp280.Pressure() 133 gobottest.Assert(t, err, errors.New("write error")) 134 gobottest.Assert(t, press, float32(0.0)) 135 } 136 137 func TestBMP280DriverPressureReadError(t *testing.T) { 138 bmp280, adaptor := initTestBMP280DriverWithStubbedAdaptor() 139 bmp280.Start() 140 141 adaptor.i2cReadImpl = func([]byte) (int, error) { 142 return 0, errors.New("read error") 143 } 144 press, err := bmp280.Pressure() 145 gobottest.Assert(t, err, errors.New("read error")) 146 gobottest.Assert(t, press, float32(0.0)) 147 } 148 149 func TestBMP280DriverSetName(t *testing.T) { 150 b := initTestBMP280Driver() 151 b.SetName("TESTME") 152 gobottest.Assert(t, b.Name(), "TESTME") 153 } 154 155 func TestBMP280DriverOptions(t *testing.T) { 156 b := NewBMP280Driver(newI2cTestAdaptor(), WithBus(2)) 157 gobottest.Assert(t, b.GetBusOrDefault(1), 2) 158 }