gobot.io/x/gobot/v2@v2.1.0/platforms/pebble/pebble_driver_test.go (about)

     1  package pebble
     2  
     3  import (
     4  	"testing"
     5  	"time"
     6  
     7  	"gobot.io/x/gobot/v2"
     8  	"gobot.io/x/gobot/v2/gobottest"
     9  )
    10  
    11  var _ gobot.Driver = (*Driver)(nil)
    12  
    13  func initTestDriver() *Driver {
    14  	return NewDriver(NewAdaptor())
    15  }
    16  
    17  func TestDriverStart(t *testing.T) {
    18  	d := initTestDriver()
    19  	gobottest.Assert(t, d.Start(), nil)
    20  }
    21  
    22  func TestDriverHalt(t *testing.T) {
    23  	d := initTestDriver()
    24  	gobottest.Assert(t, d.Halt(), nil)
    25  }
    26  
    27  func TestDriver(t *testing.T) {
    28  	d := initTestDriver()
    29  
    30  	gobottest.Assert(t, d.Name(), "Pebble")
    31  	gobottest.Assert(t, d.Connection().Name(), "Pebble")
    32  
    33  	sem := make(chan bool)
    34  	d.SendNotification("Hello")
    35  	d.SendNotification("World")
    36  
    37  	gobottest.Assert(t, d.Messages[0], "Hello")
    38  	gobottest.Assert(t, d.PendingMessage(), "Hello")
    39  	gobottest.Assert(t, d.PendingMessage(), "World")
    40  	gobottest.Assert(t, d.PendingMessage(), "")
    41  
    42  	d.On(d.Event("button"), func(data interface{}) {
    43  		sem <- true
    44  	})
    45  
    46  	d.PublishEvent("button", "")
    47  
    48  	select {
    49  	case <-sem:
    50  	case <-time.After(100 * time.Millisecond):
    51  		t.Errorf("Button Event was not published")
    52  	}
    53  
    54  	d.On(d.Event("accel"), func(data interface{}) {
    55  		sem <- true
    56  	})
    57  
    58  	d.Command("publish_event")(map[string]interface{}{"name": "accel", "data": "100"})
    59  
    60  	select {
    61  	case <-sem:
    62  	case <-time.After(100 * time.Millisecond):
    63  		t.Errorf("Accel Event was not published")
    64  	}
    65  
    66  	d.Command("send_notification")(map[string]interface{}{"message": "Hey buddy!"})
    67  	gobottest.Assert(t, d.Messages[0], "Hey buddy!")
    68  
    69  	message := d.Command("pending_message")(map[string]interface{}{})
    70  	gobottest.Assert(t, message, "Hey buddy!")
    71  }