Browse files

whitespace

  • Loading branch information...
1 parent fc013ea commit c9349795118472f961696d45296220d754c9edc8 @nathanmacinnes committed Aug 2, 2012
Showing with 220 additions and 220 deletions.
  1. +175 −175 lib/pretendr.js
  2. +45 −45 test/spec.js
View
350 lib/pretendr.js
@@ -3,192 +3,192 @@
"use strict";
var makeTemplate,
- pretendr;
+ pretendr;
makeTemplate = function (t) {
- var fake,
- i,
- ownTemplate,
- returnValue,
- subTemplate,
- templateObj;
- templateObj = {
- returnValue : function (val) {
- returnValue = val;
- },
- fake : function (fn) {
- fake = fn;
- },
- template : function (tem) {
- ownTemplate = tem;
- },
- generate : function (o) {
- var i,
- p;
- p = this.apply(pretendr(t, o));
- for (i in t) {
- if (t.hasOwnProperty(i) && (typeof t[i] === 'object' ||
- typeof t[i] === 'function')) {
- this[i].apply(p[i]);
- }
- }
- return p;
- },
- apply : function (p) {
- if (returnValue !== undefined) {
- p.returnValue(returnValue);
- }
- if (fake !== undefined) {
- p.fake(fake);
- }
- if (ownTemplate !== undefined) {
- p.template(ownTemplate);
- }
- return p;
- }
- };
- for (i in t) {
- if (t.hasOwnProperty(i) && (typeof t[i] === 'object' ||
- typeof t[i] === 'function')) {
- subTemplate = makeTemplate(t[i]);
- templateObj[i] = subTemplate;
- }
- }
- return templateObj;
+ var fake,
+ i,
+ ownTemplate,
+ returnValue,
+ subTemplate,
+ templateObj;
+ templateObj = {
+ returnValue : function (val) {
+ returnValue = val;
+ },
+ fake : function (fn) {
+ fake = fn;
+ },
+ template : function (tem) {
+ ownTemplate = tem;
+ },
+ generate : function (o) {
+ var i,
+ p;
+ p = this.apply(pretendr(t, o));
+ for (i in t) {
+ if (t.hasOwnProperty(i) && (typeof t[i] === 'object' ||
+ typeof t[i] === 'function')) {
+ this[i].apply(p[i]);
+ }
+ }
+ return p;
+ },
+ apply : function (p) {
+ if (returnValue !== undefined) {
+ p.returnValue(returnValue);
+ }
+ if (fake !== undefined) {
+ p.fake(fake);
+ }
+ if (ownTemplate !== undefined) {
+ p.template(ownTemplate);
+ }
+ return p;
+ }
+ };
+ for (i in t) {
+ if (t.hasOwnProperty(i) && (typeof t[i] === 'object' ||
+ typeof t[i] === 'function')) {
+ subTemplate = makeTemplate(t[i]);
+ templateObj[i] = subTemplate;
+ }
+ }
+ return templateObj;
};
pretendr = function (o, f) {
- var ins = [],
- outs = [],
- pretendr,
- pretendrFunction,
- pretendrPrimitive;
+ var ins = [],
+ outs = [],
+ pretendr,
+ pretendrFunction,
+ pretendrPrimitive;
- pretendr = function (input, forced) {
- var i,
- indexNumber,
- mock,
- output,
- type;
- for (i = 0; i < ins.length; i++) {
- if (input === ins[i]) {
- return outs[i];
- }
- }
- ins.push(input);
- indexNumber = ins.length - 1;
- if (typeof input === 'function' || typeof input === 'object') {
- if (typeof input === 'function') {
- output = pretendrFunction();
- } else {
- if (forced) {
- mock = forced;
- } else if (input instanceof Array) {
- mock = [];
- } else {
- mock = {};
- }
- output = {
- mock : mock
- };
- }
- outs[indexNumber] = output;
- for (i in input) {
- if (input.hasOwnProperty(i)) {
- type = typeof input[i];
- if (type === 'object' || type === 'function') {
- output[i] = pretendr(input[i]);
- output.mock[i] = output[i].mock;
- } else {
- output[i] = pretendrPrimitive(input[i], output.mock, i);
- }
- }
- }
- return output;
- }
- return pretendrPrimitive(input);
- };
+ pretendr = function (input, forced) {
+ var i,
+ indexNumber,
+ mock,
+ output,
+ type;
+ for (i = 0; i < ins.length; i++) {
+ if (input === ins[i]) {
+ return outs[i];
+ }
+ }
+ ins.push(input);
+ indexNumber = ins.length - 1;
+ if (typeof input === 'function' || typeof input === 'object') {
+ if (typeof input === 'function') {
+ output = pretendrFunction();
+ } else {
+ if (forced) {
+ mock = forced;
+ } else if (input instanceof Array) {
+ mock = [];
+ } else {
+ mock = {};
+ }
+ output = {
+ mock : mock
+ };
+ }
+ outs[indexNumber] = output;
+ for (i in input) {
+ if (input.hasOwnProperty(i)) {
+ type = typeof input[i];
+ if (type === 'object' || type === 'function') {
+ output[i] = pretendr(input[i]);
+ output.mock[i] = output[i].mock;
+ } else {
+ output[i] = pretendrPrimitive(input[i], output.mock, i);
+ }
+ }
+ }
+ return output;
+ }
+ return pretendrPrimitive(input);
+ };
- pretendrFunction = function () {
- var fake,
- pretendrObj,
- returnValue,
- template;
- pretendrObj = {
- calls : [],
- instances : [],
- returnValue : function (r) {
- returnValue = r;
- },
- fake : function (fn) {
- fake = fn;
- },
- template : function (t) {
- template = makeTemplate(t);
- return template;
- },
- mock : function () {
- var call,
- i;
- call = {
- args : arguments,
- context : this
- };
- for (i = 0; i < arguments.length; i++) {
- if (typeof arguments[i] === 'function') {
- call.callback = arguments[i];
- }
- }
- pretendrObj.calls.push(call);
- if (this instanceof pretendrObj.mock) {
- 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 = template.generate();
- call.returned = call.pretendr.mock;
- } else {
- call.returned = returnValue;
- }
- return call.returned;
- }
- };
- return pretendrObj;
- };
+ pretendrFunction = function () {
+ var fake,
+ pretendrObj,
+ returnValue,
+ template;
+ pretendrObj = {
+ calls : [],
+ instances : [],
+ returnValue : function (r) {
+ returnValue = r;
+ },
+ fake : function (fn) {
+ fake = fn;
+ },
+ template : function (t) {
+ template = makeTemplate(t);
+ return template;
+ },
+ mock : function () {
+ var call,
+ i;
+ call = {
+ args : arguments,
+ context : this
+ };
+ for (i = 0; i < arguments.length; i++) {
+ if (typeof arguments[i] === 'function') {
+ call.callback = arguments[i];
+ }
+ }
+ pretendrObj.calls.push(call);
+ if (this instanceof pretendrObj.mock) {
+ 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 = template.generate();
+ call.returned = call.pretendr.mock;
+ } else {
+ call.returned = returnValue;
+ }
+ return call.returned;
+ }
+ };
+ return pretendrObj;
+ };
- pretendrPrimitive = function (val, parent, property) {
- var get,
- pretendrObj,
- set;
- pretendrObj = parent || {};
- get = function () {
- this.gets = this.gets + 1;
- return val;
- };
- set = function (v) {
- val = v;
- this.values.push(v);
- };
- if (Object.defineProperty) {
- pretendrObj.gets = 0;
- pretendrObj.values = [];
- Object.defineProperty(pretendrObj, property || 'mock', {
- get : get,
- set : set
- });
- } else {
- pretendrObj.mock = val;
- }
- return pretendrObj;
- };
+ pretendrPrimitive = function (val, parent, property) {
+ var get,
+ pretendrObj,
+ set;
+ pretendrObj = parent || {};
+ get = function () {
+ this.gets = this.gets + 1;
+ return val;
+ };
+ set = function (v) {
+ val = v;
+ this.values.push(v);
+ };
+ if (Object.defineProperty) {
+ pretendrObj.gets = 0;
+ pretendrObj.values = [];
+ Object.defineProperty(pretendrObj, property || 'mock', {
+ get : get,
+ set : set
+ });
+ } else {
+ pretendrObj.mock = val;
+ }
+ return pretendrObj;
+ };
- return pretendr(o, f);
+ return pretendr(o, f);
};
// hide the extra arg from the public
module.exports = function (o) {
- return pretendr(o);
+ return pretendr(o);
};
View
90 test/spec.js
@@ -191,51 +191,51 @@ describe("pretendr", function () {
expect(instance).to.be.a(this.p.mock);
});
});
- describe("#template", function () {
- it("should be able to define return values", function () {
- var m = this.pretendr(function () {}),
- res,
- t;
- t = m.template(function () {});
- t.returnValue('a');
- res = m.mock();
- expect(res()).to.equal('a');
- });
- it("should be able to define a fake", function () {
- var fake,
- m = this.pretendr(function () {}),
- res,
- t;
- t = m.template(function () {});
- fake = this.pretendr(function () {});
- t.fake(fake.mock);
- res = m.mock();
- res();
- expect(fake.calls).to.have.length(1);
- });
- it("should be able to have a template of its own", function () {
- var m = this.pretendr(function () {}),
- res,
- subT = { a : 'b' },
- t;
- t = m.template(function () {});
- t = t.template(subT);
- res = m.mock();
- res = res();
- expect(res).to.have.property('a', 'b');
- });
- 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("#template", function () {
+ it("should be able to define return values", function () {
+ var m = this.pretendr(function () {}),
+ res,
+ t;
+ t = m.template(function () {});
+ t.returnValue('a');
+ res = m.mock();
+ expect(res()).to.equal('a');
+ });
+ it("should be able to define a fake", function () {
+ var fake,
+ m = this.pretendr(function () {}),
+ res,
+ t;
+ t = m.template(function () {});
+ fake = this.pretendr(function () {});
+ t.fake(fake.mock);
+ res = m.mock();
+ res();
+ expect(fake.calls).to.have.length(1);
+ });
+ it("should be able to have a template of its own", function () {
+ var m = this.pretendr(function () {}),
+ res,
+ subT = { a : 'b' },
+ t;
+ t = m.template(function () {});
+ t = t.template(subT);
+ res = m.mock();
+ res = res();
+ expect(res).to.have.property('a', 'b');
+ });
+ 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 () {
var fn = function () {};

0 comments on commit c934979

Please sign in to comment.