Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

google images tests

  • Loading branch information...
commit 1e4ae576aa6fbcade3cd7204a6102e6ddbfca4c7 1 parent 5abab75
@technoweenie technoweenie authored
View
7 src/robot.coffee
@@ -9,6 +9,7 @@ class Robot
# path - String directory full of Hubot scripts to load.
constructor: (path) ->
@listeners = []
+ @Response = Robot.Response
if path then @load path
# Public: Adds a Listener that attempts to match incoming messages based on
@@ -102,9 +103,9 @@ class Listener
# Returns nothing.
call: (message) ->
if match = message.match @regex
- @callback new Response(@robot, message, match)
+ @callback new @robot.Response(@robot, message, match)
-class Response
+class Robot.Response
# Public: Responses are sent to matching listeners. Messages know about the
# content and user that made the original message, and how to reply back to
# them.
@@ -171,6 +172,6 @@ class Response
http: (url) ->
@httpClient.create(url)
-Response.prototype.httpClient = require 'scoped-http-client'
+Robot.Response.prototype.httpClient = require 'scoped-http-client'
module.exports = Robot
View
38 test/google_images_test.coffee
@@ -0,0 +1,38 @@
+Tests = require('./tests')
+assert = require 'assert'
+helper = Tests.helper()
+mu = "http://mustachify.me/?src="
+
+require('../scripts/google-images') helper
+
+# start up a danger room for google images
+danger = Tests.danger helper, (req, res, url) ->
+ res.writeHead 200
+ res.end JSON.stringify(
+ {responseData: {results: [
+ {unescapedUrl: "(#{url.query.q})"}
+ ]}}
+ )
+
+# callbacks for when hubot sends messages
+tests = [
+ (msg) -> assert.equal "#{mu}(foo)#.png", msg
+ (msg) -> assert.equal "#{mu}(foo)#.png", msg
+ (msg) -> assert.equal "#{mu}(foo)#.png", msg
+ (msg) -> assert.equal "#{mu}(foo)#.png", msg
+ (msg) -> assert.equal "(foo)#.png", msg
+ (msg) -> assert.equal "(foo)#.png", msg
+ (msg) -> assert.equal "(foo)#.png", msg
+ (msg) -> assert.equal "(animated foo)#.png", msg
+]
+
+# run the async tests
+danger.start tests, ->
+ helper.receive 'stache me foo'
+ helper.receive 'stache foo'
+ helper.receive 'mustache me foo'
+ helper.receive 'mustache foo'
+ helper.receive 'img foo'
+ helper.receive 'image me foo'
+ helper.receive 'image foo'
+ helper.receive 'animate me foo'
View
25 test/helper.coffee
@@ -1,25 +0,0 @@
-Robot = require '../src/robot'
-
-# A programmer's best friend.
-# http://timenerdworld.files.wordpress.com/2010/12/joint-venture-s1e3_1.jpg
-class Helper extends Robot
- constructor: (path) ->
- super path
- @sent = []
-
- send: (user, strings...) ->
- strings.forEach (str) =>
- @sent.push str
- @cb?()
-
- reply: (user, strings...) ->
- strings.forEach (str) =>
- @send user, "#{user.name}: #{str}"
-
- # modified to accept a string and pass the Robot.Message to super()
- receive: (text) ->
- user = new Robot.User 1, 'helper'
- super new Robot.Message(user, text)
-
-module.exports = ->
- new Helper "#{__dirname}/scripts"
View
6 test/robot_test.coffee
@@ -1,5 +1,5 @@
assert = require 'assert'
-helper = require('./helper')()
+helper = require('./tests').helper()
server = require('http').createServer (req, res) ->
res.writeHead 200
res.end "fetched"
@@ -21,9 +21,9 @@ server.listen 9001, ->
assert.ok helper.sent[2].match(/^(1|2)$/)
# set a callback for when the next message is replied to
- helper.cb = ->
+ helper.cb = (msg) ->
assert.equal 4, helper.sent.length
- assert.equal 'fetched', helper.sent[3]
+ assert.equal 'fetched', msg
server.close()
helper.receive 'http'
View
53 test/tests.coffee
@@ -0,0 +1,53 @@
+Robot = require '../src/robot'
+Url = require 'url'
+
+# A programmer's best friend.
+# http://timenerdworld.files.wordpress.com/2010/12/joint-venture-s1e3_1.jpg
+#
+# Instantiates a test-only Robot that sends messages to an optional callback
+# and a @sent array.
+exports.helper = ->
+ new Helper "#{__dirname}/scripts"
+
+# Training facility built for the Hubot scripts. Starts up a web server to
+# emulate backends (like google images) so we can test that the response
+# parsing code functions.
+exports.danger = (helper, cb) ->
+ server = require('http').createServer (req, res) ->
+ url = Url.parse req.url, true
+ cb req, res, url
+
+ server.start = (tests, cb) ->
+ server.listen 9001, ->
+ helper.cb = (messages...) ->
+ tests.shift() messages...
+ server.close() if tests.length == 0
+
+ cb()
+
+ server
+
+class Helper extends Robot
+ constructor: (path) ->
+ super path
+ @sent = []
+ @Response = Helper.Response
+
+ send: (user, strings...) ->
+ strings.forEach (str) =>
+ @sent.push str
+ @cb? strings...
+
+ reply: (user, strings...) ->
+ strings.forEach (str) =>
+ @send user, "#{user.name}: #{str}"
+
+ # modified to accept a string and pass the Robot.Message to super()
+ receive: (text) ->
+ user = new Robot.User 1, 'helper'
+ super new Robot.Message(user, text)
+
+class Helper.Response extends Robot.Response
+ http: (url) ->
+ super(url).host('127.0.0.1').port(9001)
+

0 comments on commit 1e4ae57

Please sign in to comment.
Something went wrong with that request. Please try again.