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

     1  package i2c
     2  
     3  import (
     4  	"bytes"
     5  	"errors"
     6  	"testing"
     7  
     8  	"gobot.io/x/gobot"
     9  	"gobot.io/x/gobot/gobottest"
    10  )
    11  
    12  var _ gobot.Driver = (*LIDARLiteDriver)(nil)
    13  
    14  // --------- HELPERS
    15  func initTestLIDARLiteDriver() (driver *LIDARLiteDriver) {
    16  	driver, _ = initTestLIDARLiteDriverWithStubbedAdaptor()
    17  	return
    18  }
    19  
    20  func initTestLIDARLiteDriverWithStubbedAdaptor() (*LIDARLiteDriver, *i2cTestAdaptor) {
    21  	adaptor := newI2cTestAdaptor()
    22  	return NewLIDARLiteDriver(adaptor), adaptor
    23  }
    24  
    25  // --------- TESTS
    26  
    27  func TestNewLIDARLiteDriver(t *testing.T) {
    28  	// Does it return a pointer to an instance of LIDARLiteDriver?
    29  	var bm interface{} = NewLIDARLiteDriver(newI2cTestAdaptor())
    30  	_, ok := bm.(*LIDARLiteDriver)
    31  	if !ok {
    32  		t.Errorf("NewLIDARLiteDriver() should have returned a *LIDARLiteDriver")
    33  	}
    34  
    35  	b := NewLIDARLiteDriver(newI2cTestAdaptor())
    36  	gobottest.Refute(t, b.Connection(), nil)
    37  }
    38  
    39  // Methods
    40  func TestLIDARLiteDriverStart(t *testing.T) {
    41  	hmc, _ := initTestLIDARLiteDriverWithStubbedAdaptor()
    42  
    43  	gobottest.Assert(t, hmc.Start(), nil)
    44  }
    45  
    46  func TestLIDARLiteStartConnectError(t *testing.T) {
    47  	d, adaptor := initTestLIDARLiteDriverWithStubbedAdaptor()
    48  	adaptor.Testi2cConnectErr(true)
    49  	gobottest.Assert(t, d.Start(), errors.New("Invalid i2c connection"))
    50  }
    51  
    52  func TestLIDARLiteDriverHalt(t *testing.T) {
    53  	hmc := initTestLIDARLiteDriver()
    54  
    55  	gobottest.Assert(t, hmc.Halt(), nil)
    56  }
    57  
    58  func TestLIDARLiteDriverDistance(t *testing.T) {
    59  	// when everything is happy
    60  	hmc, adaptor := initTestLIDARLiteDriverWithStubbedAdaptor()
    61  
    62  	gobottest.Assert(t, hmc.Start(), nil)
    63  
    64  	first := true
    65  	adaptor.i2cReadImpl = func(b []byte) (int, error) {
    66  		if first {
    67  			first = false
    68  			copy(b, []byte{99})
    69  			return 1, nil
    70  		}
    71  		copy(b, []byte{1})
    72  		return 1, nil
    73  	}
    74  
    75  	distance, err := hmc.Distance()
    76  
    77  	gobottest.Assert(t, err, nil)
    78  	gobottest.Assert(t, distance, int(25345))
    79  
    80  	// when insufficient bytes have been read
    81  	hmc, adaptor = initTestLIDARLiteDriverWithStubbedAdaptor()
    82  
    83  	gobottest.Assert(t, hmc.Start(), nil)
    84  
    85  	adaptor.i2cReadImpl = func([]byte) (int, error) {
    86  		return 0, nil
    87  	}
    88  
    89  	distance, err = hmc.Distance()
    90  	gobottest.Assert(t, distance, int(0))
    91  	gobottest.Assert(t, err, ErrNotEnoughBytes)
    92  
    93  	// when read error
    94  	hmc, adaptor = initTestLIDARLiteDriverWithStubbedAdaptor()
    95  
    96  	gobottest.Assert(t, hmc.Start(), nil)
    97  
    98  	adaptor.i2cReadImpl = func([]byte) (int, error) {
    99  		return 0, errors.New("read error")
   100  	}
   101  
   102  	distance, err = hmc.Distance()
   103  	gobottest.Assert(t, distance, int(0))
   104  	gobottest.Assert(t, err, errors.New("read error"))
   105  }
   106  
   107  func TestLIDARLiteDriverDistanceError1(t *testing.T) {
   108  	hmc, adaptor := initTestLIDARLiteDriverWithStubbedAdaptor()
   109  	gobottest.Assert(t, hmc.Start(), nil)
   110  
   111  	adaptor.i2cWriteImpl = func([]byte) (int, error) {
   112  		return 0, errors.New("write error")
   113  	}
   114  
   115  	distance, err := hmc.Distance()
   116  	gobottest.Assert(t, distance, int(0))
   117  	gobottest.Assert(t, err, errors.New("write error"))
   118  }
   119  
   120  func TestLIDARLiteDriverDistanceError2(t *testing.T) {
   121  	hmc, adaptor := initTestLIDARLiteDriverWithStubbedAdaptor()
   122  	gobottest.Assert(t, hmc.Start(), nil)
   123  
   124  	adaptor.i2cWriteImpl = func(b []byte) (int, error) {
   125  		if b[0] == 0x0f {
   126  			return 0, errors.New("write error")
   127  		}
   128  		return len(b), nil
   129  	}
   130  
   131  	distance, err := hmc.Distance()
   132  	gobottest.Assert(t, distance, int(0))
   133  	gobottest.Assert(t, err, errors.New("write error"))
   134  }
   135  
   136  func TestLIDARLiteDriverDistanceError3(t *testing.T) {
   137  	hmc, adaptor := initTestLIDARLiteDriverWithStubbedAdaptor()
   138  	gobottest.Assert(t, hmc.Start(), nil)
   139  
   140  	adaptor.i2cWriteImpl = func(b []byte) (int, error) {
   141  		if b[0] == 0x10 {
   142  			return 0, errors.New("write error")
   143  		}
   144  		return len(b), nil
   145  	}
   146  	adaptor.i2cReadImpl = func(b []byte) (int, error) {
   147  		buf := new(bytes.Buffer)
   148  		buf.Write([]byte{0x03})
   149  		copy(b, buf.Bytes())
   150  		return buf.Len(), nil
   151  	}
   152  
   153  	distance, err := hmc.Distance()
   154  	gobottest.Assert(t, distance, int(0))
   155  	gobottest.Assert(t, err, errors.New("write error"))
   156  }
   157  
   158  func TestLIDARLiteDriverSetName(t *testing.T) {
   159  	l := initTestLIDARLiteDriver()
   160  	l.SetName("TESTME")
   161  	gobottest.Assert(t, l.Name(), "TESTME")
   162  }
   163  
   164  func TestLIDARLiteDriverOptions(t *testing.T) {
   165  	l := NewLIDARLiteDriver(newI2cTestAdaptor(), WithBus(2))
   166  	gobottest.Assert(t, l.GetBusOrDefault(1), 2)
   167  }