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