diff options
| author | Samuel Tardieu <sam@rfc1149.net> | 2012-04-20 22:49:55 +0200 |
|---|---|---|
| committer | Samuel Tardieu <sam@rfc1149.net> | 2012-04-20 23:13:18 +0200 |
| commit | 374232fcad05372d7021393656a05b1dba293ca9 (patch) | |
| tree | f458c0e6e7482697e9d9e565fec828e7fefaed80 /tests/src/cgeo/geocaching/geopoint/HumanDistanceTest.java | |
| parent | c971abeb7416af7113c73ebc1527c9be938cdc8f (diff) | |
| download | cgeo-374232fcad05372d7021393656a05b1dba293ca9.zip cgeo-374232fcad05372d7021393656a05b1dba293ca9.tar.gz cgeo-374232fcad05372d7021393656a05b1dba293ca9.tar.bz2 | |
Add more tests for getHumanDistance()
Diffstat (limited to 'tests/src/cgeo/geocaching/geopoint/HumanDistanceTest.java')
| -rw-r--r-- | tests/src/cgeo/geocaching/geopoint/HumanDistanceTest.java | 37 |
1 files changed, 30 insertions, 7 deletions
diff --git a/tests/src/cgeo/geocaching/geopoint/HumanDistanceTest.java b/tests/src/cgeo/geocaching/geopoint/HumanDistanceTest.java index 3c7cb98..0532495 100644 --- a/tests/src/cgeo/geocaching/geopoint/HumanDistanceTest.java +++ b/tests/src/cgeo/geocaching/geopoint/HumanDistanceTest.java @@ -4,16 +4,39 @@ import cgeo.geocaching.Settings; import android.test.AndroidTestCase; +import java.util.regex.Pattern; + public class HumanDistanceTest extends AndroidTestCase { - public static void testHumanDistance() { - assertEquals("?", HumanDistance.getHumanDistance(null)); - if (Settings.isUseMetricUnits()) { - assertEquals("123 km", HumanDistance.getHumanDistance(123.456f)); - assertEquals("123 m", HumanDistance.getHumanDistance(0.123456f)); + private static void assertMatch(final String ok, final float distance) { + final String humanDistance = HumanDistance.getHumanDistance(distance); + if (!Pattern.compile('^' + ok + '$').matcher(humanDistance).find()) { + fail("getHumanDistance(" + distance + + ") [metric: " + (Settings.isUseMetricUnits() ? "yes" : "no") + + "] fails to match " + ok + ": " + humanDistance); } - else { - assertEquals("77 mi", HumanDistance.getHumanDistance(123.456f)); + } + + // Make method non-static so that Settings is initialized + @SuppressWarnings("static-method") + public void testHumanDistance() { + assertEquals("?", HumanDistance.getHumanDistance(null)); + final boolean savedMetrics = Settings.isUseMetricUnits(); + try { + Settings.setUseMetricUnits(true); + assertMatch("123 km", 122.782f); + assertMatch("123 km", 123.456f); + assertMatch("12.3 km", 12.3456f); + assertMatch("1.23 km", 1.23456f); + assertMatch("123 m", 0.123456f); + Settings.setUseMetricUnits(false); + assertMatch("76.7 mi", 123.456f); + assertMatch("7.67 mi", 12.3456f); + assertMatch("0.77 mi", 1.23456f); + assertMatch("405 ft", 0.123456f); + assertMatch("40.5 ft", 0.0123456f); + } finally { + Settings.setUseMetricUnits(savedMetrics); } } } |
