gobot.io/x/gobot@v1.16.0/drivers/i2c/mpl115a2_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 var _ gobot.Driver = (*MPL115A2Driver)(nil) 13 14 // --------- HELPERS 15 func initTestMPL115A2Driver() (driver *MPL115A2Driver) { 16 driver, _ = initTestMPL115A2DriverWithStubbedAdaptor() 17 return 18 } 19 20 func initTestMPL115A2DriverWithStubbedAdaptor() (*MPL115A2Driver, *i2cTestAdaptor) { 21 adaptor := newI2cTestAdaptor() 22 return NewMPL115A2Driver(adaptor), adaptor 23 } 24 25 // --------- TESTS 26 27 func TestNewMPL115A2Driver(t *testing.T) { 28 // Does it return a pointer to an instance of MPL115A2Driver? 29 var mpl interface{} = NewMPL115A2Driver(newI2cTestAdaptor()) 30 _, ok := mpl.(*MPL115A2Driver) 31 if !ok { 32 t.Errorf("NewMPL115A2Driver() should have returned a *MPL115A2Driver") 33 } 34 } 35 36 // Methods 37 func TestMPL115A2Driver(t *testing.T) { 38 mpl := initTestMPL115A2Driver() 39 40 gobottest.Refute(t, mpl.Connection(), nil) 41 gobottest.Assert(t, strings.HasPrefix(mpl.Name(), "MPL115A2"), true) 42 } 43 44 func TestMPL115A2DriverOptions(t *testing.T) { 45 mpl := NewMPL115A2Driver(newI2cTestAdaptor(), WithBus(2)) 46 gobottest.Assert(t, mpl.GetBusOrDefault(1), 2) 47 } 48 49 func TestMPL115A2DriverSetName(t *testing.T) { 50 mpl := initTestMPL115A2Driver() 51 mpl.SetName("TESTME") 52 gobottest.Assert(t, mpl.Name(), "TESTME") 53 } 54 55 func TestMPL115A2DriverStart(t *testing.T) { 56 mpl, _ := initTestMPL115A2DriverWithStubbedAdaptor() 57 58 gobottest.Assert(t, mpl.Start(), nil) 59 } 60 61 func TestMPL115A2StartConnectError(t *testing.T) { 62 d, adaptor := initTestMPL115A2DriverWithStubbedAdaptor() 63 adaptor.Testi2cConnectErr(true) 64 gobottest.Assert(t, d.Start(), errors.New("Invalid i2c connection")) 65 } 66 67 func TestMPL115A2DriverStartWriteError(t *testing.T) { 68 mpl, adaptor := initTestMPL115A2DriverWithStubbedAdaptor() 69 adaptor.i2cWriteImpl = func([]byte) (int, error) { 70 return 0, errors.New("write error") 71 } 72 gobottest.Assert(t, mpl.Start(), errors.New("write error")) 73 } 74 75 func TestMPL115A2DriverReadData(t *testing.T) { 76 mpl, adaptor := initTestMPL115A2DriverWithStubbedAdaptor() 77 78 adaptor.i2cReadImpl = func(b []byte) (int, error) { 79 copy(b, []byte{0x00, 0x01, 0x02, 0x04}) 80 return 4, nil 81 } 82 mpl.Start() 83 84 press, _ := mpl.Pressure() 85 temp, _ := mpl.Temperature() 86 gobottest.Assert(t, press, float32(50.007942)) 87 gobottest.Assert(t, temp, float32(116.58878)) 88 } 89 90 func TestMPL115A2DriverReadDataError(t *testing.T) { 91 mpl, adaptor := initTestMPL115A2DriverWithStubbedAdaptor() 92 mpl.Start() 93 94 adaptor.i2cWriteImpl = func([]byte) (int, error) { 95 return 0, errors.New("write error") 96 } 97 _, err := mpl.Pressure() 98 99 gobottest.Assert(t, err, errors.New("write error")) 100 } 101 102 func TestMPL115A2DriverHalt(t *testing.T) { 103 mpl := initTestMPL115A2Driver() 104 105 gobottest.Assert(t, mpl.Halt(), nil) 106 }