Skip to content

Commit

Permalink
Enforce LngLatLike param in LngLat.convert (#3232)
Browse files Browse the repository at this point in the history
  • Loading branch information
Lauren Budorick committed Sep 21, 2016
1 parent 0068728 commit 56e9761
Show file tree
Hide file tree
Showing 6 changed files with 74 additions and 5 deletions.
8 changes: 5 additions & 3 deletions js/geo/lng_lat.js
Expand Up @@ -82,9 +82,11 @@ LngLat.prototype.toString = function () {
LngLat.convert = function (input) {
if (input instanceof LngLat) {
return input;
}
if (Array.isArray(input)) {
} else if (input && input.hasOwnProperty('lng') && input.hasOwnProperty('lat')) {
return new LngLat(input.lng, input.lat);
} else if (Array.isArray(input) && input.length === 2) {
return new LngLat(input[0], input[1]);
} else {
throw new Error("`LngLatLike` argument must be specified as a LngLat instance, an object {lng: <lng>, lat: <lat>}, or an array of [<lng>, <lat>]");
}
return input;
};
9 changes: 8 additions & 1 deletion js/geo/lng_lat_bounds.js
Expand Up @@ -80,7 +80,14 @@ LngLatBounds.prototype = {
if (!sw2 || !ne2) return this;

} else {
return obj ? this.extend(LngLat.convert(obj) || LngLatBounds.convert(obj)) : this;
if (Array.isArray(obj)) {
if (obj.every(Array.isArray)) {
return this.extend(LngLatBounds.convert(obj));
} else {
return this.extend(LngLat.convert(obj));
}
}
return this;
}

if (!sw && !ne) {
Expand Down
6 changes: 5 additions & 1 deletion test/js/geo/lng_lat.test.js
Expand Up @@ -23,8 +23,12 @@ test('LngLat', function(t) {

t.test('#convert', function(t) {
t.ok(LngLat.convert([0, 10]) instanceof LngLat, 'convert creates a LngLat instance');
t.ok(LngLat.convert({lng: 0, lat: 10}) instanceof LngLat, 'convert creates a LngLat instance');
t.ok(LngLat.convert({lng: 0, lat: 0}) instanceof LngLat, 'convert creates a LngLat instance');
t.ok(LngLat.convert(new LngLat(0, 0)) instanceof LngLat, 'convert creates a LngLat instance');
t.equal(LngLat.convert('othervalue'), 'othervalue', 'passes through other values');
t.throws(function() {
LngLat.convert(0, 10);
}, "`LngLatLike` argument must be specified as a LngLat instance, an object {lng: <lng>, lat: <lat>}, or an array of [<lng>, <lat>]", 'detects and throws on invalid input');
t.end();
});

Expand Down
15 changes: 15 additions & 0 deletions test/js/geo/lng_lat_bounds.test.js
Expand Up @@ -55,6 +55,13 @@ test('LngLatBounds', function(t) {
t.equal(bounds.getNorth(), 10);
t.equal(bounds.getEast(), 10);

bounds.extend(new LngLat(-15, -15));

t.equal(bounds.getSouth(), -15);
t.equal(bounds.getWest(), -15);
t.equal(bounds.getNorth(), 10);
t.equal(bounds.getEast(), 10);

t.end();
});

Expand All @@ -68,6 +75,14 @@ test('LngLatBounds', function(t) {
t.equal(bounds1.getNorth(), 10);
t.equal(bounds1.getEast(), 10);

var bounds3 = [[-15, -15], [15, 15]];
bounds1.extend(bounds3);

t.equal(bounds1.getSouth(), -15);
t.equal(bounds1.getWest(), -15);
t.equal(bounds1.getNorth(), 15);
t.equal(bounds1.getEast(), 15);

t.end();
});

Expand Down
30 changes: 30 additions & 0 deletions test/js/ui/camera.test.js
Expand Up @@ -35,6 +35,13 @@ test('camera', function(t) {
t.end();
});

t.test('throws on invalid center argument', function(t) {
t.throws(function() {
camera.jumpTo({center: 1});
}, Error, 'throws with non-LngLatLike argument');
t.end();
});

t.test('keeps current center if not specified', function(t) {
camera.jumpTo({});
t.deepEqual(camera.getCenter(), { lng: 1, lat: 2 });
Expand Down Expand Up @@ -144,6 +151,13 @@ test('camera', function(t) {
t.end();
});

t.test('throws on invalid center argument', function(t) {
t.throws(function() {
camera.jumpTo({center: 1});
}, Error, 'throws with non-LngLatLike argument');
t.end();
});

t.test('emits move events, preserving eventData', function(t) {
var started, moved, ended,
eventData = { data: 'ok' };
Expand Down Expand Up @@ -305,6 +319,14 @@ test('camera', function(t) {
t.end();
});

t.test('throws on invalid center argument', function(t) {
var camera = createCamera();
t.throws(function() {
camera.panTo({center: 1});
}, Error, 'throws with non-LngLatLike argument');
t.end();
});

t.test('pans with specified offset', function(t) {
var camera = createCamera();
camera.panTo([100, 0], { offset: [100, 0], duration: 0 });
Expand Down Expand Up @@ -706,6 +728,14 @@ test('camera', function(t) {
t.end();
});

t.test('throws on invalid center argument', function(t) {
var camera = createCamera();
t.throws(function() {
camera.flyTo({center: 1});
}, Error, 'throws with non-LngLatLike argument');
t.end();
});

t.test('zooms to specified level', function(t) {
var camera = createCamera();
camera.flyTo({ zoom: 3.2, animate: false });
Expand Down
11 changes: 11 additions & 0 deletions test/js/ui/map.test.js
Expand Up @@ -430,6 +430,17 @@ test('Map', function(t) {
t.end();
});

t.test('throws on invalid bounds', function(t) {
var map = createMap({zoom:0});
t.throws(function() {
map.setMaxBounds([-130.4297, 50.0642], [-61.52344, 24.20688]);
}, Error, 'throws on two decoupled array coordinate arguments');
t.throws(function() {
map.setMaxBounds(-130.4297, 50.0642, -61.52344, 24.20688);
}, Error, 'throws on individual coordinate arguments');
t.end();
});

function toFixed(bounds) {
var n = 10;
return [
Expand Down

0 comments on commit 56e9761

Please sign in to comment.