Skip to content
Browse files

Fixed deep templates

Quick, commit before it breaks again! Needs a bit more refactoring before I'm happy with it.
  • Loading branch information...
1 parent b7ff337 commit 7e82ff2628205fa100de9360ee2a431f6957fff3 @nathanmacinnes committed Aug 4, 2012
Showing with 22 additions and 13 deletions.
  1. +22 −13 lib/pretendr.js
View
35 lib/pretendr.js
@@ -48,29 +48,38 @@ makeBaseObj = function (descriptor, mock) {
setReturnValue = function (value) {
attributes.returnValue = value;
};
- setTemplate = function (desc) {
- attributes.template = makeBaseObj(desc);
+ setTemplate = function (o) {
+ o = o || {};
+ if (o.hasOwnProperty('mock') && o.hasOwnProperty('clone')) {
+ attributes.template = o.clone();
+ } else {
+ attributes.template = makeBaseObj(o);
+ }
return attributes.template;
};
- makeCopy = function (obj) {
+ baseObj.clone = function (obj) {
var copy = makeBaseObj(descriptor, obj);
- if (attributes.hasOwnProperty('fake')) {
- copy.fake(attributes.fake);
- }
- if (attributes.hasOwnProperty('returnValue')) {
- copy.returnValue(attributes.returnValue);
+ this.applyAttributesTo(copy);
+ return copy;
+ };
+ baseObj.applyAttributesTo = function (other) {
+ var i;
+ for (i in attributes) {
+ if (attributes.hasOwnProperty(i)) {
+ other[i](attributes[i]);
+ }
}
- if (attributes.hasOwnProperty('template')) {
- copy.template(attributes.template.mock);
+ for (i in descriptor) {
+ if (descriptor.hasOwnProperty(i) && this.hasOwnProperty(i)) {
+ this[i].applyAttributesTo(other[i]);
+ }
}
- return copy;
};
if (typeof descriptor === 'function') {
baseObj.fake = setFake;
baseObj.returnValue = setReturnValue;
baseObj.template = setTemplate;
}
- baseObj.clone = makeCopy;
baseObj.mock = mock || makeMock(descriptor, baseObj, attributes);
if (nonPrimitive(descriptor)) {
for (i in descriptor) {
@@ -139,4 +148,4 @@ pretendrFunction = function (attributes, baseObject) {
// hide the extra arg of pretendr from the public
module.exports = function (o) {
return makeBaseObj(o);
-};
+};

0 comments on commit 7e82ff2

Please sign in to comment.
Something went wrong with that request. Please try again.