Skip to content

Commit

Permalink
Increase digispark test coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
zankich committed Dec 18, 2014
1 parent 5569fd3 commit c306033
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 30 deletions.
35 changes: 16 additions & 19 deletions platforms/digispark/digispark_adaptor.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package digispark

import (
"errors"
"fmt"
"strconv"

"github.com/hybridgroup/gobot"
Expand All @@ -15,6 +14,8 @@ var _ gpio.DigitalWriter = (*DigisparkAdaptor)(nil)
var _ gpio.PwmWriter = (*DigisparkAdaptor)(nil)
var _ gpio.ServoWriter = (*DigisparkAdaptor)(nil)

var ErrConnection = errors.New("connection error")

type DigisparkAdaptor struct {
name string
littleWire lw
Expand All @@ -30,7 +31,7 @@ func NewDigisparkAdaptor(name string) *DigisparkAdaptor {
connect: func(d *DigisparkAdaptor) (err error) {
d.littleWire = littleWireConnect()
if d.littleWire.(*littleWire).lwHandle == nil {
return errors.New(fmt.Sprintf("Error connecting to %s", d.Name()))
return ErrConnection
}
return
},
Expand Down Expand Up @@ -58,40 +59,36 @@ func (d *DigisparkAdaptor) DigitalWrite(pin string, level byte) (err error) {
return
}

err = d.littleWire.pinMode(uint8(p), 0)
if err != nil {
if err = d.littleWire.pinMode(uint8(p), 0); err != nil {
return
}
err = d.littleWire.digitalWrite(uint8(p), level)
return

return d.littleWire.digitalWrite(uint8(p), level)
}

// PwmWrite updates pwm pin with sent value
func (d *DigisparkAdaptor) PwmWrite(pin string, value byte) (err error) {
if d.pwm == false {
err = d.littleWire.pwmInit()
if err != nil {
return err
if err = d.littleWire.pwmInit(); err != nil {
return
}
err = d.littleWire.pwmUpdatePrescaler(1)
if err != nil {
return err

if err = d.littleWire.pwmUpdatePrescaler(1); err != nil {
return
}
d.pwm = true
}
err = d.littleWire.pwmUpdateCompare(value, value)
return

return d.littleWire.pwmUpdateCompare(value, value)
}

// ServoWrite updates servo location with specified angle
func (d *DigisparkAdaptor) ServoWrite(pin string, angle uint8) (err error) {
if d.servo == false {
err = d.littleWire.servoInit()
if err != nil {
return err
if err = d.littleWire.servoInit(); err != nil {
return
}
d.servo = true
}
err = d.littleWire.servoUpdateLocation(angle, angle)
return
return d.littleWire.servoUpdateLocation(angle, angle)
}
67 changes: 58 additions & 9 deletions platforms/digispark/digispark_adaptor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,10 @@ func (l *mock) pinMode(pin uint8, mode uint8) error {
l.mode = mode
return l.error()
}
func (l *mock) pwmInit() error { return l.error() }

var pwmInitErrorFunc = func() error { return nil }

func (l *mock) pwmInit() error { return pwmInitErrorFunc() }
func (l *mock) pwmStop() error { return l.error() }
func (l *mock) pwmUpdateCompare(channelA uint8, channelB uint8) error {
l.pwmChannelA = channelA
Expand All @@ -45,34 +48,80 @@ func (l *mock) servoUpdateLocation(locationA uint8, locationB uint8) error {
l.locationB = locationB
return l.error()
}
func (l *mock) error() error { return nil }

var errorFunc = func() error { return nil }

func (l *mock) error() error { return errorFunc() }

func initTestDigisparkAdaptor() *DigisparkAdaptor {
a := NewDigisparkAdaptor("bot")
a.connect = func(a *DigisparkAdaptor) (err error) { return nil }
a.littleWire = new(mock)
errorFunc = func() error { return nil }
pwmInitErrorFunc = func() error { return nil }
return a
}

func TestDigisparkAdaptor(t *testing.T) {
a := NewDigisparkAdaptor("bot")
gobot.Assert(t, a.Name(), "bot")
}

func TestDigisparkAdaptorConnect(t *testing.T) {
a := NewDigisparkAdaptor("bot")
gobot.Assert(t, a.Connect()[0], errors.New("Error connecting to bot"))
gobot.Assert(t, a.Connect()[0], ErrConnection)

a = initTestDigisparkAdaptor()
gobot.Assert(t, len(a.Connect()), 0)
}

func TestDigisparkAdaptorFinalize(t *testing.T) {
a := initTestDigisparkAdaptor()
gobot.Assert(t, len(a.Finalize()), 0)
}

func TestDigisparkAdaptorIO(t *testing.T) {
func TestDigisparkAdaptorDigitalWrite(t *testing.T) {
a := initTestDigisparkAdaptor()
a.DigitalWrite("0", uint8(1))
err := a.DigitalWrite("0", uint8(1))
gobot.Assert(t, err, nil)
gobot.Assert(t, a.littleWire.(*mock).pin, uint8(0))
gobot.Assert(t, a.littleWire.(*mock).state, uint8(1))
a.PwmWrite("1", uint8(100))
gobot.Assert(t, a.littleWire.(*mock).pwmChannelA, uint8(100))
gobot.Assert(t, a.littleWire.(*mock).pwmChannelB, uint8(100))
a.ServoWrite("2", uint8(80))

err = a.DigitalWrite("?", uint8(1))
gobot.Refute(t, err, nil)

errorFunc = func() error { return errors.New("pin mode error") }
err = a.DigitalWrite("0", uint8(1))
gobot.Assert(t, err, errors.New("pin mode error"))
}

func TestDigisparkAdaptorServoWrite(t *testing.T) {
a := initTestDigisparkAdaptor()
err := a.ServoWrite("2", uint8(80))
gobot.Assert(t, err, nil)
gobot.Assert(t, a.littleWire.(*mock).locationA, uint8(80))
gobot.Assert(t, a.littleWire.(*mock).locationB, uint8(80))

a = initTestDigisparkAdaptor()
errorFunc = func() error { return errors.New("servo error") }
err = a.ServoWrite("2", uint8(80))
gobot.Assert(t, err, errors.New("servo error"))
}

func TestDigisparkAdaptorPwmWrite(t *testing.T) {
a := initTestDigisparkAdaptor()
err := a.PwmWrite("1", uint8(100))
gobot.Assert(t, err, nil)
gobot.Assert(t, a.littleWire.(*mock).pwmChannelA, uint8(100))
gobot.Assert(t, a.littleWire.(*mock).pwmChannelB, uint8(100))

a = initTestDigisparkAdaptor()
pwmInitErrorFunc = func() error { return errors.New("pwminit error") }
err = a.PwmWrite("1", uint8(100))
gobot.Assert(t, err, errors.New("pwminit error"))

a = initTestDigisparkAdaptor()
errorFunc = func() error { return errors.New("pwm error") }
err = a.PwmWrite("1", uint8(100))
gobot.Assert(t, err, errors.New("pwm error"))
}
1 change: 0 additions & 1 deletion platforms/digispark/littleWire_servo.go

This file was deleted.

1 change: 0 additions & 1 deletion platforms/digispark/littleWire_util.go

This file was deleted.

0 comments on commit c306033

Please sign in to comment.