1
0
mirror of https://github.com/hybridgroup/gobot.git synced 2025-05-14 19:29:32 +08:00
hybridgroup.gobot/platforms/digispark/digispark_adaptor_test.go

128 lines
3.4 KiB
Go
Raw Normal View History

2014-04-28 04:23:15 -07:00
package digispark
import (
2014-11-19 18:44:55 -08:00
"errors"
2014-06-12 16:15:06 -07:00
"testing"
2014-11-04 20:37:07 -08:00
"github.com/hybridgroup/gobot"
)
2014-11-04 20:37:07 -08:00
type mock struct {
locationA uint8
locationB uint8
pwmChannelA uint8
pwmChannelB uint8
pwmPrescalerValue uint
pin uint8
mode uint8
state uint8
}
2014-11-17 16:25:01 -08:00
func (l *mock) digitalWrite(pin uint8, state uint8) error {
2014-11-04 20:37:07 -08:00
l.pin = pin
l.state = state
2014-11-17 16:25:01 -08:00
return l.error()
2014-11-04 20:37:07 -08:00
}
2014-11-17 16:25:01 -08:00
func (l *mock) pinMode(pin uint8, mode uint8) error {
2014-11-04 20:37:07 -08:00
l.pin = pin
l.mode = mode
2014-11-17 16:25:01 -08:00
return l.error()
2014-11-04 20:37:07 -08:00
}
2014-12-18 12:37:17 -08:00
var pwmInitErrorFunc = func() error { return nil }
func (l *mock) pwmInit() error { return pwmInitErrorFunc() }
2014-11-17 16:25:01 -08:00
func (l *mock) pwmStop() error { return l.error() }
func (l *mock) pwmUpdateCompare(channelA uint8, channelB uint8) error {
2014-11-04 20:37:07 -08:00
l.pwmChannelA = channelA
l.pwmChannelB = channelB
2014-11-17 16:25:01 -08:00
return l.error()
2014-11-04 20:37:07 -08:00
}
2014-11-17 16:25:01 -08:00
func (l *mock) pwmUpdatePrescaler(value uint) error {
2014-11-04 20:37:07 -08:00
l.pwmPrescalerValue = value
2014-11-17 16:25:01 -08:00
return l.error()
2014-11-04 20:37:07 -08:00
}
2014-11-17 16:25:01 -08:00
func (l *mock) servoInit() error { return l.error() }
func (l *mock) servoUpdateLocation(locationA uint8, locationB uint8) error {
2014-11-04 20:37:07 -08:00
l.locationA = locationA
l.locationB = locationB
2014-11-17 16:25:01 -08:00
return l.error()
2014-11-04 20:37:07 -08:00
}
2014-12-18 12:37:17 -08:00
var errorFunc = func() error { return nil }
func (l *mock) error() error { return errorFunc() }
2014-11-04 20:37:07 -08:00
2014-06-13 16:01:39 -07:00
func initTestDigisparkAdaptor() *DigisparkAdaptor {
a := NewDigisparkAdaptor("bot")
2014-11-17 16:25:01 -08:00
a.connect = func(a *DigisparkAdaptor) (err error) { return nil }
2014-11-04 20:37:07 -08:00
a.littleWire = new(mock)
2014-12-18 12:37:17 -08:00
errorFunc = func() error { return nil }
pwmInitErrorFunc = func() error { return nil }
2014-06-13 16:01:39 -07:00
return a
2014-06-12 16:15:06 -07:00
}
2014-12-18 12:37:17 -08:00
func TestDigisparkAdaptor(t *testing.T) {
a := NewDigisparkAdaptor("bot")
gobot.Assert(t, a.Name(), "bot")
}
2014-11-19 18:44:55 -08:00
func TestDigisparkAdaptorConnect(t *testing.T) {
a := NewDigisparkAdaptor("bot")
2014-12-18 12:37:17 -08:00
gobot.Assert(t, a.Connect()[0], ErrConnection)
a = initTestDigisparkAdaptor()
gobot.Assert(t, len(a.Connect()), 0)
2014-06-12 16:15:06 -07:00
}
2014-06-13 16:01:39 -07:00
2014-11-19 18:44:55 -08:00
func TestDigisparkAdaptorFinalize(t *testing.T) {
2014-06-13 16:01:39 -07:00
a := initTestDigisparkAdaptor()
gobot.Assert(t, len(a.Finalize()), 0)
2014-06-12 16:15:06 -07:00
}
2014-06-13 16:01:39 -07:00
2014-12-18 12:37:17 -08:00
func TestDigisparkAdaptorDigitalWrite(t *testing.T) {
2014-06-13 16:01:39 -07:00
a := initTestDigisparkAdaptor()
2014-12-18 12:37:17 -08:00
err := a.DigitalWrite("0", uint8(1))
gobot.Assert(t, err, nil)
2014-11-04 20:37:07 -08:00
gobot.Assert(t, a.littleWire.(*mock).pin, uint8(0))
gobot.Assert(t, a.littleWire.(*mock).state, uint8(1))
2014-12-18 12:37:17 -08:00
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)
2014-11-04 20:37:07 -08:00
gobot.Assert(t, a.littleWire.(*mock).locationA, uint8(80))
gobot.Assert(t, a.littleWire.(*mock).locationB, uint8(80))
2014-12-18 12:37:17 -08:00
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"))
2014-06-12 16:15:06 -07:00
}