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

     1  package i2c
     2  
     3  import (
     4  	"errors"
     5  	"testing"
     6  
     7  	"gobot.io/x/gobot"
     8  	"gobot.io/x/gobot/gobottest"
     9  )
    10  
    11  var _ gobot.Driver = (*HMC8553LDriver)(nil)
    12  
    13  // --------- HELPERS
    14  func initTestHMC8553LDriver() (driver *HMC8553LDriver) {
    15  	driver, _ = initTestHMC8553LDriverWithStubbedAdaptor()
    16  	return
    17  }
    18  
    19  func initTestHMC8553LDriverWithStubbedAdaptor() (*HMC8553LDriver, *i2cTestAdaptor) {
    20  	adaptor := newI2cTestAdaptor()
    21  	return NewHMC8553LDriver(adaptor), adaptor
    22  }
    23  
    24  // --------- TESTS
    25  
    26  func TestNewHMC8553LDriver(t *testing.T) {
    27  	// Does it return a pointer to an instance of HMC8553LDriver?
    28  	var bm interface{} = NewHMC8553LDriver(newI2cTestAdaptor())
    29  	_, ok := bm.(*HMC8553LDriver)
    30  	if !ok {
    31  		t.Errorf("NewHMC8553LDriver() should have returned a *HMC8553LDriver")
    32  	}
    33  
    34  	b := NewHMC8553LDriver(newI2cTestAdaptor())
    35  	gobottest.Refute(t, b.Connection(), nil)
    36  }
    37  
    38  // Methods
    39  func TestHMC8553LDriverStart(t *testing.T) {
    40  	hmc, adaptor := initTestHMC8553LDriverWithStubbedAdaptor()
    41  
    42  	gobottest.Assert(t, hmc.Start(), nil)
    43  
    44  	adaptor.i2cWriteImpl = func([]byte) (int, error) {
    45  		return 0, errors.New("write error")
    46  	}
    47  	err := hmc.Start()
    48  	gobottest.Assert(t, err, errors.New("write error"))
    49  }
    50  
    51  func Test8553LStartConnectError(t *testing.T) {
    52  	d, adaptor := initTestHMC8553LDriverWithStubbedAdaptor()
    53  	adaptor.Testi2cConnectErr(true)
    54  	gobottest.Assert(t, d.Start(), errors.New("Invalid i2c connection"))
    55  }
    56  
    57  func TestHMC8553LDriverHalt(t *testing.T) {
    58  	hmc := initTestHMC8553LDriver()
    59  
    60  	gobottest.Assert(t, hmc.Halt(), nil)
    61  }
    62  
    63  func TestHMC8553LDriverSetName(t *testing.T) {
    64  	d := initTestHMC8553LDriver()
    65  	d.SetName("TESTME")
    66  	gobottest.Assert(t, d.Name(), "TESTME")
    67  }
    68  
    69  func TestHMC8553LDriverOptions(t *testing.T) {
    70  	d := NewHMC8553LDriver(newI2cTestAdaptor(), WithBus(2))
    71  	gobottest.Assert(t, d.GetBusOrDefault(1), 2)
    72  }