2014-11-09 18:14:13 -08:00
|
|
|
package i2c
|
|
|
|
|
|
|
|
import (
|
2017-02-09 11:23:36 +01:00
|
|
|
"strings"
|
2014-11-09 18:14:13 -08:00
|
|
|
"testing"
|
|
|
|
|
2016-12-20 18:59:26 +01:00
|
|
|
"gobot.io/x/gobot"
|
2016-12-08 13:24:03 +01:00
|
|
|
"gobot.io/x/gobot/gobottest"
|
2014-11-09 18:14:13 -08:00
|
|
|
)
|
|
|
|
|
2016-12-20 18:59:26 +01:00
|
|
|
var _ gobot.Driver = (*MPL115A2Driver)(nil)
|
|
|
|
|
2014-11-09 18:14:13 -08:00
|
|
|
// --------- HELPERS
|
|
|
|
func initTestMPL115A2Driver() (driver *MPL115A2Driver) {
|
|
|
|
driver, _ = initTestMPL115A2DriverWithStubbedAdaptor()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func initTestMPL115A2DriverWithStubbedAdaptor() (*MPL115A2Driver, *i2cTestAdaptor) {
|
2016-09-25 14:08:18 +02:00
|
|
|
adaptor := newI2cTestAdaptor()
|
|
|
|
return NewMPL115A2Driver(adaptor), adaptor
|
2014-11-09 18:14:13 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
// --------- TESTS
|
|
|
|
|
|
|
|
func TestNewMPL115A2Driver(t *testing.T) {
|
|
|
|
// Does it return a pointer to an instance of MPL115A2Driver?
|
2016-09-25 14:08:18 +02:00
|
|
|
var mpl interface{} = NewMPL115A2Driver(newI2cTestAdaptor())
|
2014-11-09 18:14:13 -08:00
|
|
|
_, ok := mpl.(*MPL115A2Driver)
|
|
|
|
if !ok {
|
|
|
|
t.Errorf("NewMPL115A2Driver() should have returned a *MPL115A2Driver")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Methods
|
2014-12-22 13:36:52 -08:00
|
|
|
func TestMPL115A2Driver(t *testing.T) {
|
2014-11-09 18:14:13 -08:00
|
|
|
mpl := initTestMPL115A2Driver()
|
|
|
|
|
2016-09-25 14:08:18 +02:00
|
|
|
gobottest.Refute(t, mpl.Connection(), nil)
|
2017-02-09 11:23:36 +01:00
|
|
|
gobottest.Assert(t, strings.HasPrefix(mpl.Name(), "MPL115A2"), true)
|
2014-12-22 13:36:52 -08:00
|
|
|
}
|
|
|
|
|
2017-02-11 11:53:19 +01:00
|
|
|
func TestMPL115A2DriverOptions(t *testing.T) {
|
|
|
|
mpl := NewMPL115A2Driver(newI2cTestAdaptor(), WithBus(2))
|
|
|
|
gobottest.Assert(t, mpl.GetBusOrDefault(1), 2)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestMPL115A2DriverSetName(t *testing.T) {
|
|
|
|
mpl := initTestMPL115A2Driver()
|
|
|
|
mpl.SetName("TESTME")
|
|
|
|
gobottest.Assert(t, mpl.Name(), "TESTME")
|
|
|
|
}
|
|
|
|
|
2014-12-22 13:36:52 -08:00
|
|
|
func TestMPL115A2DriverStart(t *testing.T) {
|
2017-02-11 10:37:34 +01:00
|
|
|
mpl, _ := initTestMPL115A2DriverWithStubbedAdaptor()
|
|
|
|
|
|
|
|
gobottest.Assert(t, mpl.Start(), nil)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestMPL115A2DriverReadData(t *testing.T) {
|
2014-12-22 13:36:52 -08:00
|
|
|
mpl, adaptor := initTestMPL115A2DriverWithStubbedAdaptor()
|
|
|
|
|
2017-02-06 00:19:42 +01:00
|
|
|
adaptor.i2cReadImpl = func(b []byte) (int, error) {
|
|
|
|
copy(b, []byte{0x00, 0x01, 0x02, 0x04})
|
|
|
|
return 4, nil
|
2014-12-22 13:36:52 -08:00
|
|
|
}
|
2017-02-11 10:37:34 +01:00
|
|
|
mpl.Start()
|
|
|
|
mpl.GetData()
|
2016-02-22 00:21:24 -05:00
|
|
|
gobottest.Assert(t, mpl.Pressure, float32(50.007942))
|
|
|
|
gobottest.Assert(t, mpl.Temperature, float32(116.58878))
|
2014-11-09 18:14:13 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestMPL115A2DriverHalt(t *testing.T) {
|
|
|
|
mpl := initTestMPL115A2Driver()
|
|
|
|
|
2016-11-07 17:21:43 +01:00
|
|
|
gobottest.Assert(t, mpl.Halt(), nil)
|
2014-11-09 18:14:13 -08:00
|
|
|
}
|