2017-03-19 00:22:17 +08:00
|
|
|
package microbit
|
|
|
|
|
|
|
|
import (
|
|
|
|
"strings"
|
|
|
|
"testing"
|
2017-04-06 03:56:41 +08:00
|
|
|
"time"
|
2017-03-19 00:22:17 +08:00
|
|
|
|
|
|
|
"gobot.io/x/gobot"
|
|
|
|
"gobot.io/x/gobot/gobottest"
|
|
|
|
)
|
|
|
|
|
|
|
|
var _ gobot.Driver = (*TemperatureDriver)(nil)
|
|
|
|
|
|
|
|
func initTestTemperatureDriver() *TemperatureDriver {
|
2017-04-05 18:40:17 +08:00
|
|
|
d := NewTemperatureDriver(NewBleTestAdaptor())
|
2017-03-19 00:22:17 +08:00
|
|
|
return d
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestTemperatureDriver(t *testing.T) {
|
|
|
|
d := initTestTemperatureDriver()
|
|
|
|
gobottest.Assert(t, strings.HasPrefix(d.Name(), "Microbit Temperature"), true)
|
2017-04-05 18:40:17 +08:00
|
|
|
d.SetName("NewName")
|
|
|
|
gobottest.Assert(t, d.Name(), "NewName")
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestTemperatureDriverStartAndHalt(t *testing.T) {
|
|
|
|
d := initTestTemperatureDriver()
|
|
|
|
gobottest.Assert(t, d.Start(), nil)
|
|
|
|
gobottest.Assert(t, d.Halt(), nil)
|
2017-03-19 00:22:17 +08:00
|
|
|
}
|
2017-04-06 03:56:41 +08:00
|
|
|
|
|
|
|
func TestTemperatureDriverReadData(t *testing.T) {
|
|
|
|
sem := make(chan bool, 0)
|
|
|
|
a := NewBleTestAdaptor()
|
|
|
|
d := NewTemperatureDriver(a)
|
|
|
|
d.Start()
|
|
|
|
d.On(Temperature, func(data interface{}) {
|
|
|
|
gobottest.Assert(t, data, int8(0x22))
|
|
|
|
sem <- true
|
|
|
|
})
|
|
|
|
|
|
|
|
a.TestReceiveNotification([]byte{0x22}, nil)
|
|
|
|
|
|
|
|
select {
|
|
|
|
case <-sem:
|
|
|
|
case <-time.After(100 * time.Millisecond):
|
|
|
|
t.Errorf("Microbit Event \"Temperature\" was not published")
|
|
|
|
}
|
|
|
|
}
|