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

     1  package i2c
     2  
     3  import (
     4  	"errors"
     5  	"strings"
     6  	"testing"
     7  
     8  	"gobot.io/x/gobot/v2"
     9  	"gobot.io/x/gobot/v2/gobottest"
    10  )
    11  
    12  // this ensures that the implementation is based on i2c.Driver, which implements the gobot.Driver
    13  // and tests all implementations, so no further tests needed here for gobot.Driver interface
    14  var _ gobot.Driver = (*BlinkMDriver)(nil)
    15  
    16  func initTestBlinkMDriverWithStubbedAdaptor() (*BlinkMDriver, *i2cTestAdaptor) {
    17  	a := newI2cTestAdaptor()
    18  	d := NewBlinkMDriver(a)
    19  	if err := d.Start(); err != nil {
    20  		panic(err)
    21  	}
    22  	return d, a
    23  }
    24  
    25  func TestNewBlinkMDriver(t *testing.T) {
    26  	var di interface{} = NewBlinkMDriver(newI2cTestAdaptor())
    27  	d, ok := di.(*BlinkMDriver)
    28  	if !ok {
    29  		t.Errorf("NewBlinkMDriver() should have returned a *BlinkMDriver")
    30  	}
    31  	gobottest.Refute(t, d.Driver, nil)
    32  	gobottest.Assert(t, strings.HasPrefix(d.Name(), "BlinkM"), true)
    33  	gobottest.Assert(t, d.defaultAddress, 0x09)
    34  }
    35  
    36  func TestBlinkMOptions(t *testing.T) {
    37  	// This is a general test, that options are applied in constructor by using the common WithBus() option and
    38  	// least one of this driver. Further tests for options can also be done by call of "WithOption(val)(d)".
    39  	d := NewBlinkMDriver(newI2cTestAdaptor(), WithBus(2))
    40  	gobottest.Assert(t, d.GetBusOrDefault(1), 2)
    41  }
    42  
    43  func TestBlinkMStart(t *testing.T) {
    44  	d := NewBlinkMDriver(newI2cTestAdaptor())
    45  	gobottest.Assert(t, d.Start(), nil)
    46  }
    47  
    48  func TestBlinkMHalt(t *testing.T) {
    49  	d, _ := initTestBlinkMDriverWithStubbedAdaptor()
    50  	gobottest.Assert(t, d.Halt(), nil)
    51  }
    52  
    53  // Commands
    54  func TestNewBlinkMDriverCommands_Rgb(t *testing.T) {
    55  	d, _ := initTestBlinkMDriverWithStubbedAdaptor()
    56  
    57  	result := d.Command("Rgb")(rgb)
    58  	gobottest.Assert(t, result, nil)
    59  }
    60  
    61  func TestNewBlinkMDriverCommands_Fade(t *testing.T) {
    62  	d, _ := initTestBlinkMDriverWithStubbedAdaptor()
    63  
    64  	result := d.Command("Fade")(rgb)
    65  	gobottest.Assert(t, result, nil)
    66  }
    67  
    68  func TestNewBlinkMDriverCommands_FirmwareVersion(t *testing.T) {
    69  	d, a := initTestBlinkMDriverWithStubbedAdaptor()
    70  	param := make(map[string]interface{})
    71  	// When len(data) is 2
    72  	a.i2cReadImpl = func(b []byte) (int, error) {
    73  		copy(b, []byte{99, 1})
    74  		return 2, nil
    75  	}
    76  
    77  	result := d.Command("FirmwareVersion")(param)
    78  
    79  	version, _ := d.FirmwareVersion()
    80  	gobottest.Assert(t, result.(map[string]interface{})["version"].(string), version)
    81  
    82  	// When len(data) is not 2
    83  	a.i2cReadImpl = func(b []byte) (int, error) {
    84  		copy(b, []byte{99})
    85  		return 1, nil
    86  	}
    87  	result = d.Command("FirmwareVersion")(param)
    88  
    89  	version, _ = d.FirmwareVersion()
    90  	gobottest.Assert(t, result.(map[string]interface{})["version"].(string), version)
    91  }
    92  
    93  func TestNewBlinkMDriverCommands_Color(t *testing.T) {
    94  	d, _ := initTestBlinkMDriverWithStubbedAdaptor()
    95  	param := make(map[string]interface{})
    96  
    97  	result := d.Command("Color")(param)
    98  
    99  	color, _ := d.Color()
   100  	gobottest.Assert(t, result.(map[string]interface{})["color"].([]byte), color)
   101  }
   102  
   103  func TestBlinkMFirmwareVersion(t *testing.T) {
   104  	d, a := initTestBlinkMDriverWithStubbedAdaptor()
   105  	// when len(data) is 2
   106  	a.i2cReadImpl = func(b []byte) (int, error) {
   107  		copy(b, []byte{99, 1})
   108  		return 2, nil
   109  	}
   110  
   111  	version, _ := d.FirmwareVersion()
   112  	gobottest.Assert(t, version, "99.1")
   113  
   114  	// when len(data) is not 2
   115  	a.i2cReadImpl = func(b []byte) (int, error) {
   116  		copy(b, []byte{99})
   117  		return 1, nil
   118  	}
   119  
   120  	version, _ = d.FirmwareVersion()
   121  	gobottest.Assert(t, version, "")
   122  
   123  	a.i2cWriteImpl = func([]byte) (int, error) {
   124  		return 0, errors.New("write error")
   125  	}
   126  
   127  	_, err := d.FirmwareVersion()
   128  	gobottest.Assert(t, err, errors.New("write error"))
   129  }
   130  
   131  func TestBlinkMColor(t *testing.T) {
   132  	d, a := initTestBlinkMDriverWithStubbedAdaptor()
   133  	// when len(data) is 3
   134  	a.i2cReadImpl = func(b []byte) (int, error) {
   135  		copy(b, []byte{99, 1, 2})
   136  		return 3, nil
   137  	}
   138  
   139  	color, _ := d.Color()
   140  	gobottest.Assert(t, color, []byte{99, 1, 2})
   141  
   142  	// when len(data) is not 3
   143  	a.i2cReadImpl = func(b []byte) (int, error) {
   144  		copy(b, []byte{99})
   145  		return 1, nil
   146  	}
   147  
   148  	color, _ = d.Color()
   149  	gobottest.Assert(t, color, []byte{})
   150  
   151  	a.i2cWriteImpl = func([]byte) (int, error) {
   152  		return 0, errors.New("write error")
   153  	}
   154  
   155  	_, err := d.Color()
   156  	gobottest.Assert(t, err, errors.New("write error"))
   157  
   158  }
   159  
   160  func TestBlinkMFade(t *testing.T) {
   161  	d, a := initTestBlinkMDriverWithStubbedAdaptor()
   162  	a.i2cWriteImpl = func([]byte) (int, error) {
   163  		return 0, errors.New("write error")
   164  	}
   165  
   166  	err := d.Fade(100, 100, 100)
   167  	gobottest.Assert(t, err, errors.New("write error"))
   168  
   169  }
   170  
   171  func TestBlinkMRGB(t *testing.T) {
   172  	d, a := initTestBlinkMDriverWithStubbedAdaptor()
   173  	a.i2cWriteImpl = func([]byte) (int, error) {
   174  		return 0, errors.New("write error")
   175  	}
   176  
   177  	err := d.Rgb(100, 100, 100)
   178  	gobottest.Assert(t, err, errors.New("write error"))
   179  
   180  }