1
0
mirror of https://github.com/hybridgroup/gobot.git synced 2025-04-27 13:48:56 +08:00
hybridgroup.gobot/drivers/gpio/led_driver_test.go

94 lines
2.0 KiB
Go
Raw Normal View History

2014-04-27 19:34:16 -07:00
package gpio
import (
2014-12-16 13:42:48 -08:00
"errors"
"strings"
2014-06-12 20:08:06 -07:00
"testing"
2014-07-09 18:19:58 -07:00
"github.com/stretchr/testify/assert"
"gobot.io/x/gobot/v2"
2014-04-27 19:34:16 -07:00
)
var _ gobot.Driver = (*LedDriver)(nil)
func initTestLedDriver() *LedDriver {
a := newGpioTestAdaptor()
a.digitalWriteFunc = func(string, byte) (err error) {
2014-12-16 13:42:48 -08:00
return nil
}
a.pwmWriteFunc = func(string, byte) (err error) {
2014-12-16 13:42:48 -08:00
return nil
}
return NewLedDriver(a, "1")
2014-12-16 13:42:48 -08:00
}
func TestLedDriver(t *testing.T) {
var err interface{}
a := newGpioTestAdaptor()
d := NewLedDriver(a, "1")
2014-12-16 13:42:48 -08:00
assert.Equal(t, "1", d.Pin())
assert.NotNil(t, d.Connection())
2014-12-16 13:42:48 -08:00
a.digitalWriteFunc = func(string, byte) (err error) {
2014-12-16 13:42:48 -08:00
return errors.New("write error")
}
a.pwmWriteFunc = func(string, byte) (err error) {
2014-12-16 13:42:48 -08:00
return errors.New("pwm error")
}
err = d.Command("Toggle")(nil)
assert.ErrorContains(t, err.(error), "write error")
2014-12-16 13:42:48 -08:00
err = d.Command("On")(nil)
assert.ErrorContains(t, err.(error), "write error")
2014-12-16 13:42:48 -08:00
err = d.Command("Off")(nil)
assert.ErrorContains(t, err.(error), "write error")
2014-12-16 13:42:48 -08:00
err = d.Command("Brightness")(map[string]interface{}{"level": 100.0})
assert.ErrorContains(t, err.(error), "pwm error")
2014-06-12 20:08:06 -07:00
}
2014-06-13 16:01:39 -07:00
func TestLedDriverStart(t *testing.T) {
d := initTestLedDriver()
assert.NoError(t, d.Start())
2014-06-12 20:08:06 -07:00
}
2014-06-13 16:01:39 -07:00
func TestLedDriverHalt(t *testing.T) {
d := initTestLedDriver()
assert.NoError(t, d.Halt())
2014-06-12 20:08:06 -07:00
}
2014-06-13 16:01:39 -07:00
func TestLedDriverToggle(t *testing.T) {
d := initTestLedDriver()
_ = d.Off()
_ = d.Toggle()
assert.True(t, d.State())
_ = d.Toggle()
assert.False(t, d.State())
2014-06-12 20:08:06 -07:00
}
2014-06-13 16:01:39 -07:00
func TestLedDriverBrightness(t *testing.T) {
a := newGpioTestAdaptor()
d := NewLedDriver(a, "1")
a.pwmWriteFunc = func(string, byte) (err error) {
2014-12-16 13:42:48 -08:00
err = errors.New("pwm error")
return
}
assert.ErrorContains(t, d.Brightness(150), "pwm error")
2014-06-12 20:08:06 -07:00
}
func TestLEDDriverDefaultName(t *testing.T) {
a := newGpioTestAdaptor()
d := NewLedDriver(a, "1")
assert.True(t, strings.HasPrefix(d.Name(), "LED"))
}
func TestLEDDriverSetName(t *testing.T) {
a := newGpioTestAdaptor()
d := NewLedDriver(a, "1")
d.SetName("mybot")
assert.Equal(t, "mybot", d.Name())
}