Permalink
Browse files

WIP Renamed plugins, more tests

  • Loading branch information...
1 parent 7f3089a commit 7e9060e0a10b117047d2d85f8703f15b3c1484a3 @agnoster committed Oct 10, 2012
View
@@ -0,0 +1,13 @@
+TESTS=$(shell find test -name "*.test.js")
+MOCHA='./node_modules/.bin/mocha'
+
+spec:
+ $(MOCHA) --reporter spec $(TESTS)
+
+tap:
+ $(MOCHA) --reporter tap $(TESTS)
+
+test:
+ $(MOCHA) $(TESTS)
+
+.PHONY: test spec
View
@@ -42,9 +42,13 @@ function literapi(options) {
literapi.withDefaultPlugins = function (options) {
return (literapi(options)
.use(literapi.readfile)
- .use(literapi.markdown)
+ .use(literapi.markdown_parse)
+ .use(literapi.http_parse)
+ .use(literapi.http_request({ root: options.root }))
+ .use(literapi.http_response)
.use(literapi.debug)
- .use(literapi.markdown_output))
+ .use(literapi.markdown_serialize)
+ )
}
literapi.__defineGetter__('version', function() {
View
@@ -1,11 +1,12 @@
// Gently, see https://github.com/felixge/node-gently#gentlyhijackrealrequire
if (global.GENTLY) require = GENTLY.hijack(require)
+/**
+ * The debug plugin simply outputs the information on hand about each example
+ */
module.exports = {
"examples": function debug(el, next) {
- console.log('*** DEBUG ***')
console.log(el)
- console.log('*** DEBUG ***')
next()
}
}
@@ -1,7 +1,7 @@
// Gently, see https://github.com/felixge/node-gently#gentlyhijackrealrequire
if (global.GENTLY) require = GENTLY.hijack(require)
-var request = require('request')
+var debug = require('debug')('literapi:plugins:http_parse')
var requestRegex = /^(GET|PUT|POST|PATCH|DELETE|HEAD|OPTIONS|CONNECT)\s+(\/\S*)/
var responseRegex = /^([1-5]\d\d)\s*(.+)?\n/
@@ -28,21 +28,20 @@ function parse(raw) {
params.title = headers.shift()
params.headers = parseHeaders(headers)
params.body = parts.shift()
- params.remainder = parts
if ((match = requestRegex.exec(params.title))) {
params.request = true
params.method = match[1]
params.path = match[2]
+ return params
}
if ((match = responseRegex.exec(params.title))) {
params.response = true
params.code = match[1]
params.statusText = match[2]
+ return params
}
-
- return params
}
/* Internal: parse HTTP headers
@@ -65,55 +64,21 @@ function parseHeaders(headersArray) {
for (var i = 0; i < headersArray.length; i++) {
if (!(match = /([^:]+):[ \t]+(.*)/.exec(headersArray[i]))) continue
- if (sub)
- headers[match[1].toLowerCase()] = doc.substitute(match[2])
- else
- headers[match[1].toLowerCase()] = match[2]
+ headers[match[1].toLowerCase()] = match[2]
}
return headers
}
-function make_request(params, cb) {
-
- var options =
- { uri: params.uri
- , method: params.method
- , headers: params.headers
- }
+module.exports.examples = function http_parse(example, next) {
- if (params.body) options.body = params.body
- else options.headers['content-length'] = 0
-
- request(options, function (error, response, body) {
- cb(error, response)
- })
-}
-
-function http(options, example, next) {
+ if (!example.text) return next()
var params = parse(example.text)
- if (params.request) {
- params.uri = options.root + params.path
- make_request(options, params, function(err, response) {
- if (err) return next(err)
-
- example.context.response = response
- next()
- })
- } else if (params.response) {
- // match the response here
- next()
- } else {
- next()
+ if (params) {
+ example.http = params
}
-}
-module.exports = function(options) {
- return {
- examples: function http(example, next) {
- return http(options, example, next)
- }
- }
+ next()
}
@@ -0,0 +1,58 @@
+// Gently, see https://github.com/felixge/node-gently#gentlyhijackrealrequire
+if (global.GENTLY) require = GENTLY.hijack(require)
+
+var request = require('request')
+ , interpolate = require('../interpolate')
+
+/**
+ * Internal: actually make the request described by `params`
+ */
+function make_request(params, cb) {
+
+ var options =
+ { uri: params.uri
+ , method: params.method
+ , headers: params.headers
+ }
+
+ if (params.body) options.body = params.body
+ else options.headers['content-length'] = 0
+
+ request(options, function (error, response, body) {
+ cb(error, response)
+ })
+}
+
+/**
+ * Internal: interpolate the values from `context` into the `params` struct
+ */
+function interpolateRequest(params, context) {
+
+ params.body = interpolate(params.body, context, { json: true })
+ params.path = interpolate(params.body, context)
+ for (var key in params.headers) {
+ params.headers[key] = interpolate(params.headers[key], context)
+ }
+ return params
+}
+
+module.exports = function(options) {
+
+ function http_request(example, next) {
+
+ var params = example.http
+ if (!params || !params.request) return next()
+
+ params.uri = options.root + params.path
+ make_request(params, function(err, response) {
+ if (err) return next(err)
+
+ example.response = response
+ next()
+ })
+ }
+
+ return {
+ examples: http_request
+ }
+}
@@ -0,0 +1,12 @@
+// Gently, see https://github.com/felixge/node-gently#gentlyhijackrealrequire
+if (global.GENTLY) require = GENTLY.hijack(require)
+
+function http_response(example, next) {
+
+ if (!example.http || !example.http.response) return next()
+
+ function assertion(name, fn) {
+ }
+}
+
+module.exports.examples = http_response
@@ -2,7 +2,7 @@
if (global.GENTLY) require = GENTLY.hijack(require)
var Markdownstream = require('markdownstream')
- , debug = require('debug')('literapi:plugin:markdown')
+ , debug = require('debug')('literapi:plugin:markdown:parse')
function parse(markdown, cb) {
@@ -42,7 +42,9 @@ function examples(tokens) {
return examples
}
-module.exports.documents = function markdown(doc, next) {
+module.exports.documents = function markdown_parse(doc, next) {
+ if (!doc.text) return next()
+
debug('parsing text')
parse(doc.text, function(err, parsed) {
if (err) return next(err)
@@ -2,9 +2,9 @@
if (global.GENTLY) require = GENTLY.hijack(require)
var Markdownstream = require('markdownstream')
- , debug = require('debug')('literapi:plugin:markdown_output')
+ , debug = require('debug')('literapi:plugin:markdown:serialize')
-module.exports.documents = function markdown_output(doc, next) {
+module.exports.documents = function markdown_serialize(doc, next) {
doc.text = doc.markdown.parsed.join('')
debug("Created markdown output", doc.text)
next()
View
@@ -24,7 +24,7 @@
},
"preferGlobal": true,
"scripts": {
- "test": "node test/example.js"
+ "test": "make tap"
},
"dependencies": {
"vows": "~0.6",
View
@@ -1,16 +0,0 @@
-var server = require('./example/server')
- , literapi = require('../lib')
-
-var port = 74123
-
-server.listen(port)
-
-literapi.withDefaultPlugins({ root: "http://localhost:" + port })
- .use(literapi.writefile({ ext: '.new' }))
- .runWithFiles(['test/example/README.md'], function(err) {
-
- console.log('finito', arguments)
-
- if (err) throw err
- server.close()
-})
View
@@ -0,0 +1,24 @@
+var server = require('./example/server')
+ , literapi = require('../lib')
+
+describe('Literapi', function() {
+ var port = 74000 + Math.floor(Math.random() * 1000)
+
+ before(function(done) {
+ server.listen(port)
+ setTimeout(done, 1000)
+ })
+
+ after(function() {
+ server.close()
+ })
+
+ it("runs the example case succesfully", function(done){
+ literapi.withDefaultPlugins({ root: "http://localhost:" + port })
+ .use(literapi.writefile({ ext: '.new' }))
+ .runWithFiles(['test/example/README.md'], function(err) {
+ if (err) throw err
+ done()
+ })
+ })
+})
Oops, something went wrong.

0 comments on commit 7e9060e

Please sign in to comment.