Permalink
Browse files

Littered with comments

  • Loading branch information...
nathanmacinnes committed Aug 3, 2012
1 parent b30ea7b commit 228ca613139cb557610f1bc3694544161f2bd984
Showing with 45 additions and 9 deletions.
  1. +45 −9 lib/pretendr.js
View
@@ -2,14 +2,19 @@
"use strict";
-var enumerate,
- makeTemplate,
- pretendr;
+var enumerate, // fn - whether to enumerate the properties and recurse
+ makeTemplate, // fn - get a template object
+ pretendr; // fn - called by module.exports
+// Return whether the object passed in is of type 'object' or 'function'
+// This is important, because string types cause infinite recursion and other
+// types don't make much sense
enumerate = function (o) {
return typeof o === 'object' || typeof o === 'function';
};
+// Return a template object based on the given template descriptor, so that
+// pretendr objects can be created with returnValue et al already set
makeTemplate = function (t) {
var fake,
i,
@@ -68,47 +73,74 @@ makeTemplate = function (t) {
return templateObj;
};
+// Create a pretendr object. If the optional second argument is provided, that
+// object will be used as the mock object. This is to aid with instances of
+// mock constructor functions.
pretendr = function (o, f) {
- var ins = [],
- outs = [],
- pretendr,
- pretendrFunction,
- pretendrPrimitive;
+ var ins = [], // objects which have been passed in to the pretendr function
+ outs = [], // objects which have been returned by pretendr
+ pretendr, // inner fn so that ins and outs aren't recreated by recursion
+ pretendrFunction, // fn for creating mock functions
+ pretendrPrimitive; // fn for creating mock primitives
+ // an inner function is used so that ins and outs are created once per
+ // external call, then resued to prevent infinite recursion
pretendr = function (input, forced) {
var i,
indexNumber,
mock,
output;
+
+ // loop through all objects which have been passed in before. If this
+ // matches, return the corresponding out[], so that we don't get
+ // infinite recursion
for (i = 0; i < ins.length; i++) {
if (input === ins[i]) {
return outs[i];
}
}
+ // otherwise, put in the input and store the number
ins.push(input);
indexNumber = ins.length - 1;
+
+ // We want to recurse through objects and functions, so do them
+ // separately
if (enumerate(input)) {
if (typeof input === 'function') {
output = pretendrFunction();
} else {
+
+ // if an object has been passed that should be used as a basis
+ // for the mock. Used when creating instances of templates.
if (forced) {
mock = forced;
+
+ // if it's an array, we want the mock to be an array too
} else if (input instanceof Array) {
mock = [];
} else {
mock = {};
}
+
output = {
mock : mock
};
}
+
+ // Store the output before we recurse, so that it can be returned
+ // if we come across the same input again
outs[indexNumber] = output;
+
+ // Loop through and recurse if necessary
for (i in input) {
if (input.hasOwnProperty(i)) {
if (enumerate(input[i])) {
output[i] = pretendr(input[i]);
output.mock[i] = output[i].mock;
} else {
+
+ // primitive objects must be processed separately so
+ // that they can have getter and setter functions
output[i] = pretendrPrimitive(input[i], output.mock, i);
}
}
@@ -118,6 +150,8 @@ pretendr = function (o, f) {
return pretendrPrimitive(input);
};
+ // Base objects of mocked functions must have several methods, and the mocks
+ // themselves must of course be functions, so do these in their own function
pretendrFunction = function () {
var fake,
pretendrObj,
@@ -168,6 +202,8 @@ pretendr = function (o, f) {
return pretendrObj;
};
+ // Primitives must have getters and setters so that we can record what
+ // happens to them, so these are processed in their own functions too.
pretendrPrimitive = function (val, parent, property) {
var get,
pretendrObj,
@@ -197,7 +233,7 @@ pretendr = function (o, f) {
return pretendr(o, f);
};
-// hide the extra arg from the public
+// hide the extra arg of pretendr from the public
module.exports = function (o) {
return pretendr(o);
};

0 comments on commit 228ca61

Please sign in to comment.