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

     1  package i2c
     2  
     3  import (
     4  	"bytes"
     5  	"errors"
     6  	"strings"
     7  	"testing"
     8  
     9  	"gobot.io/x/gobot"
    10  	"gobot.io/x/gobot/gobottest"
    11  )
    12  
    13  var _ gobot.Driver = (*MMA7660Driver)(nil)
    14  
    15  // --------- HELPERS
    16  func initTestMMA7660Driver() (driver *MMA7660Driver) {
    17  	driver, _ = initTestMMA7660DriverWithStubbedAdaptor()
    18  	return
    19  }
    20  
    21  func initTestMMA7660DriverWithStubbedAdaptor() (*MMA7660Driver, *i2cTestAdaptor) {
    22  	adaptor := newI2cTestAdaptor()
    23  	return NewMMA7660Driver(adaptor), adaptor
    24  }
    25  
    26  // --------- TESTS
    27  
    28  func TestNewMMA7660Driver(t *testing.T) {
    29  	// Does it return a pointer to an instance of MMA7660Driver?
    30  	var mma interface{} = NewMMA7660Driver(newI2cTestAdaptor())
    31  	_, ok := mma.(*MMA7660Driver)
    32  	if !ok {
    33  		t.Errorf("NewMMA7660Driver() should have returned a *MMA7660Driver")
    34  	}
    35  }
    36  
    37  // Methods
    38  func TestMMA7660Driver(t *testing.T) {
    39  	mma := initTestMMA7660Driver()
    40  
    41  	gobottest.Refute(t, mma.Connection(), nil)
    42  	gobottest.Assert(t, strings.HasPrefix(mma.Name(), "MMA7660"), true)
    43  }
    44  
    45  func TestMMA7660DriverSetName(t *testing.T) {
    46  	d := initTestMMA7660Driver()
    47  	d.SetName("TESTME")
    48  	gobottest.Assert(t, d.Name(), "TESTME")
    49  }
    50  
    51  func TestMMA7660DriverOptions(t *testing.T) {
    52  	d := NewMMA7660Driver(newI2cTestAdaptor(), WithBus(2))
    53  	gobottest.Assert(t, d.GetBusOrDefault(1), 2)
    54  }
    55  
    56  func TestMMA7660DriverStart(t *testing.T) {
    57  	d := initTestMMA7660Driver()
    58  	gobottest.Assert(t, d.Start(), nil)
    59  }
    60  
    61  func TestMMA7660StartConnectError(t *testing.T) {
    62  	d, adaptor := initTestMMA7660DriverWithStubbedAdaptor()
    63  	adaptor.Testi2cConnectErr(true)
    64  	gobottest.Assert(t, d.Start(), errors.New("Invalid i2c connection"))
    65  }
    66  
    67  func TestMMA7660DriverStartWriteError(t *testing.T) {
    68  	mma, adaptor := initTestMMA7660DriverWithStubbedAdaptor()
    69  	adaptor.i2cWriteImpl = func([]byte) (int, error) {
    70  		return 0, errors.New("write error")
    71  	}
    72  	gobottest.Assert(t, mma.Start(), errors.New("write error"))
    73  }
    74  
    75  func TestMMA7660DriverHalt(t *testing.T) {
    76  	d := initTestMMA7660Driver()
    77  	gobottest.Assert(t, d.Halt(), nil)
    78  }
    79  
    80  func TestMMA7660DriverAcceleration(t *testing.T) {
    81  	d := initTestMMA7660Driver()
    82  	x, y, z := d.Acceleration(21.0, 21.0, 21.0)
    83  	gobottest.Assert(t, x, 1.0)
    84  	gobottest.Assert(t, y, 1.0)
    85  	gobottest.Assert(t, z, 1.0)
    86  }
    87  
    88  func TestMMA7660DriverNullXYZ(t *testing.T) {
    89  	d, _ := initTestMMA7660DriverWithStubbedAdaptor()
    90  	d.Start()
    91  	x, y, z, _ := d.XYZ()
    92  	gobottest.Assert(t, x, 0.0)
    93  	gobottest.Assert(t, y, 0.0)
    94  	gobottest.Assert(t, z, 0.0)
    95  }
    96  
    97  func TestMMA7660DriverXYZ(t *testing.T) {
    98  	d, adaptor := initTestMMA7660DriverWithStubbedAdaptor()
    99  	d.Start()
   100  
   101  	adaptor.i2cReadImpl = func(b []byte) (int, error) {
   102  		buf := new(bytes.Buffer)
   103  		buf.Write([]byte{0x11, 0x12, 0x13})
   104  		copy(b, buf.Bytes())
   105  		return buf.Len(), nil
   106  	}
   107  
   108  	x, y, z, _ := d.XYZ()
   109  	gobottest.Assert(t, x, 17.0)
   110  	gobottest.Assert(t, y, 18.0)
   111  	gobottest.Assert(t, z, 19.0)
   112  }
   113  
   114  func TestMMA7660DriverXYZError(t *testing.T) {
   115  	d, adaptor := initTestMMA7660DriverWithStubbedAdaptor()
   116  	d.Start()
   117  
   118  	adaptor.i2cReadImpl = func(b []byte) (int, error) {
   119  		return 0, errors.New("read error")
   120  	}
   121  
   122  	_, _, _, err := d.XYZ()
   123  	gobottest.Assert(t, err, errors.New("read error"))
   124  }
   125  
   126  func TestMMA7660DriverXYZNotReady(t *testing.T) {
   127  	d, adaptor := initTestMMA7660DriverWithStubbedAdaptor()
   128  	d.Start()
   129  
   130  	adaptor.i2cReadImpl = func(b []byte) (int, error) {
   131  		buf := new(bytes.Buffer)
   132  		buf.Write([]byte{0x40, 0x40, 0x40})
   133  		copy(b, buf.Bytes())
   134  		return buf.Len(), nil
   135  	}
   136  
   137  	_, _, _, err := d.XYZ()
   138  	gobottest.Assert(t, err, ErrNotReady)
   139  }