diff --git a/tests/BaseTypes/Class.html b/tests/BaseTypes/Class.html index 17a038c450..228c8765d1 100644 --- a/tests/BaseTypes/Class.html +++ b/tests/BaseTypes/Class.html @@ -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"); + }