Skip to content

Commit

Permalink
Add utils tests
Browse files Browse the repository at this point in the history
  • Loading branch information
zankich committed Apr 16, 2014
1 parent 646362a commit c1b6849
Show file tree
Hide file tree
Showing 3 changed files with 111 additions and 17 deletions.
16 changes: 16 additions & 0 deletions test_helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -72,3 +81,10 @@ func newTestRobot(name string) *Robot {
},
}
}

func newTestStruct() *testStruct {
s := new(testStruct)
s.i = 10
s.f = 0.2
return s
}
29 changes: 12 additions & 17 deletions utils.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package gobot

import (
"encoding/json"
"github.com/tarm/goserial"
"io"
"math"
Expand Down Expand Up @@ -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)
Expand All @@ -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 {
Expand All @@ -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))
}
Expand All @@ -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
}
83 changes: 83 additions & 0 deletions utils_test.go
Original file line number Diff line number Diff line change
@@ -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?"))
})
})
})

0 comments on commit c1b6849

Please sign in to comment.