gobot.io/x/gobot/v2@v2.1.0/platforms/microbit/button_driver_test.go (about)

     1  package microbit
     2  
     3  import (
     4  	"strings"
     5  	"testing"
     6  	"time"
     7  
     8  	"gobot.io/x/gobot/v2"
     9  	"gobot.io/x/gobot/v2/gobottest"
    10  )
    11  
    12  var _ gobot.Driver = (*ButtonDriver)(nil)
    13  
    14  func initTestButtonDriver() *ButtonDriver {
    15  	d := NewButtonDriver(NewBleTestAdaptor())
    16  	return d
    17  }
    18  
    19  func TestButtonDriver(t *testing.T) {
    20  	d := initTestButtonDriver()
    21  	gobottest.Assert(t, strings.HasPrefix(d.Name(), "Microbit Button"), true)
    22  	d.SetName("NewName")
    23  	gobottest.Assert(t, d.Name(), "NewName")
    24  }
    25  
    26  func TestButtonDriverStartAndHalt(t *testing.T) {
    27  	d := initTestButtonDriver()
    28  	gobottest.Assert(t, d.Start(), nil)
    29  	gobottest.Assert(t, d.Halt(), nil)
    30  }
    31  
    32  func TestButtonDriverReadData(t *testing.T) {
    33  	sem := make(chan bool)
    34  	a := NewBleTestAdaptor()
    35  	d := NewButtonDriver(a)
    36  	d.Start()
    37  	d.On(ButtonB, func(data interface{}) {
    38  		sem <- true
    39  	})
    40  
    41  	a.TestReceiveNotification([]byte{1}, nil)
    42  
    43  	select {
    44  	case <-sem:
    45  	case <-time.After(100 * time.Millisecond):
    46  		t.Errorf("Microbit Event \"ButtonB\" was not published")
    47  	}
    48  }