Skip to content

Commit

Permalink
Add tddjs.each to make the properties whose prototype chain has DontE…
Browse files Browse the repository at this point in the history
…num property but shadowed in its own object be shadowed correctly
  • Loading branch information
kaznum committed Aug 29, 2012
1 parent 4f4d542 commit d5b7fac
Show file tree
Hide file tree
Showing 2 changed files with 106 additions and 0 deletions.
69 changes: 69 additions & 0 deletions 7/src/tdd.js
Expand Up @@ -66,5 +66,74 @@ tddjs.isOwnProperty = (function () {
};
} else {
// Provide an emulation if you can live with possibly inaccurate results
throw new TypeError("hasOwnProperty is not a function");
}
}());

tddjs.each = (function () {
function unEnumerated(object, properties) {
var length = properties.length;

// initialize
for (var i = 0; i < length; i++) {
object[properties[i]] = true;
}

var enumerated = length;
for (var prop in object) {
if (tddjs.isOwnProperty(object, prop)) {
enumerated--;
object[prop] = false;
}
}

if (!enumerated) {
return;
}

var needsFix = [];

for(i = 0; i < length; i++) {
if(object[properties[i]]) {
needsFix.push(properties[i]);
}
}

return needsFix;
}

var oFixes = unEnumerated({}, ["toString", "toLocaleString",
"valueOf", "hasOwnProperty",
"isPrototypeOf", "constructor",
"propertyIsEnumerable"]);
var fFixes = unEnumerated(function () {}, ["call", "apply", "prototype"]);
if (fFixes && oFixes) {
fFixes = oFixes.concat(fFixes);
}

var needsFix = { "object": oFixes, "function": fFixes };

return function (object, callback) {
if (typeof callback != "function") {
throw new TypeError("callback is not a function");
}

for (var prop in object) {
if (tddjs.isOwnProperty(object, prop)) {
callback(prop, object[prop]);
}
}

var fixes = needsFix[typeof object];
if (fixes) {
var property;
for(var i = 0, l = fixes.length; i < l; i++) {
property = fixes[i];

if (tddjs.isOwnProperty(object, property)) {
callback(property, object[property]);
}
}
}
};
}());
37 changes: 37 additions & 0 deletions 7/test/property_enumeration_test.js
@@ -0,0 +1,37 @@
TestCase("PropertyEnumerationTest", {
"test should enumerate shadowed object properties" : function () {
var object = {
toString: "toString",
toLocaleString: "toLocaleString",
valueOf: "valueOf",
hasOwnProperty: "hasOwnProperty",
isPrototypeOf: "isPrototypeOf",
propertyIsEnumerable: "propertyIsEnumerable",
constructor: "constructor",
bar: "bar"
};

var result = [];

tddjs.each(object, function (property) {
result.push(property);
});

assertEquals(8, result.length);
},

"test should enumerate shadowed function properties" : function () {
var object = function () {};
object.prototype = "prototype";
object.prototype = true;
object.call = "call";
object.apply = "apply";
object.foo = "foo";
var result = [];
tddjs.each(object, function (property) {
result.push(property);
});

assertEquals(["apply", "call", "foo", "prototype"], result.sort());
}
});

0 comments on commit d5b7fac

Please sign in to comment.