Skip to content

Commit

Permalink
add Class tests corresponding to IGN GeoPortal's usage patterns, no f…
Browse files Browse the repository at this point in the history
…unctional change

git-svn-id: http://svn.openlayers.org/trunk/openlayers@12211 dc9f47b5-9b13-0410-9fdd-eb0c1a62fdaf
  • Loading branch information
Éric Lemoine committed Aug 8, 2011
1 parent d79aa2e commit 22001a4
Showing 1 changed file with 148 additions and 0 deletions.
148 changes: 148 additions & 0 deletions tests/BaseTypes/Class.html
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand Down

0 comments on commit 22001a4

Please sign in to comment.