All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.fitbur.assertj.api.AbstractFloatAssert Maven / Gradle / Ivy

There is a newer version: 1.0.0
Show newest version
/**
 * 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.Floats;
import com.fitbur.assertj.util.VisibleForTesting;

/**
 * Base class for all implementations of assertions for {@link Float}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 Alex Ruiz
 * @author Ansgar Konermann
 * @author Mikhail Mazursky
 * @author Nicolas François
 */
public abstract class AbstractFloatAssert> extends AbstractComparableAssert
    implements FloatingPointNumberAssert {

  @VisibleForTesting
  Floats floats = Floats.instance();

  protected AbstractFloatAssert(Float actual, Class selfType) {
    super(actual, selfType);
  }

  /** {@inheritDoc} */
  @Override
  public S isNaN() {
    floats.assertIsNaN(info, actual);
    return myself;
  }

  /** {@inheritDoc} */
  @Override
  public S isNotNaN() {
    floats.assertIsNotNaN(info, actual);
    return myself;
  }

  /** {@inheritDoc} */
  @Override
  public S isZero() {
    floats.assertIsZero(info, actual);
    return myself;
  }

  /** {@inheritDoc} */
  @Override
  public S isNotZero() {
    floats.assertIsNotZero(info, actual);
    return myself;
  }

  /** {@inheritDoc} */
  @Override
  public S isPositive() {
    floats.assertIsPositive(info, actual);
    return myself;
  }

  /** {@inheritDoc} */
  @Override
  public S isNegative() {
    floats.assertIsNegative(info, actual);
    return myself;
  }

  /** {@inheritDoc} */
  @Override
  public S isNotNegative() {
    floats.assertIsNotNegative(info, actual);
    return myself;
  }

  /** {@inheritDoc} */
  @Override
  public S isNotPositive() {
    floats.assertIsNotPositive(info, actual);
    return myself;
  }

  /**
   * Verifies that the actual value is equal to the given one.
   * 

* Example: *

 // assertions will pass:
   * assertThat(1.0f).isEqualTo(1.0f);
   * assertThat(1f).isEqualTo(1.0f);
   * 
   * // assertions will fail:
   * assertThat(0.0f).isEqualTo(1.0f);
   * assertThat(-1.0f).isEqualTo(1.0f);
*

* @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(float expected) { floats.assertEqual(info, actual, expected); 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.1f).isCloseTo(8.2f, within(0.2f));
   *
   * // you can use offset if you prefer
   * assertThat(8.1f).isCloseTo(8.2f, offset(0.2f));
   *
   * // if difference is exactly equals to 0.1, it's ok
   * assertThat(8.1f).isCloseTo(8.2f, within(0.1f));
   *
   * // assertion will fail
   * assertThat(8.1f).isCloseTo(8.2f, within(0.01f));
* * Beware that java floating point number precision might have some unexpected behavior, e.g. the assertion below * fails: *
  // fails because 8.1f - 8.0f is evaluated to 0.10000038f in java.
   * assertThat(8.1f).isCloseTo(8.0f, within(0.1f));
* * @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 AssertionError if the actual value is not equal to the given one. */ // duplicate javadoc of isCloseTo(Float other, Offset offset but can't define it in super class public S isCloseTo(final float other, final Offset offset) { floats.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.1f).isCloseTo(new Float(8.2f), within(0.2f));
   *
   * // you can use offset if you prefer
   * assertThat(8.1f).isCloseTo(new Float(8.2f), offset(0.2f));
   *
   * // if difference is exactly equals to the offset (0.1), it's ok
   * assertThat(8.1f).isCloseTo(new Float(8.2f), within(0.1f));
   *
   * // assertion will fail
   * assertThat(8.1f).isCloseTo(new Float(8.2f), within(0.01f));
* * Beware that java floating point number precision might have some unexpected behavior, e.g. the assertion below * fails: *
  // fails because 8.1f - 8.0f is evaluated to 0.10000038f in java.
   * assertThat(8.1f).isCloseTo(new Float(8.0f), within(0.1f));
* * @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 other number is {@code null}. * @throws AssertionError if the actual value is not equal to the given one. */ @Override public S isCloseTo(Float other, Offset offset) { floats.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 float: *

 // assertions will pass:
   * assertThat(11.0f).isCloseTo(new Float(10.0f), withinPercentage(20f));
   *
   * // if difference is exactly equals to the computed offset (1.0), it's ok
   * assertThat(11.0f).isCloseTo(new Float(10.0f), withinPercentage(10f));
   *
   * // assertion will fail
   * assertThat(11.0f).isCloseTo(new Float(10.0f), withinPercentage(5f));
* * @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(Float expected, Percentage percentage) { floats.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 float: *

 // assertions will pass:
   * assertThat(11.0f).isCloseTo(10.0f, withinPercentage(20f));
   *
   * // if difference is exactly equals to the computed offset (1.0), it's ok
   * assertThat(11.0f).isCloseTo(10.0f, withinPercentage(10f));
   *
   * // assertion will fail
   * assertThat(11.0f).isCloseTo(10.0f, withinPercentage(5f));
* * @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(float expected, Percentage percentage) { floats.assertIsCloseToPercentage(info, actual, expected, percentage); return myself; } /** {@inheritDoc} */ @Override public S isEqualTo(Float expected, Offset offset) { floats.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: *

 // assertion will pass
   * assertThat(8.1f).isEqualTo(8.2f, offset(0.1f));
   *
   * // within is an alias of offset
   * assertThat(8.1f).isEqualTo(8.2f, within(0.1f));
   *
   * // assertion will fail
   * assertThat(8.1f).isEqualTo(8.2f, offset(0.01f));
* * Beware that java floating point number precision might have some unexpected behavior, e.g. the assertion below * fails: *
  // fails because 8.1f - 8.0f is evaluated to 0.10000038f in java.
   * assertThat(8.1f).isEqualTo(8.0f, offset(0.1f));
* * @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 AssertionError if the actual value is {@code null}. * @throws AssertionError if the actual value is not equal to the given one. */ public S isEqualTo(float expected, Offset offset) { floats.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.0f).isNotEqualTo(1.0f);
   * assertThat(-1.0f).isNotEqualTo(1.0f);
   * 
   * // assertions will fail:
   * assertThat(1.0f).isNotEqualTo(1.0f);
   * assertThat(1f).isNotEqualTo(1.0f);
*

* @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(float other) { floats.assertNotEqual(info, actual, other); return myself; } /** * Verifies that the actual value is less than the given one. *

* Example: *

 // assertions will pass:
   * assertThat(1.0f).isLessThan(2.0f);
   * assertThat(1.0f).isLessThan(1.01f);
   * 
   * // assertions will fail:
   * assertThat(2.0f).isLessThan(1.0f);
   * assertThat(1.0f).isLessThan(1.0f);
*

* @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(float other) { floats.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.0f).isLessThanOrEqualTo(1.0f);
   * assertThat(1.0f).isLessThanOrEqualTo(1.0f);
   * 
   * // assertion will fail:
   * assertThat(2.0f).isLessThanOrEqualTo(1.0f);
*

* @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(float other) { floats.assertLessThanOrEqualTo(info, actual, other); return myself; } /** * Verifies that the actual value is greater than the given one. *

* Example: *

 // assertions will pass:
   * assertThat(2.0f).isGreaterThan(1.0f);
   * assertThat(2.0f).isGreaterThan(1.99f);
   * 
   * // assertions will fail:
   * assertThat(1.0f).isGreaterThan(1.0f);
   * assertThat(1.0f).isGreaterThan(2.0f);
*

* @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(float other) { floats.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.0f).isGreaterThanOrEqualTo(1.0f);
   * assertThat(1.0f).isGreaterThanOrEqualTo(1.0f);
   * 
   * // assertions will fail:
   * assertThat(1.0f).isGreaterThanOrEqualTo(2.0f);
   * assertThat(1.0f).isGreaterThanOrEqualTo(0.99f);
*

* @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(float other) { floats.assertGreaterThanOrEqualTo(info, actual, other); return myself; } /** {@inheritDoc} */ @Override public S isBetween(Float start, Float end) { floats.assertIsBetween(info, actual, start, end); return myself; } /** {@inheritDoc} */ @Override public S isStrictlyBetween(Float start, Float end) { floats.assertIsStrictlyBetween(info, actual, start, end); return myself; } @Override public S usingComparator(Comparator customComparator) { super.usingComparator(customComparator); floats = new Floats(new ComparatorBasedComparisonStrategy(customComparator)); return myself; } @Override public S usingDefaultComparator() { super.usingDefaultComparator(); floats = Floats.instance(); return myself; } }