Permalink
Browse files

Fixed infinite recursion in templates

  • Loading branch information...
nathanmacinnes committed Aug 3, 2012
1 parent 228ca61 commit 5e3ba3f0c18d68b6906d525b88fcd0fc26f17b31
Showing with 26 additions and 1 deletion.
  1. +16 −1 lib/pretendr.js
  2. +10 −0 test/spec.js
View
@@ -4,7 +4,9 @@
var enumerate, // fn - whether to enumerate the properties and recurse
makeTemplate, // fn - get a template object
- pretendr; // fn - called by module.exports
+ pretendr, // fn - called by module.exports
+ templateIns = [], // list of template inputs
+ templateOuts = []; // template outputs they match to prevent infinit loops
// Return whether the object passed in is of type 'object' or 'function'
// This is important, because string types cause infinite recursion and other
@@ -18,13 +20,25 @@ enumerate = function (o) {
makeTemplate = function (t) {
var fake,
i,
+ index,
+ ins,
+ outs,
ownTemplate,
returnValue,
setFake,
setReturnValue,
setTemplate,
subTemplate,
templateObj;
+ for (i = 0; i < templateIns.length; i++) {
+ if (templateIns[i] === t) {
+ return templateOuts[i];
+ }
+ }
+
+ templateIns.push(t);
+ index = templateIns.length - 1;
+
setReturnValue = function (val) {
returnValue = val;
};
@@ -70,6 +84,7 @@ makeTemplate = function (t) {
templateObj[i] = makeTemplate(t[i]);
}
}
+ templateOuts[index] = templateObj;
return templateObj;
};
View
@@ -358,6 +358,16 @@ describe("pretendr", function () {
.and.not.have.property('fake')
.and.not.have.property('template');
});
+ it("should not handle infinite loops", function () {
+ var mockObj = this.pretendr(function () {}),
+ templateDescriptor,
+ template;
+ templateDescriptor = {};
+ templateDescriptor.a = templateDescriptor;
+ expect(function () {
+ mockObj.template(templateDescriptor);
+ }).to.not.throwError();
+ });
});
describe("array", function () {
beforeEach(function () {

0 comments on commit 5e3ba3f

Please sign in to comment.