gobot.io/x/gobot@v1.16.0/drivers/i2c/adafruit1109_driver_test.go (about)

     1  package i2c
     2  
     3  import (
     4  	"errors"
     5  	"fmt"
     6  	"testing"
     7  
     8  	"gobot.io/x/gobot/gobottest"
     9  )
    10  
    11  func initTestAdafruit1109DriverWithStubbedAdaptor() (*Adafruit1109Driver, *i2cTestAdaptor) {
    12  	adaptor := newI2cTestAdaptor()
    13  	return NewAdafruit1109Driver(adaptor), adaptor
    14  }
    15  
    16  func TestAdafruit1109DriverStart(t *testing.T) {
    17  	ada, _ := initTestAdafruit1109DriverWithStubbedAdaptor()
    18  	gobottest.Assert(t, ada.Start(), nil)
    19  }
    20  
    21  func TestAdafruit1109DriverStartWriteErr(t *testing.T) {
    22  	d, adaptor := initTestAdafruit1109DriverWithStubbedAdaptor()
    23  	adaptor.i2cWriteImpl = func([]byte) (int, error) {
    24  		return 0, errors.New("write error")
    25  	}
    26  	gobottest.Assert(t, d.Start(), errors.New("write error"))
    27  }
    28  
    29  func TestAdafruit1109DriverStartReadErr(t *testing.T) {
    30  	d, adaptor := initTestAdafruit1109DriverWithStubbedAdaptor()
    31  	adaptor.i2cReadImpl = func([]byte) (int, error) {
    32  		return 0, errors.New("read error")
    33  	}
    34  	gobottest.Assert(t, d.Start(), errors.New("read error"))
    35  }
    36  
    37  func TestAdafruit1109Driver_parseId(t *testing.T) {
    38  	// arrange
    39  	ports := []string{"A", "B"}
    40  	for _, port := range ports {
    41  		for pin := uint8(0); pin <= 7; pin++ {
    42  			id := fmt.Sprintf("%s_%d", port, pin)
    43  			t.Run(id, func(t *testing.T) {
    44  				// act
    45  				got := adafruit1109ParseId(id)
    46  				// assert
    47  				gobottest.Assert(t, got, adafruit1109PortPin{port, pin})
    48  			})
    49  		}
    50  	}
    51  }