2014-04-30 23:10:44 +08:00
|
|
|
package gobot
|
2013-11-24 01:12:57 +08:00
|
|
|
|
|
|
|
import (
|
2014-07-24 00:48:09 +08:00
|
|
|
"crypto/rand"
|
2014-11-21 10:00:32 +08:00
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"log"
|
2014-03-27 13:24:45 +08:00
|
|
|
"math"
|
2014-07-24 00:48:09 +08:00
|
|
|
"math/big"
|
2014-11-21 10:00:32 +08:00
|
|
|
"reflect"
|
|
|
|
"runtime"
|
|
|
|
"strings"
|
|
|
|
"testing"
|
2013-11-24 01:12:57 +08:00
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
2014-11-30 16:19:53 +08:00
|
|
|
var (
|
2014-12-31 21:15:52 +08:00
|
|
|
// ErrUnknownEvent is the error resulting if the specified Event does not exist
|
2014-11-30 16:19:53 +08:00
|
|
|
ErrUnknownEvent = errors.New("Event does not exist")
|
|
|
|
)
|
|
|
|
|
2014-11-21 10:00:32 +08:00
|
|
|
var eventError = func(e *Event) (err error) {
|
|
|
|
if e == nil {
|
2014-11-30 16:19:53 +08:00
|
|
|
err = ErrUnknownEvent
|
2014-11-21 10:00:32 +08:00
|
|
|
log.Println(err.Error())
|
|
|
|
return
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2014-11-30 16:19:53 +08:00
|
|
|
var errFunc = func(t *testing.T, message string) {
|
|
|
|
t.Errorf(message)
|
|
|
|
}
|
|
|
|
|
2014-11-21 10:00:32 +08:00
|
|
|
func logFailure(t *testing.T, message string) {
|
|
|
|
_, file, line, _ := runtime.Caller(2)
|
|
|
|
s := strings.Split(file, "/")
|
2014-11-30 16:19:53 +08:00
|
|
|
errFunc(t, fmt.Sprintf("%v:%v: %v", s[len(s)-1], line, message))
|
2014-11-21 10:00:32 +08:00
|
|
|
}
|
2014-11-30 16:19:53 +08:00
|
|
|
|
2014-12-31 21:15:52 +08:00
|
|
|
// Assert checks if a and b are equal, emis a t.Errorf if they are not equal.
|
2014-11-21 10:00:32 +08:00
|
|
|
func Assert(t *testing.T, a interface{}, b interface{}) {
|
|
|
|
if !reflect.DeepEqual(a, b) {
|
|
|
|
logFailure(t, fmt.Sprintf("%v - \"%v\", should equal, %v - \"%v\"",
|
|
|
|
a, reflect.TypeOf(a), b, reflect.TypeOf(b)))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-12-31 21:15:52 +08:00
|
|
|
// Refute checks if a and b are equal, emis a t.Errorf if they are equal.
|
2014-11-21 10:00:32 +08:00
|
|
|
func Refute(t *testing.T, a interface{}, b interface{}) {
|
|
|
|
if reflect.DeepEqual(a, b) {
|
|
|
|
logFailure(t, fmt.Sprintf("%v - \"%v\", should not equal, %v - \"%v\"",
|
|
|
|
a, reflect.TypeOf(a), b, reflect.TypeOf(b)))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-11-13 07:00:40 +08:00
|
|
|
// Every triggers f every t time until the end of days. It does not wait for the
|
|
|
|
// previous execution of f to finish before it fires the next f.
|
2014-05-03 18:20:02 +08:00
|
|
|
func Every(t time.Duration, f func()) {
|
|
|
|
c := time.Tick(t)
|
2014-10-16 01:57:07 +08:00
|
|
|
|
2013-11-24 01:12:57 +08:00
|
|
|
go func() {
|
|
|
|
for {
|
2014-04-27 01:02:59 +08:00
|
|
|
<-c
|
2013-11-24 01:12:57 +08:00
|
|
|
go f()
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
}
|
|
|
|
|
2014-11-13 07:00:40 +08:00
|
|
|
// After triggers f after t duration.
|
2014-05-03 18:20:02 +08:00
|
|
|
func After(t time.Duration, f func()) {
|
|
|
|
time.AfterFunc(t, f)
|
2013-11-24 01:12:57 +08:00
|
|
|
}
|
|
|
|
|
2014-12-31 21:15:52 +08:00
|
|
|
// Publish emits val to all subscribers of e. Returns ErrUnknownEvent if Event
|
|
|
|
// does not exist.
|
2014-11-21 10:00:32 +08:00
|
|
|
func Publish(e *Event, val interface{}) (err error) {
|
|
|
|
if err = eventError(e); err == nil {
|
|
|
|
e.Write(val)
|
|
|
|
}
|
|
|
|
return
|
2013-12-31 11:06:13 +08:00
|
|
|
}
|
|
|
|
|
2014-12-31 21:15:52 +08:00
|
|
|
// On executes f when e is Published to. Returns ErrUnknownEvent if Event
|
|
|
|
// does not exist.
|
2014-11-21 10:00:32 +08:00
|
|
|
func On(e *Event, f func(s interface{})) (err error) {
|
|
|
|
if err = eventError(e); err == nil {
|
|
|
|
e.Callbacks = append(e.Callbacks, callback{f, false})
|
|
|
|
}
|
|
|
|
return
|
2014-07-14 11:27:38 +08:00
|
|
|
}
|
|
|
|
|
2014-12-31 21:15:52 +08:00
|
|
|
// Once is similar to On except that it only executes f one time. Returns
|
|
|
|
//ErrUnknownEvent if Event does not exist.
|
2014-11-21 10:00:32 +08:00
|
|
|
func Once(e *Event, f func(s interface{})) (err error) {
|
|
|
|
if err = eventError(e); err == nil {
|
|
|
|
e.Callbacks = append(e.Callbacks, callback{f, true})
|
|
|
|
}
|
|
|
|
return
|
2013-12-31 11:06:13 +08:00
|
|
|
}
|
|
|
|
|
2014-11-13 07:00:40 +08:00
|
|
|
// Rand returns a positive random int up to max
|
2013-11-24 01:12:57 +08:00
|
|
|
func Rand(max int) int {
|
2014-07-24 00:48:09 +08:00
|
|
|
i, _ := rand.Int(rand.Reader, big.NewInt(int64(max)))
|
|
|
|
return int(i.Int64())
|
2013-11-24 01:12:57 +08:00
|
|
|
}
|
|
|
|
|
2014-11-13 07:00:40 +08:00
|
|
|
// FromScale returns a converted input from min, max to 0.0...1.0.
|
2014-03-27 13:24:45 +08:00
|
|
|
func FromScale(input, min, max float64) float64 {
|
|
|
|
return (input - math.Min(min, max)) / (math.Max(min, max) - math.Min(min, max))
|
|
|
|
}
|
|
|
|
|
2014-11-13 07:00:40 +08:00
|
|
|
// ToScale returns a converted input from 0...1 to min...max scale.
|
|
|
|
// If input is less than min then ToScale returns min.
|
|
|
|
// If input is greater than max then ToScale returns max
|
2014-03-27 13:24:45 +08:00
|
|
|
func ToScale(input, min, max float64) float64 {
|
|
|
|
i := input*(math.Max(min, max)-math.Min(min, max)) + math.Min(min, max)
|
|
|
|
if i < math.Min(min, max) {
|
|
|
|
return math.Min(min, max)
|
|
|
|
} else if i > math.Max(min, max) {
|
|
|
|
return math.Max(min, max)
|
|
|
|
} else {
|
|
|
|
return i
|
|
|
|
}
|
|
|
|
}
|