From a21abad5a97c837207cc42c63b02d98f348b3aec Mon Sep 17 00:00:00 2001 From: deadprogram Date: Sun, 9 Apr 2017 09:15:09 +0200 Subject: [PATCH] sysfs: minor type corrections and increase test coverage Signed-off-by: deadprogram --- sysfs/i2c_device.go | 8 +-- sysfs/i2c_device_test.go | 111 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 115 insertions(+), 4 deletions(-) diff --git a/sysfs/i2c_device.go b/sysfs/i2c_device.go index 9f08b53d1..fb0827297 100644 --- a/sysfs/i2c_device.go +++ b/sysfs/i2c_device.go @@ -48,10 +48,10 @@ type i2cSmbusIoctlData struct { type I2cOperations interface { io.ReadWriteCloser - ReadByte() (val uint8, err error) + ReadByte() (val byte, err error) ReadByteData(reg uint8) (val uint8, err error) ReadWordData(reg uint8) (val uint16, err error) - WriteByte(val uint8) (err error) + WriteByte(val byte) (err error) WriteByteData(reg uint8, val uint8) (err error) WriteWordData(reg uint8, val uint16) (err error) WriteBlockData(reg uint8, b []byte) (err error) @@ -116,7 +116,7 @@ func (d *i2cDevice) Close() (err error) { return d.file.Close() } -func (d *i2cDevice) ReadByte() (val uint8, err error) { +func (d *i2cDevice) ReadByte() (val byte, err error) { if d.funcs&I2C_FUNC_SMBUS_READ_BYTE == 0 { return 0, fmt.Errorf("SMBus read byte not supported") } @@ -146,7 +146,7 @@ func (d *i2cDevice) ReadWordData(reg uint8) (val uint16, err error) { return data, err } -func (d *i2cDevice) WriteByte(val uint8) (err error) { +func (d *i2cDevice) WriteByte(val byte) (err error) { if d.funcs&I2C_FUNC_SMBUS_WRITE_BYTE == 0 { return fmt.Errorf("SMBus write byte not supported") } diff --git a/sysfs/i2c_device_test.go b/sysfs/i2c_device_test.go index 83e33c134..a1c9b21f2 100644 --- a/sysfs/i2c_device_test.go +++ b/sysfs/i2c_device_test.go @@ -61,6 +61,25 @@ func TestNewI2cDevice(t *testing.T) { gobottest.Assert(t, err, nil) } +func TestNewI2cDeviceReadByte(t *testing.T) { + fs := NewMockFilesystem([]string{ + "/dev/i2c-1", + }) + SetFilesystem(fs) + + i, err := NewI2cDevice("/dev/i2c-1") + var _ I2cDevice = i + + gobottest.Assert(t, err, nil) + + i.SetAddress(0xff) + i.funcs = I2C_FUNC_SMBUS_READ_BYTE + + val, e := i.ReadByte() + gobottest.Assert(t, val, byte(0)) + gobottest.Assert(t, e, nil) +} + func TestNewI2cDeviceReadByteNotSupported(t *testing.T) { SetSyscall(&MockSyscall{}) i, err := NewI2cDevice("/dev/i2c-1") @@ -74,6 +93,24 @@ func TestNewI2cDeviceReadByteNotSupported(t *testing.T) { gobottest.Assert(t, err.Error(), "SMBus read byte not supported") } +func TestNewI2cDeviceWriteByte(t *testing.T) { + fs := NewMockFilesystem([]string{ + "/dev/i2c-1", + }) + SetFilesystem(fs) + + i, err := NewI2cDevice("/dev/i2c-1") + var _ I2cDevice = i + + gobottest.Assert(t, err, nil) + + i.SetAddress(0xff) + i.funcs = I2C_FUNC_SMBUS_WRITE_BYTE + + e := i.WriteByte(0x01) + gobottest.Assert(t, e, nil) +} + func TestNewI2cDeviceWriteByteNotSupported(t *testing.T) { SetSyscall(&MockSyscall{}) i, err := NewI2cDevice("/dev/i2c-1") @@ -87,6 +124,25 @@ func TestNewI2cDeviceWriteByteNotSupported(t *testing.T) { gobottest.Assert(t, err.Error(), "SMBus write byte not supported") } +func TestNewI2cDeviceReadByteData(t *testing.T) { + fs := NewMockFilesystem([]string{ + "/dev/i2c-1", + }) + SetFilesystem(fs) + + i, err := NewI2cDevice("/dev/i2c-1") + var _ I2cDevice = i + + gobottest.Assert(t, err, nil) + + i.SetAddress(0xff) + i.funcs = I2C_FUNC_SMBUS_READ_BYTE_DATA + + v, e := i.ReadByteData(0x01) + gobottest.Assert(t, v, byte(0)) + gobottest.Assert(t, e, nil) +} + func TestNewI2cDeviceReadByteDataNotSupported(t *testing.T) { SetSyscall(&MockSyscall{}) i, err := NewI2cDevice("/dev/i2c-1") @@ -100,6 +156,24 @@ func TestNewI2cDeviceReadByteDataNotSupported(t *testing.T) { gobottest.Assert(t, err.Error(), "SMBus read byte data not supported") } +func TestNewI2cDeviceWriteByteData(t *testing.T) { + fs := NewMockFilesystem([]string{ + "/dev/i2c-1", + }) + SetFilesystem(fs) + + i, err := NewI2cDevice("/dev/i2c-1") + var _ I2cDevice = i + + gobottest.Assert(t, err, nil) + + i.SetAddress(0xff) + i.funcs = I2C_FUNC_SMBUS_WRITE_BYTE_DATA + + e := i.WriteByteData(0x01, 0x02) + gobottest.Assert(t, e, nil) +} + func TestNewI2cDeviceWriteByteDataNotSupported(t *testing.T) { SetSyscall(&MockSyscall{}) i, err := NewI2cDevice("/dev/i2c-1") @@ -113,6 +187,25 @@ func TestNewI2cDeviceWriteByteDataNotSupported(t *testing.T) { gobottest.Assert(t, err.Error(), "SMBus write byte data not supported") } +func TestNewI2cDeviceReadWordData(t *testing.T) { + fs := NewMockFilesystem([]string{ + "/dev/i2c-1", + }) + SetFilesystem(fs) + + i, err := NewI2cDevice("/dev/i2c-1") + var _ I2cDevice = i + + gobottest.Assert(t, err, nil) + + i.SetAddress(0xff) + i.funcs = I2C_FUNC_SMBUS_READ_WORD_DATA + + v, e := i.ReadWordData(0x01) + gobottest.Assert(t, v, uint16(0)) + gobottest.Assert(t, e, nil) +} + func TestNewI2cDeviceReadWordDataNotSupported(t *testing.T) { SetSyscall(&MockSyscall{}) i, err := NewI2cDevice("/dev/i2c-1") @@ -126,6 +219,24 @@ func TestNewI2cDeviceReadWordDataNotSupported(t *testing.T) { gobottest.Assert(t, err.Error(), "SMBus read word data not supported") } +func TestNewI2cDeviceWriteWordData(t *testing.T) { + fs := NewMockFilesystem([]string{ + "/dev/i2c-1", + }) + SetFilesystem(fs) + + i, err := NewI2cDevice("/dev/i2c-1") + var _ I2cDevice = i + + gobottest.Assert(t, err, nil) + + i.SetAddress(0xff) + i.funcs = I2C_FUNC_SMBUS_WRITE_WORD_DATA + + e := i.WriteWordData(0x01, 0x0102) + gobottest.Assert(t, e, nil) +} + func TestNewI2cDeviceWriteWordDataNotSupported(t *testing.T) { SetSyscall(&MockSyscall{}) i, err := NewI2cDevice("/dev/i2c-1")