diff --git a/test_helper.go b/test_helper.go index d666230e8..3d2c9072e 100644 --- a/test_helper.go +++ b/test_helper.go @@ -2,6 +2,15 @@ package gobot import "fmt" +type testStruct struct { + i int + f float64 +} + +func (me *testStruct) Hello(name string, message string) string { + return fmt.Sprintf("Hello %v! %v", name, message) +} + type null struct{} func (null) Write(p []byte) (int, error) { @@ -72,3 +81,10 @@ func newTestRobot(name string) *Robot { }, } } + +func newTestStruct() *testStruct { + s := new(testStruct) + s.i = 10 + s.f = 0.2 + return s +} diff --git a/utils.go b/utils.go index 3e090ad6a..d0f305b13 100644 --- a/utils.go +++ b/utils.go @@ -1,7 +1,6 @@ package gobot import ( - "encoding/json" "github.com/tarm/goserial" "io" "math" @@ -47,14 +46,6 @@ func On(c chan interface{}, f func(s interface{})) { }() } -func parseDuration(t string) time.Duration { - dur, err := time.ParseDuration(t) - if err != nil { - panic(err) - } - return dur -} - func Rand(max int) int { rand.Seed(time.Now().UTC().UnixNano()) return rand.Intn(max) @@ -77,9 +68,10 @@ func ConnectToSerial(port string, baud int) io.ReadWriteCloser { return s } -func IsUrl(url string) bool { - rp := regexp.MustCompile("([^A-Za-z0-9]+).([^A-Za-z0-9]+).([^A-Za-z0-9]+)") - return rp.MatchString(url) +func IsUrl(s string) bool { + ip := regexp.MustCompile("([^A-Za-z0-9]+).([^A-Za-z0-9]+).([^A-Za-z0-9]+)") + url := regexp.MustCompile("([a-zA-Z0-9]+://)?([a-zA-Z0-9_]+:[a-zA-Z0-9_]+@)?([a-zA-Z0-9.-]+\\.[A-Za-z]{2,4})(:[0-9]+)?(/.*)?") + return ip.MatchString(s) || url.MatchString(s) } func Call(thing interface{}, method string, params ...interface{}) []reflect.Value { @@ -97,11 +89,6 @@ func FieldByNamePtr(thing interface{}, field string) reflect.Value { return reflect.ValueOf(thing).Elem().FieldByName(field) } -func toJson(obj interface{}) string { - b, _ := json.Marshal(obj) - return string(b) -} - func FromScale(input, min, max float64) float64 { return (input - math.Min(min, max)) / (math.Max(min, max) - math.Min(min, max)) } @@ -116,3 +103,11 @@ func ToScale(input, min, max float64) float64 { return i } } + +func parseDuration(t string) time.Duration { + dur, err := time.ParseDuration(t) + if err != nil { + panic(err) + } + return dur +} diff --git a/utils_test.go b/utils_test.go new file mode 100644 index 000000000..0c37e9382 --- /dev/null +++ b/utils_test.go @@ -0,0 +1,83 @@ +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 true on url or ip address", func() { + Expect(IsUrl("127.0.0.1")).To(Equal(true)) + Expect(IsUrl("http://www.google.com")).To(Equal(true)) + Expect(IsUrl("/dev/ttyACM0")).To(Equal(false)) + }) + 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?")) + }) + }) +})