Adding test spec.

This commit is contained in:
Tim Schaub
2012-06-18 18:15:51 +02:00
parent 62f3f70910
commit b9d690044f
9 changed files with 3650 additions and 0 deletions

View File

@@ -0,0 +1,31 @@
describe("ol.Bounds", function() {
it("allows flexible construction", function() {
var bounds, proj;
// array with min/max
bounds = ol.bounds([1, 2, 3, 4]);
expect(bounds.minX()).toBe(1);
expect(bounds.minY()).toBe(2);
expect(bounds.maxX()).toBe(3);
expect(bounds.maxY()).toBe(4);
// object config
bounds = ol.bounds({
minX: 9, maxX: 10, minY: 11, maxY: 12,
projection: ol.projection("bar")
});
expect(bounds.minX()).toBe(9);
expect(bounds.maxX()).toBe(10);
expect(bounds.minY()).toBe(11);
expect(bounds.maxY()).toBe(12);
proj = bounds.projection();
expect(proj instanceof ol.Projection).toBe(true);
expect(proj.code()).toBe("bar");
});
});

View File

@@ -0,0 +1,125 @@
describe("ol.Location", function() {
it("allows flexible construction", function() {
var loc;
// nowhere
loc = ol.loc();
expect(loc instanceof ol.Location).toBe(true);
// obj config
loc = ol.loc({x: 10, y: 20});
expect(loc.x()).toBe(10);
expect(loc.y()).toBe(20);
// array config
loc = ol.loc([30, 40]);
expect(loc.x()).toBe(30);
expect(loc.y()).toBe(40);
});
it("accounts for the third dimension", function() {
var loc;
// obj config
loc = ol.loc({x: 10, y: 20, z: 30});
expect(loc.z()).toBe(30);
// array config
loc = ol.loc([40, 50, 60]);
expect(loc.z()).toBe(60);
});
it("is mutable", function() {
var loc = ol.loc({x: 10, y: 20, z: 15});
loc.z(30);
expect(loc.x()).toBe(10);
expect(loc.y()).toBe(20);
expect(loc.z()).toBe(30);
});
it("has no default projection", function() {
var loc = ol.loc({x: 1, y: 2});
expect(loc.projection()).toBeUndefined();
});
it("allows projection to be set", function() {
var proj;
// at construction
var loc = ol.loc({x: 1, y: 2, projection: "EPSG:4326"});
proj = loc.projection();
expect(proj instanceof ol.Projection).toBe(true);
expect(proj.code()).toBe("EPSG:4326");
// after construction
loc.projection("EPSG:3857");
expect(loc.projection().code()).toBe("EPSG:3857");
// setting projection does not transform coordinates
expect(loc.x()).toBe(1);
expect(loc.y()).toBe(2);
});
it("can be transformed (to mercator)", function() {
var loc = ol.loc({x: 10, y: 20, projection: "EPSG:4326"});
var trans = loc.transform("EPSG:3857");
expect(trans instanceof ol.Location).toBe(true);
expect(trans.projection().code()).toBe("EPSG:3857");
expect(trans.x().toFixed(3)).toBe("1113194.908");
expect(trans.y().toFixed(3)).toBe("2273030.927");
expect(loc.x().toFixed(3)).toBe("10.000");
expect(loc.y().toFixed(3)).toBe("20.000");
});
it("can be transformed (to geographic)", function() {
var loc = ol.loc({x: 1113195, y: 2273031, projection: "EPSG:3857"});
var trans = loc.transform("EPSG:4326");
expect(trans instanceof ol.Location).toBe(true);
expect(trans.projection().code()).toBe("EPSG:4326");
expect(trans.x().toFixed(3)).toBe("10.000");
expect(trans.y().toFixed(3)).toBe("20.000");
});
it("should not be transformable if it has no projection", function() {
var loc = ol.loc([1, 2]);
expect(function() {
loc.transform("EPSG:4326");
}).toThrow();
});
it("is destroyable", function() {
var loc = ol.loc([1, 2]);
loc.destroy();
expect(loc.config).toBeUndefined();
expect(function() {
loc.x();
}).toThrow();
});
});

150
test/spec/ol/Map.test.js Normal file
View File

