Add geom.Collection & geom.collection.

Also make MultiPoint and multipoint inherit from Collection/collection
respectively.
This commit is contained in:
Marc Jansen
2012-06-21 15:53:46 +02:00
parent 6ca9112af2
commit 0374d1abc7
6 changed files with 499 additions and 0 deletions

View File

@@ -0,0 +1,185 @@
describe("ol.geom.collection", function() {
var c;
beforeEach(function() {
c = ol.geom.collection([
ol.geom.point([0, 1]),
ol.geom.linestring([
ol.geom.point([2, 3]),
ol.geom.point([4, 5])
])
]);
});
afterEach(function() {
c = null;
});
describe("can construct instances with some components", function() {
it("works for instances of ol.geom.Geometry", function(){
expect( c ).toBeA( ol.geom.Collection );
});
});
describe("can construct instances without any components", function() {
it("works with an empty array", function(){
c = ol.geom.collection([]);
expect( c ).toBeA( ol.geom.Collection );
});
it("works without arguments", function(){
c = ol.geom.collection();
expect( c ).toBeA( ol.geom.Collection );
});
});
describe("the method 'add'", function() {
it("exists", function(){
expect( c.add ).toBeA( Function );
});
describe("can be used as setter", function(){
it("works with a single point specification and an index", function(){
var p = ol.geom.point([24,7]);
c.add(p, 0);
expect(c.components().length).toBe(3);
var firstComponent = c.components()[0];
expect( firstComponent.x() + ',' + firstComponent.y() ).toBe( '24,7' );
});
it("the index is functional", function(){
var p = ol.geom.point([24,7]);
c.add(p, 1);
expect(c.components().length).toBe(3);
var firstComponent = c.components()[0], // untouched
secondComponent = c.components()[1], // this should be ours
thirdComponent = c.components()[2]; // shifted here
expect( firstComponent.x() + ',' + firstComponent.y() ).toBe( '0,1' );
expect( secondComponent.x() + ',' + secondComponent.y() ).toBe( '24,7' );
expect( thirdComponent ).toBeA( ol.geom.LineString );
});
it("the index is optional", function(){
var p = ol.geom.point([24,7]);
c.add(p);
expect(c.components().length).toBe(3);
var thirdComponent = c.components()[2];
expect( thirdComponent.x() + ',' + thirdComponent.y() ).toBe( '24,7' );
});
it("returns the collection instance", function(){
var p = ol.geom.point([24,7]);
var returned = c.add(p);
expect(returned).toBe(c);
});
});
});
describe("the method 'addAll'", function(){
it("exists", function(){
expect( c.addAll ).toBeA( Function );
});
describe("can be used as setter", function(){
it("works with an array of points and an index", function(){
var ps = [
ol.geom.point([24,7]),
ol.geom.point([7,11])
];
c.addAll(ps, 0);
expect(c.components().length).toBe(4);
var firstComponent = c.components()[0],
secondComponent = c.components()[1];
expect( firstComponent.x() + ',' + firstComponent.y() ).toBe( '24,7' );
expect( secondComponent.x() + ',' + secondComponent.y() ).toBe( '7,11' );
});
it("the index is functional", function(){
var ps = [
ol.geom.point([24,7]),
ol.geom.point({x:7, y:11})
];
c.addAll(ps, 1);
expect(c.components().length).toBe(4);
var firstComponent = c.components()[0], // untouched
secondComponent = c.components()[1], // this should be ours
thirdComponent = c.components()[2], // this should be ours
fourthComponent = c.components()[3]; // shifted here
expect( firstComponent.x() + ',' + firstComponent.y() ).toBe( '0,1' );
expect( secondComponent.x() + ',' + secondComponent.y() ).toBe( '24,7' );
expect( thirdComponent.x() + ',' + thirdComponent.y() ).toBe( '7,11' );
expect( fourthComponent ).toBeA( ol.geom.LineString );
});
it("the index is optional", function(){
var ps = [
ol.geom.point([24,7]),
ol.geom.point({x:7, y:11})
];
c.addAll(ps);
expect(c.components().length).toBe(4);
var thirdComponent = c.components()[2],
fourthComponent = c.components()[3];
expect( thirdComponent.x() + ',' + thirdComponent.y() ).toBe( '24,7' );
expect( fourthComponent.x() + ',' + fourthComponent.y() ).toBe( '7,11' );
});
it("returns the collection instance", function(){
var ps = [
ol.geom.point([24,7]),
ol.geom.point({x:7, y:11})
];
var returned = c.addAll(ps);
expect(returned).toBe(c);
});
});
});
describe("the method 'remove'", function() {
it("exists", function(){
expect( c.add ).toBeA( Function );
});
it("works with a single point", function(){
var p = c.components()[0];
c.remove(p);
expect(c.components().length).toBe(1);
var firstComponent = c.components()[0];
expect( firstComponent ).toBeA( ol.geom.LineString );
});
it("works with an array of point specifications", function(){
var ps = [
c.components()[1],
c.components()[0]
];
c.remove(ps);
expect(c.components().length).toBe(0);
});
});
});

