hybridgroup.gobot/sysfs/digital_pin_test.go

125 lines
2.9 KiB
Go
Raw Normal View History

2014-10-31 07:06:04 +08:00
package sysfs
import (
"errors"
"os"
"syscall"
2014-10-31 07:06:04 +08:00
"testing"
"gobot.io/x/gobot/gobottest"
2014-10-31 07:06:04 +08:00
)
func TestDigitalPin(t *testing.T) {
2014-11-08 08:21:39 +08:00
fs := NewMockFilesystem([]string{
"/sys/class/gpio/export",
"/sys/class/gpio/unexport",
"/sys/class/gpio/gpio10/value",
"/sys/class/gpio/gpio10/direction",
})
2014-10-31 07:06:04 +08:00
2014-11-08 08:21:39 +08:00
SetFilesystem(fs)
2014-10-31 07:06:04 +08:00
2014-11-04 11:02:20 +08:00
pin := NewDigitalPin(10, "custom").(*digitalPin)
gobottest.Assert(t, pin.pin, "10")
gobottest.Assert(t, pin.label, "custom")
2014-10-31 07:06:04 +08:00
2014-11-04 11:02:20 +08:00
pin = NewDigitalPin(10).(*digitalPin)
gobottest.Assert(t, pin.pin, "10")
gobottest.Assert(t, pin.label, "gpio10")
gobottest.Assert(t, pin.value, nil)
2014-10-31 07:06:04 +08:00
err := pin.Unexport()
gobottest.Assert(t, err, nil)
gobottest.Assert(t, fs.Files["/sys/class/gpio/unexport"].Contents, "10")
2014-10-31 07:06:04 +08:00
err = pin.Export()
gobottest.Assert(t, err, nil)
gobottest.Assert(t, fs.Files["/sys/class/gpio/export"].Contents, "10")
gobottest.Refute(t, pin.value, nil)
2014-10-31 07:06:04 +08:00
err = pin.Write(1)
gobottest.Assert(t, err, nil)
gobottest.Assert(t, fs.Files["/sys/class/gpio/gpio10/value"].Contents, "1")
2014-10-31 07:06:04 +08:00
err = pin.Direction(IN)
gobottest.Assert(t, err, nil)
gobottest.Assert(t, fs.Files["/sys/class/gpio/gpio10/direction"].Contents, "in")
2014-10-31 07:06:04 +08:00
data, _ := pin.Read()
gobottest.Assert(t, 1, data)
2014-11-08 08:21:39 +08:00
pin2 := NewDigitalPin(30, "custom")
err = pin2.Write(1)
gobottest.Refute(t, err, nil)
2014-11-08 08:21:39 +08:00
data, err = pin2.Read()
gobottest.Refute(t, err, nil)
gobottest.Assert(t, data, 0)
writeFile = func(File, []byte) (int, error) {
return 0, &os.PathError{Err: syscall.EINVAL}
}
err = pin.Unexport()
gobottest.Assert(t, err, nil)
writeFile = func(File, []byte) (int, error) {
return 0, &os.PathError{Err: errors.New("write error")}
}
err = pin.Unexport()
gobottest.Assert(t, err.(*os.PathError).Err, errors.New("write error"))
writeFile = func(File, []byte) (int, error) {
return 0, &os.PathError{Err: syscall.EBUSY}
}
err = pin.Export()
gobottest.Assert(t, err, nil)
writeFile = func(File, []byte) (int, error) {
return 0, &os.PathError{Err: errors.New("write error")}
}
err = pin.Export()
gobottest.Assert(t, err.(*os.PathError).Err, errors.New("write error"))
2014-10-31 07:06:04 +08:00
}
func TestDigitalPinExportError(t *testing.T) {
fs := NewMockFilesystem([]string{
"/sys/class/gpio/export",
"/sys/class/gpio/unexport",
"/sys/class/gpio/gpio10/value",
"/sys/class/gpio/gpio10/direction",
})
SetFilesystem(fs)
pin := NewDigitalPin(10, "custom").(*digitalPin)
writeFile = func(File, []byte) (int, error) {
return 0, &os.PathError{Err: syscall.EBUSY}
}
err := pin.Export()
gobottest.Refute(t, err, nil)
}
func TestDigitalPinUnexportError(t *testing.T) {
fs := NewMockFilesystem([]string{
"/sys/class/gpio/export",
"/sys/class/gpio/unexport",
"/sys/class/gpio/gpio10/value",
"/sys/class/gpio/gpio10/direction",
})
SetFilesystem(fs)
pin := NewDigitalPin(10, "custom").(*digitalPin)
writeFile = func(File, []byte) (int, error) {
return 0, &os.PathError{Err: syscall.EBUSY}
}
err := pin.Unexport()
gobottest.Refute(t, err, nil)
}