tester: improve API surface and implement one more test function in lis2mdl driver

Signed-off-by: deadprogram <ron@hybridgroup.com>
This commit is contained in:
deadprogram
2020-08-26 22:59:35 +02:00
committed by Ron Evans
parent dd34b83e9f
commit 2ae950e96d
4 changed files with 76 additions and 30 deletions
+8 -12
View File
@@ -1,27 +1,23 @@
package tester
import (
qt "github.com/frankban/quicktest"
)
// I2CBus implements the I2C interface in memory for testing.
type I2CBus struct {
C *qt.C
Devices []*I2CDevice
c Failer
devices []*I2CDevice
}
// NewI2CBus returns an I2CBus mock I2C instance that uses c to flag errors
// if they happen. After creating a I2C instance, add devices
// to it with addDevice before using NewI2CBus interface.
func NewI2CBus(c *qt.C) *I2CBus {
func NewI2CBus(c Failer) *I2CBus {
return &I2CBus{
C: c,
c: c,
}
}
// AddDevice adds a new mock device to the mock I2C bus.
func (bus *I2CBus) AddDevice(d *I2CDevice) {
bus.Devices = append(bus.Devices, d)
bus.devices = append(bus.devices, d)
}
// ReadRegister implements I2C.ReadRegister.
@@ -42,11 +38,11 @@ func (bus *I2CBus) Tx(addr uint16, input, output []byte) error {
// FindDevice returns the device with the given address.
func (bus *I2CBus) FindDevice(addr uint8) *I2CDevice {
for _, dev := range bus.Devices {
if dev.Addr == addr {
for _, dev := range bus.devices {
if dev.Addr() == addr {
return dev
}
}
bus.C.Fatalf("invalid device addr %#x passed to i2c bus", addr)
bus.c.Fatalf("invalid device addr %#x passed to i2c bus", addr)
panic("unreachable")
}