2022-12-09 02:29:51 +08:00
|
|
|
package adaptors
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"testing"
|
|
|
|
|
2023-10-20 16:27:09 +08:00
|
|
|
"github.com/stretchr/testify/assert"
|
2023-05-20 20:25:21 +08:00
|
|
|
"gobot.io/x/gobot/v2/drivers/i2c"
|
|
|
|
"gobot.io/x/gobot/v2/system"
|
2022-12-09 02:29:51 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
// make sure that this Adaptor fulfills all the required interfaces
|
|
|
|
var _ i2c.Connector = (*I2cBusAdaptor)(nil)
|
|
|
|
|
|
|
|
const i2cBus1 = "/dev/i2c-1"
|
|
|
|
|
|
|
|
func initTestI2cAdaptorWithMockedFilesystem(mockPaths []string) (*I2cBusAdaptor, *system.MockFilesystem) {
|
|
|
|
sys := system.NewAccesser()
|
|
|
|
sys.UseMockSyscall()
|
|
|
|
fs := sys.UseMockFilesystem(mockPaths)
|
|
|
|
validator := func(busNr int) error {
|
|
|
|
if busNr > 1 {
|
|
|
|
return fmt.Errorf("%d not valid", busNr)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
a := NewI2cBusAdaptor(sys, validator, 1)
|
|
|
|
if err := a.Connect(); err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
return a, fs
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestI2cWorkflow(t *testing.T) {
|
|
|
|
a, _ := initTestI2cAdaptorWithMockedFilesystem([]string{i2cBus1})
|
2023-10-20 16:27:09 +08:00
|
|
|
assert.Equal(t, 0, len(a.buses))
|
2022-12-09 02:29:51 +08:00
|
|
|
|
2022-12-10 20:10:23 +08:00
|
|
|
con, err := a.GetI2cConnection(0xff, 1)
|
2023-10-27 02:34:19 +08:00
|
|
|
assert.NoError(t, err)
|
2023-10-20 16:27:09 +08:00
|
|
|
assert.Equal(t, 1, len(a.buses))
|
2022-12-09 02:29:51 +08:00
|
|
|
|
|
|
|
_, err = con.Write([]byte{0x00, 0x01})
|
2023-10-27 02:34:19 +08:00
|
|
|
assert.NoError(t, err)
|
2022-12-09 02:29:51 +08:00
|
|
|
|
|
|
|
data := []byte{42, 42}
|
|
|
|
_, err = con.Read(data)
|
2023-10-27 02:34:19 +08:00
|
|
|
assert.NoError(t, err)
|
2023-10-20 16:27:09 +08:00
|
|
|
assert.Equal(t, []byte{0x00, 0x01}, data)
|
2022-12-09 02:29:51 +08:00
|
|
|
|
2023-10-27 02:34:19 +08:00
|
|
|
assert.NoError(t, a.Finalize())
|
2023-10-20 16:27:09 +08:00
|
|
|
assert.Equal(t, 0, len(a.buses))
|
2022-12-09 02:29:51 +08:00
|
|
|
}
|
|
|
|
|
2022-12-10 20:10:23 +08:00
|
|
|
func TestI2cGetI2cConnection(t *testing.T) {
|
2022-12-09 02:29:51 +08:00
|
|
|
// arrange
|
|
|
|
a, _ := initTestI2cAdaptorWithMockedFilesystem([]string{i2cBus1})
|
|
|
|
// assert working connection
|
2022-12-10 20:10:23 +08:00
|
|
|
c1, e1 := a.GetI2cConnection(0xff, 1)
|
2023-10-27 02:34:19 +08:00
|
|
|
assert.NoError(t, e1)
|
2023-10-20 16:27:09 +08:00
|
|
|
assert.NotNil(t, c1)
|
|
|
|
assert.Equal(t, 1, len(a.buses))
|
2022-12-09 02:29:51 +08:00
|
|
|
// assert invalid bus gets error
|
2022-12-10 20:10:23 +08:00
|
|
|
c2, e2 := a.GetI2cConnection(0x01, 99)
|
2023-10-26 02:21:18 +08:00
|
|
|
assert.ErrorContains(t, e2, "99 not valid")
|
2023-10-20 16:27:09 +08:00
|
|
|
assert.Nil(t, c2)
|
|
|
|
assert.Equal(t, 1, len(a.buses))
|
2022-12-09 02:29:51 +08:00
|
|
|
// assert unconnected gets error
|
2023-10-27 02:34:19 +08:00
|
|
|
assert.NoError(t, a.Finalize())
|
2022-12-10 20:10:23 +08:00
|
|
|
c3, e3 := a.GetI2cConnection(0x01, 99)
|
2023-10-26 02:21:18 +08:00
|
|
|
assert.ErrorContains(t, e3, "not connected")
|
2023-10-20 16:27:09 +08:00
|
|
|
assert.Nil(t, c3)
|
|
|
|
assert.Equal(t, 0, len(a.buses))
|
2022-12-09 02:29:51 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestI2cFinalize(t *testing.T) {
|
|
|
|
// arrange
|
|
|
|
a, fs := initTestI2cAdaptorWithMockedFilesystem([]string{i2cBus1})
|
|
|
|
// assert that finalize before connect is working
|
2023-10-27 02:34:19 +08:00
|
|
|
assert.NoError(t, a.Finalize())
|
2022-12-09 02:29:51 +08:00
|
|
|
// arrange
|
2023-10-27 02:34:19 +08:00
|
|
|
assert.NoError(t, a.Connect())
|
2023-06-13 01:51:25 +08:00
|
|
|
_, _ = a.GetI2cConnection(0xaf, 1)
|
2023-10-20 16:27:09 +08:00
|
|
|
assert.Equal(t, 1, len(a.buses))
|
2022-12-10 20:10:23 +08:00
|
|
|
// assert that Finalize after GetI2cConnection is working and clean up
|
2023-10-27 02:34:19 +08:00
|
|
|
assert.NoError(t, a.Finalize())
|
2023-10-20 16:27:09 +08:00
|
|
|
assert.Equal(t, 0, len(a.buses))
|
2022-12-09 02:29:51 +08:00
|
|
|
// assert that finalize after finalize is working
|
2023-10-27 02:34:19 +08:00
|
|
|
assert.NoError(t, a.Finalize())
|
2022-12-09 02:29:51 +08:00
|
|
|
// assert that close error is recognized
|
2023-10-27 02:34:19 +08:00
|
|
|
assert.NoError(t, a.Connect())
|
2022-12-10 20:10:23 +08:00
|
|
|
con, _ := a.GetI2cConnection(0xbf, 1)
|
2023-10-20 16:27:09 +08:00
|
|
|
assert.Equal(t, 1, len(a.buses))
|
2023-06-13 01:51:25 +08:00
|
|
|
_, _ = con.Write([]byte{0xbf})
|
2022-12-09 02:29:51 +08:00
|
|
|
fs.WithCloseError = true
|
|
|
|
err := a.Finalize()
|
2023-10-20 16:27:09 +08:00
|
|
|
assert.Contains(t, err.Error(), "close error")
|
2022-12-09 02:29:51 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestI2cReConnect(t *testing.T) {
|
|
|
|
// arrange
|
|
|
|
a, _ := initTestI2cAdaptorWithMockedFilesystem([]string{i2cBus1})
|
2023-10-27 02:34:19 +08:00
|
|
|
assert.NoError(t, a.Finalize())
|
2022-12-09 02:29:51 +08:00
|
|
|
// act
|
2023-10-27 02:34:19 +08:00
|
|
|
assert.NoError(t, a.Connect())
|
2022-12-09 02:29:51 +08:00
|
|
|
// assert
|
2023-10-20 16:27:09 +08:00
|
|
|
assert.NotNil(t, a.buses)
|
|
|
|
assert.Equal(t, 0, len(a.buses))
|
2022-12-09 02:29:51 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestI2cGetDefaultBus(t *testing.T) {
|
|
|
|
a := NewI2cBusAdaptor(nil, nil, 2)
|
2023-10-20 16:27:09 +08:00
|
|
|
assert.Equal(t, 2, a.DefaultI2cBus())
|
2022-12-09 02:29:51 +08:00
|
|
|
}
|