2017-03-21 18:38:18 +08:00
|
|
|
package gpio
|
|
|
|
|
|
|
|
import (
|
2017-04-04 21:42:45 +08:00
|
|
|
"errors"
|
2017-03-21 18:38:18 +08:00
|
|
|
"strings"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"gobot.io/x/gobot"
|
|
|
|
"gobot.io/x/gobot/gobottest"
|
|
|
|
)
|
|
|
|
|
|
|
|
var _ gobot.Driver = (*BuzzerDriver)(nil)
|
|
|
|
|
|
|
|
func initTestBuzzerDriver(conn DigitalWriter) *BuzzerDriver {
|
|
|
|
return NewBuzzerDriver(conn, "1")
|
|
|
|
}
|
|
|
|
|
2017-03-21 18:55:46 +08:00
|
|
|
func TestBuzzerDriverDefaultName(t *testing.T) {
|
2017-03-21 18:38:18 +08:00
|
|
|
g := initTestBuzzerDriver(newGpioTestAdaptor())
|
|
|
|
gobottest.Assert(t, strings.HasPrefix(g.Name(), "Buzzer"), true)
|
|
|
|
}
|
|
|
|
|
2017-03-21 18:55:46 +08:00
|
|
|
func TestBuzzerDriverSetName(t *testing.T) {
|
|
|
|
g := initTestBuzzerDriver(newGpioTestAdaptor())
|
|
|
|
g.SetName("mybot")
|
|
|
|
gobottest.Assert(t, g.Name(), "mybot")
|
|
|
|
}
|
|
|
|
|
2017-03-21 18:38:18 +08:00
|
|
|
func TestBuzzerDriverStart(t *testing.T) {
|
|
|
|
d := initTestBuzzerDriver(newGpioTestAdaptor())
|
|
|
|
gobottest.Assert(t, d.Start(), nil)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestBuzzerDriverHalt(t *testing.T) {
|
|
|
|
d := initTestBuzzerDriver(newGpioTestAdaptor())
|
|
|
|
gobottest.Assert(t, d.Halt(), nil)
|
|
|
|
}
|
2017-03-21 18:59:38 +08:00
|
|
|
|
|
|
|
func TestBuzzerDriverToggle(t *testing.T) {
|
|
|
|
d := initTestBuzzerDriver(newGpioTestAdaptor())
|
|
|
|
d.Off()
|
|
|
|
d.Toggle()
|
|
|
|
gobottest.Assert(t, d.State(), true)
|
|
|
|
d.Toggle()
|
|
|
|
gobottest.Assert(t, d.State(), false)
|
|
|
|
}
|
2017-03-21 19:37:27 +08:00
|
|
|
|
|
|
|
func TestBuzzerDriverTone(t *testing.T) {
|
|
|
|
d := initTestBuzzerDriver(newGpioTestAdaptor())
|
|
|
|
gobottest.Assert(t, d.Tone(100, 0.01), nil)
|
|
|
|
}
|
2017-04-04 21:42:45 +08:00
|
|
|
|
|
|
|
func TestBuzzerDriverOnError(t *testing.T) {
|
|
|
|
a := newGpioTestAdaptor()
|
|
|
|
d := initTestBuzzerDriver(a)
|
|
|
|
a.TestAdaptorDigitalWrite(func() (err error) {
|
|
|
|
return errors.New("write error")
|
|
|
|
})
|
|
|
|
|
|
|
|
gobottest.Assert(t, d.On(), errors.New("write error"))
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestBuzzerDriverOffError(t *testing.T) {
|
|
|
|
a := newGpioTestAdaptor()
|
|
|
|
d := initTestBuzzerDriver(a)
|
|
|
|
a.TestAdaptorDigitalWrite(func() (err error) {
|
|
|
|
return errors.New("write error")
|
|
|
|
})
|
|
|
|
|
|
|
|
gobottest.Assert(t, d.Off(), errors.New("write error"))
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestBuzzerDriverToneError(t *testing.T) {
|
|
|
|
a := newGpioTestAdaptor()
|
|
|
|
d := initTestBuzzerDriver(a)
|
|
|
|
a.TestAdaptorDigitalWrite(func() (err error) {
|
|
|
|
return errors.New("write error")
|
|
|
|
})
|
|
|
|
|
|
|
|
gobottest.Assert(t, d.Tone(100, 0.01), errors.New("write error"))
|
|
|
|
}
|