1
0
mirror of https://github.com/kidoman/embd synced 2024-12-22 21:00:05 +01:00
embd/gpiodriver_test.go

138 lines
2.9 KiB
Go
Raw Normal View History

2014-03-23 02:32:26 +05:30
package embd
import "testing"
2014-03-23 04:59:35 +05:30
type fakeDigitalPin struct {
n int
}
func (p *fakeDigitalPin) N() int {
return p.n
}
func (*fakeDigitalPin) SetDirection(dir Direction) error {
return nil
}
func (*fakeDigitalPin) Read() (int, error) {
return 0, nil
}
func (*fakeDigitalPin) Write(val int) error {
return nil
}
func (*fakeDigitalPin) ActiveLow(b bool) error {
return nil
}
func (*fakeDigitalPin) PullUp() error {
return nil
}
func (*fakeDigitalPin) PullDown() error {
return nil
}
func (*fakeDigitalPin) Close() error {
return nil
}
func newFakeDigitalPin(n int) DigitalPin {
return &fakeDigitalPin{n}
}
2014-03-23 02:32:26 +05:30
func TestGpioDriverDigitalPin(t *testing.T) {
var tests = []struct {
key interface{}
n int
}{
{1, 1},
}
var pinMap = PinMap{
&PinDesc{ID: "P1_1", Aliases: []string{"1"}, Caps: CapNormal, DigitalLogical: 1},
}
2014-03-23 04:59:35 +05:30
driver := newGPIODriver(pinMap, newFakeDigitalPin, nil)
for _, test := range tests {
pin, err := driver.DigitalPin(test.key)
if err != nil {
t.Errorf("Looking up %v: unexpected error: %v", test.key, err)
continue
}
if pin.N() != test.n {
t.Errorf("Looking up %v: got %v, want %v", test.key, pin.N(), test.n)
}
}
}
type fakeAnalogPin struct {
n int
}
func (p *fakeAnalogPin) N() int {
return p.n
}
func (*fakeAnalogPin) Read() (int, error) {
return 0, nil
}
func (*fakeAnalogPin) Write(val int) error {
return nil
}
func (*fakeAnalogPin) Close() error {
return nil
}
func newFakeAnalogPin(n int) AnalogPin {
return &fakeAnalogPin{n}
}
func TestGpioDriverAnalogPin(t *testing.T) {
var tests = []struct {
key interface{}
n int
}{
{1, 1},
}
var pinMap = PinMap{
&PinDesc{ID: "P1_1", Aliases: []string{"1"}, Caps: CapAnalog, AnalogLogical: 1},
}
driver := newGPIODriver(pinMap, nil, newFakeAnalogPin)
2014-03-23 02:32:26 +05:30
for _, test := range tests {
2014-03-23 04:59:35 +05:30
pin, err := driver.AnalogPin(test.key)
2014-03-23 02:32:26 +05:30
if err != nil {
t.Errorf("Looking up %v: unexpected error: %v", test.key, err)
continue
}
2014-03-23 04:59:35 +05:30
if pin.N() != test.n {
t.Errorf("Looking up %v: got %v, want %v", test.key, pin.N(), test.n)
2014-03-23 02:32:26 +05:30
}
}
}
2014-03-23 04:59:35 +05:30
func TestGpioDriverUnavailablePinType(t *testing.T) {
var pinMap = PinMap{
&PinDesc{ID: "P1_1", Aliases: []string{"1"}, Caps: CapNormal, DigitalLogical: 1},
&PinDesc{ID: "P1_2", Aliases: []string{"1"}, Caps: CapAnalog, AnalogLogical: 1},
}
driver := newGPIODriver(pinMap, nil, nil)
_, err := driver.DigitalPin(1)
if err == nil {
t.Fatal("Looking up digital pin 1: did not get error")
}
expected := "gpio: digital io not supported on this host"
if err.Error() != expected {
t.Fatalf("Looking up digital pin 1: got error %q, expected %q", err, expected)
}
_, err = driver.AnalogPin(1)
if err == nil {
t.Fatal("Looking up analog pin 1: did not get error")
}
expected = "gpio: analog io not supported on this host"
if err.Error() != expected {
t.Fatalf("Looking up analog pin 1: got error %q, expected %q", err, expected)
}
}