Skip to content

Commit

Permalink
sysfs: minor type corrections and increase test coverage
Browse files Browse the repository at this point in the history
Signed-off-by: deadprogram <[email protected]>
  • Loading branch information
deadprogram committed Apr 9, 2017
1 parent b5e0460 commit a21abad
Show file tree
Hide file tree
Showing 2 changed files with 115 additions and 4 deletions.
8 changes: 4 additions & 4 deletions sysfs/i2c_device.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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")
}
Expand Down Expand Up @@ -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")
}
Expand Down
111 changes: 111 additions & 0 deletions sysfs/i2c_device_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand All @@ -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")
Expand All @@ -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")
Expand All @@ -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")
Expand All @@ -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")
Expand All @@ -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")
Expand Down

0 comments on commit a21abad

Please sign in to comment.