95 lines
2.1 KiB
Go
95 lines
2.1 KiB
Go
package gpio
|
|
|
|
import (
|
|
"errors"
|
|
"strings"
|
|
"testing"
|
|
|
|
"gobot.io/x/gobot/v2"
|
|
"gobot.io/x/gobot/v2/gobottest"
|
|
)
|
|
|
|
var _ gobot.Driver = (*LedDriver)(nil)
|
|
|
|
func initTestLedDriver() *LedDriver {
|
|
a := newGpioTestAdaptor()
|
|
a.testAdaptorDigitalWrite = func(string, byte) (err error) {
|
|
return nil
|
|
}
|
|
a.testAdaptorPwmWrite = func(string, byte) (err error) {
|
|
return nil
|
|
}
|
|
return NewLedDriver(a, "1")
|
|
}
|
|
|
|
func TestLedDriver(t *testing.T) {
|
|
var err interface{}
|
|
a := newGpioTestAdaptor()
|
|
d := NewLedDriver(a, "1")
|
|
|
|
gobottest.Assert(t, d.Pin(), "1")
|
|
gobottest.Refute(t, d.Connection(), nil)
|
|
|
|
a.testAdaptorDigitalWrite = func(string, byte) (err error) {
|
|
return errors.New("write error")
|
|
}
|
|
a.testAdaptorPwmWrite = func(string, byte) (err error) {
|
|
return errors.New("pwm error")
|
|
}
|
|
|
|
err = d.Command("Toggle")(nil)
|
|
gobottest.Assert(t, err.(error), errors.New("write error"))
|
|
|
|
err = d.Command("On")(nil)
|
|
gobottest.Assert(t, err.(error), errors.New("write error"))
|
|
|
|
err = d.Command("Off")(nil)
|
|
gobottest.Assert(t, err.(error), errors.New("write error"))
|
|
|
|
err = d.Command("Brightness")(map[string]interface{}{"level": 100.0})
|
|
gobottest.Assert(t, err.(error), errors.New("pwm error"))
|
|
|
|
}
|
|
|
|
func TestLedDriverStart(t *testing.T) {
|
|
d := initTestLedDriver()
|
|
gobottest.Assert(t, d.Start(), nil)
|
|
}
|
|
|
|
func TestLedDriverHalt(t *testing.T) {
|
|
d := initTestLedDriver()
|
|
gobottest.Assert(t, d.Halt(), nil)
|
|
}
|
|
|
|
func TestLedDriverToggle(t *testing.T) {
|
|
d := initTestLedDriver()
|
|
_ = d.Off()
|
|
_ = d.Toggle()
|
|
gobottest.Assert(t, d.State(), true)
|
|
_ = d.Toggle()
|
|
gobottest.Assert(t, d.State(), false)
|
|
}
|
|
|
|
func TestLedDriverBrightness(t *testing.T) {
|
|
a := newGpioTestAdaptor()
|
|
d := NewLedDriver(a, "1")
|
|
a.testAdaptorPwmWrite = func(string, byte) (err error) {
|
|
err = errors.New("pwm error")
|
|
return
|
|
}
|
|
gobottest.Assert(t, d.Brightness(150), errors.New("pwm error"))
|
|
}
|
|
|
|
func TestLEDDriverDefaultName(t *testing.T) {
|
|
a := newGpioTestAdaptor()
|
|
d := NewLedDriver(a, "1")
|
|
gobottest.Assert(t, strings.HasPrefix(d.Name(), "LED"), true)
|
|
}
|
|
|
|
func TestLEDDriverSetName(t *testing.T) {
|
|
a := newGpioTestAdaptor()
|
|
d := NewLedDriver(a, "1")
|
|
d.SetName("mybot")
|
|
gobottest.Assert(t, d.Name(), "mybot")
|
|
}
|