[OpenLayers-Commits] r12211 - trunk/openlayers/tests/BaseTypes

commits-20090109 at openlayers.org commits-20090109 at openlayers.org
Mon Aug 8 00:58:06 EDT 2011


Author: erilem
Date: 2011-08-07 21:58:05 -0700 (Sun, 07 Aug 2011)
New Revision: 12211

Modified:
   trunk/openlayers/tests/BaseTypes/Class.html
Log:
add Class tests corresponding to IGN GeoPortal's usage patterns, no functional change

Modified: trunk/openlayers/tests/BaseTypes/Class.html
===================================================================
--- trunk/openlayers/tests/BaseTypes/Class.html	2011-08-08 04:50:36 UTC (rev 12210)
+++ trunk/openlayers/tests/BaseTypes/Class.html	2011-08-08 04:58:05 UTC (rev 12211)
@@ -308,6 +308,154 @@
         t.ok(!(bad instanceof OpenLayers.Control), "bad is a control, but it is also a layer and we cannot have two superclasses");
         t.ok(bad instanceof OpenLayers.Layer, "bad is a layer, it inherits from the layer first");
     }
+
+    //
+    // IGN's GeoPortal API overwrite prototypes of OpenLayers constructors.
+    // The tests below aim to cover their usage pattens.
+    //
+
+    // the overwrite function under test
+    function overwrite(C, o) {
+        if(typeof o.initialize === "function" &&
+            C === C.prototype.initialize) {
+            // OL 2.11
+
+            var proto = C.prototype;
+            var staticProps = OpenLayers.Util.extend({}, C);
+
+            C = o.initialize;
+
+            C.prototype = proto;
+            OpenLayers.Util.extend(C, staticProps);
+        }
+        OpenLayers.Util.extend(C.prototype, o);
+        return C;
+    }
+
+    function test_overwrite_1(t) {
+        // overwrite constructor
+        t.plan(1);
+        var A = OpenLayers.Class({
+            initialize: function() {
+                this.a = "foo";
+            }
+        });
+        A = overwrite(A, {
+            initialize: function() {
+                this.a = "bar";
+            }
+        });
+        var a = new A;
+        t.eq(a.a, "bar", "ctor overwritten");
+    }
+
+    function test_overwrite_2(t) {
+        // overwrite regular method
+        t.plan(1);
+        var A = OpenLayers.Class({
+            initialize: function() {
+            },
+            method: function() {
+                this.a = "foo";
+            }
+        });
+        A = overwrite(A, {
+            method: function() {
+                this.a = "bar";
+            }
+        });
+        var a = new A;
+        a.method();
+        t.eq(a.a, "bar", "method overwritten");
+    }
+
+    function test_overwrite_3(t) {
+        // overwrite constructor of subclass
+        t.plan(1);
+        var A = OpenLayers.Class({
+            initialize: function() {
+                this.a = "foo";
+            }
+        });
+        var B = OpenLayers.Class(A, {
+            initialize: function() {
+                A.prototype.initialize.call(this);
+            }
+        });
+        B = overwrite(B, {
+            initialize: function() {
+                A.prototype.initialize.call(this);
+                this.a = "bar";
+            }
+        });
+        var b = new B;
+        t.eq(b.a, "bar", "ctor overwritten");
+    }
+
+    function test_overwrite_4(t) {
+        // overwrite constructor of parent class
+        t.plan(1);
+        var A = OpenLayers.Class({
+            initialize: function() {
+                this.a = "foo";
+            }
+        });
+        var B = OpenLayers.Class(A, {
+            initialize: function() {
+                A.prototype.initialize.call(this);
+            }
+        });
+        A = overwrite(A, {
+            initialize: function() {
+                this.a = "bar";
+            }
+        });
+        var b = new B;
+        t.eq(b.a, "bar", "ctor overwritten");
+    }
+
+    // This test doesn't currently pass.
+    /*
+    function test_overwrite_5(t) {
+        // overwrite constructor of parent class, which itself
+        // doesn't defined "initialize"
+        t.plan(1);
+        var A = OpenLayers.Class({
+            initialize: function() {
+                this.a = "foo";
+            }
+        });
+        var B = OpenLayers.Class(A, {});
+        A = overwrite(A, {
+            initialize: function() {
+                this.a = "bar";
+            }
+        });
+        var b = new B;
+        t.eq(b.a, "bar", "ctor overwritten");
+    }
+    */
+
+    function test_overwrite_6(t) {
+        // with static methods
+        t.plan(1);
+        var A = OpenLayers.Class({
+            initialize: function() {
+            }
+        });
+        A.staticMethod = function() {};
+        A = overwrite(A, {
+            initialize: function() {
+            }
+        });
+        var exc = false;
+        try {
+            A.staticMethod();
+        } catch(e) {
+            exc = true;
+        }
+        t.ok(!exc, "static method still there");
+    }
   </script>
 </head>
 <body>



More information about the Commits mailing list