Permalink
Browse files

cleanup

  • Loading branch information...
1 parent c3494c0 commit 8b85f530690ad3b659dac86614375df7d70e806c @nathanmacinnes committed Aug 4, 2012
Showing with 21 additions and 9 deletions.
  1. +21 −9 lib/pretendr.js
View
30 lib/pretendr.js
@@ -4,7 +4,7 @@
var nonPrimitive, // fn - whether to enumerate the properties and recurse
makeTemplate, // fn - get a template object
- makeBaseObj,
+ makePretendr,
makeMock,
pretendr, // fn - called by module.exports
pretendrFunction,
@@ -18,12 +18,12 @@ nonPrimitive = function (o) {
return typeof o === 'object' || typeof o === 'function';
};
-makeBaseObj = function (descriptor, mock) {
+makePretendr = function (descriptor, mock) {
var ins = [],
outs = [],
- subMakeBaseObj;
+ subMakePretendr;
- subMakeBaseObj = function (descriptor, mock) {
+ subMakePretendr = function (descriptor, mock) {
var attributes = {},
baseObj = {},
makeCopy,
@@ -37,6 +37,7 @@ makeBaseObj = function (descriptor, mock) {
// infinite recursion
for (i = 0; i < ins.length; i++) {
if (descriptor === ins[i]) {
+
// if we have, return the output which corresponds to this
// input
return outs[i];
@@ -58,24 +59,33 @@ makeBaseObj = function (descriptor, mock) {
if (o.hasOwnProperty('mock') && o.hasOwnProperty('clone')) {
attributes.template = o.clone();
} else {
- attributes.template = makeBaseObj(o);
+ attributes.template = makePretendr(o);
}
return attributes.template;
};
+
+ // make a copy of this pretendr object
baseObj.clone = function (obj) {
- var copy = makeBaseObj(descriptor, obj);
+ var copy = makePretendr(descriptor, obj);
this.applyAttributesTo(copy);
return copy;
};
+
+ // the attributes object which contains the return value, et al, can be
+ // copied to the supplied other object with this function
baseObj.applyAttributesTo = function (other) {
var i;
+
// The attribute properties conveniently have the same name as the
// pretendr methods, so just loop through them.
for (i in attributes) {
if (attributes.hasOwnProperty(i)) {
other[i](attributes[i]);
}
}
+
+ // recurse to descendents, as long as the objects actually have the
+ // same descendents
for (i in descriptor) {
if (descriptor.hasOwnProperty(i) && this.hasOwnProperty(i)) {
this[i].applyAttributesTo(other[i]);
@@ -91,16 +101,17 @@ makeBaseObj = function (descriptor, mock) {
if (nonPrimitive(descriptor)) {
for (i in descriptor) {
if (descriptor.hasOwnProperty(i)) {
- baseObj[i] = subMakeBaseObj(descriptor[i]);
+ baseObj[i] = subMakePretendr(descriptor[i]);
baseObj.mock[i] = baseObj[i].mock;
}
}
}
return baseObj;
};
- return subMakeBaseObj(descriptor, mock);
+ return subMakePretendr(descriptor, mock);
};
+// create the mock property of this object
makeMock = function (desc, base, attr) {
if (desc instanceof Array) {
return [];
@@ -114,6 +125,7 @@ makeMock = function (desc, base, attr) {
return desc;
};
+// create mock functions
pretendrFunction = function (attributes, baseObject) {
var fn;
baseObject.calls = [];
@@ -154,5 +166,5 @@ pretendrFunction = function (attributes, baseObject) {
// hide the extra arg of pretendr from the public
module.exports = function (o) {
- return makeBaseObj(o);
+ return makePretendr(o);
};

0 comments on commit 8b85f53

Please sign in to comment.