001 /* 002 * Created on Oct 24, 2010 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with 005 * the License. You may obtain a copy of the License at 006 * 007 * http://www.apache.org/licenses/LICENSE-2.0 008 * 009 * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on 010 * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the 011 * specific language governing permissions and limitations under the License. 012 * 013 * Copyright @2010-2011 the original author or authors. 014 */ 015 package org.fest.assertions.api; 016 017 import java.util.Comparator; 018 019 import org.fest.assertions.core.FloatingPointNumberAssert; 020 import org.fest.assertions.data.Offset; 021 import org.fest.assertions.internal.Floats; 022 import org.fest.util.ComparatorBasedComparisonStrategy; 023 import org.fest.util.VisibleForTesting; 024 025 /** 026 * Assertion methods for floats. 027 * <p> 028 * To create an instance of this class, invoke <code>{@link Assertions#assertThat(Float)}</code> or 029 * <code>{@link Assertions#assertThat(float)}</code>. 030 * </p> 031 * 032 * @author Yvonne Wang 033 * @author Alex Ruiz 034 * @author Ansgar Konermann 035 * @author Mikhail Mazursky 036 */ 037 public class FloatAssert extends AbstractComparableAssert<FloatAssert, Float> implements FloatingPointNumberAssert<Float> { 038 039 @VisibleForTesting Floats floats = Floats.instance(); 040 041 protected FloatAssert(Float actual) { 042 super(actual, FloatAssert.class); 043 } 044 045 /** {@inheritDoc} */ 046 public FloatAssert isNaN() { 047 floats.assertIsNaN(info, actual); 048 return this; 049 } 050 051 /** {@inheritDoc} */ 052 public FloatAssert isNotNaN() { 053 floats.assertIsNotNaN(info, actual); 054 return this; 055 } 056 057 /** {@inheritDoc} */ 058 public FloatAssert isZero() { 059 floats.assertIsZero(info, actual); 060 return this; 061 } 062 063 /** {@inheritDoc} */ 064 public FloatAssert isNotZero() { 065 floats.assertIsNotZero(info, actual); 066 return this; 067 } 068 069 /** {@inheritDoc} */ 070 public FloatAssert isPositive() { 071 floats.assertIsPositive(info, actual); 072 return this; 073 } 074 075 /** {@inheritDoc} */ 076 public FloatAssert isNegative() { 077 floats.assertIsNegative(info, actual); 078 return this; 079 } 080 081 /** 082 * Verifies that the actual value is equal to the given one. 083 * @param expected the given value to compare the actual value to. 084 * @return {@code this} assertion object. 085 * @throws AssertionError if the actual value is {@code null}. 086 * @throws AssertionError if the actual value is not equal to the given one. 087 */ 088 public FloatAssert isEqualTo(float expected) { 089 floats.assertEqual(info, actual, expected); 090 return this; 091 } 092 093 /** {@inheritDoc} */ 094 public FloatAssert isEqualTo(Float expected, Offset<Float> offset) { 095 floats.assertEqual(info, actual, expected, offset); 096 return this; 097 } 098 099 /** 100 * Verifies that the actual value is equal to the given one, within a positive offset. 101 * @param expected the given value to compare the actual value to. 102 * @param offset the given positive offset. 103 * @return {@code this} assertion object. 104 * @throws NullPointerException if the given offset is {@code null}. 105 * @throws AssertionError if the actual value is {@code null}. 106 * @throws AssertionError if the actual value is not equal to the given one. 107 */ 108 public FloatAssert isEqualTo(float expected, Offset<Float> offset) { 109 floats.assertEqual(info, actual, expected, offset); 110 return this; 111 } 112 113 /** 114 * Verifies that the actual value is not equal to the given one. 115 * @param other the given value to compare the actual value to. 116 * @return {@code this} assertion object. 117 * @throws AssertionError if the actual value is {@code null}. 118 * @throws AssertionError if the actual value is equal to the given one. 119 */ 120 public FloatAssert isNotEqualTo(float other) { 121 floats.assertNotEqual(info, actual, other); 122 return this; 123 } 124 125 /** 126 * Verifies that the actual value is less than the given one. 127 * @param other the given value to compare the actual value to. 128 * @return {@code this} assertion object. 129 * @throws AssertionError if the actual value is {@code null}. 130 * @throws AssertionError if the actual value is equal to or greater than the given one. 131 */ 132 public FloatAssert isLessThan(float other) { 133 floats.assertLessThan(info, actual, other); 134 return this; 135 } 136 137 /** 138 * Verifies that the actual value is less than or equal to the given one. 139 * @param other the given value to compare the actual value to. 140 * @return {@code this} assertion object. 141 * @throws AssertionError if the actual value is {@code null}. 142 * @throws AssertionError if the actual value is greater than the given one. 143 */ 144 public FloatAssert isLessThanOrEqualTo(float other) { 145 floats.assertLessThanOrEqualTo(info, actual, other); 146 return this; 147 } 148 149 /** 150 * Verifies that the actual value is greater than the given one. 151 * @param other the given value to compare the actual value to. 152 * @return {@code this} assertion object. 153 * @throws AssertionError if the actual value is {@code null}. 154 * @throws AssertionError if the actual value is equal to or less than the given one. 155 */ 156 public FloatAssert isGreaterThan(float other) { 157 floats.assertGreaterThan(info, actual, other); 158 return this; 159 } 160 161 /** 162 * Verifies that the actual value is greater than or equal to the given one. 163 * @param other the given value to compare the actual value to. 164 * @return {@code this} assertion object. 165 * @throws AssertionError if the actual value is {@code null}. 166 * @throws AssertionError if the actual value is less than the given one. 167 */ 168 public FloatAssert isGreaterThanOrEqualTo(float other) { 169 floats.assertGreaterThanOrEqualTo(info, actual, other); 170 return this; 171 } 172 173 @Override 174 public FloatAssert usingComparator(Comparator<? super Float> customComparator) { 175 super.usingComparator(customComparator); 176 this.floats = new Floats(new ComparatorBasedComparisonStrategy(customComparator)); 177 return myself; 178 } 179 180 @Override 181 public FloatAssert usingDefaultComparator() { 182 super.usingDefaultComparator(); 183 this.floats = Floats.instance(); 184 return myself; 185 } 186 }