hybridgroup.gobot/utils_test.go

94 lines
1.7 KiB
Go

package gobot
import (
"strings"
"testing"
"time"
"gobot.io/x/gobot/v2/gobottest"
)
func TestEvery(t *testing.T) {
i := 0
begin := time.Now()
sem := make(chan time.Time, 1)
Every(2*time.Millisecond, func() {
i++
if i == 2 {
sem <- time.Now()
}
})
<-sem
if time.Since(begin) < 4*time.Millisecond {
t.Error("Test should have taken at least 4 milliseconds")
}
}
func TestEveryWhenStopped(t *testing.T) {
sem := make(chan bool)
done := Every(100*time.Millisecond, func() {
sem <- true
})
select {
case <-sem:
done.Stop()
case <-time.After(190 * time.Millisecond):
done.Stop()
t.Errorf("Every was not called")
}
select {
case <-time.After(190 * time.Millisecond):
case <-sem:
t.Error("Every should have stopped")
}
}
func TestAfter(t *testing.T) {
i := 0
sem := make(chan bool)
After(100*time.Millisecond, func() {
i++
sem <- true
})
select {
case <-sem:
case <-time.After(190 * time.Millisecond):
t.Errorf("After was not called")
}
gobottest.Assert(t, i, 1)
}
func TestFromScale(t *testing.T) {
gobottest.Assert(t, FromScale(5, 0, 10), 0.5)
}
func TestToScale(t *testing.T) {
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)
}
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)
}
func TestRand(t *testing.T) {
a := Rand(10000)
b := Rand(10000)
if a == b {
t.Errorf("%v should not equal %v", a, b)
}
}
func TestDefaultName(t *testing.T) {
name := DefaultName("tester")
gobottest.Assert(t, strings.Contains(name, "tester"), true)
}