Permalink
Browse files

Implemented recursion in templates

  • Loading branch information...
1 parent a2cf455 commit 294a2403cce5d697b77b2ffd9905eb64e60ad0b0 @nathanmacinnes committed Aug 2, 2012
Showing with 70 additions and 25 deletions.
  1. +59 −25 lib/pretendr.js
  2. +11 −0 test/spec.js
View
84 lib/pretendr.js
@@ -1,13 +1,57 @@
/*jslint node: true, plusplus: true, indent: 4, maxlen: 80 */
-module.exports = function (o) {
+var makeTemplate,
+ pretendr;
+
+makeTemplate = function (t) {
+ var fake,
+ i,
+ returnValue,
+ subTemplate,
+ templateObj;
+ templateObj = {
+ returnValue : function (val) {
+ returnValue = val;
+ },
+ fake : function (fn) {
+ fake = fn;
+ },
+ generate : function (o) {
+ var i,
+ p;
+ p = this.apply(pretendr(t, o));
+ for (i in t) {
+ this[i].apply(p[i]);
+ }
+ return p;
+ },
+ apply : function (p) {
+ if (returnValue !== undefined) {
+ p.returnValue(returnValue);
+ }
+ if (fake !== undefined) {
+ p.fake(fake);
+ }
+ return p;
+ }
+ };
+ for (i in t) {
+ if (t.hasOwnProperty(i)) {
+ subTemplate = makeTemplate(t[i]);
+ templateObj[i] = subTemplate;
+ }
+ }
+ return templateObj;
+};
+
+pretendr = function (o, f) {
"use strict";
var ins = [],
outs = [],
pretendr,
pretendrFunction,
pretendrPrimitive;
-
+
pretendr = function (input, forced) {
var i,
indexNumber,
@@ -56,8 +100,6 @@ module.exports = function (o) {
pretendrFunction = function () {
var fake,
pretendrObj,
- templateFake,
- templateReturnValue,
returnValue,
template;
pretendrObj = {
@@ -70,15 +112,8 @@ module.exports = function (o) {
fake = fn;
},
template : function (t) {
- template = t;
- return {
- returnValue : function (val) {
- templateReturnValue = val;
- },
- fake : function (fn) {
- templateFake = fn;
- }
- };
+ template = makeTemplate(t);
+ return template;
},
mock : function () {
var call,
@@ -94,20 +129,14 @@ module.exports = function (o) {
}
pretendrObj.calls.push(call);
if (this instanceof pretendrObj.mock) {
- pretendrObj.instances.push(pretendr(
- template,
- this
- ));
+ if (!template) {
+ template = pretendrObj.template();
+ }
+ pretendrObj.instances.push(template.generate(this));
} else if (fake) {
call.returned = fake.apply(this, arguments);
} else if (template) {
- call.pretendr = module.exports(template);
- if (templateReturnValue) {
- call.pretendr.returnValue(templateReturnValue);
- }
- if (templateFake) {
- call.pretendr.fake(templateFake);
- }
+ call.pretendr = template.generate();
call.returned = call.pretendr.mock;
} else {
call.returned = returnValue;
@@ -144,5 +173,10 @@ module.exports = function (o) {
return pretendrObj;
};
- return pretendr(o);
+ return pretendr(o, f);
};
+
+// hide the extra arg from the public
+module.exports = function (o) {
+ return pretendr(o);
+};
View
11 test/spec.js
@@ -213,6 +213,17 @@ describe("pretendr", function () {
res();
expect(fake.calls).to.have.length(1);
});
+ it("should be deep like pretendr objects", function () {
+ var m = this.pretendr(function () {}),
+ res,
+ t;
+ t = m.template({
+ method : function () {}
+ });
+ t.method.returnValue(4);
+ res = m.mock();
+ expect(res.method()).to.equal(4);
+ });
});
describe("with properties", function () {
it("should mock the properties", function () {

0 comments on commit 294a240

Please sign in to comment.