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

i2c: increase test coverage for mma7660 and mpu6050

Signed-off-by: deadprogram <ron@hybridgroup.com>
This commit is contained in:
deadprogram 2017-04-04 11:50:31 +02:00
parent 94f1a6d045
commit 305ef3c4bf
3 changed files with 51 additions and 2 deletions

View File

@ -1,6 +1,8 @@
package i2c
import "gobot.io/x/gobot"
import (
"gobot.io/x/gobot"
)
const mma7660Address = 0x4c

View File

@ -1,6 +1,8 @@
package i2c
import (
"bytes"
"errors"
"strings"
"testing"
@ -56,6 +58,14 @@ func TestMMA7660DriverStart(t *testing.T) {
gobottest.Assert(t, d.Start(), nil)
}
func TestMMA7660DriverStartWriteError(t *testing.T) {
mma, adaptor := initTestMMA7660DriverWithStubbedAdaptor()
adaptor.i2cWriteImpl = func([]byte) (int, error) {
return 0, errors.New("write error")
}
gobottest.Assert(t, mma.Start(), errors.New("write error"))
}
func TestMMA7660DriverHalt(t *testing.T) {
d := initTestMMA7660Driver()
gobottest.Assert(t, d.Halt(), nil)
@ -69,7 +79,7 @@ func TestMMA7660DriverAcceleration(t *testing.T) {
gobottest.Assert(t, z, 1.0)
}
func TestMMA7660DriverXYZ(t *testing.T) {
func TestMMA7660DriverNullXYZ(t *testing.T) {
d, _ := initTestMMA7660DriverWithStubbedAdaptor()
d.Start()
x, y, z, _ := d.XYZ()
@ -77,3 +87,20 @@ func TestMMA7660DriverXYZ(t *testing.T) {
gobottest.Assert(t, y, 0.0)
gobottest.Assert(t, z, 0.0)
}
func TestMMA7660DriverXYZ(t *testing.T) {
d, adaptor := initTestMMA7660DriverWithStubbedAdaptor()
d.Start()
adaptor.i2cReadImpl = func(b []byte) (int, error) {
buf := new(bytes.Buffer)
buf.Write([]byte{0x11, 0x12, 0x13})
copy(b, buf.Bytes())
return buf.Len(), nil
}
x, y, z, _ := d.XYZ()
gobottest.Assert(t, x, 17.0)
gobottest.Assert(t, y, 18.0)
gobottest.Assert(t, z, 19.0)
}

View File

@ -1,6 +1,7 @@
package i2c
import (
"errors"
"strings"
"testing"
@ -51,6 +52,14 @@ func TestMPU6050DriverStart(t *testing.T) {
gobottest.Assert(t, mpu.Start(), nil)
}
func TestMPU6050DriverStartWriteError(t *testing.T) {
mpu, adaptor := initTestMPU6050DriverWithStubbedAdaptor()
adaptor.i2cWriteImpl = func([]byte) (int, error) {
return 0, errors.New("write error")
}
gobottest.Assert(t, mpu.Start(), errors.New("write error"))
}
func TestMPU6050DriverHalt(t *testing.T) {
mpu := initTestMPU6050Driver()
@ -69,6 +78,17 @@ func TestMPU6050DriverReadData(t *testing.T) {
gobottest.Assert(t, mpu.Temperature, int16(36))
}
func TestMPU6050DriverReadDataError(t *testing.T) {
mpu, adaptor := initTestMPU6050DriverWithStubbedAdaptor()
mpu.Start()
adaptor.i2cReadImpl = func(b []byte) (int, error) {
return 0, errors.New("write error")
}
gobottest.Assert(t, mpu.GetData(), errors.New("write error"))
}
func TestMPU6050DriverSetName(t *testing.T) {
mpu := initTestMPU6050Driver()
mpu.SetName("TESTME")