2014-04-27 19:34:16 -07:00
|
|
|
package gpio
|
2014-04-26 03:11:51 -07:00
|
|
|
|
|
|
|
import (
|
2014-12-16 13:42:48 -08:00
|
|
|
"errors"
|
2014-06-12 20:08:06 -07:00
|
|
|
"testing"
|
2014-12-16 13:42:48 -08:00
|
|
|
"time"
|
2014-09-27 11:45:52 -07:00
|
|
|
|
|
|
|
"github.com/hybridgroup/gobot"
|
2014-04-26 03:11:51 -07:00
|
|
|
)
|
|
|
|
|
2014-12-16 13:42:48 -08:00
|
|
|
func TestAnalogSensorDriver(t *testing.T) {
|
|
|
|
d := NewAnalogSensorDriver(newGpioTestAdaptor("adaptor"), "bot", "1")
|
|
|
|
gobot.Assert(t, d.Name(), "bot")
|
|
|
|
gobot.Assert(t, d.Connection().Name(), "adaptor")
|
|
|
|
|
|
|
|
d = NewAnalogSensorDriver(newGpioTestAdaptor("adaptor"), "bot", "1", 30*time.Second)
|
|
|
|
gobot.Assert(t, d.interval, 30*time.Second)
|
|
|
|
|
|
|
|
testAdaptorAnalogRead = func() (val int, err error) {
|
|
|
|
val = 100
|
|
|
|
return
|
|
|
|
}
|
|
|
|
ret := d.Command("Read")(nil).(map[string]interface{})
|
|
|
|
|
|
|
|
gobot.Assert(t, ret["val"].(int), 100)
|
|
|
|
gobot.Assert(t, ret["err"], nil)
|
2014-06-12 20:08:06 -07:00
|
|
|
}
|
2014-04-26 03:11:51 -07:00
|
|
|
|
2014-06-13 16:01:39 -07:00
|
|
|
func TestAnalogSensorDriverStart(t *testing.T) {
|
2014-12-16 13:42:48 -08:00
|
|
|
sem := make(chan bool, 1)
|
|
|
|
|
|
|
|
d := NewAnalogSensorDriver(newGpioTestAdaptor("adaptor"), "bot", "1")
|
|
|
|
|
2014-11-19 23:21:19 -08:00
|
|
|
gobot.Assert(t, len(d.Start()), 0)
|
2014-12-16 13:42:48 -08:00
|
|
|
|
|
|
|
gobot.On(d.Event(Data), func(data interface{}) {
|
|
|
|
gobot.Assert(t, data.(int), 100)
|
|
|
|
sem <- true
|
|
|
|
})
|
|
|
|
|
|
|
|
testAdaptorAnalogRead = func() (val int, err error) {
|
|
|
|
val = 100
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
select {
|
|
|
|
case <-sem:
|
|
|
|
case <-time.After(15 * time.Millisecond):
|
|
|
|
t.Errorf("AnalogSensor Event \"Data\" was not published")
|
|
|
|
}
|
|
|
|
|
|
|
|
gobot.On(d.Event(Error), func(data interface{}) {
|
|
|
|
gobot.Assert(t, data.(error).Error(), "read error")
|
|
|
|
sem <- true
|
|
|
|
})
|
|
|
|
|
|
|
|
testAdaptorAnalogRead = func() (val int, err error) {
|
|
|
|
err = errors.New("read error")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
select {
|
|
|
|
case <-sem:
|
|
|
|
case <-time.After(15 * time.Millisecond):
|
|
|
|
t.Errorf("AnalogSensor Event \"Error\" was not published")
|
|
|
|
}
|
2014-06-12 20:08:06 -07:00
|
|
|
}
|
2014-06-13 12:39:02 -07:00
|
|
|
|
2014-06-13 16:01:39 -07:00
|
|
|
func TestAnalogSensorDriverHalt(t *testing.T) {
|
2014-12-16 13:42:48 -08:00
|
|
|
d := NewAnalogSensorDriver(newGpioTestAdaptor("adaptor"), "bot", "1")
|
2014-11-19 23:21:19 -08:00
|
|
|
gobot.Assert(t, len(d.Halt()), 0)
|
2014-06-12 20:08:06 -07:00
|
|
|
}
|