2014-10-30 16:06:04 -07:00
|
|
|
package sysfs
|
|
|
|
|
|
|
|
import (
|
|
|
|
"os"
|
|
|
|
"testing"
|
2014-12-31 06:12:25 -08:00
|
|
|
|
|
|
|
"github.com/hybridgroup/gobot"
|
2014-10-30 16:06:04 -07:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestNewI2cDevice(t *testing.T) {
|
2014-11-07 16:21:39 -08:00
|
|
|
fs := NewMockFilesystem([]string{})
|
|
|
|
SetFilesystem(fs)
|
|
|
|
|
|
|
|
i, err := NewI2cDevice(os.DevNull, 0xff)
|
|
|
|
gobot.Refute(t, err, nil)
|
|
|
|
|
|
|
|
fs = NewMockFilesystem([]string{
|
|
|
|
"/dev/i2c-1",
|
|
|
|
})
|
|
|
|
|
|
|
|
SetFilesystem(fs)
|
|
|
|
|
|
|
|
i, err = NewI2cDevice("/dev/i2c-1", 0xff)
|
|
|
|
gobot.Refute(t, err, nil)
|
|
|
|
|
2014-11-07 16:56:13 -08:00
|
|
|
SetSyscall(&MockSyscall{})
|
2014-11-07 16:21:39 -08:00
|
|
|
|
|
|
|
i, err = NewI2cDevice("/dev/i2c-1", 0xff)
|
2015-07-03 18:57:29 -07:00
|
|
|
var _ I2cDevice = i
|
|
|
|
|
|
|
|
gobot.Assert(t, err, nil)
|
|
|
|
|
|
|
|
gobot.Assert(t, i.SetAddress(0xff), nil)
|
|
|
|
|
|
|
|
buf := []byte{0x01, 0x02, 0x03}
|
|
|
|
|
|
|
|
n, err := i.Write(buf)
|
|
|
|
|
|
|
|
gobot.Assert(t, n, len(buf))
|
2014-11-07 16:21:39 -08:00
|
|
|
gobot.Assert(t, err, nil)
|
2015-07-03 18:57:29 -07:00
|
|
|
|
|
|
|
buf = make([]byte, 4)
|
|
|
|
|
|
|
|
n, err = i.Read(buf)
|
|
|
|
|
2016-02-06 18:51:55 -08:00
|
|
|
gobot.Assert(t, n, 3)
|
2015-07-03 18:57:29 -07:00
|
|
|
gobot.Assert(t, err, nil)
|
|
|
|
|
2014-10-30 16:06:04 -07:00
|
|
|
}
|