Skip to content

Commit

Permalink
microbit: refactoring and increase test coverage for IOPinDriver
Browse files Browse the repository at this point in the history
Signed-off-by: deadprogram <[email protected]>
  • Loading branch information
deadprogram committed Apr 15, 2017
1 parent c457be7 commit 89daf5c
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 3 deletions.
20 changes: 17 additions & 3 deletions platforms/microbit/io_pin_driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package microbit
import (
"bytes"
"encoding/binary"
"errors"
"strconv"

"gobot.io/x/gobot"
Expand Down Expand Up @@ -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
}
Expand All @@ -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
}
Expand All @@ -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
}
Expand Down Expand Up @@ -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.
Expand Down
44 changes: 44 additions & 0 deletions platforms/microbit/io_pin_driver_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package microbit

import (
"errors"
"strings"
"testing"

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

0 comments on commit 89daf5c

Please sign in to comment.