Skip to content

Instantly share code, notes, and snippets.

@airportyh
Created Jan 4, 2012
Embed
What would you like to do?
Script to detect whether a test file is written in QUnit or Jasmine
// Script to detect whether a js unit test file is written in QUnit or Jasmine.
// Usage:
// node detect.js <test file>
var parser = require('uglify-js').parser
, fs = require('fs')
, filename = process.argv[2]
var patterns = {
jasmine: [
function(node){
return node[0] === 'call' &&
node[1][0] === 'name' &&
node[1][1] === 'describe' &&
node[2][0][0] === 'string'
},
function(node){
return node[0] === 'call' &&
node[1][0] === 'name' &&
node[1][1] === 'it' &&
node[2][0][0] === 'string'
},
function(node){
return node[0] === 'call' &&
node[1][0] === 'name' &&
node[1][1] === 'expect'
}
],
qunit: [
function(node){
return node[0] === 'call' &&
node[1][0] === 'name' &&
node[1][1] === 'test' &&
node[2][0][0] === 'string'
},
function(node){
return node[0] === 'call' &&
node[1][0] === 'name' &&
node[1][1] === 'ok'
},
function(node){
return node[0] === 'call' &&
node[1][0] === 'name' &&
node[1][1] === 'equal'
}
]
}
var tally = {
jasmine: 0,
qunit: 0
}
function parse(filename){
var ast = parser.parse(String(fs.readFileSync(filename)))
return ast
}
function patternMatches(node, test){
try{
return test(node)
}catch(e){
return false
}
}
function detectPatterns(node){
for (var framework in patterns){
patterns[framework].forEach(function(test){
if (patternMatches(node, test)){
tally[framework]++
}
})
}
}
function traverse(node, visit){
visit(node)
if (node instanceof Array){
for (var i = 0, len = node.length; i < len; i++)
traverse(node[i], visit)
}
}
var ast = parse(filename)
traverse(ast, detectPatterns)
for (var key in tally){
console.log(tally[key] + ' points for ' + key + '.')
}
console.log('Tests were written in ' + (tally.qunit > tally.jasmine ?
'QUnit': 'Jasmine') + '.')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment