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

     1  package i2c
     2  
     3  import (
     4  	"bytes"
     5  	"errors"
     6  	"strings"
     7  	"testing"
     8  
     9  	"gobot.io/x/gobot/v2"
    10  	"gobot.io/x/gobot/v2/gobottest"
    11  )
    12  
    13  // this ensures that the implementation is based on i2c.Driver, which implements the gobot.Driver
    14  // and tests all implementations, so no further tests needed here for gobot.Driver interface
    15  var _ gobot.Driver = (*LIDARLiteDriver)(nil)
    16  
    17  func initTestLIDARLiteDriver() (driver *LIDARLiteDriver) {
    18  	driver, _ = initTestLIDARLiteDriverWithStubbedAdaptor()
    19  	return
    20  }
    21  
    22  func initTestLIDARLiteDriverWithStubbedAdaptor() (*LIDARLiteDriver, *i2cTestAdaptor) {
    23  	a := newI2cTestAdaptor()
    24  	d := NewLIDARLiteDriver(a)
    25  	if err := d.Start(); err != nil {
    26  		panic(err)
    27  	}
    28  	return d, a
    29  }
    30  
    31  func TestNewLIDARLiteDriver(t *testing.T) {
    32  	var di interface{} = NewLIDARLiteDriver(newI2cTestAdaptor())
    33  	d, ok := di.(*LIDARLiteDriver)
    34  	if !ok {
    35  		t.Errorf("NewLIDARLiteDriver() should have returned a *LIDARLiteDriver")
    36  	}
    37  	gobottest.Refute(t, d.Driver, nil)
    38  	gobottest.Assert(t, strings.HasPrefix(d.Name(), "LIDARLite"), true)
    39  	gobottest.Assert(t, d.defaultAddress, 0x62)
    40  }
    41  
    42  func TestLIDARLiteDriverOptions(t *testing.T) {
    43  	// This is a general test, that options are applied in constructor by using the common WithBus() option and
    44  	// least one of this driver. Further tests for options can also be done by call of "WithOption(val)(d)".
    45  	d := NewLIDARLiteDriver(newI2cTestAdaptor(), WithBus(2))
    46  	gobottest.Assert(t, d.GetBusOrDefault(1), 2)
    47  }
    48  
    49  func TestLIDARLiteDriverStart(t *testing.T) {
    50  	d := NewLIDARLiteDriver(newI2cTestAdaptor())
    51  	gobottest.Assert(t, d.Start(), nil)
    52  }
    53  
    54  func TestLIDARLiteDriverHalt(t *testing.T) {
    55  	d := initTestLIDARLiteDriver()
    56  	gobottest.Assert(t, d.Halt(), nil)
    57  }
    58  
    59  func TestLIDARLiteDriverDistance(t *testing.T) {
    60  	// when everything is happy
    61  	d, a := initTestLIDARLiteDriverWithStubbedAdaptor()
    62  	first := true
    63  	a.i2cReadImpl = func(b []byte) (int, error) {
    64  		if first {
    65  			first = false
    66  			copy(b, []byte{99})
    67  			return 1, nil
    68  		}
    69  		copy(b, []byte{1})
    70  		return 1, nil
    71  	}
    72  
    73  	distance, err := d.Distance()
    74  
    75  	gobottest.Assert(t, err, nil)
    76  	gobottest.Assert(t, distance, int(25345))
    77  
    78  	// when insufficient bytes have been read
    79  	d, a = initTestLIDARLiteDriverWithStubbedAdaptor()
    80  	a.i2cReadImpl = func([]byte) (int, error) {
    81  		return 0, nil
    82  	}
    83  
    84  	distance, err = d.Distance()
    85  	gobottest.Assert(t, distance, int(0))
    86  	gobottest.Assert(t, err, ErrNotEnoughBytes)
    87  
    88  	// when read error
    89  	d, a = initTestLIDARLiteDriverWithStubbedAdaptor()
    90  	a.i2cReadImpl = func([]byte) (int, error) {
    91  		return 0, errors.New("read error")
    92  	}
    93  
    94  	distance, err = d.Distance()
    95  	gobottest.Assert(t, distance, int(0))
    96  	gobottest.Assert(t, err, errors.New("read error"))
    97  }
    98  
    99  func TestLIDARLiteDriverDistanceError1(t *testing.T) {
   100  	d, a := initTestLIDARLiteDriverWithStubbedAdaptor()
   101  	a.i2cWriteImpl = func([]byte) (int, error) {
   102  		return 0, errors.New("write error")
   103  	}
   104  
   105  	distance, err := d.Distance()
   106  	gobottest.Assert(t, distance, int(0))
   107  	gobottest.Assert(t, err, errors.New("write error"))
   108  }
   109  
   110  func TestLIDARLiteDriverDistanceError2(t *testing.T) {
   111  	d, a := initTestLIDARLiteDriverWithStubbedAdaptor()
   112  	a.i2cWriteImpl = func(b []byte) (int, error) {
   113  		if b[0] == 0x0f {
   114  			return 0, errors.New("write error")
   115  		}
   116  		return len(b), nil
   117  	}
   118  
   119  	distance, err := d.Distance()
   120  	gobottest.Assert(t, distance, int(0))
   121  	gobottest.Assert(t, err, errors.New("write error"))
   122  }
   123  
   124  func TestLIDARLiteDriverDistanceError3(t *testing.T) {
   125  	d, a := initTestLIDARLiteDriverWithStubbedAdaptor()
   126  	a.i2cWriteImpl = func(b []byte) (int, error) {
   127  		if b[0] == 0x10 {
   128  			return 0, errors.New("write error")
   129  		}
   130  		return len(b), nil
   131  	}
   132  	a.i2cReadImpl = func(b []byte) (int, error) {
   133  		buf := new(bytes.Buffer)
   134  		buf.Write([]byte{0x03})
   135  		copy(b, buf.Bytes())
   136  		return buf.Len(), nil
   137  	}
   138  
   139  	distance, err := d.Distance()
   140  	gobottest.Assert(t, distance, int(0))
   141  	gobottest.Assert(t, err, errors.New("write error"))
   142  }