forked from hybridgroup/gobot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils_test.go
135 lines (118 loc) · 2.32 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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
package gobot
import (
"fmt"
"testing"
"time"
)
func TestAssert(t *testing.T) {
err := ""
errFunc = func(t *testing.T, message string) {
err = message
}
Assert(t, 1, 1)
if err != "" {
t.Errorf("Assert failed: 1 should equal 1")
}
Assert(t, 1, 2)
if err == "" {
t.Errorf("Assert failed: 1 should not equal 2")
}
}
func TestRefute(t *testing.T) {
err := ""
errFunc = func(t *testing.T, message string) {
err = message
}
Refute(t, 1, 2)
if err != "" {
t.Errorf("Refute failed: 1 should not be 2")
}
Refute(t, 1, 1)
if err == "" {
t.Errorf("Refute failed: 1 should not be 1")
}
}
func TestEvery(t *testing.T) {
i := 0
begin := time.Now().UnixNano()
sem := make(chan int64, 1)
Every(2*time.Millisecond, func() {
i++
if i == 2 {
sem <- time.Now().UnixNano()
}
})
end := <-sem
if end-begin < 4000000 {
t.Error("Test should have taken at least 4 milliseconds")
}
}
func TestAfter(t *testing.T) {
i := 0
After(1*time.Millisecond, func() {
i++
})
<-time.After(2 * time.Millisecond)
Assert(t, i, 1)
}
func TestPublish(t *testing.T) {
e := &Event{Chan: make(chan interface{}, 1)}
Publish(e, 1)
Publish(e, 2)
Publish(e, 3)
Publish(e, 4)
i := <-e.Chan
Assert(t, i, 1)
var e1 = (*Event)(nil)
Assert(t, Publish(e1, 4), ErrUnknownEvent)
}
func TestOn(t *testing.T) {
var i int
e := NewEvent()
On(e, func(data interface{}) {
i = data.(int)
})
Publish(e, 10)
<-time.After(1 * time.Millisecond)
Assert(t, i, 10)
var e1 = (*Event)(nil)
err := On(e1, func(data interface{}) {
i = data.(int)
})
Assert(t, err, ErrUnknownEvent)
}
func TestOnce(t *testing.T) {
i := 0
e := NewEvent()
Once(e, func(data interface{}) {
i += data.(int)
})
On(e, func(data interface{}) {
i += data.(int)
})
Publish(e, 10)
<-time.After(1 * time.Millisecond)
Publish(e, 10)
<-time.After(1 * time.Millisecond)
Assert(t, i, 30)
var e1 = (*Event)(nil)
err := Once(e1, func(data interface{}) {
i = data.(int)
})
Assert(t, err, ErrUnknownEvent)
}
func TestFromScale(t *testing.T) {
Assert(t, FromScale(5, 0, 10), 0.5)
}
func TestToScale(t *testing.T) {
Assert(t, ToScale(500, 0, 10), 10.0)
Assert(t, ToScale(-1, 0, 10), 0.0)
Assert(t, ToScale(0.5, 0, 10), 5.0)
}
func TestRand(t *testing.T) {
a := Rand(1000)
b := Rand(1000)
if a == b {
t.Error(fmt.Sprintf("%v should not equal %v", a, b))
}
}