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