Skip to content

Commit

Permalink
Fixes #248 : Primitive Value Approximation Assertion
Browse files Browse the repository at this point in the history
  • Loading branch information
joel-costigliola committed Mar 15, 2015
1 parent 932339d commit 7cf60a3
Show file tree
Hide file tree
Showing 33 changed files with 1,417 additions and 398 deletions.
58 changes: 58 additions & 0 deletions src/main/java/org/assertj/core/api/AbstractByteAssert.java
Expand Up @@ -14,6 +14,7 @@

import java.util.Comparator;

import org.assertj.core.data.Offset;
import org.assertj.core.internal.Bytes;
import org.assertj.core.internal.ComparatorBasedComparisonStrategy;
import org.assertj.core.util.VisibleForTesting;
Expand Down Expand Up @@ -317,7 +318,64 @@ public S isStrictlyBetween(Byte start, Byte end) {
bytes.assertIsStrictlyBetween(info, actual, start, end);
return myself;
}

/**
* Verifies that the actual byte is close to the given one within the given offset.<br>
* If difference is equal to offset value, assertion is considered valid.
* <p>
* Example :
*
* <pre><code class='java'>
* // assertions will pass:
* assertThat((byte)5).isCloseTo((byte)7, within((byte)3));
*
* // if difference is exactly equals to the offset, it's ok
* assertThat((byte)5).isCloseTo((byte)7, within((byte)2));
*
* // assertion will fail
* assertThat((byte)5).isCloseTo((byte)7, within((byte)1));
* </code></pre>
*
* @param expected the given byte to compare the actual value to.
* @param offset the given positive offset.
* @return {@code this} assertion object.
* @throws NullPointerException if the given offset is {@code null}.
* @throws AssertionError if the actual value is not equal to the given one.
*/
public S isCloseTo(byte expected, Offset<Byte> offset) {
bytes.assertIsCloseTo(info, actual, expected, offset);
return myself;
}

/**
* Verifies that the actual Byte is close to the given one within the given offset.<br>
* If difference is equal to offset value, assertion is considered valid.
* <p>
* Example :
*
* <pre><code class='java'>
* // assertions will pass:
* assertThat((byte)5).isCloseTo(new Byte("7"), within((byte)3));
*
* // if difference is exactly equals to the offset, it's ok
* assertThat((byte)5).isCloseTo(new Byte("7"), within((byte)2));
*
* // assertion will fail
* assertThat((byte)5).isCloseTo(new Byte("7"), within((byte)1));
* </code></pre>
*
* @param expected the given Byte to compare the actual value to.
* @param offset the given positive offset.
* @return {@code this} assertion object.
* @throws NullPointerException if the given offset is {@code null}.
* @throws NullPointerException if the expected Byte is {@code null}.
* @throws AssertionError if the actual value is not equal to the given one.
*/
public S isCloseTo(Byte expected, Offset<Byte> offset) {
bytes.assertIsCloseTo(info, actual, expected, offset);
return myself;
}

@Override
public S usingComparator(Comparator<? super Byte> customComparator) {
super.usingComparator(customComparator);
Expand Down
5 changes: 2 additions & 3 deletions src/main/java/org/assertj/core/api/AbstractFloatAssert.java
Expand Up @@ -112,7 +112,7 @@ public S isEqualTo(float expected) {
* Verifies that the actual number is close to the given one within the given offset.<br>
* If difference is equal to offset value, assertion is considered valid.
* <p>
* Example with double:
* Example:
*
* <pre><code class='java'>
* // assertion will pass:
Expand All @@ -137,7 +137,6 @@ public S isEqualTo(float expected) {
* @param offset the given positive offset.
* @return {@code this} assertion object.
* @throws NullPointerException if the given offset is {@code null}.
* @throws NullPointerException if the expected number is {@code null}.
* @throws AssertionError if the actual value is not equal to the given one.
*/
// duplicate javadoc of isCloseTo(Float other, Offset<Float> offset but can't define it in super class
Expand Down Expand Up @@ -175,7 +174,7 @@ public S isCloseTo(final float other, final Offset<Float> offset) {
* @param offset the given positive offset.
* @return {@code this} assertion object.
* @throws NullPointerException if the given offset is {@code null}.
* @throws NullPointerException if the expected number is {@code null}.
* @throws NullPointerException if the other number is {@code null}.
* @throws AssertionError if the actual value is not equal to the given one.
*/
@Override
Expand Down
59 changes: 59 additions & 0 deletions src/main/java/org/assertj/core/api/AbstractIntegerAssert.java
Expand Up @@ -14,6 +14,7 @@

import java.util.Comparator;

import org.assertj.core.data.Offset;
import org.assertj.core.internal.ComparatorBasedComparisonStrategy;
import org.assertj.core.internal.Integers;
import org.assertj.core.util.VisibleForTesting;
Expand Down Expand Up @@ -181,6 +182,64 @@ public S isStrictlyBetween(Integer start, Integer end) {
return myself;
}

/**
* Verifies that the actual int is close to the given one within the given offset.<br>
* If difference is equal to offset value, assertion is considered valid.
* <p>
* Example:
*
* <pre><code class='java'>
* // assertions will pass:
* assertThat(5).isCloseTo(7, within(3));
*
* // if difference is exactly equals to the offset, it's ok
* assertThat(5).isCloseTo(7, within(2));
*
* // assertion will fail
* assertThat(5).isCloseTo(7, within(1));
* </code></pre>
*
* @param expected the given int to compare the actual value to.
* @param offset the given positive offset.
* @return {@code this} assertion object.
* @throws NullPointerException if the given offset is {@code null}.
* @throws AssertionError if the actual value is not equal to the given one.
*/
public S isCloseTo(int expected, Offset<Integer> offset) {
integers.assertIsCloseTo(info, actual, expected, offset);
return myself;
}

/**
* Verifies that the actual Integer is close to the given one within the given offset.<br>
* If difference is equal to offset value, assertion is considered valid.
* <p>
* Example:
*
* <pre><code class='java'>
* // assertions will pass:
* assertThat(5).isCloseTo(new Integer(7), within(3));
*
* // if difference is exactly equals to the offset (0.1), it's ok
* assertThat(5).isCloseTo(new Integer(7), within(2));
*
* // assertion will fail
* assertThat(5).isCloseTo(new Integer(7), within(1));
* </code></pre>
*
* @param expected the given Integer to compare the actual value to.
* @param offset the given positive offset.
* @return {@code this} assertion object.
* @throws NullPointerException if the given offset is {@code null}.
* @throws NullPointerException if the expected Integer is {@code null}.
* @throws AssertionError if the actual value is not equal to the given one.
*/
public S isCloseTo(Integer expected, Offset<Integer> offset) {
integers.assertIsCloseTo(info, actual, expected, offset);
return myself;
}


@Override
public S usingComparator(Comparator<? super Integer> customComparator) {
super.usingComparator(customComparator);
Expand Down

0 comments on commit 7cf60a3

Please sign in to comment.