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 import static org.fest.assertions.Formatting.inBrackets; 019 import static org.fest.util.Strings.concat; 020 021 /** 022 * Understands assertion methods for <code>char</code>s. To create a new instance of this class use the 023 * method <code>{@link Assertions#assertThat(char)}</code>. 024 * 025 * @author Yvonne Wang 026 * @author David DIDIER 027 */ 028 public class CharAssert extends PrimitiveAssert { 029 030 private final char actual; 031 032 /** 033 * Creates a new </code>{@link CharAssert}</code>. 034 * @param actual the target to verify. 035 */ 036 protected CharAssert(char 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>("Some value").isEqualTo(otherValue); 048 * </pre> 049 * </p> 050 * @param description the description of the actual value. 051 * @return this assertion object. 052 */ 053 public CharAssert 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>("Some value").isEqualTo(otherValue); 066 * </pre> 067 * </p> 068 * @param description the description of the actual value. 069 * @return this assertion object. 070 */ 071 public CharAssert 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("Some value")).isEqualTo(otherValue); 083 * </pre> 084 * </p> 085 * @param description the description of the actual value. 086 * @return this assertion object. 087 */ 088 public CharAssert 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("Some value")).isEqualTo(otherValue); 101 * </pre> 102 * </p> 103 * @param description the description of the actual value. 104 * @return this assertion object. 105 */ 106 public CharAssert describedAs(Description description) { 107 return as(description); 108 } 109 110 /** 111 * Verifies that the actual <code>char</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>char</code> value is not equal to the given one. 115 */ 116 public CharAssert isEqualTo(char expected) { 117 if (actual == expected) return this; 118 failIfCustomMessageIsSet(); 119 throw failure(unexpectedNotEqual(actual, expected)); 120 } 121 122 /** 123 * Verifies that the actual <code>char</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>char</code> value is equal to the given one. 127 */ 128 public CharAssert isNotEqualTo(char other) { 129 if (actual != other) return this; 130 failIfCustomMessageIsSet(); 131 throw failure(unexpectedEqual(actual, other)); 132 } 133 134 /** 135 * Verifies that the actual <code>char</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>char</code> value is not greater than the given one. 139 */ 140 public CharAssert isGreaterThan(char other) { 141 if (actual > other) return this; 142 failIfCustomMessageIsSet(); 143 throw failure(unexpectedLessThanOrEqualTo(actual, other)); 144 } 145 146 /** 147 * Verifies that the actual <code>char</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>char</code> value is not less than the given one. 151 */ 152 public CharAssert isLessThan(char other) { 153 if (actual < other) return this; 154 failIfCustomMessageIsSet(); 155 throw failure(unexpectedGreaterThanOrEqualTo(actual, other)); 156 } 157 158 /** 159 * Verifies that the actual <code>char</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>char</code> value is not greater than or equal to the given one. 163 */ 164 public CharAssert isGreaterThanOrEqualTo(char other) { 165 if (actual >= other) return this; 166 failIfCustomMessageIsSet(); 167 throw failure(unexpectedLessThan(actual, other)); 168 } 169 170 /** 171 * Verifies that the actual <code>char</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>char</code> value is not less than or equal to the given one. 175 */ 176 public CharAssert isLessThanOrEqualTo(char other) { 177 if (actual <= other) return this; 178 failIfCustomMessageIsSet(); 179 throw failure(unexpectedGreaterThan(actual, other)); 180 } 181 182 /** 183 * Verifies that the actual <code>char</code> value is an uppercase value. 184 * @return this assertion object. 185 * @throws AssertionError if the actual <code>char</code> value is not an uppercase value. 186 */ 187 public CharAssert isUpperCase() { 188 if (Character.isUpperCase(actual)) return this; 189 failIfCustomMessageIsSet(); 190 throw failure(concat(inBrackets(actual), " should be an uppercase character")); 191 } 192 193 /** 194 * Verifies that the actual <code>char</code> value is an lowercase value. 195 * @return this assertion object. 196 * @throws AssertionError if the actual <code>char</code> value is not an lowercase value. 197 */ 198 public CharAssert isLowerCase() { 199 if (Character.isLowerCase(actual)) return this; 200 failIfCustomMessageIsSet(); 201 throw failure(concat(inBrackets(actual), " should be a lowercase character")); 202 } 203 204 /** {@inheritDoc} */ 205 public CharAssert overridingErrorMessage(String message) { 206 replaceDefaultErrorMessagesWith(message); 207 return this; 208 } 209 }