2023-11-16 03:51:52 +08:00
|
|
|
//nolint:forcetypeassert,dupl // ok here
|
2017-03-01 08:23:50 +08:00
|
|
|
package microbit
|
|
|
|
|
|
|
|
import (
|
|
|
|
"strings"
|
|
|
|
"testing"
|
2017-04-06 03:56:41 +08:00
|
|
|
"time"
|
2017-03-01 08:23:50 +08:00
|
|
|
|
2023-10-20 16:27:09 +08:00
|
|
|
"github.com/stretchr/testify/assert"
|
2023-11-12 21:17:02 +08:00
|
|
|
"github.com/stretchr/testify/require"
|
2023-11-16 03:51:52 +08:00
|
|
|
|
2023-05-20 20:25:21 +08:00
|
|
|
"gobot.io/x/gobot/v2"
|
2024-02-11 01:02:09 +08:00
|
|
|
"gobot.io/x/gobot/v2/drivers/ble"
|
2024-02-05 01:50:43 +08:00
|
|
|
"gobot.io/x/gobot/v2/drivers/ble/testutil"
|
2017-03-01 08:23:50 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
var _ gobot.Driver = (*AccelerometerDriver)(nil)
|
|
|
|
|
2024-02-05 01:50:43 +08:00
|
|
|
func TestNewAccelerometerDriver(t *testing.T) {
|
|
|
|
d := NewAccelerometerDriver(testutil.NewBleTestAdaptor())
|
|
|
|
assert.IsType(t, &AccelerometerDriver{}, d)
|
2023-10-20 16:27:09 +08:00
|
|
|
assert.True(t, strings.HasPrefix(d.Name(), "Microbit Accelerometer"))
|
2024-02-05 01:50:43 +08:00
|
|
|
assert.NotNil(t, d.Eventer)
|
2017-04-05 18:40:17 +08:00
|
|
|
}
|
|
|
|
|
2024-02-11 01:02:09 +08:00
|
|
|
func TestNewAccelerometerDriverWithName(t *testing.T) {
|
|
|
|
// This is a general test, that options are applied in constructor by using the common WithName() option. Further
|
|
|
|
// tests for options can also be done by call of "WithOption(val).apply(cfg)".
|
|
|
|
// arrange
|
|
|
|
const newName = "new name"
|
|
|
|
a := testutil.NewBleTestAdaptor()
|
|
|
|
// act
|
|
|
|
d := NewAccelerometerDriver(a, ble.WithName(newName))
|
|
|
|
// assert
|
|
|
|
assert.Equal(t, newName, d.Name())
|
|
|
|
}
|
|
|
|
|
2024-02-05 01:50:43 +08:00
|
|
|
func TestAccelerometerStartAndHalt(t *testing.T) {
|
|
|
|
d := NewAccelerometerDriver(testutil.NewBleTestAdaptor())
|
2023-11-12 21:17:02 +08:00
|
|
|
require.NoError(t, d.Start())
|
|
|
|
require.NoError(t, d.Halt())
|
2017-03-01 08:23:50 +08:00
|
|
|
}
|
2017-04-06 03:56:41 +08:00
|
|
|
|
2024-02-05 01:50:43 +08:00
|
|
|
func TestAccelerometerReadData(t *testing.T) {
|
2023-05-19 20:16:22 +08:00
|
|
|
sem := make(chan bool)
|
2024-02-05 01:50:43 +08:00
|
|
|
a := testutil.NewBleTestAdaptor()
|
2017-04-06 03:56:41 +08:00
|
|
|
d := NewAccelerometerDriver(a)
|
2024-02-05 01:50:43 +08:00
|
|
|
require.NoError(t, d.Start())
|
|
|
|
|
|
|
|
err := d.On("accelerometer", func(data interface{}) {
|
2023-11-12 21:17:02 +08:00
|
|
|
assert.InDelta(t, float32(8.738), data.(*AccelerometerData).X, 0.0)
|
|
|
|
assert.InDelta(t, float32(8.995), data.(*AccelerometerData).Y, 0.0)
|
|
|
|
assert.InDelta(t, float32(9.252), data.(*AccelerometerData).Z, 0.0)
|
2017-04-06 03:56:41 +08:00
|
|
|
sem <- true
|
|
|
|
})
|
|
|
|
|
2024-02-05 01:50:43 +08:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
2024-02-11 01:02:09 +08:00
|
|
|
a.SendTestDataToSubscriber([]byte{0x22, 0x22, 0x23, 0x23, 0x24, 0x24})
|
2017-04-06 03:56:41 +08:00
|
|
|
|
|
|
|
select {
|
|
|
|
case <-sem:
|
|
|
|
case <-time.After(100 * time.Millisecond):
|
2024-02-13 17:33:46 +08:00
|
|
|
require.Fail(t, "Microbit Event \"Accelerometer\" was not published")
|
2017-04-06 03:56:41 +08:00
|
|
|
}
|
|
|
|
}
|