View File

@@ -0,0 +1,114 @@
describe("ol.geom.Collection", function() {
var c;
beforeEach(function(){
c = new ol.geom.Collection([
new ol.geom.Point(10,20),
new ol.geom.LineString([
new ol.geom.Point(47,11),
new ol.geom.Point(3.14,2.8)
])
]);
});
afterEach(function(){
c = null;
});
it("constructs instances", function() {
expect( c ).toBeA( ol.geom.Collection );
});
it("can construct instances without any components", function() {
// empty array
c = new ol.geom.Collection([]);
expect( c ).toBeA( ol.geom.Collection );
// no argument at all
c = new ol.geom.Collection();
expect( c ).toBeA( ol.geom.Collection );
});
it("inherits from ol.geom.Geometry", function() {
expect( c ).toBeA( ol.geom.Geometry );
});
it("has a working getter for components", function() {
var components = c.getComponents();
expect( components ).toBeA( Array );
expect( components.length ).toBe( 2 );
expect( components[0] ).toBeA( ol.geom.Point );
expect( components[1] ).toBeA( ol.geom.LineString );
expect( components[0].getX() + ',' + components[0].getY()).toBe( '10,20' );
expect( components[1].getVertices()[0].getX() + ',' + components[1].getVertices()[0].getY()).toBe( '47,11' );
});
it("has a working setter for components", function() {
c.setComponents([
new ol.geom.Point(30,40),
new ol.geom.LineString([
new ol.geom.Point(3,9),
new ol.geom.Point(4,16)
])
]);
var components = c.getComponents();
expect( components.length ).toBe( 2 );
expect( components[0] ).toBeA( ol.geom.Point );
expect( components[1] ).toBeA( ol.geom.LineString );
expect( components[0].getX() + ',' + components[0].getY()).toBe( '30,40' );
expect( components[1].getVertices()[0].getX() + ',' + components[1].getVertices()[0].getY()).toBe( '3,9' );
});
it("has a method to add components", function() {
c.addComponent(
new ol.geom.Point(30,40),
1
);
c.addComponent(
new ol.geom.LineString([
new ol.geom.Point(5,25),
new ol.geom.Point(6,36)
]),
0
);
var components = c.getComponents();
expect( components.length ).toBe( 4 );
expect( components[0].getVertices()[0].getX() + ',' + components[0].getVertices()[0].getY()).toBe( '5,25' );
expect( components[1].getX() + ',' + components[1].getY()).toBe( '10,20' );
expect( components[2].getX() + ',' + components[2].getY()).toBe( '30,40' );
expect( components[3].getVertices()[0].getX() + ',' + components[3].getVertices()[0].getY()).toBe( '47,11' );
});
it("has a method to remove components", function() {
c.setComponents([
new ol.geom.Point(0,10),
new ol.geom.Point(10,20),
new ol.geom.Point(20,30),
new ol.geom.Point(30,40)
]);
var p = c.getComponents()[2]; // 20,30;
c.removeComponent( p );
var components = c.getComponents();
expect( components.length ).toBe( 3 );
expect( components[0].getX() + ',' + components[0].getY()).toBe( '0,10' );
expect( components[1].getX() + ',' + components[1].getY()).toBe( '10,20' );
expect( components[2].getX() + ',' + components[2].getY()).toBe( '30,40' );
});
});