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

     1  package i2c
     2  
     3  import (
     4  	"errors"
     5  	"strings"
     6  	"testing"
     7  
     8  	"bytes"
     9  
    10  	"gobot.io/x/gobot"
    11  	"gobot.io/x/gobot/gobottest"
    12  )
    13  
    14  var _ gobot.Driver = (*BH1750Driver)(nil)
    15  
    16  // --------- HELPERS
    17  func initTestBH1750Driver() (driver *BH1750Driver) {
    18  	driver, _ = initTestBH1750DriverWithStubbedAdaptor()
    19  	return
    20  }
    21  
    22  func initTestBH1750DriverWithStubbedAdaptor() (*BH1750Driver, *i2cTestAdaptor) {
    23  	adaptor := newI2cTestAdaptor()
    24  	return NewBH1750Driver(adaptor), adaptor
    25  }
    26  
    27  // --------- TESTS
    28  
    29  func TestNewBH1750Driver(t *testing.T) {
    30  	// Does it return a pointer to an instance of BH1750Driver?
    31  	var mma interface{} = NewBH1750Driver(newI2cTestAdaptor())
    32  	_, ok := mma.(*BH1750Driver)
    33  	if !ok {
    34  		t.Errorf("NewBH1750Driver() should have returned a *BH1750Driver")
    35  	}
    36  }
    37  
    38  // Methods
    39  func TestBH1750Driver(t *testing.T) {
    40  	mma := initTestBH1750Driver()
    41  
    42  	gobottest.Refute(t, mma.Connection(), nil)
    43  	gobottest.Assert(t, strings.HasPrefix(mma.Name(), "BH1750"), true)
    44  }
    45  
    46  func TestBH1750DriverSetName(t *testing.T) {
    47  	d := initTestBH1750Driver()
    48  	d.SetName("TESTME")
    49  	gobottest.Assert(t, d.Name(), "TESTME")
    50  }
    51  
    52  func TestBH1750DriverOptions(t *testing.T) {
    53  	d := NewBH1750Driver(newI2cTestAdaptor(), WithBus(2))
    54  	gobottest.Assert(t, d.GetBusOrDefault(1), 2)
    55  }
    56  
    57  func TestBH1750DriverStart(t *testing.T) {
    58  	d := initTestBH1750Driver()
    59  	gobottest.Assert(t, d.Start(), nil)
    60  }
    61  
    62  func TestBH1750StartConnectError(t *testing.T) {
    63  	d, adaptor := initTestBH1750DriverWithStubbedAdaptor()
    64  	adaptor.Testi2cConnectErr(true)
    65  	gobottest.Assert(t, d.Start(), errors.New("Invalid i2c connection"))
    66  }
    67  
    68  func TestBH1750DriverStartWriteError(t *testing.T) {
    69  	mma, adaptor := initTestBH1750DriverWithStubbedAdaptor()
    70  	adaptor.i2cWriteImpl = func([]byte) (int, error) {
    71  		return 0, errors.New("write error")
    72  	}
    73  	gobottest.Assert(t, mma.Start(), errors.New("write error"))
    74  }
    75  
    76  func TestBH1750DriverHalt(t *testing.T) {
    77  	d := initTestBH1750Driver()
    78  	gobottest.Assert(t, d.Halt(), nil)
    79  }
    80  
    81  func TestBH1750DriverNullLux(t *testing.T) {
    82  	d, _ := initTestBH1750DriverWithStubbedAdaptor()
    83  	d.Start()
    84  	lux, _ := d.Lux()
    85  	gobottest.Assert(t, lux, 0)
    86  }
    87  
    88  func TestBH1750DriverLux(t *testing.T) {
    89  	d, adaptor := initTestBH1750DriverWithStubbedAdaptor()
    90  	d.Start()
    91  
    92  	adaptor.i2cReadImpl = func(b []byte) (int, error) {
    93  		buf := new(bytes.Buffer)
    94  		buf.Write([]byte{0x05, 0xb0})
    95  		copy(b, buf.Bytes())
    96  		return buf.Len(), nil
    97  	}
    98  
    99  	lux, _ := d.Lux()
   100  	gobottest.Assert(t, lux, 1213)
   101  }
   102  
   103  func TestBH1750DriverNullRawSensorData(t *testing.T) {
   104  	d, _ := initTestBH1750DriverWithStubbedAdaptor()
   105  	d.Start()
   106  	level, _ := d.RawSensorData()
   107  	gobottest.Assert(t, level, 0)
   108  }
   109  
   110  func TestBH1750DriverRawSensorData(t *testing.T) {
   111  	d, adaptor := initTestBH1750DriverWithStubbedAdaptor()
   112  	d.Start()
   113  
   114  	adaptor.i2cReadImpl = func(b []byte) (int, error) {
   115  		buf := new(bytes.Buffer)
   116  		buf.Write([]byte{0x05, 0xb0})
   117  		copy(b, buf.Bytes())
   118  		return buf.Len(), nil
   119  	}
   120  
   121  	level, _ := d.RawSensorData()
   122  	gobottest.Assert(t, level, 1456)
   123  }
   124  
   125  func TestBH1750DriverLuxError(t *testing.T) {
   126  	d, adaptor := initTestBH1750DriverWithStubbedAdaptor()
   127  	d.Start()
   128  
   129  	adaptor.i2cReadImpl = func(b []byte) (int, error) {
   130  		return 0, errors.New("wrong number of bytes read")
   131  	}
   132  
   133  	_, err := d.Lux()
   134  	gobottest.Assert(t, err, errors.New("wrong number of bytes read"))
   135  }
   136  
   137  func TestBH1750DriverRawSensorDataError(t *testing.T) {
   138  	d, adaptor := initTestBH1750DriverWithStubbedAdaptor()
   139  	d.Start()
   140  
   141  	adaptor.i2cReadImpl = func(b []byte) (int, error) {
   142  		return 0, errors.New("wrong number of bytes read")
   143  	}
   144  
   145  	_, err := d.RawSensorData()
   146  	gobottest.Assert(t, err, errors.New("wrong number of bytes read"))
   147  }
   148