hybridgroup.gobot/utils_test.go

88 lines
1.5 KiB
Go
Raw Normal View History

2014-04-16 12:10:32 +08:00
package gobot
import (
"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
"gobot.io/x/gobot/gobottest"
)
2014-11-30 16:19:53 +08:00
2014-06-13 05:38:03 +08:00
func TestEvery(t *testing.T) {
i := 0
2014-10-29 06:50:32 +08:00
begin := time.Now().UnixNano()
sem := make(chan int64, 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 {
sem <- time.Now().UnixNano()
}
2014-06-13 05:38:03 +08:00
})
2014-10-29 06:50:32 +08:00
end := <-sem
if end-begin < 4000000 {
t.Error("Test should have taken at least 4 milliseconds")
}
2014-06-13 05:38:03 +08:00
}
func TestEveryWhenStopped(t *testing.T) {
sem := make(chan bool)
done := Every(50*time.Millisecond, func() {
sem <- true
})
select {
case <-sem:
done.Stop()
case <-time.After(60 * time.Millisecond):
t.Errorf("Every was not called")
}
select {
case <-time.After(60 * time.Millisecond):
case <-sem:
t.Error("Every should have stopped")
}
}
2014-06-13 05:38:03 +08:00
func TestAfter(t *testing.T) {
i := 0
sem := make(chan bool)
2014-06-13 05:38:03 +08:00
After(1*time.Millisecond, func() {
i++
sem <- true
2014-06-13 05:38:03 +08:00
})
select {
case <-sem:
case <-time.After(10 * time.Millisecond):
t.Errorf("After was not called")
}
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) {
gobottest.Assert(t, FromScale(5, 0, 10), 0.5)
2014-06-13 05:38:03 +08:00
}
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)
2014-06-13 05:38:03 +08:00
}
func TestRand(t *testing.T) {
a := Rand(1000)
b := Rand(1000)
if a == b {
t.Errorf("%v should not equal %v", a, b)
2014-06-13 05:38:03 +08:00
}
}
func TestDefaultName(t *testing.T) {
name := DefaultName("tester")
gobottest.Assert(t, strings.Contains(name, "tester"), true)
}