1
0
mirror of https://github.com/hybridgroup/gobot.git synced 2025-04-26 13:48:49 +08:00

i2c: increase test coverage

Signed-off-by: deadprogram <ron@hybridgroup.com>
This commit is contained in:
deadprogram 2017-04-12 11:47:42 +02:00
parent 3b5d873f4b
commit 000eeaeaf3
19 changed files with 127 additions and 9 deletions

View File

@ -57,6 +57,12 @@ func TestAdafruitMotorHatDriverStartReadError(t *testing.T) {
gobottest.Assert(t, d.Start(), errors.New("read error"))
}
func TestAdafruitMotorHatDriverStartConnectError(t *testing.T) {
d, adaptor := initTestAdafruitMotorHatDriverWithStubbedAdaptor()
adaptor.Testi2cConnectErr(true)
gobottest.Assert(t, d.Start(), errors.New("Invalid i2c connection"))
}
func TestAdafruitMotorHatDriverHalt(t *testing.T) {
ada, _ := initTestAdafruitMotorHatDriverWithStubbedAdaptor()

View File

@ -112,6 +112,12 @@ func TestBlinkMDriverStart(t *testing.T) {
gobottest.Assert(t, blinkM.Start(), errors.New("write error"))
}
func TestBlinkMDriverStartConnectError(t *testing.T) {
d, adaptor := initTestBlinkDriverWithStubbedAdaptor()
adaptor.Testi2cConnectErr(true)
gobottest.Assert(t, d.Start(), errors.New("Invalid i2c connection"))
}
func TestBlinkMDriverHalt(t *testing.T) {
blinkM := initTestBlinkMDriver()
gobottest.Assert(t, blinkM.Halt(), nil)

View File

@ -43,6 +43,12 @@ func TestBME280DriverStart(t *testing.T) {
gobottest.Assert(t, bme280.Start(), nil)
}
func TestBME280StartConnectError(t *testing.T) {
d, adaptor := initTestBME280DriverWithStubbedAdaptor()
adaptor.Testi2cConnectErr(true)
gobottest.Assert(t, d.Start(), errors.New("Invalid i2c connection"))
}
func TestBME280DriverStartWriteError(t *testing.T) {
bme280, adaptor := initTestBME280DriverWithStubbedAdaptor()
adaptor.i2cWriteImpl = func([]byte) (int, error) {

View File

@ -44,6 +44,12 @@ func TestBMP180DriverStart(t *testing.T) {
gobottest.Assert(t, bmp180.Start(), nil)
}
func TestBMP180StartConnectError(t *testing.T) {
d, adaptor := initTestBMP180DriverWithStubbedAdaptor()
adaptor.Testi2cConnectErr(true)
gobottest.Assert(t, d.Start(), errors.New("Invalid i2c connection"))
}
func TestBMP180DriverStartWriteError(t *testing.T) {
bmp180, adaptor := initTestBMP180DriverWithStubbedAdaptor()
adaptor.i2cWriteImpl = func([]byte) (int, error) {

View File

@ -43,6 +43,12 @@ func TestBMP280DriverStart(t *testing.T) {
gobottest.Assert(t, bmp280.Start(), nil)
}
func TestBMP280StartConnectError(t *testing.T) {
d, adaptor := initTestBMP280DriverWithStubbedAdaptor()
adaptor.Testi2cConnectErr(true)
gobottest.Assert(t, d.Start(), errors.New("Invalid i2c connection"))
}
func TestBMP280DriverStartWriteError(t *testing.T) {
bmp280, adaptor := initTestBMP280DriverWithStubbedAdaptor()
adaptor.i2cWriteImpl = func([]byte) (int, error) {

View File

@ -38,6 +38,12 @@ func TestDRV2605LDriverStart(t *testing.T) {
gobottest.Assert(t, d.Start(), nil)
}
func TestDRV2605LStartConnectError(t *testing.T) {
d, adaptor := initTestDriverAndAdaptor()
adaptor.Testi2cConnectErr(true)
gobottest.Assert(t, d.Start(), errors.New("Invalid i2c connection"))
}
func TestDRVD2605DriverStartWriteError(t *testing.T) {
d, a := initTestDriverAndAdaptor()
a.i2cWriteImpl = func(b []byte) (int, error) {

View File

@ -1,6 +1,7 @@
package i2c
import (
"errors"
"fmt"
"sync"
)
@ -23,10 +24,17 @@ type i2cTestAdaptor struct {
name string
written []byte
mtx sync.Mutex
i2cConnectErr bool
i2cReadImpl func([]byte) (int, error)
i2cWriteImpl func([]byte) (int, error)
}
func (t *i2cTestAdaptor) Testi2cConnectErr(val bool) {
t.mtx.Lock()
defer t.mtx.Unlock()
t.i2cConnectErr = val
}
func (t *i2cTestAdaptor) Testi2cReadImpl(f func([]byte) (int, error)) {
t.mtx.Lock()
defer t.mtx.Unlock()
@ -152,6 +160,9 @@ func (t *i2cTestAdaptor) WriteBlockData(reg uint8, b []byte) (err error) {
}
func (t *i2cTestAdaptor) GetConnection( /* address */ int /* bus */, int) (connection Connection, err error) {
if t.i2cConnectErr {
return nil, errors.New("Invalid i2c connection")
}
return t, nil
}
@ -166,6 +177,7 @@ func (t *i2cTestAdaptor) Finalize() (err error) { return }
func newI2cTestAdaptor() *i2cTestAdaptor {
return &i2cTestAdaptor{
i2cConnectErr: false,
i2cReadImpl: func([]byte) (int, error) {
return 0, nil
},

View File

@ -48,6 +48,12 @@ func TestHMC6352DriverStart(t *testing.T) {
gobottest.Assert(t, err, errors.New("write error"))
}
func TestHMC6352StartConnectError(t *testing.T) {
d, adaptor := initTestHMC6352DriverWithStubbedAdaptor()
adaptor.Testi2cConnectErr(true)
gobottest.Assert(t, d.Start(), errors.New("Invalid i2c connection"))
}
func TestHMC6352DriverHalt(t *testing.T) {
hmc := initTestHMC6352Driver()

View File

@ -57,6 +57,12 @@ func TestJHD1313MDriverStart(t *testing.T) {
gobottest.Assert(t, d.Start(), nil)
}
func TestJHD1313MStartConnectError(t *testing.T) {
d, adaptor := initTestJHD1313M1DriverWithStubbedAdaptor()
adaptor.Testi2cConnectErr(true)
gobottest.Assert(t, d.Start(), errors.New("Invalid i2c connection"))
}
func TestJHD1313MDriverStartWriteError(t *testing.T) {
d, adaptor := initTestJHD1313M1DriverWithStubbedAdaptor()
adaptor.i2cWriteImpl = func([]byte) (int, error) {

View File

@ -46,6 +46,12 @@ func TestL3GD20HDriverStart(t *testing.T) {
gobottest.Assert(t, d.Start(), nil)
}
func TestL3GD20HStartConnectError(t *testing.T) {
d, adaptor := initTestL3GD20HDriverWithStubbedAdaptor()
adaptor.Testi2cConnectErr(true)
gobottest.Assert(t, d.Start(), errors.New("Invalid i2c connection"))
}
func TestL3GD20HDriverStartWriteError(t *testing.T) {
d, adaptor := initTestL3GD20HDriverWithStubbedAdaptor()
adaptor.i2cWriteImpl = func([]byte) (int, error) {

View File

@ -43,6 +43,12 @@ func TestLIDARLiteDriverStart(t *testing.T) {
gobottest.Assert(t, hmc.Start(), nil)
}
func TestLIDARLiteStartConnectError(t *testing.T) {
d, adaptor := initTestLIDARLiteDriverWithStubbedAdaptor()
adaptor.Testi2cConnectErr(true)
gobottest.Assert(t, d.Start(), errors.New("Invalid i2c connection"))
}
func TestLIDARLiteDriverHalt(t *testing.T) {
hmc := initTestLIDARLiteDriver()

View File

@ -92,6 +92,12 @@ func TestMCP23017DriverStart(t *testing.T) {
gobottest.Assert(t, err, errors.New("write error"))
}
func TestMCP23017StartConnectError(t *testing.T) {
d, adaptor := initTestMCP23017DriverWithStubbedAdaptor(0)
adaptor.Testi2cConnectErr(true)
gobottest.Assert(t, d.Start(), errors.New("Invalid i2c connection"))
}
func TestMCP23017DriverHalt(t *testing.T) {
mcp := initTestMCP23017Driver(0)
gobottest.Assert(t, mcp.Halt(), nil)

View File

@ -58,6 +58,12 @@ func TestMMA7660DriverStart(t *testing.T) {
gobottest.Assert(t, d.Start(), nil)
}
func TestMMA7660StartConnectError(t *testing.T) {
d, adaptor := initTestMMA7660DriverWithStubbedAdaptor()
adaptor.Testi2cConnectErr(true)
gobottest.Assert(t, d.Start(), errors.New("Invalid i2c connection"))
}
func TestMMA7660DriverStartWriteError(t *testing.T) {
mma, adaptor := initTestMMA7660DriverWithStubbedAdaptor()
adaptor.i2cWriteImpl = func([]byte) (int, error) {

View File

@ -58,6 +58,12 @@ func TestMPL115A2DriverStart(t *testing.T) {
gobottest.Assert(t, mpl.Start(), nil)
}
func TestMPL115A2StartConnectError(t *testing.T) {
d, adaptor := initTestMPL115A2DriverWithStubbedAdaptor()
adaptor.Testi2cConnectErr(true)
gobottest.Assert(t, d.Start(), errors.New("Invalid i2c connection"))
}
func TestMPL115A2DriverStartWriteError(t *testing.T) {
mpl, adaptor := initTestMPL115A2DriverWithStubbedAdaptor()
adaptor.i2cWriteImpl = func([]byte) (int, error) {

View File

@ -52,6 +52,12 @@ func TestMPU6050DriverStart(t *testing.T) {
gobottest.Assert(t, mpu.Start(), nil)
}
func TestMPU6050StartConnectError(t *testing.T) {
d, adaptor := initTestMPU6050DriverWithStubbedAdaptor()
adaptor.Testi2cConnectErr(true)
gobottest.Assert(t, d.Start(), errors.New("Invalid i2c connection"))
}
func TestMPU6050DriverStartWriteError(t *testing.T) {
mpu, adaptor := initTestMPU6050DriverWithStubbedAdaptor()
adaptor.i2cWriteImpl = func([]byte) (int, error) {

View File

@ -38,14 +38,18 @@ func TestNewSHT3xDriver(t *testing.T) {
// Methods
// Test Start
func TestSHT3xDriverStart(t *testing.T) {
sht3x, _ := initTestSHT3xDriverWithStubbedAdaptor()
gobottest.Assert(t, sht3x.Start(), nil)
}
// Test Halt
func TestSHT3xStartConnectError(t *testing.T) {
d, adaptor := initTestSHT3xDriverWithStubbedAdaptor()
adaptor.Testi2cConnectErr(true)
gobottest.Assert(t, d.Start(), errors.New("Invalid i2c connection"))
}
func TestSHT3xDriverHalt(t *testing.T) {
sht3x := initTestSHT3xDriver()

View File

@ -1,6 +1,7 @@
package i2c
import (
"errors"
"fmt"
"reflect"
"strings"
@ -71,14 +72,18 @@ func TestNewSSD1306Driver(t *testing.T) {
// Methods
// Test Start
func TestSSD1306DriverStart(t *testing.T) {
s, _ := initTestSSD1306DriverWithStubbedAdaptor()
gobottest.Assert(t, s.Start(), nil)
}
// Test Halt
func TestSSD1306StartConnectError(t *testing.T) {
d, adaptor := initTestSSD1306DriverWithStubbedAdaptor()
adaptor.Testi2cConnectErr(true)
gobottest.Assert(t, d.Start(), errors.New("Invalid i2c connection"))
}
func TestSSD1306DriverHalt(t *testing.T) {
s := initTestSSD1306Driver()

View File

@ -34,6 +34,12 @@ func TestTSL2561DriverStart(t *testing.T) {
gobottest.Refute(t, d.Connection(), nil)
}
func TestTSL2561StartConnectError(t *testing.T) {
d, adaptor := initTestTSL2561Driver()
adaptor.Testi2cConnectErr(true)
gobottest.Assert(t, d.Start(), errors.New("Invalid i2c connection"))
}
func TestTSL2561DriverStartError(t *testing.T) {
d, adaptor := initTestTSL2561Driver()
adaptor.i2cWriteImpl = func([]byte) (int, error) {

View File

@ -1,6 +1,7 @@
package i2c
import (
"errors"
"strings"
"testing"
"time"
@ -75,6 +76,12 @@ func TestWiichuckDriverStart(t *testing.T) {
}
func TestWiichuckStartConnectError(t *testing.T) {
d, adaptor := initTestWiichuckDriverWithStubbedAdaptor()
adaptor.Testi2cConnectErr(true)
gobottest.Assert(t, d.Start(), errors.New("Invalid i2c connection"))
}
func TestWiichuckDriverHalt(t *testing.T) {
wii := initTestWiichuckDriver()