Skip to content

Commit

Permalink
spitest: add missing lock to Playback.Connect(); improve coverage
Browse files Browse the repository at this point in the history
Improve test coverage to 100% for gpiostream, gpiostreamtest, gpiotest, spitest.
  • Loading branch information
maruel committed Aug 12, 2018
1 parent 3692515 commit d54e035
Show file tree
Hide file tree
Showing 5 changed files with 112 additions and 8 deletions.
8 changes: 8 additions & 0 deletions conn/gpio/gpiostream/gpiostream_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,14 @@ func TestBitStream(t *testing.T) {
}
}

func TestBitStream_Empty(t *testing.T) {
var b [16]byte
s := BitStream{Bits: b[:]}
if d := s.Duration(); d != 0 {
t.Fatal(d)
}
}

func TestEdgeStream(t *testing.T) {
s := EdgeStream{Freq: physic.KiloHertz, Edges: []uint16{1000, 1}}
if f := s.Frequency(); f != physic.KiloHertz {
Expand Down
9 changes: 9 additions & 0 deletions conn/gpio/gpiostream/gpiostreamtest/gpiostreamtest_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@ func TestPinIn(t *testing.T) {
if s := p.String(); s != "Yo" {
t.Fatal(s)
}
if err := p.Halt(); err != nil {
t.Fatal(err)
}
if err := p.Close(); err != nil {
t.Fatal(err)
}
Expand Down Expand Up @@ -136,6 +139,9 @@ func TestPinOutPlayback(t *testing.T) {
if s := p.String(); s != "Yo" {
t.Fatal(s)
}
if err := p.Halt(); err != nil {
t.Fatal(err)
}
if err := p.Close(); err != nil {
t.Fatal(err)
}
Expand Down Expand Up @@ -179,6 +185,9 @@ func TestPinOutRecord(t *testing.T) {
if s := p.String(); s != "Yo" {
t.Fatal(s)
}
if err := p.Halt(); err != nil {
t.Fatal(err)
}
}

func TestPinOutRecord_fail(t *testing.T) {
Expand Down
38 changes: 31 additions & 7 deletions conn/gpio/gpiotest/gpiotest_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,24 @@ import (

func TestPin(t *testing.T) {
p := &Pin{N: "GPIO1", Num: 10, Fn: "I2C1_SDA"}
// conn.Resource
if s := p.String(); s != "GPIO1(10)" {
t.Fatal(s)
}
if err := p.Halt(); err != nil {
t.Fatal(err)
}
// pin.Pin
if n := p.Number(); n != 10 {
t.Fatal(n)
}
if n := p.Name(); n != "GPIO1" {
t.Fatal(n)
}
if f := p.Function(); f != "I2C1_SDA" {
t.Fatal(f)
}
// gpio.PinIn
if err := p.In(gpio.PullDown, gpio.NoEdge); err != nil {
t.Fatal(err)
}
Expand All @@ -39,11 +51,15 @@ func TestPin(t *testing.T) {
if pull := p.Pull(); pull != gpio.PullUp {
t.Fatal(pull)
}
if pull := p.DefaultPull(); pull != gpio.PullUp {
t.Fatal(pull)
}
// gpio.PinOut
if err := p.Out(gpio.Low); err != nil {
t.Fatal(err)
}
if err := p.Halt(); err != nil {
t.Fatal(err)
if err := p.PWM(gpio.DutyHalf, physic.KiloHertz); err != nil {
t.Fatalf("unexpected failure: %v", err)
}
}

Expand Down Expand Up @@ -85,21 +101,29 @@ func TestLogPinIO(t *testing.T) {
if l.Real() != p {
t.Fatal("unexpected real pin")
}
if err := l.Out(gpio.High); err != nil {
t.Fatal(err)
}
// gpio.PinIn
if err := l.In(gpio.PullNoChange, gpio.NoEdge); err != nil {
t.Fatal(err)
}
if l.Read() != gpio.High {
t.Fatal("unexpected level")
if v := l.Read(); v != gpio.Low {
t.Fatalf("unexpected level %v", v)
}
if l.Pull() != gpio.PullNoChange {
t.Fatal("unexpected pull")
}
if l.WaitForEdge(0) {
t.Fatal("unexpected edge")
}
// gpio.PinOut
if err := l.Out(gpio.High); err != nil {
t.Fatal(err)
}
if v := l.Read(); v != gpio.High {
t.Fatalf("unexpected level %v", v)
}
if err := l.PWM(gpio.DutyHalf, physic.KiloHertz); err != nil {
t.Fatalf("unexpected failure: %v", err)
}
}

func TestAll(t *testing.T) {
Expand Down
2 changes: 2 additions & 0 deletions conn/spi/spitest/spitest.go
Original file line number Diff line number Diff line change
Expand Up @@ -256,6 +256,8 @@ func (p *Playback) LimitSpeed(f physic.Frequency) error {

// Connect implements spi.PortCloser.
func (p *Playback) Connect(f physic.Frequency, mode spi.Mode, bits int) (spi.Conn, error) {
p.Lock()
defer p.Unlock()
if p.Initialized {
return nil, conntest.Errorf("spitest: Connect cannot be called twice")
}
Expand Down
63 changes: 62 additions & 1 deletion conn/spi/spitest/spitest_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ package spitest

import (
"bytes"
"errors"
"io/ioutil"
"log"
"testing"
Expand All @@ -14,6 +15,7 @@ import (
"periph.io/x/periph/conn/conntest"
"periph.io/x/periph/conn/gpio"
"periph.io/x/periph/conn/gpio/gpiotest"
"periph.io/x/periph/conn/physic"
"periph.io/x/periph/conn/spi"
)

Expand All @@ -27,9 +29,24 @@ func TestRecordRaw(t *testing.T) {
if err != nil {
t.Fatal(err)
}
if _, err := r.Connect(0, spi.Mode0, 0); err == nil {
t.Fatal("Can't call Connect twice")
}
if err := c.TxPackets(nil); err == nil {
t.Fatal("not yet implemented")
}
if v := c.String(); v != "recordraw" {
t.Fatal(v)
}
if v := c.Duplex(); v != conn.Half {
t.Fatal(v)
}
if err := c.Tx([]byte{1}, nil); err != nil {
t.Fatal(err)
}
if !bytes.Equal(b.Bytes(), []byte{1}) {
t.Fatal(b.Bytes())
}
if err := r.Close(); err != nil {
t.Fatal(err)
}
Expand All @@ -47,6 +64,15 @@ func TestRecord_empty(t *testing.T) {
if err != nil {
t.Fatal(err)
}
if _, err := r.Connect(0, spi.Mode0, 0); err == nil {
t.Fatal("Can't call Connect twice")
}
if v := c.String(); v != "record" {
t.Fatal(v)
}
if v := c.Duplex(); v != conn.DuplexUnknown {
t.Fatal(v)
}
if c.Tx(nil, []byte{'a'}) == nil {
t.Fatal("Port is nil")
}
Expand Down Expand Up @@ -101,7 +127,12 @@ func TestRecord_Tx_empty(t *testing.T) {
}

func TestPlayback(t *testing.T) {
p := Playback{}
p := Playback{
CLKPin: &gpiotest.Pin{N: "CLK"},
MOSIPin: &gpiotest.Pin{N: "MOSI"},
MISOPin: &gpiotest.Pin{N: "MISO"},
CSPin: &gpiotest.Pin{N: "CS"},
}
if s := p.String(); s != "playback" {
t.Fatal(s)
}
Expand All @@ -112,9 +143,24 @@ func TestPlayback(t *testing.T) {
if err != nil {
t.Fatal(err)
}
if _, err := p.Connect(0, spi.Mode0, 0); err == nil {
t.Fatal("Can't call Connect twice")
}
if err := c.TxPackets(nil); err == nil {
t.Fatal("not yet implemented")
}
if n := c.(spi.Pins).CLK().Name(); n != "CLK" {
t.Fatal(n)
}
if n := c.(spi.Pins).MOSI().Name(); n != "MOSI" {
t.Fatal(n)
}
if n := c.(spi.Pins).MISO().Name(); n != "MISO" {
t.Fatal(n)
}
if n := c.(spi.Pins).CS().Name(); n != "CS" {
t.Fatal(n)
}
if err := p.Close(); err != nil {
t.Fatal(err)
}
Expand Down Expand Up @@ -228,6 +274,21 @@ func TestRecord_Playback(t *testing.T) {
}
}

type connectFail struct {
Playback
}

func (c *connectFail) Connect(f physic.Frequency, mode spi.Mode, bits int) (spi.Conn, error) {
return nil, errors.New("foo")
}

func TestRecord_Fail(t *testing.T) {
r := Record{Port: &connectFail{}}
if _, err := r.Connect(0, spi.Mode0, 0); err == nil || err.Error() != "foo" {
t.Fatal("should have failed")
}
}

func TestLog_Playback(t *testing.T) {
r := Log{
PortCloser: &Playback{
Expand Down

0 comments on commit d54e035

Please sign in to comment.