Skip to content

Commit

Permalink
Merge pull request #79 from DataValues/coordFormatterTests
Browse files Browse the repository at this point in the history
More GeoCoordinateFormatter rounding tests
  • Loading branch information
mariushoch authored Oct 12, 2016
2 parents b582a61 + 47f36e0 commit 49ea605
Show file tree
Hide file tree
Showing 2 changed files with 98 additions and 9 deletions.
7 changes: 3 additions & 4 deletions src/Formatters/GeoCoordinateFormatter.php
Original file line number Diff line number Diff line change
Expand Up @@ -313,11 +313,11 @@ private function getInDecimalMinuteFormat( $floatDegrees, $precision ) {
* (60 for minutes, 3600 for seconds)
* @param float $degreePrecision
*
* @return float The number of digits to show after the decimal point
* @return int The number of digits to show after the decimal point
* (resp. before, if the result is negative).
*/
private function getSignificantDigits( $unitsPerDegree, $degreePrecision ) {
return ceil( -log10( $unitsPerDegree * $degreePrecision ) );
return (int)ceil( -log10( $unitsPerDegree * $degreePrecision ) );
}

/**
Expand All @@ -328,8 +328,7 @@ private function getSignificantDigits( $unitsPerDegree, $degreePrecision ) {
*/
private function formatNumber( $number, $digits = 0 ) {
// TODO: use NumberLocalizer
$digits = $digits < 0 ? 0 : $digits;
return sprintf( '%.' . (int)$digits . 'F', $number );
return sprintf( '%.' . ( $digits > 0 ? $digits : 0 ) . 'F', $number );
}

}
100 changes: 95 additions & 5 deletions tests/unit/Formatters/GeoCoordinateFormatterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,26 @@ public function floatNotationProvider() {
10,
'-60, 40'
),
'rounding degrees down' => array(
new LatLongValue( -14.9, 14.9 ),
10,
'-10, 10'
),
'rounding degrees up' => array(
new LatLongValue( -15, 15 ),
10,
'-20, 20'
),
'rounding fractions down' => array(
new LatLongValue( -0.049, 0.049 ),
0.1,
'0, 0'
),
'rounding fractions up' => array(
new LatLongValue( -0.05, 0.05 ),
0.1,
'-0.1, 0.1'
),
);
}

Expand Down Expand Up @@ -123,6 +143,26 @@ public function decimalDegreeNotationProvider() {
10,
'-60°, 40°'
),
'rounding degrees down' => array(
new LatLongValue( -14.9, 14.9 ),
10,
'-10°, 10°'
),
'rounding degrees up' => array(
new LatLongValue( -15, 15 ),
10,
'-20°, 20°'
),
'rounding fractions down' => array(
new LatLongValue( -0.049, 0.049 ),
0.1,
'0.0°, 0.0°'
),
'rounding fractions up' => array(
new LatLongValue( -0.05, 0.05 ),
0.1,
'-0.1°, 0.1°'
),
);
}

Expand Down Expand Up @@ -199,6 +239,26 @@ public function decimalMinuteNotationProvider() {
10,
'-60°, 40°'
),
'rounding minutes down' => array(
new LatLongValue( -14.9 / 60, 14.9 / 60 ),
10 / 60,
'-0° 10\', 0° 10\''
),
'rounding minutes up' => array(
new LatLongValue( -15 / 60, 15 / 60 ),
10 / 60,
'-0° 20\', 0° 20\''
),
'rounding fractions down' => array(
new LatLongValue( -0.049 / 60, 0.049 / 60 ),
0.1 / 60,
'0° 0.0\', 0° 0.0\''
),
'rounding fractions up' => array(
new LatLongValue( -0.05 / 60, 0.05 / 60 ),
0.1 / 60,
'-0° 0.1\', 0° 0.1\''
),
);
}

Expand Down Expand Up @@ -275,11 +335,41 @@ public function decimalMinuteSecondNotationProvider() {
1,
'-56°, 37°'
),
'degree/100, case A' => array(
new LatLongValue( 52.01, 10.01 ),
0.01,
'52° 0\' 36", 10° 0\' 36"'
),
'degree/100, case B' => array(
new LatLongValue( 52.02, 10.02 ),
0.01,
'52° 1\' 12", 10° 1\' 12"'
),
'ten degrees' => array(
new LatLongValue( -55.755786, 37.25633 ),
10,
'-60°, 40°'
),
'rounding seconds down' => array(
new LatLongValue( -14.9 / 3600, 14.9 / 3600 ),
10 / 3600,
'-0° 0\' 10", 0° 0\' 10"'
),
'rounding seconds up' => array(
new LatLongValue( -15 / 3600, 15 / 3600 ),
10 / 3600,
'-0° 0\' 20", 0° 0\' 20"'
),
'rounding fractions down' => array(
new LatLongValue( -0.049 / 3600, 0.049 / 3600 ),
0.1 / 3600,
'0° 0\' 0.0", 0° 0\' 0.0"'
),
'rounding fractions up' => array(
new LatLongValue( -0.05 / 3600, 0.05 / 3600 ),
0.1 / 3600,
'-0° 0\' 0.1", 0° 0\' 0.1"'
),
);
}

Expand All @@ -302,14 +392,14 @@ public function testDecimalMinuteSecondNotationRoundTrip( LatLongValue $latLong,
private function assertFormatsCorrectly( LatLongValue $latLong, FormatterOptions $options, $expected ) {
$formatter = new GeoCoordinateFormatter( $options );

$this->assertEquals(
$this->assertSame(
$expected,
$formatter->format( $latLong ),
'format()'
);

$precision = $options->getOption( GeoCoordinateFormatter::OPT_PRECISION );
$this->assertEquals(
$this->assertSame(
$expected,
$formatter->formatLatLongValue( $latLong, $precision ),
'formatLatLongValue()'
Expand All @@ -326,7 +416,7 @@ private function assertRoundTrip( LatLongValue $value, FormatterOptions $options
// NOTE: $parsed may be != $coord, because of rounding, so we can't compare directly.
$formattedParsed = $formatter->format( $parsed );

$this->assertEquals( $formatted, $formattedParsed );
$this->assertSame( $formatted, $formattedParsed );
}

public function testDirectionalOptionGetsAppliedForDecimalMinutes() {
Expand Down Expand Up @@ -494,7 +584,7 @@ public function testFormatWithInvalidPrecision_fallsBackToDefaultPrecision( $pre
$formatter = new GeoCoordinateFormatter( $options );

$formatted = $formatter->format( new LatLongValue( 1.2, 3.4 ) );
$this->assertEquals( '1.2, 3.4', $formatted );
$this->assertSame( '1.2, 3.4', $formatted );
}

/**
Expand All @@ -504,7 +594,7 @@ public function testFormatLatLongValueWithInvalidPrecision_fallsBackToDefaultPre
$formatter = new GeoCoordinateFormatter( new FormatterOptions() );

$formatted = $formatter->formatLatLongValue( new LatLongValue( 1.2, 3.4 ), $precision );
$this->assertEquals( '1.2, 3.4', $formatted );
$this->assertSame( '1.2, 3.4', $formatted );
}

public function invalidPrecisionProvider() {
Expand Down

0 comments on commit 49ea605

Please sign in to comment.