From cf175b9557c0653be28146344dfa7b04e572dd68 Mon Sep 17 00:00:00 2001 From: Adrian Zankich Date: Thu, 12 Jun 2014 14:38:03 -0700 Subject: [PATCH] Update tests for api and core --- .travis.yml | 3 - api/api_suite_test.go | 20 ---- api/api_test.go | 265 ++++++++++++++++++++++-------------------- gobot_suite_test.go | 14 --- gobot_test.go | 72 +++++------- test_helper.go | 17 ++- utils.go | 4 +- utils_test.go | 138 +++++++++++----------- 8 files changed, 248 insertions(+), 285 deletions(-) delete mode 100644 api/api_suite_test.go delete mode 100644 gobot_suite_test.go diff --git a/.travis.yml b/.travis.yml index 73f71bd37..233faa5fd 100644 --- a/.travis.yml +++ b/.travis.yml @@ -10,9 +10,6 @@ install: - go get github.com/onsi/gomega - go get code.google.com/p/go.tools/cmd/cover - go get github.com/mattn/goveralls - - go get github.com/go-martini/martini - - go get github.com/martini-contrib/auth - - go get github.com/martini-contrib/cors - go get github.com/hybridgroup/go-ardrone/client - go get github.com/tarm/goserial #- go get github.com/hybridgroup/go-sdl2/sdl diff --git a/api/api_suite_test.go b/api/api_suite_test.go deleted file mode 100644 index 22011f173..000000000 --- a/api/api_suite_test.go +++ /dev/null @@ -1,20 +0,0 @@ -package api - -import ( - . "github.com/onsi/ginkgo" - . "github.com/onsi/gomega" - "log" - "testing" -) - -type null struct{} - -func (null) Write(p []byte) (int, error) { - return len(p), nil -} - -func TestApi(t *testing.T) { - log.SetOutput(new(null)) - RegisterFailHandler(Fail) - RunSpecs(t, "Api Suite") -} diff --git a/api/api_test.go b/api/api_test.go index 3d62a40d8..471457274 100644 --- a/api/api_test.go +++ b/api/api_test.go @@ -4,136 +4,145 @@ import ( "bytes" "encoding/json" "github.com/hybridgroup/gobot" - . "github.com/onsi/ginkgo" - . "github.com/onsi/gomega" "io/ioutil" + "log" "net/http" "net/http/httptest" + "testing" ) -var _ = Describe("API", func() { - var ( - m *gobot.Gobot - a *api - ) - - BeforeEach(func() { - m = gobot.NewGobot() - a = NewAPI(m) - a.start = func(m *api) {} - a.Start() - - m.Robots = []*gobot.Robot{ - gobot.NewTestRobot("Robot 1"), - gobot.NewTestRobot("Robot 2"), - gobot.NewTestRobot("Robot 3"), - } - }) - - Context("when valid", func() { - It("should return all robots", func() { - request, _ := http.NewRequest("GET", "/robots", nil) - response := httptest.NewRecorder() - a.server.ServeHTTP(response, request) - - body, _ := ioutil.ReadAll(response.Body) - var i []map[string]interface{} - json.Unmarshal(body, &i) - Expect(len(i)).To(Equal(3)) - }) - It("should return robot", func() { - request, _ := http.NewRequest("GET", "/robots/Robot%201", nil) - response := httptest.NewRecorder() - a.server.ServeHTTP(response, request) - - body, _ := ioutil.ReadAll(response.Body) - var i map[string]interface{} - json.Unmarshal(body, &i) - Expect(i["name"].(string)).To(Equal("Robot 1")) - }) - It("should return all robot devices", func() { - request, _ := http.NewRequest("GET", "/robots/Robot%201/devices", nil) - response := httptest.NewRecorder() - a.server.ServeHTTP(response, request) - - body, _ := ioutil.ReadAll(response.Body) - var i []map[string]interface{} - json.Unmarshal(body, &i) - Expect(len(i)).To(Equal(3)) - }) - It("should return robot commands", func() { - request, _ := http.NewRequest("GET", "/robots/Robot%201/commands", nil) - response := httptest.NewRecorder() - a.server.ServeHTTP(response, request) - - body, _ := ioutil.ReadAll(response.Body) - var i []string - json.Unmarshal(body, &i) - Expect(i).To(Equal([]string{"robotTestFunction"})) - }) - It("should execute robot command", func() { - request, _ := http.NewRequest("GET", "/robots/Robot%201/commands/robotTestFunction", bytes.NewBufferString(`{"message":"Beep Boop"}`)) - request.Header.Add("Content-Type", "application/json") - response := httptest.NewRecorder() - a.server.ServeHTTP(response, request) - - body, _ := ioutil.ReadAll(response.Body) - var i interface{} - json.Unmarshal(body, &i) - Expect(i).To(Equal("hey Robot 1, Beep Boop")) - }) - It("should not execute unknown robot command", func() { - request, _ := http.NewRequest("GET", "/robots/Robot%201/commands/robotTestFuntion1", bytes.NewBufferString(`{"message":"Beep Boop"}`)) - request.Header.Add("Content-Type", "application/json") - response := httptest.NewRecorder() - a.server.ServeHTTP(response, request) - - body, _ := ioutil.ReadAll(response.Body) - var i interface{} - json.Unmarshal(body, &i) - Expect(i).To(Equal("Unknown Command")) - }) - It("should return robot device", func() { - request, _ := http.NewRequest("GET", "/robots/Robot%201/devices/Device%201", nil) - response := httptest.NewRecorder() - a.server.ServeHTTP(response, request) - - body, _ := ioutil.ReadAll(response.Body) - var i map[string]interface{} - json.Unmarshal(body, &i) - Expect(i["name"].(string)).To(Equal("Device 1")) - }) - It("should return device commands", func() { - request, _ := http.NewRequest("GET", "/robots/Robot%201/devices/Device%201/commands", nil) - response := httptest.NewRecorder() - a.server.ServeHTTP(response, request) - - body, _ := ioutil.ReadAll(response.Body) - var i []string - json.Unmarshal(body, &i) - Expect(i).To(Equal([]string{"TestDriverCommand", "DriverCommand"})) - }) - It("should execute device command", func() { - request, _ := http.NewRequest("GET", "/robots/Robot%201/devices/Device%201/commands/TestDriverCommand", bytes.NewBufferString(`{"name":"human"}`)) - request.Header.Add("Content-Type", "application/json") - response := httptest.NewRecorder() - a.server.ServeHTTP(response, request) - - body, _ := ioutil.ReadAll(response.Body) - var i interface{} - json.Unmarshal(body, &i) - Expect(i).To(Equal("hello human")) - }) - It("should not execute unknown device command", func() { - request, _ := http.NewRequest("GET", "/robots/Robot%201/devices/Device%201/commands/DriverCommand1", bytes.NewBufferString(`{"name":"human"}`)) - request.Header.Add("Content-Type", "application/json") - response := httptest.NewRecorder() - a.server.ServeHTTP(response, request) - - body, _ := ioutil.ReadAll(response.Body) - var i interface{} - json.Unmarshal(body, &i) - Expect(i).To(Equal("Unknown Command")) - }) - }) -}) +type null struct{} + +func (null) Write(p []byte) (int, error) { + return len(p), nil +} + +var m *gobot.Gobot +var a *api + +func init() { + log.SetOutput(new(null)) + m = gobot.NewGobot() + a = NewAPI(m) + a.start = func(m *api) {} + a.Start() + + m.Robots = []*gobot.Robot{ + gobot.NewTestRobot("Robot 1"), + gobot.NewTestRobot("Robot 2"), + gobot.NewTestRobot("Robot 3"), + } +} + +func TestRobots(t *testing.T) { + request, _ := http.NewRequest("GET", "/robots", nil) + response := httptest.NewRecorder() + a.server.ServeHTTP(response, request) + + body, _ := ioutil.ReadAll(response.Body) + var i []map[string]interface{} + json.Unmarshal(body, &i) + gobot.Expect(t, len(i), 3) +} + +func TestRobot(t *testing.T) { + request, _ := http.NewRequest("GET", "/robots/Robot%201", nil) + response := httptest.NewRecorder() + a.server.ServeHTTP(response, request) + + body, _ := ioutil.ReadAll(response.Body) + var i map[string]interface{} + json.Unmarshal(body, &i) + gobot.Expect(t, i["name"].(string), "Robot 1") +} + +func TestRobotDevices(t *testing.T) { + request, _ := http.NewRequest("GET", "/robots/Robot%201/devices", nil) + response := httptest.NewRecorder() + a.server.ServeHTTP(response, request) + + body, _ := ioutil.ReadAll(response.Body) + var i []map[string]interface{} + json.Unmarshal(body, &i) + gobot.Expect(t, len(i), 3) +} + +func TestRobotCommands(t *testing.T) { + request, _ := http.NewRequest("GET", "/robots/Robot%201/commands", nil) + response := httptest.NewRecorder() + a.server.ServeHTTP(response, request) + + body, _ := ioutil.ReadAll(response.Body) + var i []string + json.Unmarshal(body, &i) + gobot.Expect(t, i, []string{"robotTestFunction"}) +} +func TestExecuteRobotCommand(t *testing.T) { + request, _ := http.NewRequest("GET", "/robots/Robot%201/commands/robotTestFunction", bytes.NewBufferString(`{"message":"Beep Boop"}`)) + request.Header.Add("Content-Type", "application/json") + response := httptest.NewRecorder() + a.server.ServeHTTP(response, request) + + body, _ := ioutil.ReadAll(response.Body) + var i interface{} + json.Unmarshal(body, &i) + gobot.Expect(t, i, "hey Robot 1, Beep Boop") +} + +func TestUnknownRobotCommand(t *testing.T) { + request, _ := http.NewRequest("GET", "/robots/Robot%201/commands/robotTestFuntion1", bytes.NewBufferString(`{"message":"Beep Boop"}`)) + request.Header.Add("Content-Type", "application/json") + response := httptest.NewRecorder() + a.server.ServeHTTP(response, request) + + body, _ := ioutil.ReadAll(response.Body) + var i interface{} + json.Unmarshal(body, &i) + gobot.Expect(t, i, "Unknown Command") +} + +func TestRobotDevice(t *testing.T) { + request, _ := http.NewRequest("GET", "/robots/Robot%201/devices/Device%201", nil) + response := httptest.NewRecorder() + a.server.ServeHTTP(response, request) + + body, _ := ioutil.ReadAll(response.Body) + var i map[string]interface{} + json.Unmarshal(body, &i) + gobot.Expect(t, i["name"].(string), "Device 1") +} + +func TestRobotDeviceCommands(t *testing.T) { + request, _ := http.NewRequest("GET", "/robots/Robot%201/devices/Device%201/commands", nil) + response := httptest.NewRecorder() + a.server.ServeHTTP(response, request) + + body, _ := ioutil.ReadAll(response.Body) + var i []string + json.Unmarshal(body, &i) + gobot.Expect(t, i, []string{"TestDriverCommand", "DriverCommand"}) +} + +func TestExecuteRobotDeviceCommand(t *testing.T) { + request, _ := http.NewRequest("GET", "/robots/Robot%201/devices/Device%201/commands/TestDriverCommand", bytes.NewBufferString(`{"name":"human"}`)) + request.Header.Add("Content-Type", "application/json") + response := httptest.NewRecorder() + a.server.ServeHTTP(response, request) + + body, _ := ioutil.ReadAll(response.Body) + var i interface{} + json.Unmarshal(body, &i) + gobot.Expect(t, i, "hello human") +} + +func TestUnknownRobotDeviceCommand(t *testing.T) { + request, _ := http.NewRequest("GET", "/robots/Robot%201/devices/Device%201/commands/DriverCommand1", bytes.NewBufferString(`{"name":"human"}`)) + request.Header.Add("Content-Type", "application/json") + response := httptest.NewRecorder() + a.server.ServeHTTP(response, request) + + body, _ := ioutil.ReadAll(response.Body) + var i interface{} + json.Unmarshal(body, &i) + gobot.Expect(t, i, "Unknown Command") +} diff --git a/gobot_suite_test.go b/gobot_suite_test.go deleted file mode 100644 index 0c8e34601..000000000 --- a/gobot_suite_test.go +++ /dev/null @@ -1,14 +0,0 @@ -package gobot - -import ( - . "github.com/onsi/ginkgo" - . "github.com/onsi/gomega" - "log" - "testing" -) - -func TestGobot(t *testing.T) { - log.SetOutput(new(null)) - RegisterFailHandler(Fail) - RunSpecs(t, "Gobot Suite") -} diff --git a/gobot_test.go b/gobot_test.go index 9b0bf7c9b..cc6c0559b 100644 --- a/gobot_test.go +++ b/gobot_test.go @@ -1,54 +1,36 @@ package gobot import ( - . "github.com/onsi/ginkgo" - . "github.com/onsi/gomega" + "log" "os" + "testing" ) -var _ = Describe("Gobot", func() { - var ( - g *Gobot - ) +var g *Gobot - BeforeEach(func() { - g = NewGobot() - g.trap = func(c chan os.Signal) { - c <- os.Interrupt - } - g.Robots = []*Robot{ - newTestRobot("Robot 1"), - newTestRobot("Robot 2"), - newTestRobot("Robot 3"), - } - g.Start() - }) +func init() { + log.SetOutput(new(null)) + g = NewGobot() + g.trap = func(c chan os.Signal) { + c <- os.Interrupt + } + g.Robots = []*Robot{ + newTestRobot("Robot 1"), + newTestRobot("Robot 2"), + newTestRobot("Robot 3"), + } +} - Context("when valid", func() { - It("should Find the specific robot", func() { - Expect(g.Robot("Robot 1").Name).To(Equal("Robot 1")) - }) - It("should return nil if Robot doesn't exist", func() { - Expect(g.Robot("Robot 4")).To(BeNil()) - }) - It("Device should return nil if device doesn't exist", func() { - Expect(g.Robot("Robot 1").Device("Device 4")).To(BeNil()) - }) - It("Device should return device", func() { - Expect(g.Robot("Robot 1").Device("Device 1").Name).To(Equal("Device 1")) - }) - It("Devices should return devices", func() { - Expect(len(g.Robot("Robot 1").Devices())).To(Equal(3)) - }) - It("Connection should return nil if connection doesn't exist", func() { - Expect(g.Robot("Robot 1").Connection("Connection 4")).To(BeNil()) - }) - It("Connection should return connection", func() { - Expect(g.Robot("Robot 1").Connection("Connection 1").Name).To(Equal("Connection 1")) - }) - It("Connections should return connections", func() { - Expect(len(g.Robot("Robot 1").Connections())).To(Equal(3)) - }) +func TestStart(t *testing.T) { + g.Start() +} - }) -}) +func TestRobot(t *testing.T) { + Expect(t, g.Robot("Robot 1").Name, "Robot 1") + Expect(t, g.Robot("Robot 4"), (*Robot)(nil)) + Expect(t, g.Robot("Robot 1").Device("Device 4"), (*device)(nil)) + Expect(t, g.Robot("Robot 1").Device("Device 1").Name, "Device 1") + Expect(t, len(g.Robot("Robot 1").Devices()), 3) + Expect(t, g.Robot("Robot 1").Connection("Connection 4"), (*connection)(nil)) + Expect(t, len(g.Robot("Robot 1").Connections()), 3) +} diff --git a/test_helper.go b/test_helper.go index 6a0f17e18..29035a790 100644 --- a/test_helper.go +++ b/test_helper.go @@ -2,6 +2,8 @@ package gobot import ( "fmt" + "reflect" + "testing" ) type testStruct struct { @@ -9,6 +11,12 @@ type testStruct struct { f float64 } +func Expect(t *testing.T, a interface{}, b interface{}) { + if !reflect.DeepEqual(a, b) { + t.Errorf("Got %v - type %v, Expected %v - type %v", a, reflect.TypeOf(a), b, reflect.TypeOf(b)) + } +} + func (t *testStruct) Hello(name string, message string) string { return fmt.Sprintf("Hello %v! %v", name, message) } @@ -84,15 +92,14 @@ func newTestRobot(name string) *Robot { r.AddCommand("robotTestFunction", func(params map[string]interface{}) interface{} { message := params["message"].(string) robot := params["robot"].(string) - fmt.Println(params) return fmt.Sprintf("hey %v, %v", robot, message) }) return r } func newTestStruct() *testStruct { - s := new(testStruct) - s.i = 10 - s.f = 0.2 - return s + return &testStruct{ + i: 10, + f: 0.2, + } } diff --git a/utils.go b/utils.go index 7428ac4a2..eb959dc95 100644 --- a/utils.go +++ b/utils.go @@ -36,8 +36,8 @@ func On(e *Event, f func(s interface{})) { } func Rand(max int) int { - rand.Seed(time.Now().UTC().UnixNano()) - return rand.Intn(max) + r := rand.New(rand.NewSource(time.Now().UTC().UnixNano())) + return r.Intn(max) } func Call(thing interface{}, method string, params ...interface{}) []reflect.Value { diff --git a/utils_test.go b/utils_test.go index 260c7d199..fcb7760be 100644 --- a/utils_test.go +++ b/utils_test.go @@ -1,77 +1,79 @@ package gobot import ( - . "github.com/onsi/ginkgo" - . "github.com/onsi/gomega" + "fmt" + "testing" "time" ) -var _ = Describe("Utils", func() { +func TestEvery(t *testing.T) { + i := 0 + Every(2*time.Millisecond, func() { + i++ + }) + time.Sleep(5 * time.Millisecond) + Expect(t, i, 2) +} + +func TestAfter(t *testing.T) { + i := 0 + After(1*time.Millisecond, func() { + i++ + }) + time.Sleep(2 * time.Millisecond) + Expect(t, i, 1) +} - var ( - testInterface interface{} - ) +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 + Expect(t, i, 1) +} - Context("when valid", func() { - It("should execute function at every interval", func() { - var i = 0 - Every(2*time.Millisecond, func() { - i++ - }) - time.Sleep(5 * time.Millisecond) - Expect(2).To(Equal(i)) - }) - It("should execute function after specific interval", func() { - var i = 0 - After(1*time.Millisecond, func() { - i = i + 1 - }) - time.Sleep(2 * time.Millisecond) - Expect(i).To(Equal(1)) - }) - It("should Publish message to channel without blocking", func() { - e := &Event{Chan: make(chan interface{}, 1)} - Publish(e, 1) - Publish(e, 2) - Publish(e, 3) - Publish(e, 4) - i := <-e.Chan - Expect(i.(int)).To(Equal(1)) - }) - It("should execute function on event", func() { - var i int - e := NewEvent() - On(e, func(data interface{}) { - i = data.(int) - }) - Publish(e, 10) - time.Sleep(1 * time.Millisecond) - 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?")) - }) +func TestOn(t *testing.T) { + var i int + e := NewEvent() + On(e, func(data interface{}) { + i = data.(int) }) -}) + Publish(e, 10) + time.Sleep(1 * time.Millisecond) + Expect(t, i, 10) +} + +func TestFromScale(t *testing.T) { + Expect(t, FromScale(5, 0, 10), 0.5) +} + +func TestToScale(t *testing.T) { + Expect(t, ToScale(500, 0, 10), 10.0) + Expect(t, ToScale(-1, 0, 10), 0.0) + Expect(t, ToScale(0.5, 0, 10), 5.0) +} + +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)) + } +} + +func TestFieldByName(t *testing.T) { + testInterface := *newTestStruct() + Expect(t, FieldByName(testInterface, "i").Int(), int64(10)) +} + +func TestFieldByNamePtr(t *testing.T) { + testInterface := newTestStruct() + Expect(t, FieldByNamePtr(testInterface, "f").Float(), 0.2) +} + +func TestCall(t *testing.T) { + testInterface := newTestStruct() + Expect(t, Call(testInterface, "Hello", "Human", "How are you?")[0].String(), "Hello Human! How are you?") +}