[OpenLayers-Commits] r12197 - in trunk/openlayers: lib/OpenLayers/BaseTypes tests/BaseTypes

commits-20090109 at openlayers.org commits-20090109 at openlayers.org
Fri Jul 29 05:24:09 EDT 2011


Author: marcjansen
Date: 2011-07-29 02:24:07 -0700 (Fri, 29 Jul 2011)
New Revision: 12197

Modified:
   trunk/openlayers/lib/OpenLayers/BaseTypes/LonLat.js
   trunk/openlayers/tests/BaseTypes/LonLat.html
Log:
add method OpenLayers.LonLat.fromArray for API-consistency. r=bartvde, p=me (closes #3443)

Modified: trunk/openlayers/lib/OpenLayers/BaseTypes/LonLat.js
===================================================================
--- trunk/openlayers/lib/OpenLayers/BaseTypes/LonLat.js	2011-07-28 11:44:07 UTC (rev 12196)
+++ trunk/openlayers/lib/OpenLayers/BaseTypes/LonLat.js	2011-07-29 09:24:07 UTC (rev 12197)
@@ -190,3 +190,22 @@
     var pair = str.split(",");
     return new OpenLayers.LonLat(pair[0], pair[1]);
 };
+
+/** 
+ * Function: fromArray
+ * Alternative constructor that builds a new <OpenLayers.LonLat> from an 
+ *     array of two numbers that represent lon- and lat-values.
+ * 
+ * Parameters:
+ * arr - {Array(Float)} Array of lon/lat values (e.g. [5,-42])
+ * 
+ * Returns:
+ * {<OpenLayers.LonLat>} New <OpenLayers.LonLat> object built from the 
+ *                       passed-in array.
+ */
+OpenLayers.LonLat.fromArray = function(arr) {
+    var gotArr = OpenLayers.Util.isArray(arr),
+        lon = gotArr && arr[0],
+        lat = gotArr && arr[1];
+    return new OpenLayers.LonLat(lon, lat);
+};

Modified: trunk/openlayers/tests/BaseTypes/LonLat.html
===================================================================
--- trunk/openlayers/tests/BaseTypes/LonLat.html	2011-07-28 11:44:07 UTC (rev 12196)
+++ trunk/openlayers/tests/BaseTypes/LonLat.html	2011-07-29 09:24:07 UTC (rev 12197)
@@ -112,7 +112,53 @@
         var ll = new OpenLayers.LonLat(6, 5);
         t.ok( lonlat.equals(ll), "lonlat is set correctly");
     }
+    
+    function test_LonLat_fromArray(t) {
+        t.plan( 3 );
+        
+        // (1 test) must return a OpenLayers.LonLat-instance 
+        lonlat = OpenLayers.LonLat.fromArray([6,5]);
+        t.ok( lonlat instanceof OpenLayers.LonLat, "OpenLayers.LonLat.fromArray returns LonLat object" );
 
+        var ll = new OpenLayers.LonLat(6, 5);
+        // (1 test) must return correct LonLat-object
+        t.ok( lonlat.equals(ll), "lonlat is set correctly");
+        
+        
+        // (1 test) check how function deals with illegal arguments, it should 
+        // never throw an exception and always return an instance of 
+        // OpenLayers.LonLat.
+        var unexpectedResult = false,
+            undef,
+            checkArgs = [
+                {},
+                '',
+                6,
+                false,
+                true,
+                [undef, 5],
+                [6, undef]
+            ],
+            returnedVal;
+            
+        try {
+            for(var i = 0, len = checkArgs.length; i < len; i++ ){
+                returnedVal = OpenLayers.LonLat.fromArray( checkArgs[i] );
+                if (!(returnedVal instanceof OpenLayers.LonLat) ) {
+                    unexpectedResult = true;
+                    break;
+                }
+            }
+            // no arguments at all
+            returnedVal = OpenLayers.LonLat.fromArray();
+            unexpectedResult = !(returnedVal instanceof OpenLayers.LonLat);
+        } catch(e) {
+            unexpectedResult = true;
+        } finally {
+            t.ok(!unexpectedResult, "OpenLayers.LonLat.fromArray always returns an instance of OpenLayers.LonLat and doesn't throw an exception when called with unexpected argument.");
+        }
+    }
+
     function test_LonLat_transform(t) {
         t.plan( 6 );
         lonlat = new OpenLayers.LonLat(10, -10);



More information about the Commits mailing list