001 /* 002 * Created on Feb 14, 2008 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 @2008-2009 the original author or authors. 015 */ 016 package org.fest.assertions; 017 018 import static org.fest.assertions.ArrayInspection.copy; 019 import static org.fest.assertions.ErrorMessages.unexpectedEqual; 020 import static org.fest.assertions.ErrorMessages.unexpectedNotEqual; 021 022 import java.util.Arrays; 023 024 /** 025 * Understands assertion methods for <code>float</code> arrays. To create a new instance of this class use the 026 * method <code>{@link Assertions#assertThat(float[])}</code>. 027 * 028 * @author Yvonne Wang 029 * @author Alex Ruiz 030 */ 031 public class FloatArrayAssert extends ArrayAssert<float[]> { 032 033 /** 034 * Creates a new </code>{@link FloatArrayAssert}</code>. 035 * @param actual the target to verify. 036 */ 037 protected FloatArrayAssert(float... actual) { 038 super(actual); 039 } 040 041 /** {@inheritDoc} */ 042 public FloatArrayAssert as(String description) { 043 description(description); 044 return this; 045 } 046 047 /** {@inheritDoc} */ 048 public FloatArrayAssert describedAs(String description) { 049 return as(description); 050 } 051 052 /** {@inheritDoc} */ 053 public FloatArrayAssert as(Description description) { 054 description(description); 055 return this; 056 } 057 058 /** {@inheritDoc} */ 059 public FloatArrayAssert describedAs(Description description) { 060 return as(description); 061 } 062 063 /** 064 * Verifies that the actual <code>float</code> array contains the given values. 065 * @param values the values to look for. 066 * @return this assertion object. 067 * @throws AssertionError if the actual <code>float</code> array is <code>null</code>. 068 * @throws NullPointerException if the given <code>float</code> array is <code>null</code>. 069 * @throws AssertionError if the actual <code>float</code> array does not contain the given values. 070 */ 071 public FloatArrayAssert contains(float...values) { 072 isNotNull(); 073 validateIsNotNull(values); 074 assertContains(copy(values)); 075 return this; 076 } 077 078 /** 079 * Verifies that the actual <code>float</code> array contains the given values <strong>only</strong>. 080 * @param values the values to look for. 081 * @return this assertion object. 082 * @throws AssertionError if the actual <code>float</code> array is <code>null</code>. 083 * @throws NullPointerException if the given <code>float</code> array is <code>null</code>. 084 * @throws AssertionError if the actual <code>float</code> array does not contain the given objects, or if the actual 085 * <code>float</code> array contains elements other than the ones specified. 086 */ 087 public FloatArrayAssert containsOnly(float...values) { 088 isNotNull(); 089 validateIsNotNull(values); 090 assertContainsOnly(copy(values)); 091 return this; 092 } 093 094 /** 095 * Verifies that the actual <code>float</code> array does not contain the given values. 096 * @param values the values the array should exclude. 097 * @return this assertion object. 098 * @throws AssertionError if the actual <code>float</code> array is <code>null</code>. 099 * @throws NullPointerException if the given <code>float</code> array is <code>null</code>. 100 * @throws AssertionError if the actual <code>Object</code> array contains any of the given values. 101 */ 102 public FloatArrayAssert excludes(float...values) { 103 isNotNull(); 104 validateIsNotNull(values); 105 assertExcludes(copy(values)); 106 return this; 107 } 108 109 private void validateIsNotNull(float[] values) { 110 if (values == null) 111 throw new NullPointerException(formattedErrorMessage("the given array of floats should not be null")); 112 } 113 114 /** 115 * Verifies that the actual <code>float</code> array satisfies the given condition. 116 * @param condition the given condition. 117 * @return this assertion object. 118 * @throws NullPointerException if the given condition is <code>null</code>. 119 * @throws AssertionError if the actual <code>float</code> array does not satisfy the given condition. 120 * @see #is(Condition) 121 */ 122 public FloatArrayAssert satisfies(Condition<float[]> condition) { 123 assertSatisfies(condition); 124 return this; 125 } 126 127 /** 128 * Verifies that the actual <code>float</code> array does not satisfy the given condition. 129 * @param condition the given condition. 130 * @return this assertion object. 131 * @throws NullPointerException if the given condition is <code>null</code>. 132 * @throws AssertionError if the actual <code>float</code> array satisfies the given condition. 133 * @see #isNot(Condition) 134 */ 135 public FloatArrayAssert doesNotSatisfy(Condition<float[]> condition) { 136 assertDoesNotSatisfy(condition); 137 return this; 138 } 139 140 141 /** 142 * Alias for <code>{@link #satisfies(Condition)}</code>. 143 * @param condition the given condition. 144 * @return this assertion object. 145 * @throws NullPointerException if the given condition is <code>null</code>. 146 * @throws AssertionError if the actual <code>float</code> array does not satisfy the given condition. 147 * @since 1.2 148 */ 149 public FloatArrayAssert is(Condition<float[]> condition) { 150 assertIs(condition); 151 return this; 152 } 153 154 /** 155 * Alias for <code>{@link #doesNotSatisfy(Condition)}</code>. 156 * @param condition the given condition. 157 * @return this assertion object. 158 * @throws NullPointerException if the given condition is <code>null</code>. 159 * @throws AssertionError if the actual <code>float</code> array satisfies the given condition. 160 * @since 1.2 161 */ 162 public FloatArrayAssert isNot(Condition<float[]> condition) { 163 assertIsNot(condition); 164 return this; 165 } 166 167 /** 168 * Verifies that the actual <code>float</code> array is not <code>null</code>. 169 * @return this assertion object. 170 * @throws AssertionError if the actual <code>float</code> array is <code>null</code>. 171 */ 172 public FloatArrayAssert isNotNull() { 173 assertThatActualIsNotNull(); 174 return this; 175 } 176 177 /** 178 * Verifies that the actual <code>float</code> array contains at least on element. 179 * @return this assertion object. 180 * @throws AssertionError if the actual <code>float</code> array is <code>null</code>. 181 * @throws AssertionError if the actual <code>float</code> array is empty. 182 */ 183 public FloatArrayAssert isNotEmpty() { 184 assertThatActualIsNotEmpty(); 185 return this; 186 } 187 188 /** 189 * Verifies that the actual <code>float</code> array is equal to the given array. Array equality is checked by 190 * <code>{@link Arrays#equals(float[], float[])}</code>. 191 * @param expected the given array to compare the actual array to. 192 * @return this assertion object. 193 * @throws AssertionError if the actual <code>float</code> array is not equal to the given one. 194 */ 195 public FloatArrayAssert isEqualTo(float[] expected) { 196 if (Arrays.equals(actual, expected)) return this; 197 failIfCustomMessageIsSet(); 198 throw failure(unexpectedNotEqual(actual, expected)); 199 } 200 201 /** 202 * Verifies that the actual <code>float</code> array is not equal to the given array. Array equality is checked by 203 * <code>{@link Arrays#equals(float[], float[])}</code>. 204 * @param array the given array to compare the actual array to. 205 * @return this assertion object. 206 * @throws AssertionError if the actual <code>float</code> array is equal to the given one. 207 */ 208 public FloatArrayAssert isNotEqualTo(float[] array) { 209 if (!Arrays.equals(actual, array)) return this; 210 failIfCustomMessageIsSet(); 211 throw failure(unexpectedEqual(actual, array)); 212 } 213 214 /** 215 * Verifies that the number of elements in the actual <code>float</code> array is equal to the given one. 216 * @param expected the expected number of elements in the actual <code>float</code> array. 217 * @return this assertion object. 218 * @throws AssertionError if the actual <code>float</code> array is <code>null</code>. 219 * @throws AssertionError if the number of elements in the actual <code>float</code> array is not equal to the given 220 * one. 221 */ 222 public FloatArrayAssert hasSize(int expected) { 223 assertThatActualHasSize(expected); 224 return this; 225 } 226 227 /** 228 * Verifies that the actual <code>float</code> array is the same as the given array. 229 * @param expected the given array to compare the actual array to. 230 * @return this assertion object. 231 * @throws AssertionError if the actual <code>float</code> array is not the same as the given one. 232 */ 233 public FloatArrayAssert isSameAs(float[] expected) { 234 assertSameAs(expected); 235 return this; 236 } 237 238 /** 239 * Verifies that the actual <code>float</code> array is not the same as the given array. 240 * @param expected the given array to compare the actual array to. 241 * @return this assertion object. 242 * @throws AssertionError if the actual <code>float</code> array is the same as the given one. 243 */ 244 public FloatArrayAssert isNotSameAs(float[] expected) { 245 assertNotSameAs(expected); 246 return this; 247 } 248 249 /** {@inheritDoc} */ 250 public FloatArrayAssert overridingErrorMessage(String message) { 251 replaceDefaultErrorMessagesWith(message); 252 return this; 253 } 254 }