1package org.hamcrest.number;
2
3import org.hamcrest.AbstractMatcherTest;
4import org.hamcrest.Matcher;
5
6import java.math.BigDecimal;
7
8import static org.hamcrest.MatcherAssert.assertThat;
9import static org.hamcrest.core.IsNot.not;
10import static org.hamcrest.number.OrderingComparison.*;
11
12public class OrderingComparisonTest extends AbstractMatcherTest {
13
14    @Override
15    protected Matcher<Integer> createMatcher() {
16        return greaterThan(1);
17    }
18
19    public void testDescription() {
20      assertDescription("a value greater than <1>", greaterThan(1));
21      assertDescription("a value equal to or greater than <1>", greaterThanOrEqualTo(1));
22      assertDescription("a value equal to <1>", comparesEqualTo(1));
23      assertDescription("a value less than or equal to <1>", lessThanOrEqualTo(1));
24      assertDescription("a value less than <1>", lessThan(1));
25    }
26
27    public void testMismatchDescriptions() {
28      assertMismatchDescription("<0> was less than <1>", greaterThan(1), 0);
29      assertMismatchDescription("<1> was equal to <1>", greaterThan(1), 1);
30      assertMismatchDescription("<1> was greater than <0>", lessThan(0), 1);
31      assertMismatchDescription("<2> was equal to <2>", lessThan(2), 2);
32    }
33
34    public void testComparesObjectsForGreaterThan() {
35        assertThat(2, greaterThan(1));
36        assertThat(0, not(greaterThan(1)));
37    }
38
39    public void testComparesObjectsForLessThan() {
40        assertThat(2, lessThan(3));
41        assertThat(0, lessThan(1));
42    }
43
44
45    public void testComparesObjectsForEquality() {
46      assertThat(3, comparesEqualTo(3));
47      assertThat("aa", comparesEqualTo("aa"));
48    }
49
50    public void testAllowsForInclusiveComparisons() {
51        assertThat("less", 1, lessThanOrEqualTo(1));
52        assertThat("greater", 1, greaterThanOrEqualTo(1));
53    }
54
55    public void testSupportsDifferentTypesOfComparableObjects() {
56        assertThat(1.1, greaterThan(1.0));
57        assertThat("cc", greaterThan("bb"));
58    }
59
60    public void testComparesBigDecimalsWithDifferentScalesCorrectlyForIssue20() {
61      assertThat(new BigDecimal("10.0"), greaterThanOrEqualTo(new BigDecimal("10")));
62      assertThat(new BigDecimal(10), greaterThanOrEqualTo(new BigDecimal("10.0")));
63      assertThat(new BigDecimal("2"), comparesEqualTo(new BigDecimal("2.000")));
64    }
65
66    public void testComparesCustomTypesWhoseCompareToReturnsValuesGreaterThatOne() {
67        assertThat(new CustomInt(5), lessThan(new CustomInt(10)));
68    }
69
70    private static final class CustomInt implements Comparable<CustomInt> {
71        private final int value;
72        public CustomInt(int value) {
73            this.value = value;
74        }
75
76        public int compareTo(CustomInt other) {
77            return value - other.value;
78        }
79    }
80}
81