@@ -0,0 +1,150 @@
describe("ol.Map", function() {
// HPI - Hipster Programming Interface
// EPI - Enterprise Programming Interface
it("should be easy to make a map", function() {
var map = ol.map();
expect(map instanceof ol.Map).toBe(true);
});
it("should be easy to set the map center", function() {
var map, center;
// with array
map = ol.map();
map.center([-110, 45]);
center = map.center();
expect(center.x().toFixed(3)).toBe("-110.000");
expect(center.y().toFixed(3)).toBe("45.000");
expect(center instanceof ol.Location).toBe(true);
// with object literal
map.center({x: -111, y: 46});
center = map.center();
expect(center.x().toFixed(3)).toBe("-111.000");
expect(center.y().toFixed(3)).toBe("46.000");
expect(center instanceof ol.Location).toBe(true);
// more verbose
map = ol.map({
center: ol.loc({x: -112, y: 47})
});
center = map.center();
expect(center.x().toFixed(3)).toBe("-112.000");
expect(center.y().toFixed(3)).toBe("47.000");
expect(center instanceof ol.Location).toBe(true);
});
it("allows flexible setting of center and zoom", function() {
var map, center, zoom;
// chained
map = ol.map().center([1, 2]).zoom(3);
center = map.center();
zoom = map.zoom();
expect(center.x().toFixed(3)).toBe("1.000");
expect(center.y().toFixed(3)).toBe("2.000");
expect(zoom).toBe(3);
// all at once
map = ol.map({
center: [1, 2],
zoom: 6
});
center = map.center();
zoom = map.zoom();
expect(center.x().toFixed(3)).toBe("4.000");
expect(center.y().toFixed(3)).toBe("5.000");
expect(zoom).toBe(6);
});
it("has a default projection", function() {
var map = ol.map();
var proj = map.projection();
expect(proj instanceof ol.Projection).toBe(true);
expect(proj.code()).toBe("EPSG:3857");
});
it("allows projection to be set", function() {
var proj;
// at construction
var map = ol.map({projection: "EPSG:4326"});
proj = map.projection();
expect(proj instanceof ol.Projection).toBe(true);
expect(proj.code()).toBe("EPSG:4326");
// after construction
map.projection("EPSG:3857");
proj = map.projection();
expect(proj instanceof ol.Projection).toBe(true);
expect(proj.code()).toBe("EPSG:3857");
});
it("allows a user projection to be set", function() {
var proj;
var map = ol.map();
proj = map.userProjection();
expect(proj instanceof ol.Projection).toBe(true);
expect(proj.code()).toBe("EPSG:4326");
map.center([10, 20]);
map.userProjection("EPSG:3857");
var center = map.center();
expect(center.x().toFixed(3)).toBe("1113194.908");
expect(center.y().toFixed(3)).toBe("2273030.927");
});
it("provides feedback when you mess up", function() {
var map;
if (ol.DEBUG) {
// misspelling
expect(function() {
map = ol.map({
centre: [1, 2]
});
}).toThrow(new Error("Unsupported config property: centre"));
} else {
expect(function() {
map = ol.map({
centre: [1, 2]
});
}).not.toThrow();
}
});
it("is destroyable", function() {
var map = ol.map();
map.center([1, 2]);
map.destroy();
expect(function() {
map.center([3, 4]);
}).toThrow();
});
});

View File

@@ -0,0 +1,42 @@
describe("ol.Projection", function() {
it("constructs instances", function() {
var p;
p = ol.projection("foo");
expect(p.code()).toBe("foo");
p = ol.projection({code: "bar"});
expect(p.code()).toBe("bar");
});
it("allows units to be set", function() {
var p;
p = ol.projection("foo");
expect(p.units()).toBeUndefined();
p = ol.projection({code: "foo", units: "m"});
expect(p.units()).toBe("m");
});
it("handles transforms", function() {
var orig = {x: 10, y: 20, z: 30};
var point = ol.Projection.transform(orig, "EPSG:4326", "EPSG:900913");
expect(point.x.toFixed(3)).toBe("1113194.908");
expect(point.y.toFixed(3)).toBe("2273030.927");
expect(point.z).toBe(30);
// original remains unchanged
expect(orig.x).toBe(10);
expect(orig.y).toBe(20);
expect(orig.z).toBe(30);
});
});