hybridgroup.gobot/drivers/i2c/blinkm_driver_test.go

181 lines
4.5 KiB
Go
Raw Normal View History

//nolint:forcetypeassert // ok here
2014-04-28 09:54:41 +08:00
package i2c
import (
2014-12-23 05:36:52 +08:00
"errors"
"strings"
2014-06-14 03:48:00 +08:00
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"gobot.io/x/gobot/v2"
)
// this ensures that the implementation is based on i2c.Driver, which implements the gobot.Driver
// and tests all implementations, so no further tests needed here for gobot.Driver interface
var _ gobot.Driver = (*BlinkMDriver)(nil)
func initTestBlinkMDriverWithStubbedAdaptor() (*BlinkMDriver, *i2cTestAdaptor) {
a := newI2cTestAdaptor()
d := NewBlinkMDriver(a)
if err := d.Start(); err != nil {
panic(err)
}
return d, a
2014-09-12 04:38:08 +08:00
}
func TestNewBlinkMDriver(t *testing.T) {
var di interface{} = NewBlinkMDriver(newI2cTestAdaptor())
d, ok := di.(*BlinkMDriver)
2014-09-12 04:38:08 +08:00
if !ok {
require.Fail(t, "NewBlinkMDriver() should have returned a *BlinkMDriver")
2014-09-12 04:38:08 +08:00
}
assert.NotNil(t, d.Driver)
assert.True(t, strings.HasPrefix(d.Name(), "BlinkM"))
assert.Equal(t, 0x09, d.defaultAddress)
}
func TestBlinkMOptions(t *testing.T) {
// This is a general test, that options are applied in constructor by using the common WithBus() option and
// least one of this driver. Further tests for options can also be done by call of "WithOption(val)(d)".
d := NewBlinkMDriver(newI2cTestAdaptor(), WithBus(2))
assert.Equal(t, 2, d.GetBusOrDefault(1))
}
func TestBlinkMStart(t *testing.T) {
d := NewBlinkMDriver(newI2cTestAdaptor())
require.NoError(t, d.Start())
}
func TestBlinkMHalt(t *testing.T) {
d, _ := initTestBlinkMDriverWithStubbedAdaptor()
require.NoError(t, d.Halt())
2014-09-12 04:38:08 +08:00
}
// Commands
func TestNewBlinkMDriverCommands_Rgb(t *testing.T) {
d, _ := initTestBlinkMDriverWithStubbedAdaptor()
2014-09-12 04:38:08 +08:00
result := d.Command("Rgb")(rgb)
assert.Nil(t, result)
2014-09-12 04:38:08 +08:00
}
func TestNewBlinkMDriverCommands_Fade(t *testing.T) {
d, _ := initTestBlinkMDriverWithStubbedAdaptor()
result := d.Command("Fade")(rgb)
assert.Nil(t, result)
2014-09-12 04:38:08 +08:00
}
func TestNewBlinkMDriverCommands_FirmwareVersion(t *testing.T) {
d, a := initTestBlinkMDriverWithStubbedAdaptor()
2014-09-12 04:38:08 +08:00
param := make(map[string]interface{})
// When len(data) is 2
a.i2cReadImpl = func(b []byte) (int, error) {
copy(b, []byte{99, 1})
return 2, nil
2014-09-12 04:38:08 +08:00
}
result := d.Command("FirmwareVersion")(param)
2014-09-12 04:38:08 +08:00
version, _ := d.FirmwareVersion()
assert.Equal(t, version, result.(map[string]interface{})["version"].(string))
2014-09-12 04:38:08 +08:00
// When len(data) is not 2
a.i2cReadImpl = func(b []byte) (int, error) {
copy(b, []byte{99})
return 1, nil
2014-09-12 04:38:08 +08:00
}
result = d.Command("FirmwareVersion")(param)
2014-09-12 04:38:08 +08:00
version, _ = d.FirmwareVersion()
assert.Equal(t, version, result.(map[string]interface{})["version"].(string))
2014-09-12 04:38:08 +08:00
}
func TestNewBlinkMDriverCommands_Color(t *testing.T) {
d, _ := initTestBlinkMDriverWithStubbedAdaptor()
2014-09-12 04:38:08 +08:00
param := make(map[string]interface{})
result := d.Command("Color")(param)
2014-09-12 04:38:08 +08:00
color, _ := d.Color()
assert.Equal(t, color, result.(map[string]interface{})["color"].([]byte))
2014-09-12 04:38:08 +08:00
}
func TestBlinkMFirmwareVersion(t *testing.T) {
d, a := initTestBlinkMDriverWithStubbedAdaptor()
2014-09-12 04:38:08 +08:00
// when len(data) is 2
a.i2cReadImpl = func(b []byte) (int, error) {
copy(b, []byte{99, 1})
return 2, nil
2014-09-12 04:38:08 +08:00
}
version, _ := d.FirmwareVersion()
assert.Equal(t, "99.1", version)
2014-09-12 04:38:08 +08:00
// when len(data) is not 2
a.i2cReadImpl = func(b []byte) (int, error) {
copy(b, []byte{99})
return 1, nil
2014-09-12 04:38:08 +08:00
}
version, _ = d.FirmwareVersion()
assert.Equal(t, "", version)
2014-12-23 05:36:52 +08:00
a.i2cWriteImpl = func([]byte) (int, error) {
return 0, errors.New("write error")
2014-12-23 05:36:52 +08:00
}
_, err := d.FirmwareVersion()
require.ErrorContains(t, err, "write error")
2014-09-12 04:38:08 +08:00
}
func TestBlinkMColor(t *testing.T) {
d, a := initTestBlinkMDriverWithStubbedAdaptor()
2014-09-12 04:38:08 +08:00
// when len(data) is 3
a.i2cReadImpl = func(b []byte) (int, error) {
copy(b, []byte{99, 1, 2})
return 3, nil
2014-09-12 04:38:08 +08:00
}
color, _ := d.Color()
assert.Equal(t, []byte{99, 1, 2}, color)
2014-09-12 04:38:08 +08:00
// when len(data) is not 3
a.i2cReadImpl = func(b []byte) (int, error) {
copy(b, []byte{99})
return 1, nil
2014-09-12 04:38:08 +08:00
}
color, _ = d.Color()
assert.Equal(t, []byte{}, color)
2014-09-12 04:38:08 +08:00
a.i2cWriteImpl = func([]byte) (int, error) {
return 0, errors.New("write error")
2014-12-23 05:36:52 +08:00
}
_, err := d.Color()
require.ErrorContains(t, err, "write error")
2014-12-23 05:36:52 +08:00
}
func TestBlinkMFade(t *testing.T) {
d, a := initTestBlinkMDriverWithStubbedAdaptor()
a.i2cWriteImpl = func([]byte) (int, error) {
return 0, errors.New("write error")
2014-12-23 05:36:52 +08:00
}
err := d.Fade(100, 100, 100)
require.ErrorContains(t, err, "write error")
2014-12-23 05:36:52 +08:00
}
func TestBlinkMRGB(t *testing.T) {
d, a := initTestBlinkMDriverWithStubbedAdaptor()
a.i2cWriteImpl = func([]byte) (int, error) {
return 0, errors.New("write error")
2014-12-23 05:36:52 +08:00
}
err := d.Rgb(100, 100, 100)
require.ErrorContains(t, err, "write error")
2014-06-14 03:48:00 +08:00
}