2014-04-27 19:34:16 -07:00
|
|
|
package gpio
|
|
|
|
|
|
|
|
import (
|
2014-12-16 13:42:48 -08:00
|
|
|
"errors"
|
2017-03-21 11:55:46 +01:00
|
|
|
"strings"
|
2014-06-12 20:08:06 -07:00
|
|
|
"testing"
|
2014-07-09 18:19:58 -07:00
|
|
|
|
2023-10-20 10:27:09 +02:00
|
|
|
"github.com/stretchr/testify/assert"
|
2023-05-20 14:25:21 +02:00
|
|
|
"gobot.io/x/gobot/v2"
|
2014-04-27 19:34:16 -07:00
|
|
|
)
|
|
|
|
|
2016-12-23 09:37:09 +01:00
|
|
|
var _ gobot.Driver = (*LedDriver)(nil)
|
|
|
|
|
2017-04-02 17:15:45 +02:00
|
|
|
func initTestLedDriver() *LedDriver {
|
|
|
|
a := newGpioTestAdaptor()
|
2023-10-27 20:46:45 +02:00
|
|
|
a.digitalWriteFunc = func(string, byte) (err error) {
|
2014-12-16 13:42:48 -08:00
|
|
|
return nil
|
|
|
|
}
|
2023-10-27 20:46:45 +02:00
|
|
|
a.pwmWriteFunc = func(string, byte) (err error) {
|
2014-12-16 13:42:48 -08:00
|
|
|
return nil
|
|
|
|
}
|
2017-04-02 17:15:45 +02:00
|
|
|
return NewLedDriver(a, "1")
|
2014-12-16 13:42:48 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestLedDriver(t *testing.T) {
|
|
|
|
var err interface{}
|
2017-04-02 17:15:45 +02:00
|
|
|
a := newGpioTestAdaptor()
|
|
|
|
d := NewLedDriver(a, "1")
|
2014-12-16 13:42:48 -08:00
|
|
|
|
2023-10-20 10:27:09 +02:00
|
|
|
assert.Equal(t, "1", d.Pin())
|
|
|
|
assert.NotNil(t, d.Connection())
|
2014-12-16 13:42:48 -08:00
|
|
|
|
2023-10-27 20:46:45 +02:00
|
|
|
a.digitalWriteFunc = func(string, byte) (err error) {
|
2014-12-16 13:42:48 -08:00
|
|
|
return errors.New("write error")
|
|
|
|
}
|
2023-10-27 20:46:45 +02:00
|
|
|
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)
|
2023-10-25 20:21:18 +02:00
|
|
|
assert.ErrorContains(t, err.(error), "write error")
|
2014-12-16 13:42:48 -08:00
|
|
|
|
|
|
|
err = d.Command("On")(nil)
|
2023-10-25 20:21:18 +02:00
|
|
|
assert.ErrorContains(t, err.(error), "write error")
|
2014-12-16 13:42:48 -08:00
|
|
|
|
|
|
|
err = d.Command("Off")(nil)
|
2023-10-25 20:21:18 +02:00
|
|
|
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})
|
2023-10-25 20:21:18 +02:00
|
|
|
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) {
|
2017-04-02 17:15:45 +02:00
|
|
|
d := initTestLedDriver()
|
2023-10-26 20:34:19 +02:00
|
|
|
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) {
|
2017-04-02 17:15:45 +02:00
|
|
|
d := initTestLedDriver()
|
2023-10-26 20:34:19 +02:00
|
|
|
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) {
|
2017-04-02 17:15:45 +02:00
|
|
|
d := initTestLedDriver()
|
2023-06-12 19:51:25 +02:00
|
|
|
_ = d.Off()
|
|
|
|
_ = d.Toggle()
|
2023-10-20 10:27:09 +02:00
|
|
|
assert.True(t, d.State())
|
2023-06-12 19:51:25 +02:00
|
|
|
_ = d.Toggle()
|
2023-10-20 10:27:09 +02:00
|
|
|
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) {
|
2017-04-02 17:15:45 +02:00
|
|
|
a := newGpioTestAdaptor()
|
|
|
|
d := NewLedDriver(a, "1")
|
2023-10-27 20:46:45 +02:00
|
|
|
a.pwmWriteFunc = func(string, byte) (err error) {
|
2014-12-16 13:42:48 -08:00
|
|
|
err = errors.New("pwm error")
|
|
|
|
return
|
|
|
|
}
|
2023-10-25 20:21:18 +02:00
|
|
|
assert.ErrorContains(t, d.Brightness(150), "pwm error")
|
2014-06-12 20:08:06 -07:00
|
|
|
}
|
2017-03-21 11:55:46 +01:00
|
|
|
|
|
|
|
func TestLEDDriverDefaultName(t *testing.T) {
|
2017-04-02 17:15:45 +02:00
|
|
|
a := newGpioTestAdaptor()
|
|
|
|
d := NewLedDriver(a, "1")
|
2023-10-20 10:27:09 +02:00
|
|
|
assert.True(t, strings.HasPrefix(d.Name(), "LED"))
|
2017-03-21 11:55:46 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestLEDDriverSetName(t *testing.T) {
|
2017-04-02 17:15:45 +02:00
|
|
|
a := newGpioTestAdaptor()
|
|
|
|
d := NewLedDriver(a, "1")
|
2017-03-21 11:55:46 +01:00
|
|
|
d.SetName("mybot")
|
2023-10-20 10:27:09 +02:00
|
|
|
assert.Equal(t, "mybot", d.Name())
|
2017-03-21 11:55:46 +01:00
|
|
|
}
|