1
0
mirror of https://github.com/hybridgroup/gobot.git synced 2025-05-09 19:29:27 +08:00

i2c: correctly return error, and increase test coverage for adafruit hat driver

Signed-off-by: deadprogram <ron@hybridgroup.com>
This commit is contained in:
deadprogram 2017-04-11 15:50:56 +02:00
parent 8b4c1e1b8a
commit 15be67be79
2 changed files with 37 additions and 2 deletions

View File

@ -1,6 +1,7 @@
package i2c
import (
"errors"
"log"
"math"
"time"
@ -326,7 +327,7 @@ func (a *AdafruitMotorHatDriver) setPin(conn Connection, pin byte, value int32)
if value == 1 {
return a.setPWM(conn, pin, 4096, 0)
}
return nil
return errors.New("Invalid pin")
}
// SetDCMotorSpeed will set the appropriate pins to run the specified DC motor

View File

@ -41,7 +41,7 @@ func TestAdafruitMotorHatDriverStart(t *testing.T) {
gobottest.Assert(t, ada.Start(), nil)
}
func TestAdafruitMotorHatDriverStartError(t *testing.T) {
func TestAdafruitMotorHatDriverStartWriteError(t *testing.T) {
d, adaptor := initTestAdafruitMotorHatDriverWithStubbedAdaptor()
adaptor.i2cWriteImpl = func([]byte) (int, error) {
return 0, errors.New("write error")
@ -49,6 +49,14 @@ func TestAdafruitMotorHatDriverStartError(t *testing.T) {
gobottest.Assert(t, d.Start(), errors.New("write error"))
}
func TestAdafruitMotorHatDriverStartReadError(t *testing.T) {
d, adaptor := initTestAdafruitMotorHatDriverWithStubbedAdaptor()
adaptor.i2cReadImpl = func([]byte) (int, error) {
return 0, errors.New("read error")
}
gobottest.Assert(t, d.Start(), errors.New("read error"))
}
func TestAdafruitMotorHatDriverHalt(t *testing.T) {
ada, _ := initTestAdafruitMotorHatDriverWithStubbedAdaptor()
@ -74,6 +82,18 @@ func TestAdafruitMotorHatDriverSetServoMotorFreq(t *testing.T) {
gobottest.Assert(t, err, nil)
}
func TestAdafruitMotorHatDriverSetServoMotorFreqError(t *testing.T) {
ada, a := initTestAdafruitMotorHatDriverWithStubbedAdaptor()
gobottest.Assert(t, ada.Start(), nil)
a.i2cWriteImpl = func([]byte) (int, error) {
return 0, errors.New("write error")
}
freq := 60.0
gobottest.Assert(t, ada.SetServoMotorFreq(freq), errors.New("write error"))
}
func TestAdafruitMotorHatDriverSetServoMotorPulse(t *testing.T) {
ada, _ := initTestAdafruitMotorHatDriverWithStubbedAdaptor()
@ -86,6 +106,20 @@ func TestAdafruitMotorHatDriverSetServoMotorPulse(t *testing.T) {
gobottest.Assert(t, err, nil)
}
func TestAdafruitMotorHatDriverSetServoMotorPulseError(t *testing.T) {
ada, a := initTestAdafruitMotorHatDriverWithStubbedAdaptor()
gobottest.Assert(t, ada.Start(), nil)
a.i2cWriteImpl = func([]byte) (int, error) {
return 0, errors.New("write error")
}
var channel byte = 7
var on int32 = 1234
var off int32 = 4321
gobottest.Assert(t, ada.SetServoMotorPulse(channel, on, off), errors.New("write error"))
}
func TestAdafruitMotorHatDriverSetDCMotorSpeed(t *testing.T) {
ada, _ := initTestAdafruitMotorHatDriverWithStubbedAdaptor()