Created
January 4, 2012 06:19
-
-
Save airportyh/1558769 to your computer and use it in GitHub Desktop.
Script to detect whether a test file is written in QUnit or Jasmine
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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