Permalink
Browse files

Add mocha reporters

  • Loading branch information...
1 parent cb3f74a commit dbe83b935a929cda66f713f3b18309a85216bf82 @agnoster committed Oct 17, 2012
View
@@ -8,6 +8,7 @@ function parse_args(args) {
return optimist(args)
.usage('Usage: $0 [-vawqhd] -r http://api.server:port/root file1 [file2 [...]]')
.describe('r', 'Root URI for the API').alias('r', 'root')
+ .describe('R', 'Reporter to use for test results').alias('R', 'reporter')
.describe('v', 'Return the version number').alias('v', 'version')
.describe('a', 'Show all results, not just errors').alias('a', 'all')
.describe('w', 'Overwrite the document with annotations about successes and failures').alias('w', 'write')
@@ -28,11 +29,12 @@ function parse_opts(opts) {
// Options
if (argv.w) options.write = true
if (argv.d) options.delay *= 2
- if (argv.a) options.reporter = require('vows/lib/vows/reporters/spec')
+ if (argv.a) options.reporter = 'Spec'
if (argv.r) options.root = argv.r
+ if (argv.R) options.reporter = argv.R
if (argv.q) {
options.quiet = true
- options.reporter = require('./reporters/quiet')
+ options.reporter = 'Dot'
}
options.files = argv._
@@ -66,8 +68,9 @@ function run(params) {
var options = extract_options(params || process.argv.slice(2))
- literapi.withDefaultPlugins(options).runWithFiles(options.files, function(results) {
- console.log('done')
+ literapi.withDefaultPlugins(options).runWithFiles(options.files, function(err, results) {
+ if (err) throw err
+
var status = 0
if (results.broken) status += 1
if (results.errored) status += 2
View
@@ -5,26 +5,29 @@ var fs = require('fs')
, debug = require('debug')('literapi')
, path = require('path')
, plugins = require('./plugins')
+ , types = require('./types')
function LiterAPI(options) {
this.options = options
this.stack = []
- this.book = { documents: [], context: {} }
+ this.book = new types.book
}
LiterAPI.prototype = {}
LiterAPI.prototype.use = plugins.use
LiterAPI.prototype.run = function run(cb) {
+ debug('Running plugin stack')
plugins.run(this.stack, 0, 0, this.book, cb)
}
LiterAPI.prototype.runWithFiles = function runWithFiles(files, cb) {
debug('runWithFiles', files)
files.forEach(function(file) {
- this.book.documents.push({
- filename: file
+ this.book.addDocument({
+ filename: file,
+ title: file
})
}.bind(this))
@@ -43,12 +46,14 @@ literapi.withDefaultPlugins = function (options) {
, literapi.http_parse
, literapi.http_request({ root: options.root })
, literapi.http_match_response
- , literapi.default_reporter({ verbose: true })
- , literapi.markdown_serialize
+ //, literapi.default_reporter({ verbose: true })
+ , literapi.mocha_reporter(options.reporter || 'dot')
])
- if (options.write)
- l.use(literapi.writefile)
+ if (options.write) {
+ l.use(literapi.markdown_serialize)
+ l.use(literapi.writefile())
+ }
return l
}
View
@@ -33,30 +33,31 @@ function run(stack, level, layer, el, cb) {
}
// Errors pop right up
- if (err) {
- cb(err)
- }
+ if (err) return cb(err, layer)
try {
if (plugin.min_level == level) {
// Keep processing at this level
layer++
+ debug('running plugin')
plugin[lname](el, next)
} else {
// Move one level up
debug("up", level, plugin.min_level)
- async.mapSeries(el[levelName[level + 1]], function(child, cb) {
- child.context = el.context
+ var map = async.mapSeries
+ //if (lname == 'books') map = async.map
+ map(el[levelName[level + 1]], function(child, cb) {
run(stack, level + 1, layer, child, cb)
}, function(err, results) {
+ if (err) return cb(err)
layer = results[0]
debug("Moved back to level", level, "at layer", layer)
- next(err)
+ next()
})
}
} catch (err) {
debug("Caught error", err)
- next(err)
+ cb(err)
}
}
@@ -11,11 +11,8 @@ module.exports = function(options) {
}
function verbose_reporter(example, next) {
- example.ok && example.ok.forEach(function(success) {
- console.log('+', success)
- })
- example.fail && example.fail.forEach(function(failure) {
- console.log('X', failure)
+ example.tests && example.tests.forEach(function(test) {
+ console.log(test.status, test.title)
})
next()
}
@@ -3,7 +3,7 @@ if (global.GENTLY) require = GENTLY.hijack(require)
var interpolate = require('../interpolate')
, JSONExp = require('jsonexp')
- , should = require('should')
+ , assert = require('assert')
, debug = require('debug')('literapi:plugin:http:match_response')
function http_match_response(example, next) {
@@ -16,71 +16,73 @@ function http_match_response(example, next) {
debug('enter')
- function assert(label, test) {
- debug('assert', label)
+ function addTest(title, assertion) {
+ debug('addTest', title)
+ var test = example.addTest({ title: title })
try {
- test()
+ assertion()
debug('succeeded')
- example.ok = example.ok || []
- example.ok.push({ label: label })
+ test.ok()
} catch(e) {
debug('failed: ', e)
- example.fail = example.fail || []
- example.fail.push({ label: label, error: e })
+ example.fail = true
+ test.fail(e)
}
}
- //vowsCompiler.prototype.matchers = function (params) {
- function assertBodyMatches(actual, expected, context) {
- var exp
- try { exp = new JSONExp(expected) } catch (e) {}
-
- if (exp) {
- assert("Body matches JSONExp: " + expected.replace(/\s+/g, " "), function() {
+ function assertBodyMatches(actual, expected, bindings) {
+ try {
+ var exp = new JSONExp(expected)
+ addTest("Body matches JSONExp: " + expected.replace(/\s+/g, " "), function() {
- should.notEqual(actual, '', "No body returned")
+ assert.notEqual(actual, '', "No body returned")
- var parsed = JSON.parse(actual)
- exp.assert(parsed, { namespace: context, merge: true })
+ try {
+ var parsed = JSON.parse(actual)
+ } catch(e) {
+ assert.fail(actual, expected, "Could not parse JSON")
+ }
+ exp.assert(parsed, { namespace: bindings, merge: true })
})
- } else {
- debug("Failed to compile expression, treating as raw text", e)
+ } catch (e) {
+ debug("Failed to compile expression '" + expected + "', treating as raw text", e)
- assert("Body matches text: " + expected.replace(/\s+/g, " "), function() {
- should.equal(actual, expected)
+ addTest("Body matches plaintext: " + expected.replace(/\s+/g, " "), function() {
+ assert.equal(actual, expected)
})
}
}
/**
* Internal: interpolate the values from `context` into the `params` struct
*/
- function checkResponse(actual, expected, context) {
+ function checkResponse(actual, expected, bindings) {
debug('Checking response, expecting:', expected)
- assert(expected.title, function() {
- actual.statusCode.should.equal(expected.code)
+ addTest(expected.title, function() {
+ assert.equal(actual.statusCode, expected.code)
})
for (var key in expected.headers) {
var val = expected.headers[key]
- assert(key + ': ' + val, function() {
- actual.headers.should.have.property(key)
- should.ok(interpolate.match(actual.headers[key], val, context))
+ addTest(key + ': ' + val, function() {
+ assert.ok(actual.headers[key], "Header " + key + " expected, but not found")
+ assert.ok(interpolate.match(actual.headers[key], val, bindings), "Expected headers to match")
})
}
if (expected.body) {
- assertBodyMatches(actual.body, expected.body, context)
+ assertBodyMatches(actual.body, expected.body, bindings)
} else {
- assert('No body', function() {
- should.not.exist(actual.body)
+ addTest('No body', function() {
+ if (actual.body) assert.fail(actual.body, '', "Expected empty body")
})
}
}
- checkResponse(example.context.response, example.http, context)
+ example.title = example.context.request.title
+ checkResponse(example.context.response, example.http, example.context.bindings)
next()
}
@@ -78,6 +78,7 @@ module.exports.examples = function http_parse(example, next) {
if (params) {
example.http = params
+ example.title = params.title
}
next()
@@ -29,14 +29,14 @@ function make_request(params, cb) {
}
/**
- * Internal: interpolate the values from `context` into the `params` struct
+ * Internal: interpolate the values from `bindings` into the `params` struct
*/
-function interpolateRequest(params, context) {
+function interpolateRequest(params, bindings) {
- params.body = interpolate(params.body, context, { json: true })
- params.path = interpolate(params.path, context)
+ params.body = interpolate(params.body, bindings, { json: true })
+ params.path = interpolate(params.path, bindings)
for (var key in params.headers) {
- params.headers[key] = interpolate(params.headers[key], context)
+ params.headers[key] = interpolate(params.headers[key], bindings)
}
return params
}
@@ -48,13 +48,14 @@ module.exports = function(options) {
var params = example.http
if (!params || !params.request) return next()
- interpolateRequest(params, example.context)
+ interpolateRequest(params, example.context.bindings)
params.uri = options.root + params.path
make_request(params, function(err, response) {
if (err) return next(err)
example.context.response = response
+ example.context.request = params
next()
})
}
@@ -24,22 +24,18 @@ function parse(markdown, cb) {
stream.end(markdown)
}
-function examples(tokens) {
-
- var examples = []
+function addExamples(doc, tokens) {
tokens.forEach(function(token) {
if (token.type == 'code_block') {
var tags = token.tags || ''
- examples.push(
+ doc.addExample(
{ text: token.content
, pending: !!tags.match(/\bpending\b/)
, markdown: { token: token }
})
}
})
-
- return examples
}
module.exports.documents = function markdown_parse(doc, next) {
@@ -51,7 +47,7 @@ module.exports.documents = function markdown_parse(doc, next) {
debug('parsed:', parsed)
doc.markdown = { parsed: parsed }
- doc.examples = examples(parsed)
+ addExamples(doc, parsed)
debug('examples:', doc.examples)
next()
@@ -4,8 +4,21 @@ if (global.GENTLY) require = GENTLY.hijack(require)
var Markdownstream = require('markdownstream')
, debug = require('debug')('literapi:plugin:markdown:serialize')
-module.exports.documents = function markdown_serialize(doc, next) {
+function markdown_serialize_example(example, next) {
+ if (!example.pending && example.tests.length) {
+ example.markdown.token.tags = example.fail ? 'fail' : 'ok'
+ example.markdown.token.refresh()
+ }
+ next()
+}
+
+function markdown_serialize_document(doc, next) {
doc.text = doc.markdown.parsed.join('')
debug("Created markdown output", doc.text)
next()
}
+
+module.exports =
+[ { examples: markdown_serialize_example }
+, { documents: markdown_serialize_document }
+]
Oops, something went wrong.

0 comments on commit dbe83b9

Please sign in to comment.