001 /*
002 * Created on Jun 14, 2007
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
005 * in compliance with 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
010 * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
011 * or implied. See the License for the specific language governing permissions and limitations under
012 * the License.
013 *
014 * Copyright @2007-2009 the original author or authors.
015 */
016 package org.fest.assertions;
017
018 import static org.fest.assertions.ErrorMessages.*;
019
020 /**
021 * Understands assert method for <code>int</code>s. To create a new instance of this class use the
022 * method <code>{@link Assertions#assertThat(int)}</code>.
023 *
024 * @author Yvonne Wang
025 * @author David DIDIER
026 */
027 public class IntAssert extends PrimitiveAssert implements NumberAssert {
028
029 private static final int ZERO = 0;
030
031 private final int actual;
032
033 /**
034 * Creates a new </code>{@link IntAssert}</code>.
035 * @param actual the target to verify.
036 */
037 protected IntAssert(int actual) {
038 this.actual = actual;
039 }
040
041 /**
042 * Sets the description of the actual value, to be used in as message of any <code>{@link AssertionError}</code>
043 * thrown when an assertion fails. This method should be called before any assertion method, otherwise any assertion
044 * failure will not show the provided description.
045 * <p>
046 * For example:
047 * <pre>
048 * assertThat(value).<strong>as</strong>("Some value").isEqualTo(otherValue);
049 * </pre>
050 * </p>
051 * @param description the description of the actual value.
052 * @return this assertion object.
053 */
054 public IntAssert as(String description) {
055 description(description);
056 return this;
057 }
058
059 /**
060 * Alias for <code>{@link #as(String)}</code>, since "as" is a keyword in
061 * <a href="http://groovy.codehaus.org/" target="_blank">Groovy</a>. This method should be called before any assertion
062 * method, otherwise any assertion failure will not show the provided description.
063 * <p>
064 * For example:
065 * <pre>
066 * assertThat(value).<strong>describedAs</strong>("Some value").isEqualTo(otherValue);
067 * </pre>
068 * </p>
069 * @param description the description of the actual value.
070 * @return this assertion object.
071 */
072 public IntAssert describedAs(String description) {
073 return as(description);
074 }
075
076 /**
077 * Sets the description of the actual value, to be used in as message of any <code>{@link AssertionError}</code>
078 * thrown when an assertion fails. This method should be called before any assertion method, otherwise any assertion
079 * failure will not show the provided description.
080 * <p>
081 * For example:
082 * <pre>
083 * assertThat(value).<strong>as</strong>(new BasicDescription("Some value")).isEqualTo(otherValue);
084 * </pre>
085 * </p>
086 * @param description the description of the actual value.
087 * @return this assertion object.
088 */
089 public IntAssert as(Description description) {
090 description(description);
091 return this;
092 }
093
094 /**
095 * Alias for <code>{@link #as(Description)}</code>, since "as" is a keyword in
096 * <a href="http://groovy.codehaus.org/" target="_blank">Groovy</a>. This method should be called before any assertion
097 * method, otherwise any assertion failure will not show the provided description.
098 * <p>
099 * For example:
100 * <pre>
101 * assertThat(value).<strong>describedAs</strong>(new BasicDescription("Some value")).isEqualTo(otherValue);
102 * </pre>
103 * </p>
104 * @param description the description of the actual value.
105 * @return this assertion object.
106 */
107 public IntAssert describedAs(Description description) {
108 return as(description);
109 }
110
111
112 /**
113 * Verifies that the actual <code>int</code> value is equal to the given one.
114 * @param expected the value to compare the actual one to.
115 * @return this assertion object.
116 * @throws AssertionError if the actual <code>int</code> value is not equal to the given one.
117 */
118 public IntAssert isEqualTo(int expected) {
119 if (actual == expected) return this;
120 failIfCustomMessageIsSet();
121 throw failure(unexpectedNotEqual(actual, expected));
122 }
123
124 /**
125 * Verifies that the actual <code>int</code> value is not equal to the given one.
126 * @param other the given value.
127 * @return this assertion object.
128 * @throws AssertionError if the actual <code>int</code> value is equal to the given one.
129 */
130 public IntAssert isNotEqualTo(int other) {
131 if (actual != other) return this;
132 failIfCustomMessageIsSet();
133 throw failure(unexpectedEqual(actual, other));
134 }
135
136 /**
137 * Verifies that the actual <code>int</code> value is greater than the given one.
138 * @param other the given value.
139 * @return this assertion object.
140 * @throws AssertionError if the actual <code>int</code> value is not greater than the given one.
141 */
142 public IntAssert isGreaterThan(int other) {
143 if (actual > other) return this;
144 failIfCustomMessageIsSet();
145 throw failure(unexpectedLessThanOrEqualTo(actual, other));
146 }
147
148 /**
149 * Verifies that the actual <code>int</code> value is less than the given one.
150 * @param other the given value.
151 * @return this assertion object.
152 * @throws AssertionError if the actual <code>int</code> value is not less than the given one.
153 */
154 public IntAssert isLessThan(int other) {
155 if (actual < other) return this;
156 failIfCustomMessageIsSet();
157 throw failure(unexpectedGreaterThanOrEqualTo(actual, other));
158 }
159
160 /**
161 * Verifies that the actual <code>int</code> value is greater or equal to the given one.
162 * @param other the given value.
163 * @return this assertion object.
164 * @throws AssertionError if the actual <code>int</code> value is not greater than or equal to the given one.
165 */
166 public IntAssert isGreaterThanOrEqualTo(int other) {
167 if (actual >= other) return this;
168 failIfCustomMessageIsSet();
169 throw failure(unexpectedLessThan(actual, other));
170 }
171
172 /**
173 * Verifies that the actual <code>int</code> value is less or equal to the given one.
174 * @param other the given value.
175 * @return this assertion object.
176 * @throws AssertionError if the actual <code>int</code> value is not less than or equal to the given one.
177 */
178 public IntAssert isLessThanOrEqualTo(int other) {
179 if (actual <= other) return this;
180 failIfCustomMessageIsSet();
181 throw failure(unexpectedGreaterThan(actual, other));
182 }
183
184 /**
185 * Verifies that the actual <code>int</code> value is equal to zero.
186 * @return this assertion object.
187 * @throws AssertionError if the actual <code>int</code> value is not equal to zero.
188 */
189 public IntAssert isZero() {
190 return isEqualTo(ZERO);
191 }
192
193 /**
194 * Verifies that the actual <code>int</code> value is positive.
195 * @return this assertion object.
196 * @throws AssertionError if the actual <code>int</code> value is not positive.
197 */
198 public IntAssert isPositive() {
199 return isGreaterThan(ZERO);
200 }
201
202 /**
203 * Verifies that the actual <code>int</code> value is negative.
204 * @return this assertion object.
205 * @throws AssertionError if the actual <code>int</code> value is not negative.
206 */
207 public IntAssert isNegative() {
208 return isLessThan(ZERO);
209 }
210
211 /** {@inheritDoc} */
212 public IntAssert overridingErrorMessage(String message) {
213 replaceDefaultErrorMessagesWith(message);
214 return this;
215 }
216 }