Skip to content

Commit

Permalink
Fixed bug with deep template objects
Browse files Browse the repository at this point in the history
The below wouldn't work because `t.aProperty.aFunction` didn't have a
`returnValue` method
````javascript
var t = pretendrObj.template({
    aProperty : {
        aFunction : function () {}
    }
});
t.aProperty.aFunction.returnValue(1);
````
  • Loading branch information
nathanmacinnes committed Aug 2, 2012
1 parent 12f174f commit d51141e
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 10 deletions.
23 changes: 14 additions & 9 deletions lib/pretendr.js
Expand Up @@ -2,9 +2,14 @@

"use strict";

var makeTemplate,
var enumerate,
makeTemplate,
pretendr;

enumerate = function (o) {
return typeof o === 'object' || typeof o === 'function';
};

makeTemplate = function (t) {
var fake,
i,
Expand All @@ -29,14 +34,10 @@ makeTemplate = function (t) {
var i,
p;
p = this.apply(pretendr(t, o));
for (i in t) {
if (t.hasOwnProperty(i) && typeof t[i] === 'function') {
this[i].apply(p[i]);
}
}
return p;
},
apply : function (p) {
var i;
if (returnValue !== undefined) {
p.returnValue(returnValue);
}
Expand All @@ -46,6 +47,11 @@ makeTemplate = function (t) {
if (ownTemplate !== undefined) {
p.template(ownTemplate);
}
for (i in t) {
if (t.hasOwnProperty(i) && templateObj[i]) {
templateObj[i].apply(p[i]);
}
}
return p;
}
};
Expand All @@ -55,9 +61,8 @@ makeTemplate = function (t) {
templateObj.template = setTemplate;
}
for (i in t) {
if (t.hasOwnProperty(i) && typeof t[i] === 'function') {
subTemplate = makeTemplate(t[i]);
templateObj[i] = subTemplate;
if (t.hasOwnProperty(i) && enumerate(t[i])) {
templateObj[i] = makeTemplate(t[i]);
}
}
return templateObj;
Expand Down
7 changes: 6 additions & 1 deletion test/spec.js
Expand Up @@ -328,11 +328,16 @@ describe("pretendr", function () {
res,
t;
t = m.template({
method : function () {}
method : function () {},
property : {
subMethod : function () {}
}
});
t.method.returnValue(4);
t.property.subMethod.returnValue(5);
res = m.mock();
expect(res.method()).to.equal(4);
expect(res.property.subMethod()).to.equal(5);
});
it("shouldn't have meaningless methods for objects", function () {
var m = this.pretendr(function () {}),
Expand Down

0 comments on commit d51141e

Please sign in to comment.