gobot.io/x/gobot/v2@v2.1.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/v2"
    11  	"gobot.io/x/gobot/v2/gobottest"
    12  )
    13  
    14  // this ensures that the implementation is based on i2c.Driver, which implements the gobot.Driver
    15  // and tests all implementations, so no further tests needed here for gobot.Driver interface
    16  var _ gobot.Driver = (*BH1750Driver)(nil)
    17  
    18  func initTestBH1750DriverWithStubbedAdaptor() (*BH1750Driver, *i2cTestAdaptor) {
    19  	a := newI2cTestAdaptor()
    20  	d := NewBH1750Driver(a)
    21  	if err := d.Start(); err != nil {
    22  		panic(err)
    23  	}
    24  	return d, a
    25  }
    26  
    27  func TestNewBH1750Driver(t *testing.T) {
    28  	var di interface{} = NewBH1750Driver(newI2cTestAdaptor())
    29  	d, ok := di.(*BH1750Driver)
    30  	if !ok {
    31  		t.Errorf("NewBH1750Driver() should have returned a *BH1750Driver")
    32  	}
    33  	gobottest.Refute(t, d.Driver, nil)
    34  	gobottest.Assert(t, strings.HasPrefix(d.Name(), "BH1750"), true)
    35  	gobottest.Assert(t, d.defaultAddress, 0x23)
    36  }
    37  
    38  func TestBH1750Options(t *testing.T) {
    39  	// This is a general test, that options are applied in constructor by using the common WithBus() option and
    40  	// least one of this driver. Further tests for options can also be done by call of "WithOption(val)(d)".
    41  	d := NewBH1750Driver(newI2cTestAdaptor(), WithBus(2))
    42  	gobottest.Assert(t, d.GetBusOrDefault(1), 2)
    43  }
    44  
    45  func TestBH1750Start(t *testing.T) {
    46  	d := NewBH1750Driver(newI2cTestAdaptor())
    47  	gobottest.Assert(t, d.Start(), nil)
    48  }
    49  
    50  func TestBH1750Halt(t *testing.T) {
    51  	d, _ := initTestBH1750DriverWithStubbedAdaptor()
    52  	gobottest.Assert(t, d.Halt(), nil)
    53  }
    54  
    55  func TestBH1750NullLux(t *testing.T) {
    56  	d, _ := initTestBH1750DriverWithStubbedAdaptor()
    57  	lux, _ := d.Lux()
    58  	gobottest.Assert(t, lux, 0)
    59  }
    60  
    61  func TestBH1750Lux(t *testing.T) {
    62  	d, a := initTestBH1750DriverWithStubbedAdaptor()
    63  	a.i2cReadImpl = func(b []byte) (int, error) {
    64  		buf := new(bytes.Buffer)
    65  		buf.Write([]byte{0x05, 0xb0})
    66  		copy(b, buf.Bytes())
    67  		return buf.Len(), nil
    68  	}
    69  
    70  	lux, _ := d.Lux()
    71  	gobottest.Assert(t, lux, 1213)
    72  }
    73  
    74  func TestBH1750NullRawSensorData(t *testing.T) {
    75  	d, _ := initTestBH1750DriverWithStubbedAdaptor()
    76  	level, _ := d.RawSensorData()
    77  	gobottest.Assert(t, level, 0)
    78  }
    79  
    80  func TestBH1750RawSensorData(t *testing.T) {
    81  	d, a := initTestBH1750DriverWithStubbedAdaptor()
    82  	a.i2cReadImpl = func(b []byte) (int, error) {
    83  		buf := new(bytes.Buffer)
    84  		buf.Write([]byte{0x05, 0xb0})
    85  		copy(b, buf.Bytes())
    86  		return buf.Len(), nil
    87  	}
    88  
    89  	level, _ := d.RawSensorData()
    90  	gobottest.Assert(t, level, 1456)
    91  }
    92  
    93  func TestBH1750LuxError(t *testing.T) {
    94  	d, a := initTestBH1750DriverWithStubbedAdaptor()
    95  	a.i2cReadImpl = func(b []byte) (int, error) {
    96  		return 0, errors.New("wrong number of bytes read")
    97  	}
    98  
    99  	_, err := d.Lux()
   100  	gobottest.Assert(t, err, errors.New("wrong number of bytes read"))
   101  }
   102  
   103  func TestBH1750RawSensorDataError(t *testing.T) {
   104  	d, a := initTestBH1750DriverWithStubbedAdaptor()
   105  	a.i2cReadImpl = func(b []byte) (int, error) {
   106  		return 0, errors.New("wrong number of bytes read")
   107  	}
   108  
   109  	_, err := d.RawSensorData()
   110  	gobottest.Assert(t, err, errors.New("wrong number of bytes read"))
   111  }