001    /*
002     * Created on Mar 19, 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.unexpectedNotEqual;
019    import static org.fest.assertions.Fail.failIfEqual;
020    
021    /**
022     * Understands assertion methods for <code>boolean</code> values. To create a new instance of this class use the method
023     * <code>{@link Assertions#assertThat(boolean)}</code>.
024     *
025     * @author Alex Ruiz
026     * @author Yvonne Wang
027     * @author David DIDIER
028     */
029    public class BooleanAssert extends PrimitiveAssert {
030    
031      private final boolean actual;
032    
033      /**
034       * Creates a new </code>{@link BooleanAssert}</code>.
035       * @param actual the target to verify.
036       */
037      protected BooleanAssert(boolean 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>(&quot;Some value&quot;).isEqualTo(otherValue);
049       * </pre>
050       * </p>
051       * @param description the description of the actual value.
052       * @return this assertion object.
053       */
054      public BooleanAssert 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>(&quot;Some value&quot;).isEqualTo(otherValue);
067       * </pre>
068       * </p>
069       * @param description the description of the actual value.
070       * @return this assertion object.
071       */
072      public BooleanAssert 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(&quot;Some value&quot;)).isEqualTo(otherValue);
084       * </pre>
085       * </p>
086       * @param description the description of the actual value.
087       * @return this assertion object.
088       */
089      public BooleanAssert 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(&quot;Some value&quot;)).isEqualTo(otherValue);
102       * </pre>
103       * </p>
104       * @param description the description of the actual value.
105       * @return this assertion object.
106       */
107      public BooleanAssert describedAs(Description description) {
108        return as(description);
109      }
110    
111      /**
112       * Verifies that the actual <code>boolean</code> value is <code>true</code>.
113       * @throws AssertionError if the actual <code>boolean</code> value is <code>false</code>.
114       */
115      public void isTrue() {
116        isEqualTo(true);
117      }
118    
119      /**
120       * Verifies that the actual <code>boolean</code> value is <code>false</code>.
121       * @throws AssertionError if the actual <code>boolean</code> value is <code>true</code>.
122       */
123      public void isFalse() {
124        isEqualTo(false);
125      }
126    
127      /**
128       * Verifies that the actual <code>boolean</code> is equal to the given one.
129       * @param expected the given <code>boolean</code> to compare the actual <code>boolean</code> to.
130       * @return this assertion object.
131       * @throws AssertionError if the actual <code>boolean</code> is not equal to the given one.
132       */
133      public BooleanAssert isEqualTo(boolean expected) {
134        if (actual == expected) return this;
135        failIfCustomMessageIsSet();
136        throw failure(unexpectedNotEqual(actual, expected));
137      }
138    
139      /**
140       * Verifies that the actual <code>boolean</code> is not equal to the given one.
141       * @param other the given <code>boolean</code> to compare the actual <code>boolean</code> to.
142       * @return this assertion object.
143       * @throws AssertionError if the actual <code>boolean</code> is equal to the given one.
144       */
145      public BooleanAssert isNotEqualTo(boolean other) {
146        failIfEqual(customErrorMessage(), rawDescription(), actual, other);
147        return this;
148      }
149    
150      /** {@inheritDoc} */
151      public BooleanAssert overridingErrorMessage(String message) {
152        replaceDefaultErrorMessagesWith(message);
153        return this;
154      }
155    }