2016-12-20 20:25:22 +08:00
|
|
|
package aio
|
2016-09-01 18:17:43 +08:00
|
|
|
|
|
|
|
import (
|
2016-10-27 19:21:50 +08:00
|
|
|
"errors"
|
|
|
|
"fmt"
|
2017-03-23 20:13:14 +08:00
|
|
|
"strings"
|
2016-10-27 19:21:50 +08:00
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
2016-12-08 20:24:03 +08:00
|
|
|
"gobot.io/x/gobot"
|
|
|
|
"gobot.io/x/gobot/gobottest"
|
2016-09-01 18:17:43 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
var _ gobot.Driver = (*GroveTemperatureSensorDriver)(nil)
|
2016-10-27 19:21:50 +08:00
|
|
|
|
|
|
|
func TestGroveTemperatureSensorDriver(t *testing.T) {
|
2016-12-20 20:25:22 +08:00
|
|
|
testAdaptor := newAioTestAdaptor()
|
2016-10-27 19:21:50 +08:00
|
|
|
d := NewGroveTemperatureSensorDriver(testAdaptor, "123")
|
|
|
|
gobottest.Assert(t, d.Connection(), testAdaptor)
|
|
|
|
gobottest.Assert(t, d.Pin(), "123")
|
|
|
|
gobottest.Assert(t, d.interval, 10*time.Millisecond)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestGroveTempSensorPublishesTemperatureInCelsius(t *testing.T) {
|
|
|
|
sem := make(chan bool, 1)
|
2017-04-02 23:55:49 +08:00
|
|
|
a := newAioTestAdaptor()
|
|
|
|
d := NewGroveTemperatureSensorDriver(a, "1")
|
2016-10-27 19:21:50 +08:00
|
|
|
|
2017-04-02 23:55:49 +08:00
|
|
|
a.TestAdaptorAnalogRead(func() (val int, err error) {
|
2016-10-27 19:21:50 +08:00
|
|
|
val = 585
|
|
|
|
return
|
2017-04-02 23:55:49 +08:00
|
|
|
})
|
2016-10-27 19:21:50 +08:00
|
|
|
d.Once(d.Event(Data), func(data interface{}) {
|
|
|
|
gobottest.Assert(t, fmt.Sprintf("%.2f", data.(float64)), "31.62")
|
|
|
|
sem <- true
|
|
|
|
})
|
2017-04-11 18:59:21 +08:00
|
|
|
gobottest.Assert(t, d.Start(), nil)
|
2017-03-23 20:13:14 +08:00
|
|
|
|
|
|
|
select {
|
|
|
|
case <-sem:
|
2017-04-21 17:03:28 +08:00
|
|
|
case <-time.After(1 * time.Second):
|
2017-03-23 20:13:14 +08:00
|
|
|
t.Errorf("Grove Temperature Sensor Event \"Data\" was not published")
|
|
|
|
}
|
|
|
|
|
|
|
|
gobottest.Assert(t, d.Temperature(), 31.61532462352477)
|
2016-10-27 19:21:50 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestGroveTempSensorPublishesError(t *testing.T) {
|
|
|
|
sem := make(chan bool, 1)
|
2017-04-02 23:55:49 +08:00
|
|
|
a := newAioTestAdaptor()
|
|
|
|
d := NewGroveTemperatureSensorDriver(a, "1")
|
2016-10-27 19:21:50 +08:00
|
|
|
|
|
|
|
// send error
|
2017-04-02 23:55:49 +08:00
|
|
|
a.TestAdaptorAnalogRead(func() (val int, err error) {
|
2016-10-27 19:21:50 +08:00
|
|
|
err = errors.New("read error")
|
|
|
|
return
|
2017-04-02 23:55:49 +08:00
|
|
|
})
|
2016-10-27 19:21:50 +08:00
|
|
|
|
2016-11-08 00:12:14 +08:00
|
|
|
gobottest.Assert(t, d.Start(), nil)
|
2016-10-27 19:21:50 +08:00
|
|
|
|
|
|
|
// expect error
|
|
|
|
d.Once(d.Event(Error), func(data interface{}) {
|
|
|
|
gobottest.Assert(t, data.(error).Error(), "read error")
|
|
|
|
sem <- true
|
|
|
|
})
|
|
|
|
|
|
|
|
select {
|
|
|
|
case <-sem:
|
2017-04-21 17:03:28 +08:00
|
|
|
case <-time.After(1 * time.Second):
|
2016-10-27 19:21:50 +08:00
|
|
|
t.Errorf("Grove Temperature Sensor Event \"Error\" was not published")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestGroveTempSensorHalt(t *testing.T) {
|
2016-12-20 20:25:22 +08:00
|
|
|
d := NewGroveTemperatureSensorDriver(newAioTestAdaptor(), "1")
|
2016-10-27 19:21:50 +08:00
|
|
|
done := make(chan struct{})
|
|
|
|
go func() {
|
|
|
|
<-d.halt
|
|
|
|
close(done)
|
|
|
|
}()
|
2016-11-08 00:12:14 +08:00
|
|
|
gobottest.Assert(t, d.Halt(), nil)
|
2016-10-27 19:21:50 +08:00
|
|
|
select {
|
|
|
|
case <-done:
|
2017-04-21 17:03:28 +08:00
|
|
|
case <-time.After(100 * time.Millisecond):
|
2016-10-27 19:21:50 +08:00
|
|
|
t.Errorf("Grove Temperature Sensor was not halted")
|
|
|
|
}
|
|
|
|
}
|
2017-03-23 20:13:14 +08:00
|
|
|
|
|
|
|
func TestGroveTempDriverDefaultName(t *testing.T) {
|
|
|
|
d := NewGroveTemperatureSensorDriver(newAioTestAdaptor(), "1")
|
|
|
|
gobottest.Assert(t, strings.HasPrefix(d.Name(), "GroveTemperatureSensor"), true)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestGroveTempDriverSetName(t *testing.T) {
|
|
|
|
d := NewGroveTemperatureSensorDriver(newAioTestAdaptor(), "1")
|
|
|
|
d.SetName("mybot")
|
|
|
|
gobottest.Assert(t, d.Name(), "mybot")
|
|
|
|
}
|