forked from hybridgroup/gobot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils_test.go
92 lines (76 loc) · 1.6 KB
/
utils_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
package gobot
import (
"testing"
"time"
"github.com/stretchr/testify/assert"
)
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")
}
assert.Equal(t, 1, i)
}
func TestFromScale(t *testing.T) {
assert.Equal(t, 0.5, FromScale(5, 0, 10))
}
func TestToScale(t *testing.T) {
assert.Equal(t, 10.0, ToScale(500, 0, 10))
assert.Equal(t, 0.0, ToScale(-1, 0, 10))
assert.Equal(t, 5.0, ToScale(0.5, 0, 10))
}
func TestRescale(t *testing.T) {
assert.Equal(t, 5.0, Rescale(500, 0, 1000, 0, 10))
assert.Equal(t, 490.0, Rescale(-1.0, -1, 0, 490, 350))
}
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")
assert.Contains(t, name, "tester")
}