Skip to content

Commit

Permalink
Refactor beaglebone tests
Browse files Browse the repository at this point in the history
  • Loading branch information
zankich committed Nov 8, 2014
1 parent cb2d101 commit 7c60742
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 28 deletions.
4 changes: 1 addition & 3 deletions platforms/beaglebone/beaglebone_adaptor.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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
Expand Down
43 changes: 18 additions & 25 deletions platforms/beaglebone/beaglebone_adaptor_test.go
Original file line number Diff line number Diff line change
@@ -1,19 +1,13 @@
package beaglebone

import (
"io"
"os"
"testing"

"github.com/hybridgroup/gobot"
"github.com/hybridgroup/gobot/sysfs"
)

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()
Expand All @@ -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})
}

0 comments on commit 7c60742

Please sign in to comment.