forked from hybridgroup/gobot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils_test.go
78 lines (74 loc) · 2 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
package gobot
import (
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
"time"
)
var _ = Describe("Utils", func() {
var (
testInterface interface{}
)
Context("when valid", func() {
It("should execute function at every interval", func() {
var i = 0
Every("500ms", func() {
i = i + 1
})
time.Sleep(600 * time.Millisecond)
Expect(i).To(Equal(1))
time.Sleep(600 * time.Millisecond)
Expect(i).To(Equal(2))
})
It("should execute function after specific interval", func() {
var i = 0
After("500ms", func() {
i = i + 1
})
time.Sleep(600 * time.Millisecond)
Expect(i).To(Equal(1))
time.Sleep(600 * time.Millisecond)
Expect(i).To(Equal(1))
})
It("should Publish message to channel without blocking", func() {
c := make(chan interface{}, 1)
Publish(c, 1)
Publish(c, 2)
i := <-c
Expect(i.(int)).To(Equal(1))
})
It("should execution function on event", func() {
c := make(chan interface{})
var i int
On(c, func(data interface{}) {
i = data.(int)
})
c <- 10
Expect(i).To(Equal(10))
})
It("should scale the value between 0...1", func() {
Expect(FromScale(5, 0, 10)).To(Equal(0.5))
})
It("should scale the 0...1 to scale ", func() {
Expect(ToScale(500, 0, 10)).To(Equal(float64(10)))
Expect(ToScale(-1, 0, 10)).To(Equal(float64(0)))
Expect(ToScale(0.5, 0, 10)).To(Equal(float64(5)))
})
It("should return random int", func() {
a := Rand(100)
b := Rand(100)
Expect(a).NotTo(Equal(b))
})
It("should return the Field", func() {
testInterface = *newTestStruct()
Expect(FieldByName(testInterface, "i").Int()).To(Equal(int64(10)))
})
It("should return the Field from ptr", func() {
testInterface = newTestStruct()
Expect(FieldByNamePtr(testInterface, "f").Float()).To(Equal(0.2))
})
It("should call function on interface", func() {
testInterface = newTestStruct()
Expect(Call(testInterface, "Hello", "Human", "How are you?")[0].String()).To(Equal("Hello Human! How are you?"))
})
})
})