Browse files

Merge branch 'make-primitives-work-again'

Conflicts:
	lib/pretendr.js
  • Loading branch information...
2 parents 92fba77 + 0b7762e commit 5b8ddaf14e5af57d621be807394373b06a7fc17a @nathanmacinnes committed Aug 6, 2012
Showing with 62 additions and 23 deletions.
  1. +56 −18 lib/pretendr.js
  2. +6 −5 test/spec.js
View
74 lib/pretendr.js
@@ -4,10 +4,10 @@
var nonPrimitive, // fn - whether to enumerate the properties and recurse
makeTemplate, // fn - get a template object
- makePretendr,
- makeMock,
+ makePretendr, // fn - the main function whose result is returned
+ makeMock, // fn - make a mock property of the pretendr function
pretendr, // fn - called by module.exports
- pretendrFunction,
+ pretendrFunction, // fn - use for the mock property when it's a function
templateIns = [], // list of template inputs
templateOuts = []; // template outputs they match to prevent infinit loops
@@ -19,19 +19,19 @@ nonPrimitive = function (o) {
};
makePretendr = function (descriptor, mock, asTemplate) {
- var ins = [],
- outs = [],
- subMakePretendr;
-
- subMakePretendr = function (descriptor, mock) {
- var attributes = {},
- baseObj = {},
- makeCopy,
+ var ins = [], // list of descriptors which have been passed in
+ outs = [], // corresponding list of outputs
+ subMakePretendr; // sub function so that ins and outs aren't re-made
+
+ subMakePretendr = function (descriptor, mock, parentMock, propertyName) {
+ var attributes = {}, // the returnValue, template, etc.
+ baseObj = {}, // the object returned to the user
i,
- indexNum,
- setFake,
- setReturnValue,
- setTemplate;
+ indexNum, // the element number of the ins/outs array
+ propertyDefinition, // the object passed to defineProperty
+ setFake, // the fake setter
+ setReturnValue, // the returnValue setter
+ setTemplate; // the template setter
// check we haven't already created the pretendr object to prevent
// infinite recursion
@@ -97,18 +97,56 @@ makePretendr = function (descriptor, mock, asTemplate) {
baseObj.returnValue = setReturnValue;
baseObj.template = setTemplate;
}
-
+
if (!asTemplate) {
// create the mocks, either based on the object passed in (for
// creating instances) or using the makeMock function
- baseObj.mock = mock || makeMock(descriptor, baseObj, attributes);
+ mock = mock || makeMock(descriptor, baseObj, attributes);
+
+ if (Object.defineProperty) {
+ baseObj.gets = 0;
+ baseObj.values = [];
+
+ propertyDefinition = {
+ get : function () {
+ baseObj.gets++;
+ return mock;
+ },
+ set : function (val) {
+ baseObj.values.push(val);
+ mock = val;
+ }
+ };
+
+ if (parentMock) {
+ // Add monitoring to mock in parent context
+ Object.defineProperty(
+ parentMock,
+ propertyName,
+ propertyDefinition
+ );
+ }
+ // Add monitoring to mock in the base object context
+ Object.defineProperty(baseObj, 'mock', propertyDefinition);
+ } else {
+ baseObj.mock = mock;
+ }
}
if (nonPrimitive(descriptor)) {
for (i in descriptor) {
if (descriptor.hasOwnProperty(i)) {
- baseObj[i] = subMakePretendr(descriptor[i]);
+ baseObj[i] = subMakePretendr(
+ descriptor[i],
+ null,
+ baseObj.mock,
+ i
+ );
if (!asTemplate) {
baseObj.mock[i] = baseObj[i].mock;
+
+ // reset gets as it'll have been incremented
+ // during processing
+ baseObj[i].gets = 0;
}
}
}
View
11 test/spec.js
@@ -271,29 +271,30 @@ describe("pretendr", function () {
expect(this.str.mock).to.equal('a string');
expect(this.bool.mock).to.equal(true);
});
- xit("should return the number of gets", function () {
+ it("should return the number of gets", function () {
expect(this.num.gets).to.equal(0);
var assigned = this.num.mock;
expect(this.num.gets).to.equal(1);
});
- xit("should record the value changes", function () {
+ it("should record the value changes", function () {
expect(this.num.values).to.have.length(0);
this.num.mock = 6;
expect(this.num.values).to.have.property(0, 6);
});
- xit("should not try to return the gets if not supported", function () {
+ it("should not try to return the gets if not supported", function () {
var prim;
Object.defineProperty = undefined;
prim = this.pretendr(4);
expect(prim.mock).to.equal(4);
expect(prim).to.not.have.property('gets');
expect(prim).to.not.have.property('values');
});
- xit("should return gets on primitives in object context", function () {
+ it("should return gets on primitives in object context", function () {
var b,
prim = this.pretendr({
primitive : 'a'
});
+ expect(prim.primitive.gets).to.equal(0);
b = prim.mock.primitive;
expect(prim.primitive.gets).to.equal(1);
b = prim.mock.primitive;
@@ -384,7 +385,7 @@ describe("pretendr", function () {
.and.not.to.equal(this.objToMock)
.and.to.have.length(this.objToMock.length);
});
- xit("should mock the elements", function () {
+ it("should mock the elements", function () {
var assigned = this.pretendrResult.mock[0];
expect(this.pretendrResult[0].gets).to.equal(1);
assigned = this.pretendrResult.mock[2]();

0 comments on commit 5b8ddaf

Please sign in to comment.