gobot.io/x/gobot@v1.16.0/helpers_test.go (about)

     1  package gobot
     2  
     3  import "os"
     4  
     5  type NullReadWriteCloser struct{}
     6  
     7  func (NullReadWriteCloser) Write(p []byte) (int, error) {
     8  	return len(p), nil
     9  }
    10  
    11  func (NullReadWriteCloser) Read(b []byte) (int, error) {
    12  	return len(b), nil
    13  }
    14  
    15  func (NullReadWriteCloser) Close() error {
    16  	return nil
    17  }
    18  
    19  type testDriver struct {
    20  	name       string
    21  	pin        string
    22  	connection Connection
    23  	Commander
    24  }
    25  
    26  var testDriverStart = func() (err error) { return }
    27  var testDriverHalt = func() (err error) { return }
    28  
    29  func (t *testDriver) Start() (err error)     { return testDriverStart() }
    30  func (t *testDriver) Halt() (err error)      { return testDriverHalt() }
    31  func (t *testDriver) Name() string           { return t.name }
    32  func (t *testDriver) SetName(n string)       { t.name = n }
    33  func (t *testDriver) Pin() string            { return t.pin }
    34  func (t *testDriver) Connection() Connection { return t.connection }
    35  
    36  func newTestDriver(adaptor *testAdaptor, name string, pin string) *testDriver {
    37  	t := &testDriver{
    38  		name:       name,
    39  		connection: adaptor,
    40  		pin:        pin,
    41  		Commander:  NewCommander(),
    42  	}
    43  
    44  	t.AddCommand("DriverCommand", func(params map[string]interface{}) interface{} { return nil })
    45  
    46  	return t
    47  }
    48  
    49  type testAdaptor struct {
    50  	name string
    51  	port string
    52  }
    53  
    54  var testAdaptorConnect = func() (err error) { return }
    55  var testAdaptorFinalize = func() (err error) { return }
    56  
    57  func (t *testAdaptor) Finalize() (err error) { return testAdaptorFinalize() }
    58  func (t *testAdaptor) Connect() (err error)  { return testAdaptorConnect() }
    59  func (t *testAdaptor) Name() string          { return t.name }
    60  func (t *testAdaptor) SetName(n string)      { t.name = n }
    61  func (t *testAdaptor) Port() string          { return t.port }
    62  
    63  func newTestAdaptor(name string, port string) *testAdaptor {
    64  	return &testAdaptor{
    65  		name: name,
    66  		port: port,
    67  	}
    68  }
    69  
    70  func newTestRobot(name string) *Robot {
    71  	adaptor1 := newTestAdaptor("Connection1", "/dev/null")
    72  	adaptor2 := newTestAdaptor("Connection2", "/dev/null")
    73  	adaptor3 := newTestAdaptor("", "/dev/null")
    74  	driver1 := newTestDriver(adaptor1, "Device1", "0")
    75  	driver2 := newTestDriver(adaptor2, "Device2", "2")
    76  	driver3 := newTestDriver(adaptor3, "", "1")
    77  	work := func() {}
    78  	r := NewRobot(name,
    79  		[]Connection{adaptor1, adaptor2, adaptor3},
    80  		[]Device{driver1, driver2, driver3},
    81  		work,
    82  	)
    83  	r.AddCommand("RobotCommand", func(params map[string]interface{}) interface{} { return nil })
    84  	r.trap = func(c chan os.Signal) {
    85  		c <- os.Interrupt
    86  	}
    87  
    88  	return r
    89  }