From e14bfb726fae8e4dbb5ca3c9191246ffcb85add3 Mon Sep 17 00:00:00 2001 From: deadprogram Date: Sat, 1 Apr 2017 13:21:38 +0200 Subject: [PATCH] i2c: add test coverage for bmp280 Signed-off-by: deadprogram --- drivers/i2c/bmp280_driver_test.go | 26 +++++++++++++++++++++++++- 1 file changed, 25 insertions(+), 1 deletion(-) diff --git a/drivers/i2c/bmp280_driver_test.go b/drivers/i2c/bmp280_driver_test.go index eb735a6c..c52dd108 100644 --- a/drivers/i2c/bmp280_driver_test.go +++ b/drivers/i2c/bmp280_driver_test.go @@ -1,6 +1,7 @@ package i2c import ( + "bytes" "testing" "gobot.io/x/gobot" @@ -49,7 +50,30 @@ func TestBMP280DriverHalt(t *testing.T) { // TODO: implement test func TestBMP280DriverMeasurements(t *testing.T) { - + bmp280, adaptor := initTestBMP280DriverWithStubbedAdaptor() + adaptor.i2cReadImpl = func(b []byte) (int, error) { + buf := new(bytes.Buffer) + // Values produced by dumping data from actual sensor + if adaptor.written[len(adaptor.written)-1] == bmp280RegisterCalib00 { + 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}) + } else if adaptor.written[len(adaptor.written)-1] == bmp280RegisterTempData { + buf.Write([]byte{128, 243, 0}) + } else if adaptor.written[len(adaptor.written)-1] == bmp280RegisterPressureData { + buf.Write([]byte{77, 23, 48}) + } + copy(b, buf.Bytes()) + return buf.Len(), nil + } + bmp280.Start() + temp, err := bmp280.Temperature() + gobottest.Assert(t, err, nil) + gobottest.Assert(t, temp, float32(25.014637)) + pressure, err := bmp280.Pressure() + gobottest.Assert(t, err, nil) + gobottest.Assert(t, pressure, float32(99545.414)) + alt, err := bmp280.Altitude() + gobottest.Assert(t, err, nil) + gobottest.Assert(t, alt, float32(149.22713)) } func TestBMP280DriverSetName(t *testing.T) {