1
0
mirror of https://github.com/hybridgroup/gobot.git synced 2025-04-27 13:48:56 +08:00
hybridgroup.gobot/drivers/i2c/mpl115a2_driver_test.go
deadprogram ce773955b4 core: Move GPIO and I2C drivers into appropriately named 'drivers' directory
Signed-off-by: deadprogram <ron@hybridgroup.com>
2016-09-25 12:34:09 +02:00

61 lines
1.6 KiB
Go

package i2c
import (
"testing"
"time"
"github.com/hybridgroup/gobot/gobottest"
)
// --------- HELPERS
func initTestMPL115A2Driver() (driver *MPL115A2Driver) {
driver, _ = initTestMPL115A2DriverWithStubbedAdaptor()
return
}
func initTestMPL115A2DriverWithStubbedAdaptor() (*MPL115A2Driver, *i2cTestAdaptor) {
adaptor := newI2cTestAdaptor("adaptor")
return NewMPL115A2Driver(adaptor, "bot"), adaptor
}
// --------- TESTS
func TestNewMPL115A2Driver(t *testing.T) {
// Does it return a pointer to an instance of MPL115A2Driver?
var mpl interface{} = NewMPL115A2Driver(newI2cTestAdaptor("adaptor"), "bot")
_, ok := mpl.(*MPL115A2Driver)
if !ok {
t.Errorf("NewMPL115A2Driver() should have returned a *MPL115A2Driver")
}
}
// Methods
func TestMPL115A2Driver(t *testing.T) {
mpl := initTestMPL115A2Driver()
gobottest.Assert(t, mpl.Name(), "bot")
gobottest.Assert(t, mpl.Connection().Name(), "adaptor")
gobottest.Assert(t, mpl.interval, 10*time.Millisecond)
mpl = NewMPL115A2Driver(newI2cTestAdaptor("adaptor"), "bot", 100*time.Millisecond)
gobottest.Assert(t, mpl.interval, 100*time.Millisecond)
}
func TestMPL115A2DriverStart(t *testing.T) {
mpl, adaptor := initTestMPL115A2DriverWithStubbedAdaptor()
adaptor.i2cReadImpl = func() ([]byte, error) {
return []byte{0x00, 0x01, 0x02, 0x04}, nil
}
gobottest.Assert(t, len(mpl.Start()), 0)
<-time.After(100 * time.Millisecond)
gobottest.Assert(t, mpl.Pressure, float32(50.007942))
gobottest.Assert(t, mpl.Temperature, float32(116.58878))
}
func TestMPL115A2DriverHalt(t *testing.T) {
mpl := initTestMPL115A2Driver()
gobottest.Assert(t, len(mpl.Halt()), 0)
}