From 9dd9289f88aaa19fa7a64df6c5ca1d51c1669a3c Mon Sep 17 00:00:00 2001 From: euzuro Date: Fri, 13 Jul 2007 15:41:33 +0000 Subject: [PATCH] fix for #746 - add console.error()s when wrong number of argumetns passed to add() git-svn-id: http://svn.openlayers.org/trunk/openlayers@3731 dc9f47b5-9b13-0410-9fdd-eb0c1a62fdaf --- lib/OpenLayers/BaseTypes/Bounds.js | 5 +++++ lib/OpenLayers/BaseTypes/LonLat.js | 6 ++++++ lib/OpenLayers/BaseTypes/Pixel.js | 5 +++++ tests/BaseTypes/test_Bounds.html | 27 +++++++++++++++++++++++++++ tests/BaseTypes/test_LonLat.html | 22 ++++++++++++++++++---- tests/BaseTypes/test_Pixel.html | 29 ++++++++++++++++++++++------- 6 files changed, 83 insertions(+), 11 deletions(-) diff --git a/lib/OpenLayers/BaseTypes/Bounds.js b/lib/OpenLayers/BaseTypes/Bounds.js index b736aff693..dea0f65ad3 100644 --- a/lib/OpenLayers/BaseTypes/Bounds.js +++ b/lib/OpenLayers/BaseTypes/Bounds.js @@ -191,6 +191,11 @@ OpenLayers.Bounds.prototype = { * x and y values */ add:function(x, y) { + if ( (x == null) || (y == null) ) { + var msg = "You must pass both x and y values to the add function."; + OpenLayers.Console.error(msg); + return null; + } return new OpenLayers.Bounds(this.left + x, this.bottom + y, this.right + x, this.top + y); }, diff --git a/lib/OpenLayers/BaseTypes/LonLat.js b/lib/OpenLayers/BaseTypes/LonLat.js index 1c5882036f..bc1b4955d2 100644 --- a/lib/OpenLayers/BaseTypes/LonLat.js +++ b/lib/OpenLayers/BaseTypes/LonLat.js @@ -84,6 +84,12 @@ OpenLayers.LonLat.prototype = { * lat passed-in added to this's. */ add:function(lon, lat) { + if ( (lon == null) || (lat == null) ) { + var msg = "You must pass both lon and lat values " + + "to the add function."; + OpenLayers.Console.error(msg); + return null; + } return new OpenLayers.LonLat(this.lon + lon, this.lat + lat); }, diff --git a/lib/OpenLayers/BaseTypes/Pixel.js b/lib/OpenLayers/BaseTypes/Pixel.js index d86faf7a5f..171b88bcad 100644 --- a/lib/OpenLayers/BaseTypes/Pixel.js +++ b/lib/OpenLayers/BaseTypes/Pixel.js @@ -91,6 +91,11 @@ OpenLayers.Pixel.prototype = { * values passed in. */ add:function(x, y) { + if ( (x == null) || (y == null) ) { + var msg = "You must pass both x and y values to the add function."; + OpenLayers.Console.error(msg); + return null; + } return new OpenLayers.Pixel(this.x + x, this.y + y); }, diff --git a/tests/BaseTypes/test_Bounds.html b/tests/BaseTypes/test_Bounds.html index 197828f3aa..971dc9e5a9 100644 --- a/tests/BaseTypes/test_Bounds.html +++ b/tests/BaseTypes/test_Bounds.html @@ -472,6 +472,33 @@ } + function test_17_Bounds_add(t) { + t.plan( 8 ); + + origBounds = new OpenLayers.Bounds(1,2,3,4); + testBounds = origBounds.clone(); + + var bounds = testBounds.add(5, 50); + t.ok( testBounds.equals(origBounds), "testBounds is not modified by add operation"); + + var b = new OpenLayers.Bounds(6,52,8,54); + t.ok( bounds.equals(b), "bounds is set correctly"); + + //null values + var desiredMsg = "You must pass both x and y values to the add function."; + OpenLayers.Console.error = function(msg) { + t.eq(msg, desiredMsg, "error correctly reported"); + } + + bounds = testBounds.add(null, 50); + t.ok( testBounds.equals(origBounds), "testBounds is not modified by erroneous add operation (null x)"); + t.ok(bounds == null, "returns null on erroneous add operation (null x)"); + + bounds = testBounds.add(5, null); + t.ok( testBounds.equals(origBounds), "testBounds is not modified by erroneous add operation (null y)"); + t.ok(bounds == null, "returns null on erroneous add operation (null y)"); + } + // --> diff --git a/tests/BaseTypes/test_LonLat.html b/tests/BaseTypes/test_LonLat.html index 3248ef17c3..7b6920b5ea 100644 --- a/tests/BaseTypes/test_LonLat.html +++ b/tests/BaseTypes/test_LonLat.html @@ -47,16 +47,30 @@ } function test_04_LonLat_add(t) { - t.plan( 2 ); + t.plan( 8 ); - lonlatA = new OpenLayers.LonLat(10,100); + origLL = new OpenLayers.LonLat(10,100); + lonlatA = origLL.clone(); addpx = lonlatA.add(5, 50); - var ll = new OpenLayers.LonLat(10,100); - t.ok( lonlatA.equals(ll), "lonlatA is not modified by add operation"); + t.ok( lonlatA.equals(origLL), "lonlatA is not modified by add operation"); var ll = new OpenLayers.LonLat(15,150); t.ok( addpx.equals(ll), "addpx is set correctly"); + + //null values + var desiredMsg = "You must pass both lon and lat values to the add function."; + OpenLayers.Console.error = function(msg) { + t.eq(msg, desiredMsg, "error correctly reported"); + } + + addpx = lonlatA.add(null, 50); + t.ok( lonlatA.equals(origLL), "lonlatA is not modified by erroneous add operation (null lon)"); + t.ok(addpx == null, "returns null on erroneous add operation (null lon)"); + + addpx = lonlatA.add(5, null); + t.ok( lonlatA.equals(origLL), "lonlatA is not modified by erroneous add operation (null lat)"); + t.ok(addpx == null, "returns null on erroneous add operation (null lat)"); } function test_06_LonLat_equals(t) { diff --git a/tests/BaseTypes/test_Pixel.html b/tests/BaseTypes/test_Pixel.html index cb79bc0992..e5a9414cc8 100644 --- a/tests/BaseTypes/test_Pixel.html +++ b/tests/BaseTypes/test_Pixel.html @@ -61,16 +61,31 @@ } function test_07_Pixel_add(t) { - t.plan( 4 ); - oldPixel = new OpenLayers.Pixel(5,6); + t.plan( 8 ); - pixel = oldPixel.add(10,20); + var origPX = new OpenLayers.Pixel(5,6); + var oldPixel = origPX.clone(); - t.eq( oldPixel.x, 5, "oldPixel.x not modified by add operation"); - t.eq( oldPixel.y, 6, "oldPixel.y not modified by add operation"); + var pixel = oldPixel.add(10,20); + + t.ok( oldPixel.equals(origPX), "oldPixel not modified by add operation"); + + var px = new OpenLayers.Pixel(15,26); + t.ok( pixel.equals(px), "returned pixel is correct"); - t.eq( pixel.x, 15, "pixel.x is set correctly"); - t.eq( pixel.y, 26, "pixel.y is set correctly"); + //null values + var desiredMsg = "You must pass both x and y values to the add function."; + OpenLayers.Console.error = function(msg) { + t.eq(msg, desiredMsg, "error correctly reported"); + } + + pixel = oldPixel.add(null, 50); + t.ok( oldPixel.equals(origPX), "oldPixel is not modified by erroneous add operation (null x)"); + t.ok(pixel == null, "returns null on erroneous add operation (null x)"); + + addpx = oldPixel.add(5, null); + t.ok( oldPixel.equals(origPX), "oldPixel is not modified by erroneous add operation (null y)"); + t.ok(pixel == null, "returns null on erroneous add operation (null y)"); } function test_08_Pixel_offset(t) {