-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
Copy pathbuzzer_driver_test.go
103 lines (88 loc) · 2.71 KB
/
buzzer_driver_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
package gpio
import (
"errors"
"strings"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"gobot.io/x/gobot/v2"
"gobot.io/x/gobot/v2/drivers/aio"
)
var _ gobot.Driver = (*BuzzerDriver)(nil)
func initTestBuzzerDriver(conn DigitalWriter) *BuzzerDriver {
return NewBuzzerDriver(conn, "1")
}
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)
}
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)
}
func TestBuzzerToggle(t *testing.T) {
d := initTestBuzzerDriver(newGpioTestAdaptor())
require.NoError(t, d.Off())
require.NoError(t, d.Toggle())
assert.True(t, d.State())
require.NoError(t, d.Toggle())
assert.False(t, d.State())
}
func TestBuzzerTone(t *testing.T) {
d := initTestBuzzerDriver(newGpioTestAdaptor())
require.NoError(t, d.Tone(100, 0.01))
}
func TestBuzzerOnError(t *testing.T) {
a := newGpioTestAdaptor()
d := initTestBuzzerDriver(a)
a.digitalWriteFunc = func(string, byte) error {
return errors.New("write error")
}
require.EqualError(t, d.On(), "write error")
}
func TestBuzzerOffError(t *testing.T) {
a := newGpioTestAdaptor()
d := initTestBuzzerDriver(a)
a.digitalWriteFunc = func(string, byte) error {
return errors.New("write error")
}
require.EqualError(t, d.Off(), "write error")
}
func TestBuzzerToneError(t *testing.T) {
a := newGpioTestAdaptor()
d := initTestBuzzerDriver(a)
a.digitalWriteFunc = func(string, byte) error {
return errors.New("write error")
}
require.EqualError(t, d.Tone(100, 0.01), "write error")
}