Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enforce LngLatLike param in LngLat.convert #3232

Merged
merged 3 commits into from
Sep 21, 2016
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions js/geo/lng_lat.js
Original file line number Diff line number Diff line change
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.lng && input.lat) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This check is dodgy: 0, 0 is a valid point but this will fail since 0 is falsy

return input;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If given {lng, lat} object, it should return a LngLat instance rather than the object.

} 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;
};
4 changes: 3 additions & 1 deletion test/js/geo/lng_lat.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,9 @@ 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(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
30 changes: 30 additions & 0 deletions test/js/ui/camera.test.js
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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