r69459 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r69458‎ | r69459 | r69460 >
Date:02:04, 17 July 2010
Author:jeroendedauw
Status:deferred
Tags:
Comment:
Unit test improvements
Modified paths:
  • /trunk/extensions/Maps/Includes/Maps_CoordinateParser.php (modified) (history)
  • /trunk/extensions/Maps/test/MapsCoordinateParserTest.php (modified) (history)

Diff [purge]

Index: trunk/extensions/Maps/test/MapsCoordinateParserTest.php
@@ -38,13 +38,13 @@
3939 "55° 45.34716' N, 37° 37.05798' W",
4040 "55° 45.34716', -37° 37.05798'",
4141 "55° S, 37° 37.05798'W",
42 - "-55°, 37° -37.05798'",
 42+ "-55°, -37° 37.05798'",
4343 "55°S, 37°37.05798'W ",
44 - "-55°, 37°-37.05798' "
 44+ "-55°, 37°37.05798' "
4545 ),
4646 'dms' => array(
4747 "55° 45' 21\" N, 37° 37' 3\" W",
48 - "55° 45' 21\" N, -37° 37' 3\"",
 48+ "55° 45' 21\", -37° 37' 3\"",
4949 "55° 45' S, 37° 37' 3\"W",
5050 "-55°, -37° 37' 3\"",
5151 "55°45'S,37°37'3\"W ",
@@ -125,10 +125,12 @@
126126
127127 foreach( self::$coordinates['dms'] as $coord ) {
128128 $this->assertEquals( Maps_COORDS_DMS, MapsCoordinateParser::getCoordinatesType( $coord ), "$coord not recognized as dms." );
129 - }
 129+ }
 130+
