Leaflet/spec/suites/geometry/PointSpec.js

105 lines
3.1 KiB
JavaScript
Raw Normal View History

describe("Point", function() {
2013-02-05 19:51:27 +08:00
describe('constructor', function() {
2013-02-05 19:51:27 +08:00
it("creates a point with the given x and y", function() {
var p = new L.Point(1.5, 2.5);
expect(p.x).toEqual(1.5);
expect(p.y).toEqual(2.5);
});
2013-02-05 19:51:27 +08:00
it("rounds the given x and y if the third argument is true", function() {
var p = new L.Point(1.3, 2.7, true);
expect(p.x).toEqual(1);
expect(p.y).toEqual(3);
});
});
2013-02-05 19:51:27 +08:00
2010-09-13 19:58:03 +08:00
describe('#subtract', function() {
it('subtracts the given point from this one', function() {
2010-09-13 19:58:03 +08:00
var a = new L.Point(50, 30),
b = new L.Point(20, 10);
expect(a.subtract(b)).toEqual(new L.Point(30, 20));
});
});
2013-02-05 19:51:27 +08:00
2010-09-13 19:58:03 +08:00
describe('#add', function() {
it('adds given point to this one', function() {
2010-09-13 19:58:03 +08:00
expect(new L.Point(50, 30).add(new L.Point(20, 10))).toEqual(new L.Point(70, 40));
});
});
2013-02-05 19:51:27 +08:00
2010-09-13 19:58:03 +08:00
describe('#divideBy', function() {
it('divides this point by the given amount', function() {
2010-09-13 19:58:03 +08:00
expect(new L.Point(50, 30).divideBy(5)).toEqual(new L.Point(10, 6));
});
});
2013-02-05 19:51:27 +08:00
2010-09-15 21:45:02 +08:00
describe('#multiplyBy', function() {
it('multiplies this point by the given amount', function() {
2010-09-15 21:45:02 +08:00
expect(new L.Point(50, 30).multiplyBy(2)).toEqual(new L.Point(100, 60));
});
});
2013-02-05 19:51:27 +08:00
describe('#floor', function () {
it('returns a new point with floored coordinates', function () {
2013-02-05 19:51:27 +08:00
expect(new L.Point(50.56, 30.123).floor()).toEqual(new L.Point(50, 30));
});
});
describe('#distanceTo', function () {
it('calculates distance between two points', function () {
2013-02-05 19:51:27 +08:00
var p1 = new L.Point(0, 30);
var p2 = new L.Point(40, 0);
expect(p1.distanceTo(p2)).toEqual(50.0);
});
});
describe('#equals', function () {
it('returns true if points are equal', function () {
2013-02-05 19:51:27 +08:00
var p1 = new L.Point(20.4, 50.12);
var p2 = new L.Point(20.4, 50.12);
var p3 = new L.Point(20.5, 50.13);
expect(p1.equals(p2)).toBe(true);
expect(p1.equals(p3)).toBe(false);
});
});
2013-02-22 23:28:55 +08:00
describe('#contains', function () {
it('returns true if the point is bigger in absolute dimensions than the passed one', function () {
var p1 = new L.Point(50, 30),
p2 = new L.Point(-40, 20),
p3 = new L.Point(60, -20),
p4 = new L.Point(-40, -40);
expect(p1.contains(p2)).toBe(true);
expect(p1.contains(p3)).toBe(false);
expect(p1.contains(p4)).toBe(false);
});
});
2013-02-05 19:51:27 +08:00
describe('#toString', function () {
it('formats a string out of point coordinates', function () {
2013-02-05 19:51:27 +08:00
expect(new L.Point(50, 30) + '').toEqual('Point(50, 30)');
});
});
describe('L.point factory', function () {
it('leaves L.Point instances as is', function () {
2013-02-05 19:51:27 +08:00
var p = new L.Point(50, 30);
expect(L.point(p)).toBe(p);
});
it('creates a point out of three arguments', function () {
2013-02-05 19:51:27 +08:00
expect(L.point(50.1, 30.1, true)).toEqual(new L.Point(50, 30));
});
it('creates a point from an array of coordinates', function () {
2013-02-05 19:51:27 +08:00
expect(L.point([50, 30])).toEqual(new L.Point(50, 30));
});
it('does not fail on invalid arguments', function () {
2013-02-05 19:51:27 +08:00
expect(L.point(undefined)).toBe(undefined);
expect(L.point(null)).toBe(null);
});
});
});