Skip to content

Commit

Permalink
Adds tests for grove temperature sensor driver
Browse files Browse the repository at this point in the history
Signed-off-by: Warren Fernandes <[email protected]>
  • Loading branch information
wfernandes committed Oct 27, 2016
1 parent 4422474 commit 7304361
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 0 deletions.
1 change: 1 addition & 0 deletions drivers/gpio/grove_temperature_sensor_driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
var _ gobot.Driver = (*GroveTemperatureSensorDriver)(nil)

// GroveTemperatureSensorDriver represents a Temperature Sensor
// The temperature is reported in degree Celsius
type GroveTemperatureSensorDriver struct {
name string
pin string
Expand Down
70 changes: 70 additions & 0 deletions drivers/gpio/grove_temperature_sensor_driver_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,77 @@
package gpio

import (
"errors"
"fmt"
"testing"
"time"

"github.com/hybridgroup/gobot"
"github.com/wfernandes/gobot/gobottest"
)

var _ gobot.Driver = (*GroveTemperatureSensorDriver)(nil)

func TestGroveTemperatureSensorDriver(t *testing.T) {
testAdaptor := newGpioTestAdaptor()
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)
d := NewGroveTemperatureSensorDriver(newGpioTestAdaptor(), "1")

testAdaptorAnalogRead = func() (val int, err error) {
val = 585
return
}
gobottest.Assert(t, len(d.Start()), 0)

d.Once(d.Event(Data), func(data interface{}) {
gobottest.Assert(t, fmt.Sprintf("%.2f", data.(float64)), "31.62")
sem <- true
})
}

func TestGroveTempSensorPublishesError(t *testing.T) {
sem := make(chan bool, 1)
d := NewGroveTemperatureSensorDriver(newGpioTestAdaptor(), "1")

// send error
testAdaptorAnalogRead = func() (val int, err error) {
err = errors.New("read error")
return
}

gobottest.Assert(t, len(d.Start()), 0)

// expect error
d.Once(d.Event(Error), func(data interface{}) {
gobottest.Assert(t, data.(error).Error(), "read error")
sem <- true
})

select {
case <-sem:
case <-time.After(time.Second):
t.Errorf("Grove Temperature Sensor Event \"Error\" was not published")
}
}

func TestGroveTempSensorHalt(t *testing.T) {
d := NewGroveTemperatureSensorDriver(newGpioTestAdaptor(), "1")
done := make(chan struct{})
go func() {
<-d.halt
close(done)
}()
gobottest.Assert(t, len(d.Halt()), 0)
select {
case <-done:
case <-time.After(time.Millisecond):
t.Errorf("Grove Temperature Sensor was not halted")
}
}

0 comments on commit 7304361

Please sign in to comment.