130131 }
131132
132 - public function provider() {
 133+ /*
 134+ public function coordinatesProvider() {
133135 die(__METHOD__);
134136 $coords = array();
135137
@@ -138,23 +140,22 @@
139141 }
140142 }
141143 return $coords;
142 - }
 144+ }
 145+ */
143146
144147 /**
145 - * @dataProvider provider
 148+ * @dataProvider coordinatesProvider
146149 */
147150 public function testAreCoordinates( $coord ) {
148 - $this->assertTrue( MapsCoordinateParser::areCoordinates( $coord ), "$coord not recognized as coordinate." );
149 -
 151+ foreach( self::$coordinates as $coordsOfType ) {
 152+ foreach( $coordsOfType as $coord ) {
 153+ $this->assertTrue( MapsCoordinateParser::areCoordinates( $coord ), "$coord not recognized as coordinate." );
 154+ }
 155+ }
150156
151 - /*
152 - * Tests MapsCoordinateParser::areCoordinates()
153 - *
154157 foreach ( self::$fakeCoordinates as $coord ) {
155158 $this->assertFalse( MapsCoordinateParser::areCoordinates( $coord ), "$coord was recognized as coordinate." );
156159 }
157 - */
158 -
159160 }
160161
161162 /**
Index: trunk/extensions/Maps/Includes/Maps_CoordinateParser.php
@@ -18,7 +18,13 @@
1919 * Supports floats, DMS, decimal degrees, and decimal minutes notations, both directional and non-directional.
2020 * Internal representatations are arrays with lat and lon key with float values.
2121 *
22 - * TODO: it migt be a lot nicer to return the releveant segments from the regexes instead of manually parsing them out.
 22+ * TODO:
 23+ * Clean up the coordinate recognition and parsing.
 24+ * It's probably a better approach to use the regexes to do both.
 25+ * Also, the regexes can be improved, so coordinate sets composes of lat and lon
 26+ * in different notations can be recognized, and there is no need for the dms
 27+ * regex to also accept dm and dd, which can give unexpected results in certain
 28+ * usecases. The different seperator support could also be made nice.
2329 *
2430 * @ingroup Maps
2531 * @since 0.6
@@ -283,12 +289,12 @@
284290 switch ( $coordType ) {
285291 case Maps_COORDS_FLOAT:
286292 return $coordinate;
 293+ case Maps_COORDS_DD:
 294+ return self::parseDDCoordinate( $coordinate );
 295+ case Maps_COORDS_DM:
 296+ return self::parseDMCoordinate( $coordinate );
287297 case Maps_COORDS_DMS:
288298 return self::parseDMSCoordinate( $coordinate );
289 - case Maps_COORDS_DD:
290 - return self::parseDDCoordinate( $coordinate );
291 - case Maps_COORDS_DM:
292 - return self::parseDMCoordinate( $coordinate );
293299 default:
294300 throw new Exception( __METHOD__ . " does not support parsing of the $coordType coordinate type." );
295301 }
@@ -305,8 +311,8 @@
306312 */
307313 public static function areFloatCoordinates( $coordinates ) {
308314 $sep = self::getSeparatorsRegex();
309 - return preg_match( '/^(-)?\d{1,3}(\.\d{1,20})?' . $sep . '(\s)?(-)?\d{1,3}(\.\d{1,20})?$/i', $coordinates ) // Non-directional
310 - || preg_match( '/^\d{1,3}(\.\d{1,20})?(\s)?(N|S)' . $sep . '(\s)?\d{1,3}(\.\d{1,20})?(\s)?(E|W)$/i', $coordinates ); // Directional
 315+ return preg_match( '/^(-)?\d{1,3}(\.\d{1,20})?' . $sep . '(-)?\d{1,3}(\.\d{1,20})?$/i', $coordinates ) // Non-directional
 316+ || preg_match( '/^\d{1,3}(\.\d{1,20})?(N|S)' . $sep . '\d{1,3}(\.\d{1,20})?(E|W)$/i', $coordinates ); // Directional
311317 }
312318
313319 /**
@@ -320,10 +326,10 @@
321327 */
322328 public static function areDMSCoordinates( $coordinates ) {
323329 $sep = self::getSeparatorsRegex();
324 - return preg_match( '/^(-)?(\d{1,3}°)((\s)?\d{1,2}(\′|\'))?(((\s)?\d{1,2}(″|"))?|((\s)?\d{1,2}\.\d{1,2}(″|"))?)'
325 - . $sep . '(\s)?(-)?(\d{1,3}°)((\s)?\d{1,2}(\′|\'))?(((\s)?\d{1,2}(″|"))?|((\s)?\d{1,2}\.\d{1,2}(″|"))?)$/i', $coordinates ) // Non-directional
326 - || preg_match( '/^(\d{1,3}°)((\s)?\d{1,2}(\′|\'))?(((\s)?\d{1,2}(″|"))?|((\s)?\d{1,2}\.\d{1,2}(″|"))?)(\s)?(N|S)'
327 - . $sep . '(\s)?(\d{1,3}°)((\s)?\d{1,2}(\′|\'))?(((\s)?\d{1,2}(″|"))?|((\s)?\d{1,2}\.\d{1,2}(″|"))?)(\s)?(E|W)$/i', $coordinates ); // Directional
 330+ return preg_match( '/^(-)?(\d{1,3}°)(\d{1,2}(\′|\'))?((\d{1,2}(″|"))?|(\d{1,2}\.\d{1,2}(″|"))?)'
 331+ . $sep . '(-)?(\d{1,3}°)(\d{1,2}(\′|\'))?((\d{1,2}(″|"))?|(\d{1,2}\.\d{1,2}(″|"))?)$/i', $coordinates ) // Non-directional
 332+ || preg_match( '/^(\d{1,3}°)(\d{1,2}(\′|\'))?((\d{1,2}(″|"))?|(\d{1,2}\.\d{1,2}(″|"))?)(N|S)'
 333+ . $sep . '(\d{1,3}°)(\d{1,2}(\′|\'))?((\d{1,2}(″|"))?|(\d{1,2}\.\d{1,2}(″|"))?)(E|W)$/i', $coordinates ); // Directional
328334 }
329335
330336 /**
@@ -337,8 +343,8 @@
338344 */
339345 public static function areDDCoordinates( $coordinates ) {
340346 $sep = self::getSeparatorsRegex();
341 - return preg_match( '/^(-)?\d{1,3}(|\.\d{1,20})°' . $sep . '(\s)?(-)?(\s)?\d{1,3}(|\.\d{1,20})°$/i', $coordinates ) // Non-directional
342 - || preg_match( '/^\d{1,3}(|\.\d{1,20})°(\s)?(N|S)' . $sep . '(\s)?(\s)?\d{1,3}(|\.\d{1,20})°(\s)?(E|W)?$/i', $coordinates ); // Directional
 347+ return preg_match( '/^(-)?\d{1,3}(|\.\d{1,20})°' . $sep . '(-)?\d{1,3}(|\.\d{1,20})°$/i', $coordinates ) // Non-directional
 348+ || preg_match( '/^\d{1,3}(|\.\d{1,20})°(N|S)' . $sep . '\d{1,3}(|\.\d{1,20})°(E|W)?$/i', $coordinates ); // Directional
343349 }
344350
345351 /**
@@ -352,8 +358,8 @@
353359 */
354360 public static function areDMCoordinates( $coordinates ) {
355361 $sep = self::getSeparatorsRegex();
356 - return preg_match( '/(-)?\d{1,3}°(\s)?\d{1,2}(\.\d{1,20}\')?' . $sep . '(\s)?(-)?\d{1,3}°(\s)?\d{1,2}(\.\d{1,20}\')?$/i', $coordinates ) // Non-directional
357 - || preg_match( '/\d{1,3}°(\s)?\d{1,2}(\.\d{1,20}\')?(\s)?(N|S)' . $sep . '(\s)?\d{1,3}°(\s)?\d{1,2}(\.\d{1,20}\')?(\s)?(E|W)?$/i', $coordinates ); // Directional
 362+ return preg_match( '/(-)?\d{1,3}°(\d{1,2}(\.\d{1,20}\')?)?' . $sep . '(-)?\d{1,3}°(\d{1,2}(\.\d{1,20}\')?)?$/i', $coordinates ) // Non-directional
 363+ || preg_match( '/\d{1,3}°(\d{1,2}(\.\d{1,20}\')?)?(N|S)' . $sep . '\d{1,3}°(\d{1,2}(\.\d{1,20}\')?)?(E|W)?$/i', $coordinates ); // Directional
358364 }
359365
360366 /**

Status & tagging log