Refactor beaglebone tests

This commit is contained in:
Adrian Zankich 2014-11-07 18:45:11 -08:00
parent cb2d101b6d
commit 7c607426ec
2 changed files with 18 additions and 27 deletions

View File

@ -20,8 +20,6 @@ const (
UsrLed = "/sys/devices/ocp.3/gpio-leds.8/leds/beaglebone:green:"
)
var i2cLocation = "/dev/i2c-1"
var pins = map[string]int{
"P8_3": 38,
"P8_4": 39,
@ -213,7 +211,7 @@ func (b *BeagleboneAdaptor) AnalogWrite(pin string, val byte) {
// I2cStart starts a i2c device in specified address
func (b *BeagleboneAdaptor) I2cStart(address byte) {
b.i2cDevice, _ = sysfs.NewI2cDevice(i2cLocation, address)
b.i2cDevice, _ = sysfs.NewI2cDevice("/dev/i2c-1", address)
}
// I2CWrite writes data to i2c device

View File

@ -1,8 +1,6 @@
package beaglebone
import (
"io"
"os"
"testing"
"github.com/hybridgroup/gobot"
@ -10,10 +8,6 @@ import (
)
func initTestBeagleboneAdaptor() *BeagleboneAdaptor {
i2cLocation = os.DevNull
sysfs.WriteFile = func(path string, data []byte) (i int, err error) {
return
}
a := NewBeagleboneAdaptor("myAdaptor")
a.connect = func() {}
a.Connect()
@ -28,35 +22,34 @@ func TestBeagleboneAdaptorFinalize(t *testing.T) {
func TestBeagleboneAdaptorDigitalIO(t *testing.T) {
a := initTestBeagleboneAdaptor()
lastWritePath := ""
lastReadPath := ""
lastWriteData := []byte{}
sysfs.WriteFile = func(path string, data []byte) (i int, err error) {
lastWritePath = path
lastWriteData = data
return
}
sysfs.ReadFile = func(path string) (b []byte, err error) {
lastReadPath = path
return []byte("1"), nil
}
fs := sysfs.NewMockFilesystem([]string{
"/sys/class/gpio/export",
"/sys/class/gpio/unexport",
"/sys/class/gpio/gpio60/value",
"/sys/class/gpio/gpio60/direction",
"/sys/class/gpio/gpio10/value",
"/sys/class/gpio/gpio10/direction",
})
sysfs.SetFilesystem(fs)
a.DigitalWrite("P9_12", 1)
gobot.Assert(t, lastWritePath, "/sys/class/gpio/gpio60/value")
gobot.Assert(t, lastWriteData, []byte{49})
gobot.Assert(t, fs.Files["/sys/class/gpio/gpio60/value"].Contents, "1")
fs.Files["/sys/class/gpio/gpio10/value"].Contents = "1"
i := a.DigitalRead("P8_31")
gobot.Assert(t, lastReadPath, "/sys/class/gpio/gpio10/value")
gobot.Assert(t, i, 1)
}
func TestBeagleboneAdaptorI2c(t *testing.T) {
a := initTestBeagleboneAdaptor()
fs := sysfs.NewMockFilesystem([]string{
"/dev/i2c-1",
})
sysfs.SetFilesystem(fs)
sysfs.SetSyscall(&sysfs.MockSyscall{})
a.I2cStart(0xff)
var _ io.ReadWriteCloser = a.i2cDevice
a.i2cDevice = new(gobot.NullReadWriteCloser)
a.I2cWrite([]byte{0x00, 0x01})
gobot.Assert(t, a.I2cRead(2), make([]byte, 2))
gobot.Assert(t, a.I2cRead(2), []byte{0x00, 0x01})
}