Disallow nowhere.

This commit is contained in:
Tim Schaub
2012-06-20 16:11:15 +02:00
parent f74d265dec
commit 5eeb21c125
2 changed files with 22 additions and 8 deletions

View File

@@ -22,11 +22,20 @@ ol.loc = function(opt_arg){
return opt_arg; return opt_arg;
} }
var x = 0; /** @type {number|undefined} */
var y = 0; var x;
/** @type {number|undefined} */
var y;
/** @type {number|undefined} */
var z; var z;
/** @type {Object|undefined} */
var projection; var projection;
var usage = 'ol.loc accepts a coordinate array or an object with x, y, and (optional) z properties';
if (arguments.length == 1 && goog.isDef(opt_arg)) { if (arguments.length == 1 && goog.isDef(opt_arg)) {
if (goog.isArray(opt_arg)) { if (goog.isArray(opt_arg)) {
x = opt_arg[0]; x = opt_arg[0];
@@ -39,9 +48,13 @@ ol.loc = function(opt_arg){
z = opt_arg['z']; z = opt_arg['z'];
projection = opt_arg['projection']; projection = opt_arg['projection'];
} else { } else {
throw new Error('ol.loc'); throw new Error(usage);
} }
} }
if (!goog.isNumber(x) || !goog.isNumber(y)) {
throw new Error(usage);
}
if (goog.isDef(projection)) { if (goog.isDef(projection)) {
projection = ol.projection(projection); projection = ol.projection(projection);

View File

@@ -1,11 +1,12 @@
describe("ol.loc", function() { describe("ol.loc", function() {
it("allows empty construction", function() { it("doesn't allow empty construction", function() {
var loc;
expect(function() {
// nowhere
var loc = ol.loc();
}).toThrow();
// nowhere
loc = ol.loc();
expect(loc).toBeA(ol.Loc);
}); });
it("allows construction from an obj config", function() { it("allows construction from an obj config", function() {