Skip to content

Commit

Permalink
whitespace
Browse files Browse the repository at this point in the history
  • Loading branch information
nathanmacinnes committed Aug 2, 2012
1 parent fc013ea commit c934979
Show file tree
Hide file tree
Showing 2 changed files with 220 additions and 220 deletions.
350 changes: 175 additions & 175 deletions lib/pretendr.js
Expand Up @@ -3,192 +3,192 @@
"use strict"; "use strict";


var makeTemplate, var makeTemplate,
pretendr; pretendr;


makeTemplate = function (t) { makeTemplate = function (t) {
var fake, var fake,
i, i,
ownTemplate, ownTemplate,
returnValue, returnValue,
subTemplate, subTemplate,
templateObj; templateObj;
templateObj = { templateObj = {
returnValue : function (val) { returnValue : function (val) {
returnValue = val; returnValue = val;
}, },
fake : function (fn) { fake : function (fn) {
fake = fn; fake = fn;
}, },
template : function (tem) { template : function (tem) {
ownTemplate = tem; ownTemplate = tem;
}, },
generate : function (o) { generate : function (o) {
var i, var i,
p; p;
p = this.apply(pretendr(t, o)); p = this.apply(pretendr(t, o));
for (i in t) { for (i in t) {
if (t.hasOwnProperty(i) && (typeof t[i] === 'object' || if (t.hasOwnProperty(i) && (typeof t[i] === 'object' ||
typeof t[i] === 'function')) { typeof t[i] === 'function')) {
this[i].apply(p[i]); this[i].apply(p[i]);
} }
} }
return p; return p;
}, },
apply : function (p) { apply : function (p) {
if (returnValue !== undefined) { if (returnValue !== undefined) {
p.returnValue(returnValue); p.returnValue(returnValue);
} }
if (fake !== undefined) { if (fake !== undefined) {
p.fake(fake); p.fake(fake);
} }
if (ownTemplate !== undefined) { if (ownTemplate !== undefined) {
p.template(ownTemplate); p.template(ownTemplate);
} }
return p; return p;
} }
}; };
for (i in t) { for (i in t) {
if (t.hasOwnProperty(i) && (typeof t[i] === 'object' || if (t.hasOwnProperty(i) && (typeof t[i] === 'object' ||
typeof t[i] === 'function')) { typeof t[i] === 'function')) {
subTemplate = makeTemplate(t[i]); subTemplate = makeTemplate(t[i]);
templateObj[i] = subTemplate; templateObj[i] = subTemplate;
} }
} }
return templateObj; return templateObj;
}; };


pretendr = function (o, f) { pretendr = function (o, f) {
var ins = [], var ins = [],
outs = [], outs = [],
pretendr, pretendr,
pretendrFunction, pretendrFunction,
pretendrPrimitive; pretendrPrimitive;


pretendr = function (input, forced) { pretendr = function (input, forced) {
var i, var i,
indexNumber, indexNumber,
mock, mock,
output, output,
type; type;
for (i = 0; i < ins.length; i++) { for (i = 0; i < ins.length; i++) {
if (input === ins[i]) { if (input === ins[i]) {
return outs[i]; return outs[i];
} }
} }
ins.push(input); ins.push(input);
indexNumber = ins.length - 1; indexNumber = ins.length - 1;
if (typeof input === 'function' || typeof input === 'object') { if (typeof input === 'function' || typeof input === 'object') {
if (typeof input === 'function') { if (typeof input === 'function') {
output = pretendrFunction(); output = pretendrFunction();
} else { } else {
if (forced) { if (forced) {
mock = forced; mock = forced;
} else if (input instanceof Array) { } else if (input instanceof Array) {
mock = []; mock = [];
} else { } else {
mock = {}; mock = {};
} }
output = { output = {
mock : mock mock : mock
}; };
} }
outs[indexNumber] = output; outs[indexNumber] = output;
for (i in input) { for (i in input) {
if (input.hasOwnProperty(i)) { if (input.hasOwnProperty(i)) {
type = typeof input[i]; type = typeof input[i];
if (type === 'object' || type === 'function') { if (type === 'object' || type === 'function') {
output[i] = pretendr(input[i]); output[i] = pretendr(input[i]);
output.mock[i] = output[i].mock; output.mock[i] = output[i].mock;
} else { } else {
output[i] = pretendrPrimitive(input[i], output.mock, i); output[i] = pretendrPrimitive(input[i], output.mock, i);
} }
} }
} }
return output; return output;
} }
return pretendrPrimitive(input); return pretendrPrimitive(input);
}; };


pretendrFunction = function () { pretendrFunction = function () {
var fake, var fake,
pretendrObj, pretendrObj,
returnValue, returnValue,
template; template;
pretendrObj = { pretendrObj = {
calls : [], calls : [],
instances : [], instances : [],
returnValue : function (r) { returnValue : function (r) {
returnValue = r; returnValue = r;
}, },
fake : function (fn) { fake : function (fn) {
fake = fn; fake = fn;
}, },
template : function (t) { template : function (t) {
template = makeTemplate(t); template = makeTemplate(t);
return template; return template;
}, },
mock : function () { mock : function () {
var call, var call,
i; i;
call = { call = {
args : arguments, args : arguments,
context : this context : this
}; };
for (i = 0; i < arguments.length; i++) { for (i = 0; i < arguments.length; i++) {
if (typeof arguments[i] === 'function') { if (typeof arguments[i] === 'function') {
call.callback = arguments[i]; call.callback = arguments[i];
} }
} }
pretendrObj.calls.push(call); pretendrObj.calls.push(call);
if (this instanceof pretendrObj.mock) { if (this instanceof pretendrObj.mock) {
if (!template) { if (!template) {
template = pretendrObj.template(); template = pretendrObj.template();
} }
pretendrObj.instances.push(template.generate(this)); pretendrObj.instances.push(template.generate(this));
} else if (fake) { } else if (fake) {
call.returned = fake.apply(this, arguments); call.returned = fake.apply(this, arguments);
} else if (template) { } else if (template) {
call.pretendr = template.generate(); call.pretendr = template.generate();
call.returned = call.pretendr.mock; call.returned = call.pretendr.mock;
} else { } else {
call.returned = returnValue; call.returned = returnValue;
} }
return call.returned; return call.returned;
} }
}; };
return pretendrObj; return pretendrObj;
}; };


pretendrPrimitive = function (val, parent, property) { pretendrPrimitive = function (val, parent, property) {
var get, var get,
pretendrObj, pretendrObj,
set; set;
pretendrObj = parent || {}; pretendrObj = parent || {};
get = function () { get = function () {
this.gets = this.gets + 1; this.gets = this.gets + 1;
return val; return val;
}; };
set = function (v) { set = function (v) {
val = v; val = v;
this.values.push(v); this.values.push(v);
}; };
if (Object.defineProperty) { if (Object.defineProperty) {
pretendrObj.gets = 0; pretendrObj.gets = 0;
pretendrObj.values = []; pretendrObj.values = [];
Object.defineProperty(pretendrObj, property || 'mock', { Object.defineProperty(pretendrObj, property || 'mock', {
get : get, get : get,
set : set set : set
}); });
} else { } else {
pretendrObj.mock = val; pretendrObj.mock = val;
} }
return pretendrObj; return pretendrObj;
}; };


return pretendr(o, f); return pretendr(o, f);
}; };


// hide the extra arg from the public // hide the extra arg from the public
module.exports = function (o) { module.exports = function (o) {
return pretendr(o); return pretendr(o);
}; };

0 comments on commit c934979

Please sign in to comment.