001 /* 002 * Created on Jun 18, 2007 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 @2007-2009 the original author or authors. 014 */ 015 package org.fest.assertions; 016 017 import static java.lang.Float.compare; 018 import static java.lang.Math.abs; 019 import static org.fest.assertions.ErrorMessages.*; 020 import static org.fest.assertions.Formatting.inBrackets; 021 import static org.fest.util.Strings.concat; 022 023 /** 024 * Understands assertion methods for <code>float</code>s. To create a new instance of this class use the 025 * method <code>{@link Assertions#assertThat(float)}</code>. 026 * 027 * @author Yvonne Wang 028 * @author Alex Ruiz 029 */ 030 public class FloatAssert extends PrimitiveAssert implements NumberAssert { 031 032 private static final float ZERO = 0f; 033 034 private final float actual; 035 036 /** 037 * Creates a new </code>{@link FloatAssert}</code>. 038 * @param actual the target to verify. 039 */ 040 protected FloatAssert(float actual) { 041 this.actual = actual; 042 } 043 044 /** 045 * Sets the description of the actual value, to be used in as message of any <code>{@link AssertionError}</code> 046 * thrown when an assertion fails. This method should be called before any assertion method, otherwise any assertion 047 * failure will not show the provided description. 048 * <p> 049 * For example: 050 * <pre> 051 * assertThat(value).<strong>as</strong>("Some value").isEqualTo(otherValue); 052 * </pre> 053 * </p> 054 * @param description the description of the actual value. 055 * @return this assertion object. 056 */ 057 public FloatAssert as(String description) { 058 description(description); 059 return this; 060 } 061 062 /** 063 * Alias for <code>{@link #as(String)}</code>, since "as" is a keyword in 064 * <a href="http://groovy.codehaus.org/" target="_blank">Groovy</a>. This method should be called before any assertion 065 * method, otherwise any assertion failure will not show the provided description. 066 * <p> 067 * For example: 068 * <pre> 069 * assertThat(value).<strong>describedAs</strong>("Some value").isEqualTo(otherValue); 070 * </pre> 071 * </p> 072 * @param description the description of the actual value. 073 * @return this assertion object. 074 */ 075 public FloatAssert describedAs(String description) { 076 return as(description); 077 } 078 079 /** 080 * Sets the description of the actual value, to be used in as message of any <code>{@link AssertionError}</code> 081 * thrown when an assertion fails. This method should be called before any assertion method, otherwise any assertion 082 * failure will not show the provided description. 083 * <p> 084 * For example: 085 * <pre> 086 * assertThat(value).<strong>as</strong>(new BasicDescription("Some value")).isEqualTo(otherValue); 087 * </pre> 088 * </p> 089 * @param description the description of the actual value. 090 * @return this assertion object. 091 */ 092 public FloatAssert as(Description description) { 093 description(description); 094 return this; 095 } 096 097 /** 098 * Alias for <code>{@link #as(Description)}</code>, since "as" is a keyword in 099 * <a href="http://groovy.codehaus.org/" target="_blank">Groovy</a>. This method should be called before any assertion 100 * method, otherwise any assertion failure will not show the provided description. 101 * <p> 102 * For example: 103 * <pre> 104 * assertThat(value).<strong>describedAs</strong>(new BasicDescription("Some value")).isEqualTo(otherValue); 105 * </pre> 106 * </p> 107 * @param description the description of the actual value. 108 * @return this assertion object. 109 */ 110 public FloatAssert describedAs(Description description) { 111 return as(description); 112 } 113 114 /** 115 * Verifies that the actual <code>float</code> value is equal to the given one. 116 * @param expected the value to compare the actual one to. 117 * @return this assertion object. 118 * @throws AssertionError if the actual <code>float</code> value is not equal to the given one. 119 */ 120 public FloatAssert isEqualTo(float expected) { 121 if (compareTo(expected) == 0) return this; 122 failIfCustomMessageIsSet(); 123 throw failure(unexpectedNotEqual(actual, expected)); 124 } 125 126 /** 127 * Verifies that the actual <code>float</code> value is equal to the given one, within a positive delta. 128 * @param expected the value to compare the actual one to. 129 * @param delta the given delta. 130 * @return this assertion object. 131 * @throws AssertionError if the actual <code>float</code> value is not equal to the given one. 132 * @deprecated use method <code>{@link #isEqualTo(float, org.fest.assertions.Delta)}</code> instead. This method will 133 * be removed in version 2.0. 134 */ 135 @Deprecated 136 public FloatAssert isEqualTo(float expected, Delta delta) { 137 return isEqualTo(expected, delta.value); 138 } 139 140 /** 141 * Verifies that the actual <code>float</code> value is equal to tdelta.valuene, within a positive delta. 142 * @param expected the value to compare the actual one to. 143 * @param delta the given delta. 144 * @return this assertion object. 145 * @throws AssertionError if the actual <code>float</code> value is not equal to the given one. 146 * @since 1.2 147 */ 148 public FloatAssert isEqualTo(float expected, org.fest.assertions.Delta delta) { 149 return isEqualTo(expected, delta.floatValue()); 150 } 151 152 private FloatAssert isEqualTo(float expected, float deltaValue) { 153 if (compareTo(expected) == 0) return this; 154 if (abs(expected - actual) <= deltaValue) return this; 155 failIfCustomMessageIsSet(); 156 throw failure(concat(unexpectedNotEqual(actual, expected), " using delta:", inBrackets(deltaValue))); 157 } 158 159 /** 160 * Verifies that the actual <code>float</code> value is not equal to the given one. 161 * @param other the given value. 162 * @return this assertion object. 163 * @throws AssertionError if the actual <code>float</code> value is equal to the given one. 164 */ 165 public FloatAssert isNotEqualTo(float other) { 166 if (compareTo(other) != 0) return this; 167 failIfCustomMessageIsSet(); 168 throw failure(unexpectedEqual(actual, other)); 169 } 170 171 /** 172 * Verifies that the actual <code>float</code> value is greater than the given one. 173 * @param other the given value. 174 * @return this assertion object. 175 * @throws AssertionError if the actual <code>float</code> value is not greater than the given one. 176 */ 177 public FloatAssert isGreaterThan(float other) { 178 if (compareTo(other) > 0) return this; 179 failIfCustomMessageIsSet(); 180 throw failure(unexpectedLessThanOrEqualTo(actual, other)); 181 } 182 183 /** 184 * Verifies that the actual <code>float</code> value is less than the given one. 185 * @param other the given value. 186 * @return this assertion object. 187 * @throws AssertionError if the actual <code>float</code> value is not less than the given one. 188 */ 189 public FloatAssert isLessThan(float other) { 190 if (compareTo(other) < 0) return this; 191 failIfCustomMessageIsSet(); 192 throw failure(unexpectedGreaterThanOrEqualTo(actual, other)); 193 } 194 195 /** 196 * Verifies that the actual <code>float</code> value is greater or equal to the given one. 197 * @param other the given value. 198 * @return this assertion object. 199 * @throws AssertionError if the actual <code>float</code> value is not greater than or equal to the given one. 200 */ 201 public FloatAssert isGreaterThanOrEqualTo(float other) { 202 if (compareTo(other) >= 0) return this; 203 failIfCustomMessageIsSet(); 204 throw failure(unexpectedLessThan(actual, other)); 205 } 206 207 /** 208 * Verifies that the actual <code>float</code> value is less or equal to the given one. 209 * @param other the given value. 210 * @return this assertion object. 211 * @throws AssertionError if the actual <code>float</code> value is not less than or equal to the given one. 212 */ 213 public FloatAssert isLessThanOrEqualTo(float other) { 214 if (compareTo(other) <= 0) return this; 215 failIfCustomMessageIsSet(); 216 throw failure(unexpectedGreaterThan(actual, other)); 217 } 218 219 private int compareTo(float other) { 220 return compare(actual, other); 221 } 222 223 /** 224 * Verifies that the actual <code>float</code> value is equal to <code>{@link Float#NaN}</code>. 225 * @return this assertion object. 226 * @throws AssertionError if the actual <code>float</code> value is not equal to <code>NaN</code>. 227 */ 228 public FloatAssert isNaN() { 229 return isEqualTo(Float.NaN); 230 } 231 232 /** 233 * Verifies that the actual <code>float</code> value is equal to zero. 234 * @return this assertion object. 235 * @throws AssertionError if the actual <code>float</code> value is not equal to zero. 236 */ 237 public FloatAssert isZero() { 238 return isEqualTo(ZERO); 239 } 240 241 /** 242 * Verifies that the actual <code>float</code> value is positive. 243 * @return this assertion object. 244 * @throws AssertionError if the actual <code>float</code> value is not positive. 245 */ 246 public FloatAssert isPositive() { 247 return isGreaterThan(ZERO); 248 } 249 250 /** 251 * Verifies that the actual <code>float</code> value is negative. 252 * @return this assertion object. 253 * @throws AssertionError if the actual <code>float</code> value is not negative. 254 */ 255 public FloatAssert isNegative() { 256 return isLessThan(ZERO); 257 } 258 259 /** 260 * Creates a new holder for a delta value to be used in 261 * <code>{@link FloatAssert#isEqualTo(float, org.fest.assertions.FloatAssert.Delta)}</code>. 262 * @param d the delta value. 263 * @return a new delta value holder. 264 * @deprecated use method <code>{@link org.fest.assertions.Delta#delta(double)}</code> instead. This method will be 265 * removed in version 2.0. 266 */ 267 @Deprecated 268 public static Delta delta(float d) { 269 return new Delta(d); 270 } 271 272 /** 273 * Holds a delta value to be used in 274 * <code>{@link FloatAssert#isEqualTo(float, org.fest.assertions.FloatAssert.Delta)}</code>. 275 * @deprecated use top-level class <code>{@link org.fest.assertions.Delta}</code> instead. This class will be removed 276 * in version 2.0. 277 */ 278 @Deprecated 279 public static class Delta { 280 final float value; 281 282 private Delta(float value) { 283 this.value = value; 284 } 285 } 286 287 /** {@inheritDoc} */ 288 public FloatAssert overridingErrorMessage(String message) { 289 replaceDefaultErrorMessagesWith(message); 290 return this; 291 } 292 } 293 294