gobot.io/x/gobot@v1.16.0/drivers/gpio/direct_pin_driver_test.go (about)

     1  package gpio
     2  
     3  import (
     4  	"errors"
     5  	"strings"
     6  	"testing"
     7  
     8  	"gobot.io/x/gobot"
     9  	"gobot.io/x/gobot/gobottest"
    10  )
    11  
    12  var _ gobot.Driver = (*DirectPinDriver)(nil)
    13  
    14  func initTestDirectPinDriver() *DirectPinDriver {
    15  	a := newGpioTestAdaptor()
    16  	a.testAdaptorDigitalRead = func(string) (val int, err error) {
    17  		val = 1
    18  		return
    19  	}
    20  	a.testAdaptorDigitalWrite = func(string, byte) (err error) {
    21  		return errors.New("write error")
    22  	}
    23  	a.testAdaptorPwmWrite = func(string, byte) (err error) {
    24  		return errors.New("write error")
    25  	}
    26  	a.testAdaptorServoWrite = func(string, byte) (err error) {
    27  		return errors.New("write error")
    28  	}
    29  	return NewDirectPinDriver(a, "1")
    30  }
    31  
    32  func TestDirectPinDriver(t *testing.T) {
    33  	var ret map[string]interface{}
    34  	var err interface{}
    35  
    36  	d := initTestDirectPinDriver()
    37  	gobottest.Assert(t, d.Pin(), "1")
    38  	gobottest.Refute(t, d.Connection(), nil)
    39  
    40  	ret = d.Command("DigitalRead")(nil).(map[string]interface{})
    41  
    42  	gobottest.Assert(t, ret["val"].(int), 1)
    43  	gobottest.Assert(t, ret["err"], nil)
    44  
    45  	err = d.Command("DigitalWrite")(map[string]interface{}{"level": "1"})
    46  	gobottest.Assert(t, err.(error), errors.New("write error"))
    47  
    48  	err = d.Command("PwmWrite")(map[string]interface{}{"level": "1"})
    49  	gobottest.Assert(t, err.(error), errors.New("write error"))
    50  
    51  	err = d.Command("ServoWrite")(map[string]interface{}{"level": "1"})
    52  	gobottest.Assert(t, err.(error), errors.New("write error"))
    53  }
    54  
    55  func TestDirectPinDriverStart(t *testing.T) {
    56  	d := initTestDirectPinDriver()
    57  	gobottest.Assert(t, d.Start(), nil)
    58  }
    59  
    60  func TestDirectPinDriverHalt(t *testing.T) {
    61  	d := initTestDirectPinDriver()
    62  	gobottest.Assert(t, d.Halt(), nil)
    63  }
    64  
    65  func TestDirectPinDriverOff(t *testing.T) {
    66  	d := initTestDirectPinDriver()
    67  	gobottest.Refute(t, d.Off(), nil)
    68  
    69  	a := newGpioTestAdaptor()
    70  	d = NewDirectPinDriver(a, "1")
    71  	gobottest.Assert(t, d.Off(), nil)
    72  }
    73  
    74  func TestDirectPinDriverOffNotSupported(t *testing.T) {
    75  	a := &gpioTestBareAdaptor{}
    76  	d := NewDirectPinDriver(a, "1")
    77  	gobottest.Assert(t, d.Off(), errors.New("DigitalWrite is not supported by this platform"))
    78  }
    79  
    80  func TestDirectPinDriverOn(t *testing.T) {
    81  	a := newGpioTestAdaptor()
    82  	d := NewDirectPinDriver(a, "1")
    83  	gobottest.Assert(t, d.On(), nil)
    84  }
    85  
    86  func TestDirectPinDriverOnError(t *testing.T) {
    87  	d := initTestDirectPinDriver()
    88  	gobottest.Refute(t, d.On(), nil)
    89  }
    90  
    91  func TestDirectPinDriverOnNotSupported(t *testing.T) {
    92  	a := &gpioTestBareAdaptor{}
    93  	d := NewDirectPinDriver(a, "1")
    94  	gobottest.Assert(t, d.On(), errors.New("DigitalWrite is not supported by this platform"))
    95  }
    96  
    97  func TestDirectPinDriverDigitalWrite(t *testing.T) {
    98  	adaptor := newGpioTestAdaptor()
    99  	d := NewDirectPinDriver(adaptor, "1")
   100  	gobottest.Assert(t, d.DigitalWrite(1), nil)
   101  }
   102  
   103  func TestDirectPinDriverDigitalWriteNotSupported(t *testing.T) {
   104  	a := &gpioTestBareAdaptor{}
   105  	d := NewDirectPinDriver(a, "1")
   106  	gobottest.Assert(t, d.DigitalWrite(1), errors.New("DigitalWrite is not supported by this platform"))
   107  }
   108  
   109  func TestDirectPinDriverDigitalWriteError(t *testing.T) {
   110  	d := initTestDirectPinDriver()
   111  	gobottest.Refute(t, d.DigitalWrite(1), nil)
   112  }
   113  
   114  func TestDirectPinDriverDigitalRead(t *testing.T) {
   115  	d := initTestDirectPinDriver()
   116  	ret, err := d.DigitalRead()
   117  	gobottest.Assert(t, ret, 1)
   118  	gobottest.Assert(t, err, nil)
   119  }
   120  
   121  func TestDirectPinDriverDigitalReadNotSupported(t *testing.T) {
   122  	a := &gpioTestBareAdaptor{}
   123  	d := NewDirectPinDriver(a, "1")
   124  	_, e := d.DigitalRead()
   125  	gobottest.Assert(t, e, errors.New("DigitalRead is not supported by this platform"))
   126  }
   127  
   128  func TestDirectPinDriverPwmWrite(t *testing.T) {
   129  	a := newGpioTestAdaptor()
   130  	d := NewDirectPinDriver(a, "1")
   131  	gobottest.Assert(t, d.PwmWrite(1), nil)
   132  }
   133  
   134  func TestDirectPinDriverPwmWriteNotSupported(t *testing.T) {
   135  	a := &gpioTestBareAdaptor{}
   136  	d := NewDirectPinDriver(a, "1")
   137  	gobottest.Assert(t, d.PwmWrite(1), errors.New("PwmWrite is not supported by this platform"))
   138  }
   139  
   140  func TestDirectPinDriverPwmWriteError(t *testing.T) {
   141  	d := initTestDirectPinDriver()
   142  	gobottest.Refute(t, d.PwmWrite(1), nil)
   143  }
   144  
   145  func TestDirectPinDriverServoWrite(t *testing.T) {
   146  	a := newGpioTestAdaptor()
   147  	d := NewDirectPinDriver(a, "1")
   148  	gobottest.Assert(t, d.ServoWrite(1), nil)
   149  }
   150  
   151  func TestDirectPinDriverServoWriteNotSupported(t *testing.T) {
   152  	a := &gpioTestBareAdaptor{}
   153  	d := NewDirectPinDriver(a, "1")
   154  	gobottest.Assert(t, d.ServoWrite(1), errors.New("ServoWrite is not supported by this platform"))
   155  }
   156  
   157  func TestDirectPinDriverServoWriteError(t *testing.T) {
   158  	d := initTestDirectPinDriver()
   159  	gobottest.Refute(t, d.ServoWrite(1), nil)
   160  }
   161  
   162  func TestDirectPinDriverDefaultName(t *testing.T) {
   163  	d := initTestDirectPinDriver()
   164  	gobottest.Assert(t, strings.HasPrefix(d.Name(), "Direct"), true)
   165  }
   166  
   167  func TestDirectPinDriverSetName(t *testing.T) {
   168  	d := initTestDirectPinDriver()
   169  	d.SetName("mybot")
   170  	gobottest.Assert(t, d.Name(), "mybot")
   171  }