Skip to content

Commit

Permalink
Update test formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
zankich committed Jun 13, 2014
1 parent a1f0bd2 commit aed6b39
Show file tree
Hide file tree
Showing 32 changed files with 506 additions and 443 deletions.
32 changes: 18 additions & 14 deletions api/api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,30 +11,24 @@ import (
"testing"
)

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)
func initTestAPI() *api {
log.SetOutput(gobot.NullReadWriteCloser{})
g := gobot.NewGobot()
a := NewAPI(g)
a.start = func(m *api) {}
a.Start()

m.Robots = []*gobot.Robot{
g.Robots = []*gobot.Robot{
gobot.NewTestRobot("Robot 1"),
gobot.NewTestRobot("Robot 2"),
gobot.NewTestRobot("Robot 3"),
}

return a
}

func TestRobots(t *testing.T) {
a := initTestAPI()
request, _ := http.NewRequest("GET", "/robots", nil)
response := httptest.NewRecorder()
a.server.ServeHTTP(response, request)
Expand All @@ -46,6 +40,7 @@ func TestRobots(t *testing.T) {
}

func TestRobot(t *testing.T) {
a := initTestAPI()
request, _ := http.NewRequest("GET", "/robots/Robot%201", nil)
response := httptest.NewRecorder()
a.server.ServeHTTP(response, request)
Expand All @@ -57,6 +52,7 @@ func TestRobot(t *testing.T) {
}

func TestRobotDevices(t *testing.T) {
a := initTestAPI()
request, _ := http.NewRequest("GET", "/robots/Robot%201/devices", nil)
response := httptest.NewRecorder()
a.server.ServeHTTP(response, request)
Expand All @@ -68,6 +64,7 @@ func TestRobotDevices(t *testing.T) {
}

func TestRobotCommands(t *testing.T) {
a := initTestAPI()
request, _ := http.NewRequest("GET", "/robots/Robot%201/commands", nil)
response := httptest.NewRecorder()
a.server.ServeHTTP(response, request)
Expand All @@ -77,7 +74,9 @@ func TestRobotCommands(t *testing.T) {
json.Unmarshal(body, &i)
gobot.Expect(t, i, []string{"robotTestFunction"})
}

func TestExecuteRobotCommand(t *testing.T) {
a := initTestAPI()
request, _ := http.NewRequest("GET", "/robots/Robot%201/commands/robotTestFunction", bytes.NewBufferString(`{"message":"Beep Boop"}`))
request.Header.Add("Content-Type", "application/json")
response := httptest.NewRecorder()
Expand All @@ -90,6 +89,7 @@ func TestExecuteRobotCommand(t *testing.T) {
}

func TestUnknownRobotCommand(t *testing.T) {
a := initTestAPI()
request, _ := http.NewRequest("GET", "/robots/Robot%201/commands/robotTestFuntion1", bytes.NewBufferString(`{"message":"Beep Boop"}`))
request.Header.Add("Content-Type", "application/json")
response := httptest.NewRecorder()
Expand All @@ -102,6 +102,7 @@ func TestUnknownRobotCommand(t *testing.T) {
}

func TestRobotDevice(t *testing.T) {
a := initTestAPI()
request, _ := http.NewRequest("GET", "/robots/Robot%201/devices/Device%201", nil)
response := httptest.NewRecorder()
a.server.ServeHTTP(response, request)
Expand All @@ -113,6 +114,7 @@ func TestRobotDevice(t *testing.T) {
}

func TestRobotDeviceCommands(t *testing.T) {
a := initTestAPI()
request, _ := http.NewRequest("GET", "/robots/Robot%201/devices/Device%201/commands", nil)
response := httptest.NewRecorder()
a.server.ServeHTTP(response, request)
Expand All @@ -124,6 +126,7 @@ func TestRobotDeviceCommands(t *testing.T) {
}

func TestExecuteRobotDeviceCommand(t *testing.T) {
a := initTestAPI()
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()
Expand All @@ -136,6 +139,7 @@ func TestExecuteRobotDeviceCommand(t *testing.T) {
}

func TestUnknownRobotDeviceCommand(t *testing.T) {
a := initTestAPI()
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()
Expand Down
15 changes: 8 additions & 7 deletions gobot_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,9 @@ import (
"testing"
)

var g *Gobot

func init() {
log.SetOutput(new(Null))
g = NewGobot()
func initTestGobot() *Gobot {
log.SetOutput(&NullReadWriteCloser{})
g := NewGobot()
g.trap = func(c chan os.Signal) {
c <- os.Interrupt
}
Expand All @@ -19,13 +17,16 @@ func init() {
NewTestRobot("Robot 2"),
NewTestRobot("Robot 3"),
}
return g
}

func TestStart(t *testing.T) {
func TestGobotStart(t *testing.T) {
g := initTestGobot()
g.Start()
}

func TestRobot(t *testing.T) {
func TestGobotRobot(t *testing.T) {
g := initTestGobot()
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))
Expand Down
29 changes: 16 additions & 13 deletions platforms/ardrone/ardrone_adaptor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,32 +5,35 @@ import (
"testing"
)

var adaptor *ArdroneAdaptor
var d *testDrone

func init() {
d = &testDrone{}
adaptor = NewArdroneAdaptor("drone")
adaptor.connect = func(a *ArdroneAdaptor) {
func initTestArdroneAdaptor() (*ArdroneAdaptor, *testDrone) {
d := &testDrone{}
a := NewArdroneAdaptor("drone")
a.connect = func(a *ArdroneAdaptor) {
a.drone = d
}
return a, d
}

func TestFinalize(t *testing.T) {
gobot.Expect(t, adaptor.Finalize(), true)
a, _ := initTestArdroneAdaptor()
gobot.Expect(t, a.Finalize(), true)
}
func TestConnect(t *testing.T) {
gobot.Expect(t, adaptor.Connect(), true)
a, _ := initTestArdroneAdaptor()
gobot.Expect(t, a.Connect(), true)
}
func TestDisconnect(t *testing.T) {
gobot.Expect(t, adaptor.Disconnect(), true)
a, _ := initTestArdroneAdaptor()
gobot.Expect(t, a.Disconnect(), true)
}

func TestReconnect(t *testing.T) {
gobot.Expect(t, adaptor.Reconnect(), true)
a, _ := initTestArdroneAdaptor()
gobot.Expect(t, a.Reconnect(), true)
}

func TestDrone(t *testing.T) {
adaptor.Connect()
gobot.Expect(t, adaptor.Drone(), d)
a, d := initTestArdroneAdaptor()
a.Connect()
gobot.Expect(t, a.Drone(), d)
}
79 changes: 46 additions & 33 deletions platforms/ardrone/ardrone_driver_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,63 +5,76 @@ import (
"testing"
)

var driver *ArdroneDriver

func init() {
adaptor := NewArdroneAdaptor("drone")
adaptor.connect = func(a *ArdroneAdaptor) {
func initTestArdroneDriver() *ArdroneDriver {
a := NewArdroneAdaptor("drone")
a.connect = func(a *ArdroneAdaptor) {
a.drone = &testDrone{}
}
driver = NewArdroneDriver(adaptor, "drone")
adaptor.Connect()
d := NewArdroneDriver(a, "drone")
a.Connect()
return d
}

func TestStart(t *testing.T) {
gobot.Expect(t, driver.Start(), true)
func TestArdroneDriverStart(t *testing.T) {
d := initTestArdroneDriver()
gobot.Expect(t, d.Start(), true)
}

func TestHalt(t *testing.T) {
gobot.Expect(t, driver.Halt(), true)
func TestArdroneDriverHalt(t *testing.T) {
d := initTestArdroneDriver()
gobot.Expect(t, d.Halt(), true)
}
func TestTakeOff(t *testing.T) {
driver.TakeOff()
func TestArdroneDriverTakeOff(t *testing.T) {
d := initTestArdroneDriver()
d.TakeOff()
}

func TestLand(t *testing.T) {
driver.Land()
func TestArdroneDriverand(t *testing.T) {
d := initTestArdroneDriver()
d.Land()
}
func TestUp(t *testing.T) {
driver.Up(1)

func TestArdroneDriverUp(t *testing.T) {
d := initTestArdroneDriver()
d.Up(1)
}

func TestDown(t *testing.T) {
driver.Down(1)
func TestArdroneDriverDown(t *testing.T) {
d := initTestArdroneDriver()
d.Down(1)
}

func TestLeft(t *testing.T) {
driver.Left(1)
func TestArdroneDriverLeft(t *testing.T) {
d := initTestArdroneDriver()
d.Left(1)
}

func TestRight(t *testing.T) {
driver.Right(1)
func TestArdroneDriverRight(t *testing.T) {
d := initTestArdroneDriver()
d.Right(1)
}

func TestForward(t *testing.T) {
driver.Forward(1)
func TestArdroneDriverForward(t *testing.T) {
d := initTestArdroneDriver()
d.Forward(1)
}

func TestBackward(t *testing.T) {
driver.Backward(1)
func TestArdroneDriverackward(t *testing.T) {
d := initTestArdroneDriver()
d.Backward(1)
}

func TestClockwise(t *testing.T) {
driver.Clockwise(1)
func TestArdroneDriverClockwise(t *testing.T) {
d := initTestArdroneDriver()
d.Clockwise(1)
}

func TestCounterClockwise(t *testing.T) {
driver.CounterClockwise(1)
func TestArdroneDriverCounterClockwise(t *testing.T) {
d := initTestArdroneDriver()
d.CounterClockwise(1)
}

func TestHover(t *testing.T) {
driver.Hover()
func TestArdroneDriverHover(t *testing.T) {
d := initTestArdroneDriver()
d.Hover()
}
26 changes: 14 additions & 12 deletions platforms/beaglebone/beaglebone_adaptor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,23 @@ import (
"testing"
)

var b *BeagleboneAdaptor

func init() {
b = NewBeagleboneAdaptor("bot")
func initTestBeagleboneAdaptor() *BeagleboneAdaptor {
return NewBeagleboneAdaptor("bot")
}

func TestFinalize(t *testing.T) {
gobot.Expect(t, b.Finalize(), true)
func TestBeagleboneAdaptorFinalize(t *testing.T) {
a := initTestBeagleboneAdaptor()
gobot.Expect(t, a.Finalize(), true)
}
func TestConnect(t *testing.T) {
gobot.Expect(t, b.Connect(), true)
func TestBeagleboneAdaptorConnect(t *testing.T) {
a := initTestBeagleboneAdaptor()
gobot.Expect(t, a.Connect(), true)
}
func TestDisconnect(t *testing.T) {
gobot.Expect(t, b.Disconnect(), true)
func TestBeagleboneAdaptorDisconnect(t *testing.T) {
a := initTestBeagleboneAdaptor()
gobot.Expect(t, a.Disconnect(), true)
}
func TestReconnect(t *testing.T) {
gobot.Expect(t, b.Reconnect(), true)
func TestBeagleboneAdaptorReconnect(t *testing.T) {
a := initTestBeagleboneAdaptor()
gobot.Expect(t, a.Reconnect(), true)
}
32 changes: 19 additions & 13 deletions platforms/digispark/digispark_adaptor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,22 +5,28 @@ import (
"testing"
)

var d *DigisparkAdaptor

func init() {
d = NewDigisparkAdaptor("bot")
d.connect = func(d *DigisparkAdaptor) {}
func initTestDigisparkAdaptor() *DigisparkAdaptor {
a := NewDigisparkAdaptor("bot")
a.connect = func(a *DigisparkAdaptor) {}
return a
}

func TestFinalize(t *testing.T) {
gobot.Expect(t, d.Finalize(), true)
func TestDigisparkAdaptorFinalize(t *testing.T) {
a := initTestDigisparkAdaptor()
gobot.Expect(t, a.Finalize(), true)
}
func TestConnect(t *testing.T) {
gobot.Expect(t, d.Connect(), true)

func TestDigisparkAdaptorConnect(t *testing.T) {
a := initTestDigisparkAdaptor()
gobot.Expect(t, a.Connect(), true)
}
func TestDisconnect(t *testing.T) {
gobot.Expect(t, d.Disconnect(), true)

func TestDigisparkAdaptorDisconnect(t *testing.T) {
a := initTestDigisparkAdaptor()
gobot.Expect(t, a.Disconnect(), true)
}
func TestReconnect(t *testing.T) {
gobot.Expect(t, d.Reconnect(), true)

func TestDigisparkAdaptorReconnect(t *testing.T) {
a := initTestDigisparkAdaptor()
gobot.Expect(t, a.Reconnect(), true)
}
Loading

0 comments on commit aed6b39

Please sign in to comment.