gobot.io/x/gobot/v2@v2.1.0/drivers/i2c/i2c_driver_test.go (about)

     1  package i2c
     2  
     3  import (
     4  	"errors"
     5  	"strings"
     6  	"testing"
     7  
     8  	"gobot.io/x/gobot/v2"
     9  	"gobot.io/x/gobot/v2/gobottest"
    10  )
    11  
    12  var _ gobot.Driver = (*Driver)(nil)
    13  
    14  func initDriverWithStubbedAdaptor() (*Driver, *i2cTestAdaptor) {
    15  	a := newI2cTestAdaptor()
    16  	d := NewDriver(a, "I2C_BASIC", 0x15)
    17  	return d, a
    18  }
    19  
    20  func initTestDriver() *Driver {
    21  	d, _ := initDriverWithStubbedAdaptor()
    22  	return d
    23  }
    24  
    25  func TestNewDriver(t *testing.T) {
    26  	// arrange
    27  	a := newI2cTestAdaptor()
    28  	// act
    29  	var di interface{} = NewDriver(a, "I2C_BASIC", 0x15)
    30  	// assert
    31  	d, ok := di.(*Driver)
    32  	if !ok {
    33  		t.Errorf("NewDriver() should have returned a *Driver")
    34  	}
    35  	gobottest.Assert(t, strings.Contains(d.name, "I2C_BASIC"), true)
    36  	gobottest.Assert(t, d.defaultAddress, 0x15)
    37  	gobottest.Assert(t, d.connector, a)
    38  	gobottest.Assert(t, d.connection, nil)
    39  	gobottest.Assert(t, d.afterStart(), nil)
    40  	gobottest.Assert(t, d.beforeHalt(), nil)
    41  	gobottest.Refute(t, d.Config, nil)
    42  	gobottest.Refute(t, d.Commander, nil)
    43  	gobottest.Refute(t, d.mutex, nil)
    44  }
    45  
    46  func TestSetName(t *testing.T) {
    47  	// arrange
    48  	d := initTestDriver()
    49  	// act
    50  	d.SetName("TESTME")
    51  	// assert
    52  	gobottest.Assert(t, d.Name(), "TESTME")
    53  }
    54  
    55  func TestConnection(t *testing.T) {
    56  	// arrange
    57  	d := initTestDriver()
    58  	// act, assert
    59  	gobottest.Refute(t, d.Connection(), nil)
    60  }
    61  
    62  func TestStart(t *testing.T) {
    63  	// arrange
    64  	d, a := initDriverWithStubbedAdaptor()
    65  	// act, assert
    66  	gobottest.Assert(t, d.Start(), nil)
    67  	gobottest.Assert(t, 0x15, a.address)
    68  }
    69  
    70  func TestStartConnectError(t *testing.T) {
    71  	// arrange
    72  	d, a := initDriverWithStubbedAdaptor()
    73  	a.Testi2cConnectErr(true)
    74  	// act, assert
    75  	gobottest.Assert(t, d.Start(), errors.New("Invalid i2c connection"))
    76  }
    77  
    78  func TestHalt(t *testing.T) {
    79  	// arrange
    80  	d := initTestDriver()
    81  	// act, assert
    82  	gobottest.Assert(t, d.Halt(), nil)
    83  }
    84  
    85  func TestWrite(t *testing.T) {
    86  	// arrange
    87  	const (
    88  		address     = "82"
    89  		wantAddress = uint8(0x52)
    90  		value       = 0x25
    91  	)
    92  	d, a := initDriverWithStubbedAdaptor()
    93  	d.Start()
    94  	// prepare all writes
    95  	numCallsWrite := 0
    96  	a.i2cWriteImpl = func([]byte) (int, error) {
    97  		numCallsWrite++
    98  		return 0, nil
    99  	}
   100  	// act
   101  	err := d.Write(address, value)
   102  	// assert
   103  	gobottest.Assert(t, err, nil)
   104  	gobottest.Assert(t, numCallsWrite, 1)
   105  	gobottest.Assert(t, a.written[0], wantAddress)
   106  	gobottest.Assert(t, a.written[1], uint8(value))
   107  }
   108  
   109  func TestRead(t *testing.T) {
   110  	// arrange
   111  	const (
   112  		address     = "83"
   113  		wantAddress = uint8(0x53)
   114  		want        = uint8(0x44)
   115  	)
   116  	d, a := initDriverWithStubbedAdaptor()
   117  	d.Start()
   118  	// prepare all writes
   119  	numCallsWrite := 0
   120  	a.i2cWriteImpl = func(b []byte) (int, error) {
   121  		numCallsWrite++
   122  		return 0, nil
   123  	}
   124  	// prepare all reads
   125  	numCallsRead := 0
   126  	a.i2cReadImpl = func(b []byte) (int, error) {
   127  		numCallsRead++
   128  		b[0] = want
   129  		return len(b), nil
   130  	}
   131  	// act
   132  	val, err := d.Read(address)
   133  	// assert
   134  	gobottest.Assert(t, err, nil)
   135  	gobottest.Assert(t, val, int(want))
   136  	gobottest.Assert(t, numCallsWrite, 1)
   137  	gobottest.Assert(t, a.written[0], wantAddress)
   138  	gobottest.Assert(t, numCallsRead, 1)
   139  }