-
-
Notifications
You must be signed in to change notification settings - Fork 1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
gpio(hcsr04): add driver for ultrasonic ranging module
- Loading branch information
1 parent
f7f4820
commit f9715b8
Showing
18 changed files
with
987 additions
and
8 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
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
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 |
---|---|---|
@@ -0,0 +1,78 @@ | ||
package gpio | ||
|
||
import ( | ||
"fmt" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
"gobot.io/x/gobot/v2" | ||
) | ||
|
||
var _ gobot.Driver = (*Driver)(nil) | ||
|
||
func initTestDriverWithStubbedAdaptor() (*Driver, *gpioTestAdaptor) { | ||
a := newGpioTestAdaptor() | ||
d := NewDriver(a, "GPIO_BASIC") | ||
return d, a | ||
} | ||
|
||
func initTestDriver() *Driver { | ||
d, _ := initTestDriverWithStubbedAdaptor() | ||
return d | ||
} | ||
|
||
func TestNewDriver(t *testing.T) { | ||
// arrange | ||
a := newGpioTestAdaptor() | ||
// act | ||
var di interface{} = NewDriver(a, "GPIO_BASIC") | ||
// assert | ||
d, ok := di.(*Driver) | ||
if !ok { | ||
t.Errorf("NewDriver() should have returned a *Driver") | ||
} | ||
assert.Contains(t, d.name, "GPIO_BASIC") | ||
assert.Equal(t, a, d.connection) | ||
assert.NoError(t, d.afterStart()) | ||
assert.NoError(t, d.beforeHalt()) | ||
assert.NotNil(t, d.Commander) | ||
assert.NotNil(t, d.mutex) | ||
} | ||
|
||
func TestSetName(t *testing.T) { | ||
// arrange | ||
d := initTestDriver() | ||
// act | ||
d.SetName("TESTME") | ||
// assert | ||
assert.Equal(t, "TESTME", d.Name()) | ||
} | ||
|
||
func TestConnection(t *testing.T) { | ||
// arrange | ||
d, a := initTestDriverWithStubbedAdaptor() | ||
// act, assert | ||
assert.Equal(t, a, d.Connection()) | ||
} | ||
|
||
func TestStart(t *testing.T) { | ||
// arrange | ||
d := initTestDriver() | ||
// act, assert | ||
assert.NoError(t, d.Start()) | ||
// arrange after start function | ||
d.afterStart = func() error { return fmt.Errorf("after start error") } | ||
// act, assert | ||
assert.ErrorContains(t, d.Start(), "after start error") | ||
} | ||
|
||
func TestHalt(t *testing.T) { | ||
// arrange | ||
d := initTestDriver() | ||
// act, assert | ||
assert.NoError(t, d.Halt()) | ||
// arrange after start function | ||
d.beforeHalt = func() error { return fmt.Errorf("before halt error") } | ||
// act, assert | ||
assert.ErrorContains(t, d.Halt(), "before halt error") | ||
} |
Oops, something went wrong.