2017-03-21 11:38:18 +01:00
|
|
|
package gpio
|
|
|
|
|
|
|
|
import (
|
2017-04-04 15:42:45 +02:00
|
|
|
"errors"
|
2017-03-21 11:38:18 +01:00
|
|
|
"strings"
|
|
|
|
"testing"
|
|
|
|
|
2023-10-20 10:27:09 +02:00
|
|
|
"github.com/stretchr/testify/assert"
|
2023-11-12 14:17:02 +01:00
|
|
|
"github.com/stretchr/testify/require"
|
2023-11-15 20:51:52 +01:00
|
|
|
|
2023-05-20 14:25:21 +02:00
|
|
|
"gobot.io/x/gobot/v2"
|
2023-12-03 18:03:02 +01:00
|
|
|
"gobot.io/x/gobot/v2/drivers/aio"
|
2017-03-21 11:38:18 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
var _ gobot.Driver = (*BuzzerDriver)(nil)
|
|
|
|
|
|
|
|
func initTestBuzzerDriver(conn DigitalWriter) *BuzzerDriver {
|
|
|
|
return NewBuzzerDriver(conn, "1")
|
|
|
|
}
|
|
|
|
|
2023-12-03 18:03:02 +01:00
|
|
|
func TestNewBuzzerDriver(t *testing.T) {
|
|
|
|
// arrange
|
|
|
|
a := newGpioTestAdaptor()
|
|
|
|
// act
|
|
|
|
d := NewBuzzerDriver(a, "10")
|
|
|
|
// assert
|
|
|
|
assert.IsType(t, &BuzzerDriver{}, d)
|
|
|
|
// assert: gpio.driver attributes
|
|
|
|
require.NotNil(t, d.driver)
|
|
|
|
assert.True(t, strings.HasPrefix(d.driverCfg.name, "Buzzer"))
|
|
|
|
assert.Equal(t, "10", d.driverCfg.pin)
|
|
|
|
assert.Equal(t, a, d.connection)
|
|
|
|
assert.NotNil(t, d.afterStart)
|
|
|
|
assert.NotNil(t, d.beforeHalt)
|
|
|
|
assert.NotNil(t, d.Commander)
|
|
|
|
assert.NotNil(t, d.mutex)
|
|
|
|
// assert: driver specific attributes
|
|
|
|
assert.False(t, d.high)
|
|
|
|
assert.InDelta(t, 96, d.bpm, 0.0)
|
2017-03-21 11:38:18 +01:00
|
|
|
}
|
|
|
|
|
2023-12-03 18:03:02 +01:00
|
|
|
func TestNewBuzzerDriver_options(t *testing.T) {
|
|
|
|
// This is a general test, that options are applied in constructor by using the common WithName() option, least one
|
|
|
|
// option of this driver and one of another driver (which should lead to panic). Further tests for options can also
|
|
|
|
// be done by call of "WithOption(val).apply(cfg)".
|
|
|
|
// arrange
|
|
|
|
const (
|
|
|
|
myName = "song player"
|
|
|
|
)
|
|
|
|
panicFunc := func() {
|
|
|
|
NewBuzzerDriver(newGpioTestAdaptor(), "1", WithName("crazy"),
|
|
|
|
aio.WithActuatorScaler(func(float64) int { return 0 }))
|
|
|
|
}
|
|
|
|
// act
|
|
|
|
d := NewBuzzerDriver(newGpioTestAdaptor(), "1", WithName(myName))
|
|
|
|
// assert
|
|
|
|
assert.Equal(t, myName, d.Name())
|
|
|
|
assert.PanicsWithValue(t, "'scaler option for analog actuators' can not be applied on 'crazy'", panicFunc)
|
2017-03-21 11:38:18 +01:00
|
|
|
}
|
2017-03-21 11:59:38 +01:00
|
|
|
|
2023-12-03 18:03:02 +01:00
|
|
|
func TestBuzzerToggle(t *testing.T) {
|
2017-03-21 11:59:38 +01:00
|
|
|
d := initTestBuzzerDriver(newGpioTestAdaptor())
|
2023-12-03 18:03:02 +01:00
|
|
|
require.NoError(t, d.Off())
|
|
|
|
require.NoError(t, d.Toggle())
|
2023-10-20 10:27:09 +02:00
|
|
|
assert.True(t, d.State())
|
2023-12-03 18:03:02 +01:00
|
|
|
require.NoError(t, d.Toggle())
|
2023-10-20 10:27:09 +02:00
|
|
|
assert.False(t, d.State())
|
2017-03-21 11:59:38 +01:00
|
|
|
}
|
2017-03-21 12:37:27 +01:00
|
|
|
|
2023-12-03 18:03:02 +01:00
|
|
|
func TestBuzzerTone(t *testing.T) {
|
2017-03-21 12:37:27 +01:00
|
|
|
d := initTestBuzzerDriver(newGpioTestAdaptor())
|
2023-11-12 14:17:02 +01:00
|
|
|
require.NoError(t, d.Tone(100, 0.01))
|
2017-03-21 12:37:27 +01:00
|
|
|
}
|
2017-04-04 15:42:45 +02:00
|
|
|
|
2023-12-03 18:03:02 +01:00
|
|
|
func TestBuzzerOnError(t *testing.T) {
|
2017-04-04 15:42:45 +02:00
|
|
|
a := newGpioTestAdaptor()
|
|
|
|
d := initTestBuzzerDriver(a)
|
2023-11-15 20:51:52 +01:00
|
|
|
a.digitalWriteFunc = func(string, byte) error {
|
2017-04-04 15:42:45 +02:00
|
|
|
return errors.New("write error")
|
2023-10-27 20:46:45 +02:00
|
|
|
}
|
2017-04-04 15:42:45 +02:00
|
|
|
|
2023-11-23 19:01:42 +01:00
|
|
|
require.EqualError(t, d.On(), "write error")
|
2017-04-04 15:42:45 +02:00
|
|
|
}
|
|
|
|
|
2023-12-03 18:03:02 +01:00
|
|
|
func TestBuzzerOffError(t *testing.T) {
|
2017-04-04 15:42:45 +02:00
|
|
|
a := newGpioTestAdaptor()
|
|
|
|
d := initTestBuzzerDriver(a)
|
2023-11-15 20:51:52 +01:00
|
|
|
a.digitalWriteFunc = func(string, byte) error {
|
2017-04-04 15:42:45 +02:00
|
|
|
return errors.New("write error")
|
2023-10-27 20:46:45 +02:00
|
|
|
}
|
2017-04-04 15:42:45 +02:00
|
|
|
|
2023-11-23 19:01:42 +01:00
|
|
|
require.EqualError(t, d.Off(), "write error")
|
2017-04-04 15:42:45 +02:00
|
|
|
}
|
|
|
|
|
2023-12-03 18:03:02 +01:00
|
|
|
func TestBuzzerToneError(t *testing.T) {
|
2017-04-04 15:42:45 +02:00
|
|
|
a := newGpioTestAdaptor()
|
|
|
|
d := initTestBuzzerDriver(a)
|
2023-11-15 20:51:52 +01:00
|
|
|
a.digitalWriteFunc = func(string, byte) error {
|
2017-04-04 15:42:45 +02:00
|
|
|
return errors.New("write error")
|
2023-10-27 20:46:45 +02:00
|
|
|
}
|
2017-04-04 15:42:45 +02:00
|
|
|
|
2023-11-23 19:01:42 +01:00
|
|
|
require.EqualError(t, d.Tone(100, 0.01), "write error")
|
2017-04-04 15:42:45 +02:00
|
|
|
}
|