From 89daf5c324bdbc4cf64e6832e2099920ddc96149 Mon Sep 17 00:00:00 2001 From: deadprogram Date: Sat, 15 Apr 2017 17:37:33 +0200 Subject: [PATCH] microbit: refactoring and increase test coverage for IOPinDriver Signed-off-by: deadprogram --- platforms/microbit/io_pin_driver.go | 20 +++++++++-- platforms/microbit/io_pin_driver_test.go | 44 ++++++++++++++++++++++++ 2 files changed, 61 insertions(+), 3 deletions(-) diff --git a/platforms/microbit/io_pin_driver.go b/platforms/microbit/io_pin_driver.go index c64f83f1..a1f4404f 100644 --- a/platforms/microbit/io_pin_driver.go +++ b/platforms/microbit/io_pin_driver.go @@ -3,6 +3,7 @@ package microbit import ( "bytes" "encoding/binary" + "errors" "strconv" "gobot.io/x/gobot" @@ -154,7 +155,7 @@ func (b *IOPinDriver) Finalize() (err error) { // DigitalRead reads from a pin func (b *IOPinDriver) DigitalRead(pin string) (val int, err error) { - p, err := strconv.Atoi(pin) + p, err := validatedPin(pin) if err != nil { return } @@ -168,7 +169,7 @@ func (b *IOPinDriver) DigitalRead(pin string) (val int, err error) { // DigitalWrite writes to a pin func (b *IOPinDriver) DigitalWrite(pin string, level byte) (err error) { - p, err := strconv.Atoi(pin) + p, err := validatedPin(pin) if err != nil { return } @@ -181,7 +182,7 @@ func (b *IOPinDriver) DigitalWrite(pin string, level byte) (err error) { // AnalogRead reads from a pin func (b *IOPinDriver) AnalogRead(pin string) (val int, err error) { - p, err := strconv.Atoi(pin) + p, err := validatedPin(pin) if err != nil { return } @@ -217,6 +218,19 @@ func (b *IOPinDriver) ensureOutput(pin int) { } } +func validatedPin(pin string) (int, error) { + i, err := strconv.Atoi(pin) + if err != nil { + return 0, err + } + + if i < 0 || i > 2 { + return 0, errors.New("Invalid pin.") + } + + return i, nil +} + // via http://stackoverflow.com/questions/23192262/how-would-you-set-and-clear-a-single-bit-in-go // Sets the bit at pos in the integer n. diff --git a/platforms/microbit/io_pin_driver_test.go b/platforms/microbit/io_pin_driver_test.go index 217b9b74..ce13cb6e 100644 --- a/platforms/microbit/io_pin_driver_test.go +++ b/platforms/microbit/io_pin_driver_test.go @@ -1,6 +1,7 @@ package microbit import ( + "errors" "strings" "testing" @@ -53,6 +54,17 @@ func TestIOPinDriverDigitalRead(t *testing.T) { gobottest.Assert(t, val, 0) } +func TestIOPinDriverDigitalReadInvalidPin(t *testing.T) { + a := NewBleTestAdaptor() + d := NewIOPinDriver(a) + + _, err := d.DigitalRead("A3") + gobottest.Refute(t, err, nil) + + _, err = d.DigitalRead("6") + gobottest.Assert(t, err, errors.New("Invalid pin.")) +} + func TestIOPinDriverDigitalWrite(t *testing.T) { a := NewBleTestAdaptor() d := NewIOPinDriver(a) @@ -61,6 +73,14 @@ func TestIOPinDriverDigitalWrite(t *testing.T) { gobottest.Assert(t, d.DigitalWrite("0", 1), nil) } +func TestIOPinDriverDigitalWriteInvalidPin(t *testing.T) { + a := NewBleTestAdaptor() + d := NewIOPinDriver(a) + + gobottest.Refute(t, d.DigitalWrite("A3", 1), nil) + gobottest.Assert(t, d.DigitalWrite("6", 1), errors.New("Invalid pin.")) +} + func TestIOPinDriverAnalogRead(t *testing.T) { a := NewBleTestAdaptor() d := NewIOPinDriver(a) @@ -75,6 +95,17 @@ func TestIOPinDriverAnalogRead(t *testing.T) { gobottest.Assert(t, val, 128) } +func TestIOPinDriverAnalogReadInvalidPin(t *testing.T) { + a := NewBleTestAdaptor() + d := NewIOPinDriver(a) + + _, err := d.AnalogRead("A3") + gobottest.Refute(t, err, nil) + + _, err = d.AnalogRead("6") + gobottest.Assert(t, err, errors.New("Invalid pin.")) +} + func TestIOPinDriverDigitalAnalogRead(t *testing.T) { a := NewBleTestAdaptor() d := NewIOPinDriver(a) @@ -101,3 +132,16 @@ func TestIOPinDriverDigitalWriteAnalogRead(t *testing.T) { val, _ := d.AnalogRead("1") gobottest.Assert(t, val, 128) } + +func TestIOPinDriverAnalogReadDigitalWrite(t *testing.T) { + a := NewBleTestAdaptor() + d := NewIOPinDriver(a) + a.TestReadCharacteristic(func(cUUID string) ([]byte, error) { + return []byte{0, 0, 1, 128, 2, 1}, nil + }) + + val, _ := d.AnalogRead("1") + gobottest.Assert(t, val, 128) + + gobottest.Assert(t, d.DigitalWrite("1", 0), nil) +}