2014-04-16 12:10:32 +08:00
|
|
|
package gobot
|
|
|
|
|
|
|
|
import (
|
2017-04-19 17:24:09 +08:00
|
|
|
"strings"
|
2014-06-13 05:38:03 +08:00
|
|
|
"testing"
|
2014-06-07 05:44:16 +08:00
|
|
|
"time"
|
2014-11-30 16:19:53 +08:00
|
|
|
|
2023-05-20 20:25:21 +08:00
|
|
|
"gobot.io/x/gobot/v2/gobottest"
|
2016-02-22 13:21:24 +08:00
|
|
|
)
|
2014-11-30 16:19:53 +08:00
|
|
|
|
2014-06-13 05:38:03 +08:00
|
|
|
func TestEvery(t *testing.T) {
|
|
|
|
i := 0
|
2022-09-25 18:02:19 +08:00
|
|
|
begin := time.Now()
|
|
|
|
sem := make(chan time.Time, 1)
|
2014-06-13 05:38:03 +08:00
|
|
|
Every(2*time.Millisecond, func() {
|
|
|
|
i++
|
2014-10-29 06:50:32 +08:00
|
|
|
if i == 2 {
|
2022-09-25 18:02:19 +08:00
|
|
|
sem <- time.Now()
|
2014-10-29 06:50:32 +08:00
|
|
|
}
|
2014-06-13 05:38:03 +08:00
|
|
|
})
|
2022-09-25 18:02:19 +08:00
|
|
|
<-sem
|
|
|
|
if time.Since(begin) < 4*time.Millisecond {
|
2014-10-29 06:50:32 +08:00
|
|
|
t.Error("Test should have taken at least 4 milliseconds")
|
|
|
|
}
|
2014-06-13 05:38:03 +08:00
|
|
|
}
|
|
|
|
|
2016-12-09 03:32:02 +08:00
|
|
|
func TestEveryWhenStopped(t *testing.T) {
|
|
|
|
sem := make(chan bool)
|
|
|
|
|
2022-10-11 14:32:14 +08:00
|
|
|
done := Every(100*time.Millisecond, func() {
|
2016-12-09 03:32:02 +08:00
|
|
|
sem <- true
|
2016-05-14 08:21:21 +08:00
|
|
|
})
|
2016-12-09 03:32:02 +08:00
|
|
|
|
|
|
|
select {
|
|
|
|
case <-sem:
|
|
|
|
done.Stop()
|
2022-10-23 21:17:48 +08:00
|
|
|
case <-time.After(190 * time.Millisecond):
|
|
|
|
done.Stop()
|
2016-12-09 03:32:02 +08:00
|
|
|
t.Errorf("Every was not called")
|
|
|
|
}
|
|
|
|
|
|
|
|
select {
|
2022-10-23 21:17:48 +08:00
|
|
|
case <-time.After(190 * time.Millisecond):
|
2016-12-09 03:32:02 +08:00
|
|
|
case <-sem:
|
|
|
|
t.Error("Every should have stopped")
|
2016-05-14 08:21:21 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-06-13 05:38:03 +08:00
|
|
|
func TestAfter(t *testing.T) {
|
|
|
|
i := 0
|
2016-12-09 03:32:02 +08:00
|
|
|
sem := make(chan bool)
|
|
|
|
|
2022-10-23 21:17:48 +08:00
|
|
|
After(100*time.Millisecond, func() {
|
2014-06-13 05:38:03 +08:00
|
|
|
i++
|
2016-12-09 03:32:02 +08:00
|
|
|
sem <- true
|
2014-06-13 05:38:03 +08:00
|
|
|
})
|
2016-12-09 03:32:02 +08:00
|
|
|
|
|
|
|
select {
|
|
|
|
case <-sem:
|
2022-10-23 21:17:48 +08:00
|
|
|
case <-time.After(190 * time.Millisecond):
|
2016-12-09 03:32:02 +08:00
|
|
|
t.Errorf("After was not called")
|
|
|
|
}
|
|
|
|
|
2016-02-22 13:21:24 +08:00
|
|
|
gobottest.Assert(t, i, 1)
|
2014-06-13 05:38:03 +08:00
|
|
|
}
|
2014-04-16 12:10:32 +08:00
|
|
|
|
2014-06-13 05:38:03 +08:00
|
|
|
func TestFromScale(t *testing.T) {
|
2016-02-22 13:21:24 +08:00
|
|
|
gobottest.Assert(t, FromScale(5, 0, 10), 0.5)
|
2014-06-13 05:38:03 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestToScale(t *testing.T) {
|
2016-02-22 13:21:24 +08:00
|
|
|
gobottest.Assert(t, ToScale(500, 0, 10), 10.0)
|
|
|
|
gobottest.Assert(t, ToScale(-1, 0, 10), 0.0)
|
|
|
|
gobottest.Assert(t, ToScale(0.5, 0, 10), 5.0)
|
2014-06-13 05:38:03 +08:00
|
|
|
}
|
|
|
|
|
2018-08-15 23:06:52 +08:00
|
|
|
func TestRescale(t *testing.T) {
|
|
|
|
gobottest.Assert(t, Rescale(500, 0, 1000, 0, 10), 5.0)
|
|
|
|
gobottest.Assert(t, Rescale(-1.0, -1, 0, 490, 350), 490.0)
|
|
|
|
}
|
|
|
|
|
2014-06-13 05:38:03 +08:00
|
|
|
func TestRand(t *testing.T) {
|
2017-10-19 18:54:54 +08:00
|
|
|
a := Rand(10000)
|
|
|
|
b := Rand(10000)
|
2014-06-13 05:38:03 +08:00
|
|
|
if a == b {
|
2016-10-16 17:43:02 +08:00
|
|
|
t.Errorf("%v should not equal %v", a, b)
|
2014-06-13 05:38:03 +08:00
|
|
|
}
|
|
|
|
}
|
2017-04-19 17:24:09 +08:00
|
|
|
|
|
|
|
func TestDefaultName(t *testing.T) {
|
|
|
|
name := DefaultName("tester")
|
|
|
|
gobottest.Assert(t, strings.Contains(name, "tester"), true)
|
|
|
|
}
|