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.
[qunit.git] / test / same.js
index 8f1b563..3783275 100644 (file)
@@ -69,7 +69,7 @@ test("Primitive types and constants", function () {
     equals(QUnit.equiv('', false), false, "string");
     equals(QUnit.equiv('', null), false, "string");
     equals(QUnit.equiv('', undefined), false, "string");
-    
+
     // Short annotation VS new annotation
     equals(QUnit.equiv(0, new Number()), true, "short annotation VS new annotation");
     equals(QUnit.equiv(new Number(), 0), true, "short annotation VS new annotation");
@@ -136,6 +136,17 @@ test("Objects Basics.", function() {
             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");
+    }
 });
 
 
@@ -344,7 +355,7 @@ test("Date instances.", function() {
     // Date, we don't need to test Date.parse() because it returns a number.
     // Only test the Date instances by setting them a fix date.
     // The date use is midnight January 1, 1970
-    
+
     var d1 = new Date();
     d1.setTime(0); // fix the date
 
@@ -451,7 +462,7 @@ test("Complex Objects.", function() {
     function fn2() {
         return "fn2";
     }
-    
+
     // Try to invert the order of some properties to make sure it is covered.
     // It can failed when properties are compared between unsorted arrays.
     equals(QUnit.equiv(
@@ -1177,15 +1188,15 @@ test("Prototypal inheritance", function() {
 
 
 test("Instances", function() {
-    function A() {} 
-    var a1 = new A(); 
-    var a2 = new A(); 
+    function A() {}
+    var a1 = new A();
+    var a2 = new A();
 
     function B() {
         this.fn = function () {};
-    } 
-    var b1 = new B(); 
-    var b2 = new B(); 
+    }
+    var b1 = new B();
+    var b2 = new B();
 
     equals(QUnit.equiv(a1, a2), true, "Same property, same constructor");
 
@@ -1335,7 +1346,7 @@ test("Complex Instances Nesting (with function value in literals and/or in neste
 
     var b1 = new B(function () {});
     var b2 = new B(function () {});
-    equals(QUnit.equiv(a1, a2), true);
+    equals(QUnit.equiv(b1, b2), true);
 
     var c1 = new C(function () {});
     var c2 = new C(function () {});
@@ -1361,52 +1372,51 @@ test('object with references to self wont loop', function(){
     circularA.abc = circularA;
     circularB.abc = circularB;
     equals(QUnit.equiv(circularA, circularB), true, "Should not repeat test on object (ambigous test)");
-    
+
     circularA.def = 1;
     circularB.def = 1;
     equals(QUnit.equiv(circularA, circularB), true, "Should not repeat test on object (ambigous test)");
-    
+
     circularA.def = 1;
     circularB.def = 0;
     equals(QUnit.equiv(circularA, circularB), false, "Should not repeat test on object (unambigous test)");
 });
 
 test('array with references to self wont loop', function(){
-    var circularA = [], 
+    var circularA = [],
         circularB = [];
     circularA.push(circularA);
     circularB.push(circularB);
     equals(QUnit.equiv(circularA, circularB), true, "Should not repeat test on array (ambigous test)");
-    
+
     circularA.push( 'abc' );
     circularB.push( 'abc' );
     equals(QUnit.equiv(circularA, circularB), true, "Should not repeat test on array (ambigous test)");
-    
+
     circularA.push( 'hello' );
     circularB.push( 'goodbye' );
     equals(QUnit.equiv(circularA, circularB), false, "Should not repeat test on array (unambigous test)");
 });
 
 test('mixed object/array with references to self wont loop', function(){
-    var circularA = [{abc:null}], 
+    var circularA = [{abc:null}],
         circularB = [{abc:null}];
     circularA[0].abc = circularA;
     circularB[0].abc = circularB;
-    
+
     circularA.push(circularA);
     circularB.push(circularB);
     equals(QUnit.equiv(circularA, circularB), true, "Should not repeat test on object/array (ambigous test)");
-    
+
     circularA[0].def = 1;
     circularB[0].def = 1;
     equals(QUnit.equiv(circularA, circularB), true, "Should not repeat test on object/array (ambigous test)");
-    
+
     circularA[0].def = 1;
     circularB[0].def = 0;
     equals(QUnit.equiv(circularA, circularB), false, "Should not repeat test on object/array (unambigous test)");
 });
 
-
 test("Test that must be done at the end because they extend some primitive's prototype", function() {
     // Try that a function looks like our regular expression.
     // This tests if we check that a and b are really both instance of RegExp
@@ -1420,4 +1430,3 @@ test("Test that must be done at the end because they extend some primitive's pro
     // between RegExp and Function constructor because typeof on a RegExpt instance is "function"
     equals(QUnit.equiv(function () {}, re), false, "Same conversely, but ensures that function and regexp are distinct because their constructor are different");
 });
-

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