Skip to content

Commit e494b9f

Browse files
committed
Refactor to use gobottest test helpers
1 parent 96134bf commit e494b9f

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

55 files changed

+701
-736
lines changed

api/api_test.go

+41-40
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import (
1212
"time"
1313

1414
"github.com/hybridgroup/gobot"
15+
"github.com/hybridgroup/gobot/gobottest"
1516
)
1617

1718
func initTestAPI() *API {
@@ -39,22 +40,22 @@ func TestRobeaux(t *testing.T) {
3940
request, _ := http.NewRequest("GET", "/index.html", nil)
4041
response := httptest.NewRecorder()
4142
a.ServeHTTP(response, request)
42-
gobot.Assert(t, response.Code, 200)
43+
gobottest.Assert(t, response.Code, 200)
4344
// js assets
4445
request, _ = http.NewRequest("GET", "/js/script.js", nil)
4546
response = httptest.NewRecorder()
4647
a.ServeHTTP(response, request)
47-
gobot.Assert(t, response.Code, 200)
48+
gobottest.Assert(t, response.Code, 200)
4849
// css assets
4950
request, _ = http.NewRequest("GET", "/css/application.css", nil)
5051
response = httptest.NewRecorder()
5152
a.ServeHTTP(response, request)
52-
gobot.Assert(t, response.Code, 200)
53+
gobottest.Assert(t, response.Code, 200)
5354
// unknown asset
5455
request, _ = http.NewRequest("GET", "/js/fake/file.js", nil)
5556
response = httptest.NewRecorder()
5657
a.ServeHTTP(response, request)
57-
gobot.Assert(t, response.Code, 404)
58+
gobottest.Assert(t, response.Code, 404)
5859
}
5960

6061
func TestIndex(t *testing.T) {
@@ -64,8 +65,8 @@ func TestIndex(t *testing.T) {
6465

6566
a.ServeHTTP(response, request)
6667

67-
gobot.Assert(t, http.StatusMovedPermanently, response.Code)
68-
gobot.Assert(t, "/index.html", response.HeaderMap["Location"][0])
68+
gobottest.Assert(t, http.StatusMovedPermanently, response.Code)
69+
gobottest.Assert(t, "/index.html", response.HeaderMap["Location"][0])
6970
}
7071

7172
func TestMcp(t *testing.T) {
@@ -76,8 +77,8 @@ func TestMcp(t *testing.T) {
7677

7778
var body map[string]interface{}
7879
json.NewDecoder(response.Body).Decode(&body)
79-
gobot.Refute(t, body["MCP"].(map[string]interface{})["robots"], nil)
80-
gobot.Refute(t, body["MCP"].(map[string]interface{})["commands"], nil)
80+
gobottest.Refute(t, body["MCP"].(map[string]interface{})["robots"], nil)
81+
gobottest.Refute(t, body["MCP"].(map[string]interface{})["commands"], nil)
8182
}
8283

8384
func TestMcpCommands(t *testing.T) {
@@ -88,7 +89,7 @@ func TestMcpCommands(t *testing.T) {
8889

8990
var body map[string]interface{}
9091
json.NewDecoder(response.Body).Decode(&body)
91-
gobot.Assert(t, body["commands"], []interface{}{"TestFunction"})
92+
gobottest.Assert(t, body["commands"], []interface{}{"TestFunction"})
9293
}
9394

9495
func TestExecuteMcpCommand(t *testing.T) {
@@ -105,7 +106,7 @@ func TestExecuteMcpCommand(t *testing.T) {
105106
a.ServeHTTP(response, request)
106107

107108
json.NewDecoder(response.Body).Decode(&body)
108-
gobot.Assert(t, body.(map[string]interface{})["result"], "hey Beep Boop")
109+
gobottest.Assert(t, body.(map[string]interface{})["result"], "hey Beep Boop")
109110

110111
// unknown command
111112
request, _ = http.NewRequest("GET",
@@ -117,7 +118,7 @@ func TestExecuteMcpCommand(t *testing.T) {
117118
a.ServeHTTP(response, request)
118119

119120
json.NewDecoder(response.Body).Decode(&body)
120-
gobot.Assert(t, body.(map[string]interface{})["error"], "Unknown Command")
121+
gobottest.Assert(t, body.(map[string]interface{})["error"], "Unknown Command")
121122
}
122123

123124
func TestRobots(t *testing.T) {
@@ -128,7 +129,7 @@ func TestRobots(t *testing.T) {
128129

129130
var body map[string]interface{}
130131
json.NewDecoder(response.Body).Decode(&body)
131-
gobot.Assert(t, len(body["robots"].([]interface{})), 3)
132+
gobottest.Assert(t, len(body["robots"].([]interface{})), 3)
132133
}
133134

134135
func TestRobot(t *testing.T) {
@@ -141,14 +142,14 @@ func TestRobot(t *testing.T) {
141142

142143
var body map[string]interface{}
143144
json.NewDecoder(response.Body).Decode(&body)
144-
gobot.Assert(t, body["robot"].(map[string]interface{})["name"].(string), "Robot1")
145+
gobottest.Assert(t, body["robot"].(map[string]interface{})["name"].(string), "Robot1")
145146

146147
// unknown robot
147148
request, _ = http.NewRequest("GET", "/api/robots/UnknownRobot1", nil)
148149
a.ServeHTTP(response, request)
149150

150151
json.NewDecoder(response.Body).Decode(&body)
151-
gobot.Assert(t, body["error"], "No Robot found with the name UnknownRobot1")
152+
gobottest.Assert(t, body["error"], "No Robot found with the name UnknownRobot1")
152153
}
153154

154155
func TestRobotDevices(t *testing.T) {
@@ -161,14 +162,14 @@ func TestRobotDevices(t *testing.T) {
161162

162163
var body map[string]interface{}
163164
json.NewDecoder(response.Body).Decode(&body)
164-
gobot.Assert(t, len(body["devices"].([]interface{})), 3)
165+
gobottest.Assert(t, len(body["devices"].([]interface{})), 3)
165166

166167
// unknown robot
167168
request, _ = http.NewRequest("GET", "/api/robots/UnknownRobot1/devices", nil)
168169
a.ServeHTTP(response, request)
169170

170171
json.NewDecoder(response.Body).Decode(&body)
171-
gobot.Assert(t, body["error"], "No Robot found with the name UnknownRobot1")
172+
gobottest.Assert(t, body["error"], "No Robot found with the name UnknownRobot1")
172173
}
173174

174175
func TestRobotCommands(t *testing.T) {
@@ -181,14 +182,14 @@ func TestRobotCommands(t *testing.T) {
181182

182183
var body map[string]interface{}
183184
json.NewDecoder(response.Body).Decode(&body)
184-
gobot.Assert(t, body["commands"], []interface{}{"robotTestFunction"})
185+
gobottest.Assert(t, body["commands"], []interface{}{"robotTestFunction"})
185186

186187
// unknown robot
187188
request, _ = http.NewRequest("GET", "/api/robots/UnknownRobot1/commands", nil)
188189
a.ServeHTTP(response, request)
189190

190191
json.NewDecoder(response.Body).Decode(&body)
191-
gobot.Assert(t, body["error"], "No Robot found with the name UnknownRobot1")
192+
gobottest.Assert(t, body["error"], "No Robot found with the name UnknownRobot1")
192193
}
193194

194195
func TestExecuteRobotCommand(t *testing.T) {
@@ -204,7 +205,7 @@ func TestExecuteRobotCommand(t *testing.T) {
204205
a.ServeHTTP(response, request)
205206

206207
json.NewDecoder(response.Body).Decode(&body)
207-
gobot.Assert(t, body.(map[string]interface{})["result"], "hey Robot1, Beep Boop")
208+
gobottest.Assert(t, body.(map[string]interface{})["result"], "hey Robot1, Beep Boop")
208209

209210
// unknown command
210211
request, _ = http.NewRequest("GET",
@@ -216,7 +217,7 @@ func TestExecuteRobotCommand(t *testing.T) {
216217
a.ServeHTTP(response, request)
217218

218219
json.NewDecoder(response.Body).Decode(&body)
219-
gobot.Assert(t, body.(map[string]interface{})["error"], "Unknown Command")
220+
gobottest.Assert(t, body.(map[string]interface{})["error"], "Unknown Command")
220221

221222
// uknown robot
222223
request, _ = http.NewRequest("GET",
@@ -227,7 +228,7 @@ func TestExecuteRobotCommand(t *testing.T) {
227228
a.ServeHTTP(response, request)
228229

229230
json.NewDecoder(response.Body).Decode(&body)
230-
gobot.Assert(t, body.(map[string]interface{})["error"], "No Robot found with the name UnknownRobot1")
231+
gobottest.Assert(t, body.(map[string]interface{})["error"], "No Robot found with the name UnknownRobot1")
231232
}
232233

233234
func TestRobotDevice(t *testing.T) {
@@ -243,15 +244,15 @@ func TestRobotDevice(t *testing.T) {
243244

244245
var body map[string]interface{}
245246
json.NewDecoder(response.Body).Decode(&body)
246-
gobot.Assert(t, body["device"].(map[string]interface{})["name"].(string), "Device1")
247+
gobottest.Assert(t, body["device"].(map[string]interface{})["name"].(string), "Device1")
247248

248249
// unknown device
249250
request, _ = http.NewRequest("GET",
250251
"/api/robots/Robot1/devices/UnknownDevice1", nil)
251252
a.ServeHTTP(response, request)
252253

253254
json.NewDecoder(response.Body).Decode(&body)
254-
gobot.Assert(t, body["error"], "No Device found with the name UnknownDevice1")
255+
gobottest.Assert(t, body["error"], "No Device found with the name UnknownDevice1")
255256
}
256257

257258
func TestRobotDeviceCommands(t *testing.T) {
@@ -267,7 +268,7 @@ func TestRobotDeviceCommands(t *testing.T) {
267268

268269
var body map[string]interface{}
269270
json.NewDecoder(response.Body).Decode(&body)
270-
gobot.Assert(t, len(body["commands"].([]interface{})), 2)
271+
gobottest.Assert(t, len(body["commands"].([]interface{})), 2)
271272

272273
// unknown device
273274
request, _ = http.NewRequest("GET",
@@ -276,7 +277,7 @@ func TestRobotDeviceCommands(t *testing.T) {
276277
)
277278
a.ServeHTTP(response, request)
278279
json.NewDecoder(response.Body).Decode(&body)
279-
gobot.Assert(t, body["error"], "No Device found with the name UnknownDevice1")
280+
gobottest.Assert(t, body["error"], "No Device found with the name UnknownDevice1")
280281
}
281282

282283
func TestExecuteRobotDeviceCommand(t *testing.T) {
@@ -293,7 +294,7 @@ func TestExecuteRobotDeviceCommand(t *testing.T) {
293294
a.ServeHTTP(response, request)
294295

295296
json.NewDecoder(response.Body).Decode(&body)
296-
gobot.Assert(t, body.(map[string]interface{})["result"].(string), "hello human")
297+
gobottest.Assert(t, body.(map[string]interface{})["result"].(string), "hello human")
297298

298299
// unknown command
299300
request, _ = http.NewRequest("GET",
@@ -305,7 +306,7 @@ func TestExecuteRobotDeviceCommand(t *testing.T) {
305306
a.ServeHTTP(response, request)
306307

307308
json.NewDecoder(response.Body).Decode(&body)
308-
gobot.Assert(t, body.(map[string]interface{})["error"], "Unknown Command")
309+
gobottest.Assert(t, body.(map[string]interface{})["error"], "Unknown Command")
309310

310311
// unknown device
311312
request, _ = http.NewRequest("GET",
@@ -316,7 +317,7 @@ func TestExecuteRobotDeviceCommand(t *testing.T) {
316317
a.ServeHTTP(response, request)
317318

318319
json.NewDecoder(response.Body).Decode(&body)
319-
gobot.Assert(t, body.(map[string]interface{})["error"], "No Device found with the name UnknownDevice1")
320+
gobottest.Assert(t, body.(map[string]interface{})["error"], "No Device found with the name UnknownDevice1")
320321

321322
}
322323

@@ -330,14 +331,14 @@ func TestRobotConnections(t *testing.T) {
330331

331332
var body map[string]interface{}
332333
json.NewDecoder(response.Body).Decode(&body)
333-
gobot.Assert(t, len(body["connections"].([]interface{})), 3)
334+
gobottest.Assert(t, len(body["connections"].([]interface{})), 3)
334335

335336
// unknown robot
336337
request, _ = http.NewRequest("GET", "/api/robots/UnknownRobot1/connections", nil)
337338
a.ServeHTTP(response, request)
338339

339340
json.NewDecoder(response.Body).Decode(&body)
340-
gobot.Assert(t, body["error"], "No Robot found with the name UnknownRobot1")
341+
gobottest.Assert(t, body["error"], "No Robot found with the name UnknownRobot1")
341342
}
342343

343344
func TestRobotConnection(t *testing.T) {
@@ -353,7 +354,7 @@ func TestRobotConnection(t *testing.T) {
353354

354355
var body map[string]interface{}
355356
json.NewDecoder(response.Body).Decode(&body)
356-
gobot.Assert(t, body["connection"].(map[string]interface{})["name"].(string), "Connection1")
357+
gobottest.Assert(t, body["connection"].(map[string]interface{})["name"].(string), "Connection1")
357358

358359
// unknown connection
359360
request, _ = http.NewRequest("GET",
@@ -362,7 +363,7 @@ func TestRobotConnection(t *testing.T) {
362363
)
363364
a.ServeHTTP(response, request)
364365
json.NewDecoder(response.Body).Decode(&body)
365-
gobot.Assert(t, body["error"], "No Connection found with the name UnknownConnection1")
366+
gobottest.Assert(t, body["error"], "No Connection found with the name UnknownConnection1")
366367
}
367368

368369
func TestRobotDeviceEvent(t *testing.T) {
@@ -396,7 +397,7 @@ func TestRobotDeviceEvent(t *testing.T) {
396397
case resp := <-respc:
397398
reader := bufio.NewReader(resp.Body)
398399
data, _ := reader.ReadString('\n')
399-
gobot.Assert(t, data, "data: \"event-data\"\n")
400+
gobottest.Assert(t, data, "data: \"event-data\"\n")
400401
done = true
401402
case <-timer.C:
402403
t.Error("Not receiving data")
@@ -411,7 +412,7 @@ func TestRobotDeviceEvent(t *testing.T) {
411412

412413
var body map[string]interface{}
413414
json.NewDecoder(response.Body).Decode(&body)
414-
gobot.Assert(t, body["error"], "No Event found with the name UnknownEvent")
415+
gobottest.Assert(t, body["error"], "No Event found with the name UnknownEvent")
415416
}
416417

417418
func TestAPIRouter(t *testing.T) {
@@ -421,35 +422,35 @@ func TestAPIRouter(t *testing.T) {
421422
request, _ := http.NewRequest("HEAD", "/test", nil)
422423
response := httptest.NewRecorder()
423424
a.ServeHTTP(response, request)
424-
gobot.Assert(t, response.Code, 200)
425+
gobottest.Assert(t, response.Code, 200)
425426

426427
a.Get("/test", func(res http.ResponseWriter, req *http.Request) {})
427428
request, _ = http.NewRequest("GET", "/test", nil)
428429
response = httptest.NewRecorder()
429430
a.ServeHTTP(response, request)
430-
gobot.Assert(t, response.Code, 200)
431+
gobottest.Assert(t, response.Code, 200)
431432

432433
a.Post("/test", func(res http.ResponseWriter, req *http.Request) {})
433434
request, _ = http.NewRequest("POST", "/test", nil)
434435
response = httptest.NewRecorder()
435436
a.ServeHTTP(response, request)
436-
gobot.Assert(t, response.Code, 200)
437+
gobottest.Assert(t, response.Code, 200)
437438

438439
a.Put("/test", func(res http.ResponseWriter, req *http.Request) {})
439440
request, _ = http.NewRequest("PUT", "/test", nil)
440441
response = httptest.NewRecorder()
441442
a.ServeHTTP(response, request)
442-
gobot.Assert(t, response.Code, 200)
443+
gobottest.Assert(t, response.Code, 200)
443444

444445
a.Delete("/test", func(res http.ResponseWriter, req *http.Request) {})
445446
request, _ = http.NewRequest("DELETE", "/test", nil)
446447
response = httptest.NewRecorder()
447448
a.ServeHTTP(response, request)
448-
gobot.Assert(t, response.Code, 200)
449+
gobottest.Assert(t, response.Code, 200)
449450

450451
a.Options("/test", func(res http.ResponseWriter, req *http.Request) {})
451452
request, _ = http.NewRequest("OPTIONS", "/test", nil)
452453
response = httptest.NewRecorder()
453454
a.ServeHTTP(response, request)
454-
gobot.Assert(t, response.Code, 200)
455+
gobottest.Assert(t, response.Code, 200)
455456
}

api/basic_auth_test.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import (
55
"net/http/httptest"
66
"testing"
77

8-
"github.com/hybridgroup/gobot"
8+
"github.com/hybridgroup/gobot/gobottest"
99
)
1010

1111
func TestBasicAuth(t *testing.T) {
@@ -17,11 +17,11 @@ func TestBasicAuth(t *testing.T) {
1717
request.SetBasicAuth("admin", "password")
1818
response := httptest.NewRecorder()
1919
a.ServeHTTP(response, request)
20-
gobot.Assert(t, response.Code, 200)
20+
gobottest.Assert(t, response.Code, 200)
2121

2222
request, _ = http.NewRequest("GET", "/api/", nil)
2323
request.SetBasicAuth("admin", "wrongPassword")
2424
response = httptest.NewRecorder()
2525
a.ServeHTTP(response, request)
26-
gobot.Assert(t, response.Code, 401)
26+
gobottest.Assert(t, response.Code, 401)
2727
}

0 commit comments

Comments
 (0)