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