forked from hybridgroup/gobot
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adds tests for grove temperature sensor driver
Signed-off-by: Warren Fernandes <[email protected]>
- Loading branch information
1 parent
4422474
commit 7304361
Showing
2 changed files
with
71 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") | ||
} | ||
} |