package microbit import ( "strings" "testing" "time" "gobot.io/x/gobot/v2" "gobot.io/x/gobot/v2/gobottest" ) var _ gobot.Driver = (*ButtonDriver)(nil) func initTestButtonDriver() *ButtonDriver { d := NewButtonDriver(NewBleTestAdaptor()) return d } func TestButtonDriver(t *testing.T) { d := initTestButtonDriver() gobottest.Assert(t, strings.HasPrefix(d.Name(), "Microbit Button"), true) d.SetName("NewName") gobottest.Assert(t, d.Name(), "NewName") } func TestButtonDriverStartAndHalt(t *testing.T) { d := initTestButtonDriver() gobottest.Assert(t, d.Start(), nil) gobottest.Assert(t, d.Halt(), nil) } func TestButtonDriverReadData(t *testing.T) { sem := make(chan bool) a := NewBleTestAdaptor() d := NewButtonDriver(a) _ = d.Start() _ = d.On(ButtonB, func(data interface{}) { sem <- true }) a.TestReceiveNotification([]byte{1}, nil) select { case <-sem: case <-time.After(100 * time.Millisecond): t.Errorf("Microbit Event \"ButtonB\" was not published") } }