aboutsummaryrefslogtreecommitdiffstats
path: root/tests/src/cgeo/geocaching/geopoint/UnitsTest.java
blob: acc964af672d9bc1475877f13f8bcec9285d14e5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
package cgeo.geocaching.geopoint;

import static org.assertj.core.api.Assertions.assertThat;

import cgeo.CGeoTestCase;
import cgeo.geocaching.settings.Settings;
import cgeo.geocaching.settings.TestSettings;

import org.apache.commons.lang3.StringUtils;

public class UnitsTest extends CGeoTestCase {

    private static void assertDistance(final String expected, final float distance) {
        final String actual = Units.getDistanceFromKilometers(distance);
        if (!StringUtils.equals(expected, actual.replace(',', '.'))) { // make 1.2 the same as 1,2
            fail("getHumanDistance(" + distance +
                    ") [metric: " + (!Settings.isUseImperialUnits() ? "yes" : "no") +
                    "] fails to match " + expected + ": " + actual);
        }
    }

    // Make method non-static so that Settings is initialized
    @SuppressWarnings("static-method")
    public void testDistance() {
        assertThat(Units.getDistanceFromKilometers(null)).isEqualTo("?");
        final boolean savedImperial = Settings.isUseImperialUnits();
        try {
            TestSettings.setUseImperialUnits(false);
            assertDistance("123 km", 122.782f);
            assertDistance("123 km", 123.456f);
            assertDistance("12.3 km", 12.3456f);
            assertDistance("1.23 km", 1.23456f);
            assertDistance("123 m", 0.123456f);
            TestSettings.setUseImperialUnits(true);
            assertDistance("76.7 mi", 123.456f);
            assertDistance("7.67 mi", 12.3456f);
            assertDistance("0.77 mi", 1.23456f);
            assertDistance("405 ft", 0.123456f);
            assertDistance("40.5 ft", 0.0123456f);
        } finally {
            TestSettings.setUseImperialUnits(savedImperial);
        }
    }

    // Make method non-static so that Settings is initialized
    @SuppressWarnings("static-method")
    public void testSpeed() {
        assertThat(Units.getDistanceFromKilometers(null)).isEqualTo("?");
        final boolean savedImperial = Settings.isUseImperialUnits();
        try {
            TestSettings.setUseImperialUnits(false);
            assertSpeed("123 km/h", 122.782f);
            assertSpeed("123 km/h", 123.456f);
            assertSpeed("12.3 km/h", 12.3456f);
            assertSpeed("1.23 km/h", 1.23456f);
            assertSpeed("123 m/h", 0.123456f);
            TestSettings.setUseImperialUnits(true);
            assertSpeed("76.7 mph", 123.456f);
            assertSpeed("7.67 mph", 12.3456f);
            assertSpeed("0.77 mph", 1.23456f);
        } finally {
            TestSettings.setUseImperialUnits(savedImperial);
        }
    }

    private static void assertSpeed(final String expected, final float kilometersPerHour) {
        final String actual = Units.getSpeed(kilometersPerHour);
        if (!StringUtils.equals(expected, actual.replace(',', '.'))) {
            fail("speed " + actual + " does not match expected " + expected);
        }
    }

}