Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // ---------------------------------------------------------------------
- // Mocking all function calls from within the function under test
- // ---------------------------------------------------------------------
- module.exports = function(test){
- var _call_history, _mock;
- // Overall Setup: Define all functions in a global namespace
- var fnames = extend({},test.defaults);
- test.cases.forEach(function(testCase){
- extend( fnames,Object.keys(testCase.fixture))
- });
- Object.keys(fnames).forEach( function(fname) {
- global[fname] = function() {
- return mock({ fname:fname, args:arguments });
- }
- });
- if (!test.cases || test.cases.length === 0) {
- console.log("No test cases");
- return;
- } else {
- console.log("1.." + test.cases.length);
- }
- // Now process each individual test case
- test.cases.forEach( function(testCase, testCaseIndex) {
- var msg = "";
- try {
- setup(testCase.fixture);
- rval = test.func();
- assert_call_history(testCase.expected_history);
- assert_equals(testCase.expected_rval,rval);
- } catch (e) { msg = e; }
- console.log(
- result_line( testCase.name, testCaseIndex+1, msg)
- );
- if (testCase.log_history)
- console.log( callHistoryToString( call_history ));
- function result_line( name, index, msg) {
- var result = msg ? "not ok" : "ok"
- result += " " + index + " - " + name;
- if (msg) result += ": " + msg;
- return result;
- }
- });
- function setup(fixture) {
- call_history = [];
- _mock = extend({},test.defaults);
- extend(_mock,fixture);
- }
- function callHistoryToString( callHistory ) {
- return '[\n' +
- callHistory.map( function(fc) {
- return (fc instanceof FunctionCall) ?
- fc.toString() :
- (new FunctionCall(fc)).toString() }
- ).join('\n') +
- '\n]';
- }
- function assert_call_history(expected_history) {
- var epos = 0,
- $break = "break",
- stoppedAsExpected = true;
- if (!expected_history) return;
- try {
- call_history.forEach( function(fcall, cpos) {
- if (epos < expected_history.length) {
- if (expected_history.stop) {
- stoppedAsExpected = false;
- throw $break;
- }
- if (fcall.equals( expected_history[epos] )){
- epos++; // Found, OK
- }
- }
- });
- } catch (e) {
- if (e != $break) throw e;
- }
- var expectedHistoryLength = expected_history.reduce(
- function(acc,next) {
- if (!(next.stop))acc++
- return acc
- },0);
- if (epos < expectedHistoryLength) {
- console.log( callHistoryToString( call_history ) );
- console.log( callHistoryToString( expected_history ) );
- if (!stoppedAsExpected)
- throw "Call sequence continued after stop mark";
- else
- throw "Call sequence not as expected";
- }
- }
- function assert_equals( exp, act ) {
- if (exp!==act) {
- throw 'Expected value "' + exp + '" <> actual value "' + act + '"';
- }
- }
- function mock(func) {
- var fval,rval;
- if ((fval = _mock[func.fname])) {
- if (func.args.length === 0)
- rval = fval;
- else if (func.args.length == 1)
- rval = fval[func.args[0]];
- }
- call_history.push(new FunctionCall(func.fname,func.args,rval));
- return rval;
- }
- function extend(destination, source) {
- for (var property in source) {
- if (typeof source[property] === "object" &&
- source[property] !== null && destination[property]) {
- extend(destination[property], source[property]);
- } else {
- destination[property] = source[property];
- }
- }
- return destination;
- }
- };
- // --- An object type for logging function calls
- function FunctionCall( fname, args, returnValue ) {
- if (typeof arguments[0] == "object") {
- FunctionCall.call( this,
- arguments[0].fname,
- arguments[0].args,
- arguments[0].returnValue);
- }
- else {
- this.fname = fname;
- if (args) {
- this.args =
- (args instanceof Array) ?
- args : Array.prototype.slice.call( args );
- } else
- this.args = [];
- this.returnValue = returnValue;
- }
- }
- FunctionCall.prototype.equals = function( fcall2 ) {
- return this.fname == fcall2.fname &&
- this.args.every( function(arg,i) {
- return arg == fcall2.args[i]
- })
- }
- FunctionCall.prototype.toString = function() {
- var result = this.fname + "(" +
- (this.args.length > 0 ? '"' + this.args.join('","') + '"' : '') +
- ")";
- if (this.returnValue !== null) result += '-> ' + this.returnValue;
- return result;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement