com.fitbur.assertj.api.AbstractDoubleAssert Maven / Gradle / Ivy
/**
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
* specific language governing permissions and limitations under the License.
*
* Copyright 2012-2016 the original author or authors.
*/
package com.fitbur.assertj.api;
import java.util.Comparator;
import com.fitbur.assertj.data.Offset;
import com.fitbur.assertj.data.Percentage;
import com.fitbur.assertj.internal.ComparatorBasedComparisonStrategy;
import com.fitbur.assertj.internal.Doubles;
import com.fitbur.assertj.util.VisibleForTesting;
/**
* Base class for all implementations of assertions for {@link Double}s.
*
* @param the "self" type of this assertion class. Please read "Emulating 'self types' using Java Generics to simplify fluent API implementation"
* for more details.
*
* @author Yvonne Wang
* @author David DIDIER
* @author Alex Ruiz
* @author Ansgar Konermann
* @author Joel Costigliola
* @author Mikhail Mazursky
* @author Nicolas François
*/
public abstract class AbstractDoubleAssert> extends
AbstractComparableAssert implements FloatingPointNumberAssert {
@VisibleForTesting
Doubles doubles = Doubles.instance();
protected AbstractDoubleAssert(Double actual, Class> selfType) {
super(actual, selfType);
}
/** {@inheritDoc} */
@Override
public S isNaN() {
doubles.assertIsNaN(info, actual);
return myself;
}
/** {@inheritDoc} */
@Override
public S isNotNaN() {
doubles.assertIsNotNaN(info, actual);
return myself;
}
/** {@inheritDoc} */
@Override
public S isZero() {
doubles.assertIsZero(info, actual);
return myself;
}
/** {@inheritDoc} */
@Override
public S isNotZero() {
doubles.assertIsNotZero(info, actual);
return myself;
}
/** {@inheritDoc} */
@Override
public S isPositive() {
doubles.assertIsPositive(info, actual);
return myself;
}
/** {@inheritDoc} */
@Override
public S isNegative() {
doubles.assertIsNegative(info, actual);
return myself;
}
/** {@inheritDoc} */
@Override
public S isNotNegative() {
doubles.assertIsNotNegative(info, actual);
return myself;
}
/** {@inheritDoc} */
@Override
public S isNotPositive() {
doubles.assertIsNotPositive(info, actual);
return myself;
}
/**
* Verifies that the actual number is close to the given one within the given offset.
* If difference is equal to offset value, assertion is considered valid.
*
* Example:
*
// assertion will pass
* assertThat(8.1).isCloseTo(8.0, within(0.2));
*
* // you can use offset if you prefer
* assertThat(8.1).isCloseTo(8.0, offset(0.2));
*
* // if difference is exactly equals to 0.1, it's ok
* assertThat(8.1).isCloseTo(8.0, within(0.1));
*
* // assertion will fail
* assertThat(8.1).isCloseTo(8.0, within(0.01));
*
* @param other the given number 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 number is {@code null}.
* @throws AssertionError if the actual value is not equal to the given one.
*/
// duplicate javadoc of isCloseTo(double other, Offset offset but can't define it in super class
public S isCloseTo(final double other, final Offset offset) {
doubles.assertIsCloseTo(info, actual, other, offset);
return myself;
}
/**
* Verifies that the actual number is close to the given one within the given offset.
* If difference is equal to offset value, assertion is considered valid.
*
* Example:
*
// assertion will pass
* assertThat(8.1).isCloseTo(Double.valueOf(8.0), within(0.2));
*
* // you can use offset if you prefer
* assertThat(8.1).isCloseTo(Double.valueOf(8.0), offset(0.2));
*
* // if difference is exactly equals to 0.1, it's ok
* assertThat(8.1).isCloseTo(Double.valueOf(8.0), within(0.1));
*
* // assertion will fail
* assertThat(8.1).isCloseTo(Double.valueOf(8.0), within(0.01));
*
* @param other the given number 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 number is {@code null}.
* @throws AssertionError if the actual value is not equal to the given one.
*/
@Override
public S isCloseTo(Double other, Offset offset) {
doubles.assertIsCloseTo(info, actual, other, offset);
return myself;
}
/**
* Verifies that the actual number is close to the given one within the given percentage.
* If difference is equal to the percentage value, assertion is considered valid.
*
* Example with double:
*
// assertions will pass:
* assertThat(11.0).isCloseTo(Double.valueOf(10.0), withinPercentage(20d));
*
* // if difference is exactly equals to the computed offset (1.0), it's ok
* assertThat(11.0).isCloseTo(Double.valueOf(10.0), withinPercentage(10d));
*
* // assertion will fail
* assertThat(11.0).isCloseTo(Double.valueOf(10.0), withinPercentage(5d));
*
* @param expected the given number to compare the actual value to.
* @param percentage the given positive percentage.
* @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.
*/
@Override
public S isCloseTo(Double expected, Percentage percentage) {
doubles.assertIsCloseToPercentage(info, actual, expected, percentage);
return myself;
}
/**
* Verifies that the actual number is close to the given one within the given percentage.
* If difference is equal to the percentage value, assertion is considered valid.
*
* Example with double:
*
// assertions will pass:
* assertThat(11.0).isCloseTo(10.0, withinPercentage(20d));
*
* // if difference is exactly equals to the computed offset (1.0), it's ok
* assertThat(11.0).isCloseTo(10.0, withinPercentage(10d));
*
* // assertion will fail
* assertThat(11.0).isCloseTo(10.0, withinPercentage(5d));
*
* @param expected the given number to compare the actual value to.
* @param percentage the given positive percentage.
* @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.
*/
public S isCloseTo(double expected, Percentage percentage) {
doubles.assertIsCloseToPercentage(info, actual, expected, percentage);
return myself;
}
/**
* Verifies that the actual value is equal to the given one.
*
* Example:
*
// assertions will pass:
* assertThat(1.0).isEqualTo(1.0);
* assertThat(1D).isEqualTo(1.0);
*
* // assertions will fail:
* assertThat(0.0).isEqualTo(1.0);
* assertThat(-1.0).isEqualTo(1.0);
*
*
* @param expected 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 to the given one.
*/
public S isEqualTo(double expected) {
doubles.assertEqual(info, actual, expected);
return myself;
}
/** {@inheritDoc} */
@Override
public S isEqualTo(Double expected, Offset offset) {
doubles.assertEqual(info, actual, expected, offset);
return myself;
}
/**
* Verifies that the actual value is close to the given one by less than the given offset.
* If difference is equal to offset value, assertion is considered valid.
*
* Example with double:
*
// assertion will pass:
* assertThat(8.1).isEqualTo(8.0, offset(0.2));
*
* // if difference is exactly equals to the offset (0.1), it's ok
* assertThat(8.1).isEqualTo(8.0, offset(0.1));
*
* // within is an alias of offset
* assertThat(8.1).isEqualTo(8.0, within(0.1));
*
* // assertion will fail
* assertThat(8.1).isEqualTo(8.0, offset(0.01));
*
* @param expected the given value 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 number is {@code null}.
* @throws AssertionError if the actual value is not equal to the given one.
*/
public S isEqualTo(double expected, Offset offset) {
doubles.assertEqual(info, actual, expected, offset);
return myself;
}
/**
* Verifies that the actual value is not equal to the given one.
*
* Example:
*
// assertions will pass:
* assertThat(0.0).isNotEqualTo(1.0);
* assertThat(-1.0).isNotEqualTo(1.0);
*
* // assertions will fail:
* assertThat(1.0).isNotEqualTo(1.0);
* assertThat(1D).isNotEqualTo(1.0);
*
*
* @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 to the given one.
*/
public S isNotEqualTo(double other) {
doubles.assertNotEqual(info, actual, other);
return myself;
}
/**
* Verifies that the actual value is less than the given one.
*
* Example:
*
// assertion will pass:
* assertThat(1.0).isLessThan(2.0);
*
* // assertions will fail:
* assertThat(2.0).isLessThan(1.0);
* assertThat(1.0).isLessThan(1.0);
*
*
* @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 to or greater than the given one.
*/
public S isLessThan(double other) {
doubles.assertLessThan(info, actual, other);
return myself;
}
/**
* Verifies that the actual value is less than or equal to the given one.
*
* Example:
*
// assertions will pass:
* assertThat(-1.0).isLessThanOrEqualTo(1.0);
* assertThat(1.0).isLessThanOrEqualTo(1.0);
*
* // assertion will fail:
* assertThat(2.0).isLessThanOrEqualTo(1.0);
*
*
* @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 greater than the given one.
*/
public S isLessThanOrEqualTo(double other) {
doubles.assertLessThanOrEqualTo(info, actual, other);
return myself;
}
/**
* Verifies that the actual value is greater than the given one.
*
* Example:
*
// assertion will pass:
* assertThat(2.0).isGreaterThan(1.0);
*
* // assertions will fail:
* assertThat(1.0).isGreaterThan(1.0);
* assertThat(1.0).isGreaterThan(2.0);
*
*
* @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 to or less than the given one.
*/
public S isGreaterThan(double other) {
doubles.assertGreaterThan(info, actual, other);
return myself;
}
/**
* Verifies that the actual value is greater than or equal to the given one.
*
* Example:
*
// assertions will pass:
* assertThat(2.0).isGreaterThanOrEqualTo(1.0);
* assertThat(1.0).isGreaterThanOrEqualTo(1.0);
*
* // assertion will fail:
* assertThat(1.0).isGreaterThanOrEqualTo(2.0);
*
*
* @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 less than the given one.
*/
public S isGreaterThanOrEqualTo(double other) {
doubles.assertGreaterThanOrEqualTo(info, actual, other);
return myself;
}
/** {@inheritDoc} */
@Override
public S isBetween(Double start, Double end) {
doubles.assertIsBetween(info, actual, start, end);
return myself;
}
/** {@inheritDoc} */
@Override
public S isStrictlyBetween(Double start, Double end) {
doubles.assertIsStrictlyBetween(info, actual, start, end);
return myself;
}
@Override
public S usingComparator(Comparator super Double> customComparator) {
super.usingComparator(customComparator);
doubles = new Doubles(new ComparatorBasedComparisonStrategy(customComparator));
return myself;
}
@Override
public S usingDefaultComparator() {
super.usingDefaultComparator();
doubles = Doubles.instance();
return myself;
}
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy