Home page Forum Wiki Mail Jabber Devel NIX-FILES

AltLUG Git

Git-repositories of Altai Linux User Group

Allow objects with no prototype to tested against object literals.
authorDomenicDenicola <domenic@domenicdenicola.com>
Tue, 1 Nov 2011 22:49:49 +0000 (18:49 -0400)
committerDomenicDenicola <domenic@domenicdenicola.com>
Tue, 1 Nov 2011 22:49:49 +0000 (18:49 -0400)
I often use objects created with `Object.create(null)` as dictionaries, since then they don't have pre-filled dictionary entries for e.g. `constructor`, `hasOwnProperty`, `isPrototypeOf`, etc. However, in my tests, it is quite useful to test the dictionaries I create against object literals (which have `Object.prototype` as their prototype).

This patch makes an exemption to the rule that two objects must have the same constructor, for the case where one has `null` prototype and the other `Object.prototype` as its prototype.

qunit/qunit.js
test/same.js

index d630c43..9aeaf99 100644 (file)
@@ -1065,6 +1065,10 @@ QUnit.equiv = function () {
                }
        }
 
                }
        }
 
+       var getProto = Object.getPrototypeOf || function (obj) {
+               return obj.__proto__;
+       };
+
        var callbacks = function () {
 
                // for string, boolean, number and null
        var callbacks = function () {
 
                // for string, boolean, number and null
@@ -1154,7 +1158,13 @@ QUnit.equiv = function () {
                                // comparing constructors is more strict than using
                                // instanceof
                                if (a.constructor !== b.constructor) {
                                // comparing constructors is more strict than using
                                // instanceof
                                if (a.constructor !== b.constructor) {
-                                       return false;
+                                       // Allow objects with no prototype to be equivalent to
+                                       // objects with Object as their constructor.
+                                       if (!((getProto(a) === null && getProto(b) === Object.prototype) ||
+                                                 (getProto(b) === null && getProto(a) === Object.prototype)))
+                                       {
+                                               return false;
+                                       }
                                }
 
                                // stack constructor before traversing properties
                                }
 
                                // stack constructor before traversing properties
index fa7e3eb..3783275 100644 (file)
@@ -136,6 +136,17 @@ test("Objects Basics.", function() {
             a: [{ bat: undefined }]
         }
     ), false);
             a: [{ bat: undefined }]
         }
     ), false);
+
+    // Objects with no prototype, created via Object.create(null), are used e.g. as dictionaries.
+    // Being able to test equivalence against object literals is quite useful.
+    if (Object.create) {
+        equals(QUnit.equiv(Object.create(null), {}), true, "empty object with no prototype VS empty object");
+
+        var nonEmptyWithNoProto = Object.create(null);
+        nonEmptyWithNoProto.foo = "bar";
+
+        equals(QUnit.equiv(nonEmptyWithNoProto, { foo: "bar" }), true, "nonempty object with no prototype VS empty object");
+    }
 });
 
 
 });
 
 

Hosting is provided AltSPAHow to use Git
Please direct any questions on jabber alex.wolf@jabber.ru

Valid HTML 4.0 Transitional Valid CSS!

Designer Alexander Wolf