2014-04-16 12:10:32 +08:00
|
|
|
package gobot
|
|
|
|
|
|
|
|
import (
|
2014-06-13 05:38:03 +08:00
|
|
|
"fmt"
|
|
|
|
"testing"
|
2014-06-07 05:44:16 +08:00
|
|
|
"time"
|
2014-04-16 12:10:32 +08:00
|
|
|
)
|
|
|
|
|
2014-11-30 16:19:53 +08:00
|
|
|
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")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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 TestAfter(t *testing.T) {
|
|
|
|
i := 0
|
|
|
|
After(1*time.Millisecond, func() {
|
|
|
|
i++
|
|
|
|
})
|
2014-07-15 05:26:30 +08:00
|
|
|
<-time.After(2 * time.Millisecond)
|
2014-07-18 02:41:47 +08:00
|
|
|
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 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
|
2014-07-18 02:41:47 +08:00
|
|
|
Assert(t, i, 1)
|
2014-11-30 16:19:53 +08:00
|
|
|
|
|
|
|
var e1 = (*Event)(nil)
|
|
|
|
Assert(t, Publish(e1, 4), ErrUnknownEvent)
|
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 TestOn(t *testing.T) {
|
|
|
|
var i int
|
|
|
|
e := NewEvent()
|
|
|
|
On(e, func(data interface{}) {
|
|
|
|
i = data.(int)
|
2014-04-16 12:10:32 +08:00
|
|
|
})
|
2014-06-13 05:38:03 +08:00
|
|
|
Publish(e, 10)
|
2014-07-15 05:26:30 +08:00
|
|
|
<-time.After(1 * time.Millisecond)
|
2014-07-18 02:41:47 +08:00
|
|
|
Assert(t, i, 10)
|
2014-11-30 16:19:53 +08:00
|
|
|
|
|
|
|
var e1 = (*Event)(nil)
|
|
|
|
err := On(e1, func(data interface{}) {
|
|
|
|
i = data.(int)
|
|
|
|
})
|
|
|
|
Assert(t, err, ErrUnknownEvent)
|
2014-06-13 05:38:03 +08:00
|
|
|
}
|
2014-07-15 05:26:30 +08:00
|
|
|
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)
|
2014-07-18 02:41:47 +08:00
|
|
|
Assert(t, i, 30)
|
2014-11-30 16:19:53 +08:00
|
|
|
|
|
|
|
var e1 = (*Event)(nil)
|
|
|
|
err := Once(e1, func(data interface{}) {
|
|
|
|
i = data.(int)
|
|
|
|
})
|
|
|
|
Assert(t, err, ErrUnknownEvent)
|
2014-07-15 05:26:30 +08:00
|
|
|
}
|
2014-06-13 05:38:03 +08:00
|
|
|
|
|
|
|
func TestFromScale(t *testing.T) {
|
2014-07-18 02:41:47 +08:00
|
|
|
Assert(t, FromScale(5, 0, 10), 0.5)
|
2014-06-13 05:38:03 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestToScale(t *testing.T) {
|
2014-07-18 02:41:47 +08:00
|
|
|
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)
|
2014-06-13 05:38:03 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
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))
|
|
|
|
}
|
|
|
|
}
|