Skip to content

Commit

Permalink
Fixes #394 : Add isEqualByComparingTo and isNotEqualByComparingTo for…
Browse files Browse the repository at this point in the history
… Comparables
  • Loading branch information
mkordas authored and joel-costigliola committed May 12, 2015
1 parent ba6df9b commit f3d665b
Show file tree
Hide file tree
Showing 12 changed files with 172 additions and 190 deletions.
10 changes: 6 additions & 4 deletions src/main/java/org/assertj/core/api/AbstractBigDecimalAssert.java
Expand Up @@ -35,7 +35,7 @@
* @author Mikhail Mazursky
*/
public abstract class AbstractBigDecimalAssert<S extends AbstractBigDecimalAssert<S>> extends
AbstractUnevenComparableAssert<S, BigDecimal> implements NumberAssert<S, BigDecimal> {
AbstractComparableAssert<S, BigDecimal> implements NumberAssert<S, BigDecimal> {

@VisibleForTesting
BigDecimals bigDecimals = BigDecimals.instance();
Expand Down Expand Up @@ -185,7 +185,8 @@ public S isNotNegative() {
* // assertion will fail
* assertThat(new BigDecimal(&quot;8.0&quot;)).isBetween(new BigDecimal(&quot;6.0&quot;), new BigDecimal(&quot;7.0&quot;));
* </code></pre>
* Note that comparison of {@link BigDecimal} is done by value without scale consideration, i.e 2.0 and 2.00 are considered equal in value (not like {@link BigDecimal#equals(Object)}.
* Note that comparison of {@link BigDecimal} is done by value without scale consideration, i.e 2.0 and 2.00 are
* considered equal in value (not like {@link BigDecimal#equals(Object)}.
* </p>
*/
@Override
Expand Down Expand Up @@ -238,8 +239,8 @@ public S isEqualTo(String expected) {
}

/**
* Same as {@link AbstractUnevenComparableAssert#isEqualByComparingTo(Comparable) isEqualByComparingTo(BigDecimal)}
* but takes care of converting given String to {@link BigDecimal} for you.
* Same as {@link AbstractComparableAssert#isEqualByComparingTo(Comparable) isEqualByComparingTo(BigDecimal)} but
* takes care of converting given String to {@link BigDecimal} for you.
* <p>
* Example:
*
Expand Down Expand Up @@ -294,6 +295,7 @@ public S usingDefaultComparator() {
* assertThat(actual).isCloseTo(other, within(new BigDecimal("0.01")));
* </code></pre>
*/
@Override
public S isCloseTo(final BigDecimal other, final Offset<BigDecimal> offset) {
bigDecimals.assertIsCloseTo(info, actual, other, offset);
return myself;
Expand Down
26 changes: 21 additions & 5 deletions src/main/java/org/assertj/core/api/AbstractComparableAssert.java
Expand Up @@ -14,21 +14,23 @@

import java.util.Comparator;

import org.assertj.core.internal.*;
import org.assertj.core.internal.Comparables;
import org.assertj.core.internal.ComparatorBasedComparisonStrategy;
import org.assertj.core.util.VisibleForTesting;


/**
* Base class for all implementations of <code>{@link ComparableAssert}</code>.
* @param <S> the "self" type of this assertion class. Please read &quot;<a href="http://bit.ly/anMa4g" target="_blank">Emulating
*
* @param <S> the "self" type of this assertion class. Please read &quot;<a href="http://bit.ly/anMa4g"
* target="_blank">Emulating
* 'self types' using Java Generics to simplify fluent API implementation</a>&quot; for more details.
* @param <A> the type of the "actual" value.
*
* @author Alex Ruiz
* @author Mikhail Mazursky
*/
public abstract class AbstractComparableAssert<S extends AbstractComparableAssert<S, A>, A extends Comparable<? super A>> extends
AbstractObjectAssert<S, A> implements ComparableAssert<S, A> {
public abstract class AbstractComparableAssert<S extends AbstractComparableAssert<S, A>, A extends Comparable<? super A>>
extends AbstractObjectAssert<S, A> implements ComparableAssert<S, A> {

@VisibleForTesting
Comparables comparables = Comparables.instance();
Expand All @@ -37,6 +39,20 @@ protected AbstractComparableAssert(A actual, Class<?> selfType) {
super(actual, selfType);
}

/** {@inheritDoc} */
@Override
public S isEqualByComparingTo(A other) {
comparables.assertEqualByComparison(info, actual, other);
return myself;
}

/** {@inheritDoc} */
@Override
public S isNotEqualByComparingTo(A other) {
comparables.assertNotEqualByComparison(info, actual, other);
return myself;
}

/** {@inheritDoc} */
@Override
public S isLessThan(A other) {
Expand Down

This file was deleted.

56 changes: 55 additions & 1 deletion src/main/java/org/assertj/core/api/ComparableAssert.java
Expand Up @@ -12,9 +12,13 @@
*/
package org.assertj.core.api;

import java.math.BigDecimal;

/**
* Assertion methods applicable to <code>{@link Comparable}</code>s.
* @param <S> the "self" type of this assertion class. Please read &quot;<a href="http://bit.ly/anMa4g" target="_blank">Emulating
*
* @param <S> the "self" type of this assertion class. Please read &quot;<a href="http://bit.ly/anMa4g"
* target="_blank">Emulating
* 'self types' using Java Generics to simplify fluent API implementation</a>&quot; for more details.
* @param <A> the type of the "actual" value.
*
Expand All @@ -24,8 +28,55 @@
*/
public interface ComparableAssert<S extends ComparableAssert<S, A>, A extends Comparable<? super A>> {

/**
* Verifies that the actual value is equal to the given one by invoking
* <code>{@link Comparable#compareTo(Object)}</code>.
* <p>
* Example:
*
* <pre><code class='java'>
* // assertion will pass
* assertThat(1.0).isEqualByComparingTo(1.0);
* // assertion will pass because 8.0 is equal to 8.00 using {@link BigDecimal#compareTo(BigDecimal)}
* assertThat(new BigDecimal(&quot;8.0&quot;)).isEqualByComparingTo(new BigDecimal(&quot;8.00&quot;));
*
* // assertion will fail
* assertThat(new BigDecimal(1.0).isEqualByComparingTo(2.0);
* </code></pre>
*
* @param other the given value to compare the actual value to.
* @return {@code this} assertion object.
* @throws AssertionError if the actual value is {@code null}.
* @throws AssertionError if the actual value is not equal when comparing to the given one.
*/
S isEqualByComparingTo(A other);

/**
* Verifies that the actual value is not equal to the given one by invoking
* <code>{@link Comparable#compareTo(Object)}</code>.
* <p>
* Example:
*
* <pre><code class='java'>
* // assertion will pass
* assertThat(new BigDecimal(1.0).isNotEqualByComparingTo(2.0);
*
* // assertion will fail
* assertThat(1.0).isNotEqualByComparingTo(1.0);
* // assertion will fail because 8.0 is equal to 8.00 using {@link BigDecimal#compareTo(BigDecimal)}
* assertThat(new BigDecimal(&quot;8.0&quot;)).isNotEqualByComparingTo(new BigDecimal(&quot;8.00&quot;));
* </code></pre>
*
* @param other the given value to compare the actual value to.
* @return {@code this} assertion object.
* @throws AssertionError if the actual value is {@code null}.
* @throws AssertionError if the actual value is equal when comparing to the given one.
*/
S isNotEqualByComparingTo(A other);

/**
* Verifies that the actual value is less than the given one.
*
* @param other the given value to compare the actual value to.
* @return {@code this} assertion object.
* @throws AssertionError if the actual value is {@code null}.
Expand All @@ -35,6 +86,7 @@ public interface ComparableAssert<S extends ComparableAssert<S, A>, A extends Co

/**
* Verifies that the actual value is less than or equal to the given one.
*
* @param other the given value to compare the actual value to.
* @return {@code this} assertion object.
* @throws AssertionError if the actual value is {@code null}.
Expand All @@ -44,6 +96,7 @@ public interface ComparableAssert<S extends ComparableAssert<S, A>, A extends Co

/**
* Verifies that the actual value is greater than the given one.
*
* @param other the given value to compare the actual value to.
* @return {@code this} assertion object.
* @throws AssertionError if the actual value is {@code null}.
Expand All @@ -53,6 +106,7 @@ public interface ComparableAssert<S extends ComparableAssert<S, A>, A extends Co

/**
* Verifies that the actual value is greater than or equal to the given one.
*
* @param other the given value to compare the actual value to.
* @return {@code this} assertion object.
* @throws AssertionError if the actual value is {@code null}.
Expand Down
46 changes: 0 additions & 46 deletions src/main/java/org/assertj/core/api/UnevenComparableAssert.java

This file was deleted.

This file was deleted.

Expand Up @@ -34,7 +34,7 @@ public int compareTo(Object other) {
}

// we'd like to get rid of the compile error here
private static final class TestClassAssert extends AbstractUnevenComparableAssert<TestClassAssert, TestClass> {
private static final class TestClassAssert extends AbstractComparableAssert<TestClassAssert, TestClass> {

TestClassAssert(TestClass actual) {
super(actual, TestClassAssert.class);
Expand Down

This file was deleted.

Expand Up @@ -10,29 +10,26 @@
*
* Copyright 2012-2015 the original author or authors.
*/
package org.assertj.core.api.unevencomparable;

import org.assertj.core.api.AbstractUnevenComparableAssert;
import org.assertj.core.api.AbstractUnevenComparableAssertBaseTest;
import org.assertj.core.api.ConcreteUnevenComparableAssert;
package org.assertj.core.api.comparable;

import static org.mockito.Mockito.verify;

import org.assertj.core.api.AbstractComparableAssert;
import org.assertj.core.api.AbstractComparableAssertBaseTest;
import org.assertj.core.api.ConcreteComparableAssert;

/**
* Tests for <code>{@link AbstractUnevenComparableAssert#isEqualByComparingTo(Comparable)}</code>.
*
* @author Yvonne Wang
* Tests for <code>{@link AbstractComparableAssert#isEqualByComparingTo(Comparable)}</code>.
*/
public class AbstractUnevenComparableAssert_isEqualToByComparingTo_Test extends AbstractUnevenComparableAssertBaseTest {
public class AbstractComparableAssert_isEqualByComparingTo_Test extends AbstractComparableAssertBaseTest {

@Override
protected ConcreteUnevenComparableAssert invoke_api_method() {
return assertions.isEqualByComparingTo(6);
protected ConcreteComparableAssert invoke_api_method() {
return assertions.isEqualByComparingTo(0);
}

@Override
protected void verify_internal_effects() {
verify(comparables).assertEqualByComparison(getInfo(assertions), getActual(assertions), 6);
verify(comparables).assertEqualByComparison(getInfo(assertions), getActual(assertions), 0);
}
}

0 comments on commit f3d665b

Please sign in to comment.