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

     1  package i2c
     2  
     3  import (
     4  	"bytes"
     5  	"encoding/binary"
     6  	"errors"
     7  	"testing"
     8  
     9  	"gobot.io/x/gobot"
    10  	"gobot.io/x/gobot/gobottest"
    11  )
    12  
    13  var _ gobot.Driver = (*HMC6352Driver)(nil)
    14  
    15  // --------- HELPERS
    16  func initTestL3GD20HDriver() (driver *L3GD20HDriver) {
    17  	driver, _ = initTestL3GD20HDriverWithStubbedAdaptor()
    18  	return
    19  }
    20  
    21  func initTestL3GD20HDriverWithStubbedAdaptor() (*L3GD20HDriver, *i2cTestAdaptor) {
    22  	adaptor := newI2cTestAdaptor()
    23  	return NewL3GD20HDriver(adaptor), adaptor
    24  }
    25  
    26  // --------- TESTS
    27  
    28  func TestNewL3GD20HDriver(t *testing.T) {
    29  	// Does it return a pointer to an instance of HMC6352Driver?
    30  	var d interface{} = NewL3GD20HDriver(newI2cTestAdaptor())
    31  	_, ok := d.(*L3GD20HDriver)
    32  	if !ok {
    33  		t.Errorf("NewL3GD20HDriver() should have returned a *L3GD20HDriver")
    34  	}
    35  }
    36  
    37  func TestL3GD20HDriver(t *testing.T) {
    38  	d := initTestL3GD20HDriver()
    39  	gobottest.Refute(t, d.Connection(), nil)
    40  }
    41  
    42  // Methods
    43  func TestL3GD20HDriverStart(t *testing.T) {
    44  	d, _ := initTestL3GD20HDriverWithStubbedAdaptor()
    45  
    46  	gobottest.Assert(t, d.Start(), nil)
    47  }
    48  
    49  func TestL3GD20HStartConnectError(t *testing.T) {
    50  	d, adaptor := initTestL3GD20HDriverWithStubbedAdaptor()
    51  	adaptor.Testi2cConnectErr(true)
    52  	gobottest.Assert(t, d.Start(), errors.New("Invalid i2c connection"))
    53  }
    54  
    55  func TestL3GD20HDriverStartWriteError(t *testing.T) {
    56  	d, adaptor := initTestL3GD20HDriverWithStubbedAdaptor()
    57  	adaptor.i2cWriteImpl = func([]byte) (int, error) {
    58  		return 0, errors.New("write error")
    59  	}
    60  	gobottest.Assert(t, d.Start(), errors.New("write error"))
    61  }
    62  
    63  func TestL3GD20HDriverHalt(t *testing.T) {
    64  	d := initTestL3GD20HDriver()
    65  
    66  	gobottest.Assert(t, d.Halt(), nil)
    67  }
    68  
    69  func TestL3GD20HDriverScale(t *testing.T) {
    70  	d := initTestL3GD20HDriver()
    71  	gobottest.Assert(t, d.Scale(), L3GD20HScale250dps)
    72  	gobottest.Assert(t, d.getSensitivity(), float32(0.00875))
    73  
    74  	d.SetScale(L3GD20HScale500dps)
    75  	gobottest.Assert(t, d.Scale(), L3GD20HScale500dps)
    76  	gobottest.Assert(t, d.getSensitivity(), float32(0.0175))
    77  
    78  	d.SetScale(L3GD20HScale2000dps)
    79  	gobottest.Assert(t, d.Scale(), L3GD20HScale2000dps)
    80  	gobottest.Assert(t, d.getSensitivity(), float32(0.07))
    81  }
    82  
    83  func TestL3GD20HDriverMeasurement(t *testing.T) {
    84  	d, adaptor := initTestL3GD20HDriverWithStubbedAdaptor()
    85  	rawX := 5
    86  	rawY := 8
    87  	rawZ := -3
    88  	adaptor.i2cReadImpl = func(b []byte) (int, error) {
    89  		buf := new(bytes.Buffer)
    90  		binary.Write(buf, binary.LittleEndian, int16(rawX))
    91  		binary.Write(buf, binary.LittleEndian, int16(rawY))
    92  		binary.Write(buf, binary.LittleEndian, int16(rawZ))
    93  		copy(b, buf.Bytes())
    94  		return buf.Len(), nil
    95  	}
    96  
    97  	d.Start()
    98  	x, y, z, err := d.XYZ()
    99  	gobottest.Assert(t, err, nil)
   100  	var sensitivity float32 = 0.00875
   101  	gobottest.Assert(t, x, float32(rawX)*sensitivity)
   102  	gobottest.Assert(t, y, float32(rawY)*sensitivity)
   103  	gobottest.Assert(t, z, float32(rawZ)*sensitivity)
   104  }
   105  
   106  func TestL3GD20HDriverMeasurementError(t *testing.T) {
   107  	d, adaptor := initTestL3GD20HDriverWithStubbedAdaptor()
   108  	adaptor.i2cReadImpl = func(b []byte) (int, error) {
   109  		return 0, errors.New("read error")
   110  	}
   111  
   112  	d.Start()
   113  	_, _, _, err := d.XYZ()
   114  	gobottest.Assert(t, err, errors.New("read error"))
   115  }
   116  
   117  func TestL3GD20HDriverMeasurementWriteError(t *testing.T) {
   118  	d, adaptor := initTestL3GD20HDriverWithStubbedAdaptor()
   119  	d.Start()
   120  	adaptor.i2cWriteImpl = func(b []byte) (int, error) {
   121  		return 0, errors.New("write error")
   122  	}
   123  
   124  	_, _, _, err := d.XYZ()
   125  	gobottest.Assert(t, err, errors.New("write error"))
   126  }
   127  
   128  func TestL3GD20HDriverSetName(t *testing.T) {
   129  	d := initTestL3GD20HDriver()
   130  	d.SetName("TESTME")
   131  	gobottest.Assert(t, d.Name(), "TESTME")
   132  }
   133  
   134  func TestL3GD20HDriverOptions(t *testing.T) {
   135  	d := NewL3GD20HDriver(newI2cTestAdaptor(), WithBus(2))
   136  	gobottest.Assert(t, d.GetBusOrDefault(1), 2)
   137  }