mirror of
https://github.com/hybridgroup/gobot.git
synced 2025-05-09 19:29:27 +08:00
i2c: increase test coverage for lidar-lite
Signed-off-by: deadprogram <ron@hybridgroup.com>
This commit is contained in:
parent
5dc53355c7
commit
9334c3d938
@ -1,6 +1,7 @@
|
||||
package i2c
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"errors"
|
||||
"testing"
|
||||
|
||||
@ -95,17 +96,55 @@ func TestLIDARLiteDriverDistance(t *testing.T) {
|
||||
distance, err = hmc.Distance()
|
||||
gobottest.Assert(t, distance, int(0))
|
||||
gobottest.Assert(t, err, errors.New("read error"))
|
||||
}
|
||||
|
||||
// when write error
|
||||
hmc, adaptor = initTestLIDARLiteDriverWithStubbedAdaptor()
|
||||
|
||||
func TestLIDARLiteDriverDistanceError1(t *testing.T) {
|
||||
hmc, adaptor := initTestLIDARLiteDriverWithStubbedAdaptor()
|
||||
gobottest.Assert(t, hmc.Start(), nil)
|
||||
|
||||
adaptor.i2cWriteImpl = func([]byte) (int, error) {
|
||||
return 0, errors.New("write error")
|
||||
}
|
||||
|
||||
distance, err = hmc.Distance()
|
||||
distance, err := hmc.Distance()
|
||||
gobottest.Assert(t, distance, int(0))
|
||||
gobottest.Assert(t, err, errors.New("write error"))
|
||||
}
|
||||
|
||||
func TestLIDARLiteDriverDistanceError2(t *testing.T) {
|
||||
hmc, adaptor := initTestLIDARLiteDriverWithStubbedAdaptor()
|
||||
gobottest.Assert(t, hmc.Start(), nil)
|
||||
|
||||
adaptor.i2cWriteImpl = func(b []byte) (int, error) {
|
||||
if b[0] == 0x0f {
|
||||
return 0, errors.New("write error")
|
||||
}
|
||||
return len(b), nil
|
||||
}
|
||||
|
||||
distance, err := hmc.Distance()
|
||||
gobottest.Assert(t, distance, int(0))
|
||||
gobottest.Assert(t, err, errors.New("write error"))
|
||||
}
|
||||
|
||||
func TestLIDARLiteDriverDistanceError3(t *testing.T) {
|
||||
hmc, adaptor := initTestLIDARLiteDriverWithStubbedAdaptor()
|
||||
gobottest.Assert(t, hmc.Start(), nil)
|
||||
|
||||
adaptor.i2cWriteImpl = func(b []byte) (int, error) {
|
||||
if b[0] == 0x10 {
|
||||
return 0, errors.New("write error")
|
||||
}
|
||||
return len(b), nil
|
||||
}
|
||||
adaptor.i2cReadImpl = func(b []byte) (int, error) {
|
||||
buf := new(bytes.Buffer)
|
||||
buf.Write([]byte{0x03})
|
||||
copy(b, buf.Bytes())
|
||||
return buf.Len(), nil
|
||||
}
|
||||
|
||||
distance, err := hmc.Distance()
|
||||
gobottest.Assert(t, distance, int(0))
|
||||
gobottest.Assert(t, err, errors.New("write error"))
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user