gobot.io/x/gobot/v2@v2.1.0/drivers/i2c/hmc6352_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  // this ensures that the implementation is based on i2c.Driver, which implements the gobot.Driver
    13  // and tests all implementations, so no further tests needed here for gobot.Driver interface
    14  var _ gobot.Driver = (*HMC6352Driver)(nil)
    15  
    16  func initTestHMC6352DriverWithStubbedAdaptor() (*HMC6352Driver, *i2cTestAdaptor) {
    17  	a := newI2cTestAdaptor()
    18  	d := NewHMC6352Driver(a)
    19  	if err := d.Start(); err != nil {
    20  		panic(err)
    21  	}
    22  	return d, a
    23  }
    24  
    25  func TestNewHMC6352Driver(t *testing.T) {
    26  	var di interface{} = NewHMC6352Driver(newI2cTestAdaptor())
    27  	d, ok := di.(*HMC6352Driver)
    28  	if !ok {
    29  		t.Errorf("NewHMC6352Driver() should have returned a *HMC6352Driver")
    30  	}
    31  	gobottest.Refute(t, d.Driver, nil)
    32  	gobottest.Assert(t, strings.HasPrefix(d.Name(), "HMC6352"), true)
    33  	gobottest.Assert(t, d.defaultAddress, 0x21)
    34  }
    35  
    36  func TestHMC6352Options(t *testing.T) {
    37  	// This is a general test, that options are applied in constructor by using the common WithBus() option and
    38  	// least one of this driver. Further tests for options can also be done by call of "WithOption(val)(d)".
    39  	d := NewHMC6352Driver(newI2cTestAdaptor(), WithBus(2))
    40  	gobottest.Assert(t, d.GetBusOrDefault(1), 2)
    41  }
    42  
    43  func TestHMC6352Start(t *testing.T) {
    44  	d := NewHMC6352Driver(newI2cTestAdaptor())
    45  	gobottest.Assert(t, d.Start(), nil)
    46  }
    47  
    48  func TestHMC6352Halt(t *testing.T) {
    49  	d, _ := initTestHMC6352DriverWithStubbedAdaptor()
    50  	gobottest.Assert(t, d.Halt(), nil)
    51  }
    52  
    53  func TestHMC6352Heading(t *testing.T) {
    54  	// when len(data) is 2
    55  	d, a := initTestHMC6352DriverWithStubbedAdaptor()
    56  	a.i2cReadImpl = func(b []byte) (int, error) {
    57  		copy(b, []byte{99, 1})
    58  		return 2, nil
    59  	}
    60  
    61  	heading, _ := d.Heading()
    62  	gobottest.Assert(t, heading, uint16(2534))
    63  
    64  	// when len(data) is not 2
    65  	d, a = initTestHMC6352DriverWithStubbedAdaptor()
    66  	a.i2cReadImpl = func(b []byte) (int, error) {
    67  		copy(b, []byte{99})
    68  		return 1, nil
    69  	}
    70  
    71  	heading, err := d.Heading()
    72  	gobottest.Assert(t, heading, uint16(0))
    73  	gobottest.Assert(t, err, ErrNotEnoughBytes)
    74  
    75  	// when read error
    76  	d, a = initTestHMC6352DriverWithStubbedAdaptor()
    77  	a.i2cReadImpl = func([]byte) (int, error) {
    78  		return 0, errors.New("read error")
    79  	}
    80  
    81  	heading, err = d.Heading()
    82  	gobottest.Assert(t, heading, uint16(0))
    83  	gobottest.Assert(t, err, errors.New("read error"))
    84  
    85  	// when write error
    86  	d, a = initTestHMC6352DriverWithStubbedAdaptor()
    87  	a.i2cWriteImpl = func([]byte) (int, error) {
    88  		return 0, errors.New("write error")
    89  	}
    90  
    91  	heading, err = d.Heading()
    92  	gobottest.Assert(t, heading, uint16(0))
    93  	gobottest.Assert(t, err, errors.New("write error"))
    94  }