001 /*
002 * Created on Oct 24, 2010
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 @2010-2011 the original author or authors.
014 */
015 package org.fest.assertions.api;
016
017 import java.util.Comparator;
018
019 import org.fest.assertions.core.FloatingPointNumberAssert;
020 import org.fest.assertions.data.Offset;
021 import org.fest.assertions.internal.Floats;
022 import org.fest.util.ComparatorBasedComparisonStrategy;
023 import org.fest.util.VisibleForTesting;
024
025 /**
026 * Assertion methods for floats.
027 * <p>
028 * To create an instance of this class, invoke <code>{@link Assertions#assertThat(Float)}</code> or
029 * <code>{@link Assertions#assertThat(float)}</code>.
030 * </p>
031 *
032 * @author Yvonne Wang
033 * @author Alex Ruiz
034 * @author Ansgar Konermann
035 * @author Mikhail Mazursky
036 * @author Nicolas François
037 */
038 public class FloatAssert extends AbstractComparableAssert<FloatAssert, Float> implements FloatingPointNumberAssert<Float> {
039
040 @VisibleForTesting Floats floats = Floats.instance();
041
042 protected FloatAssert(Float actual) {
043 super(actual, FloatAssert.class);
044 }
045
046 /** {@inheritDoc} */
047 public FloatAssert isNaN() {
048 floats.assertIsNaN(info, actual);
049 return this;
050 }
051
052 /** {@inheritDoc} */
053 public FloatAssert isNotNaN() {
054 floats.assertIsNotNaN(info, actual);
055 return this;
056 }
057
058 /** {@inheritDoc} */
059 public FloatAssert isZero() {
060 floats.assertIsZero(info, actual);
061 return this;
062 }
063
064 /** {@inheritDoc} */
065 public FloatAssert isNotZero() {
066 floats.assertIsNotZero(info, actual);
067 return this;
068 }
069
070 /** {@inheritDoc} */
071 public FloatAssert isPositive() {
072 floats.assertIsPositive(info, actual);
073 return this;
074 }
075
076 /** {@inheritDoc} */
077 public FloatAssert isNegative() {
078 floats.assertIsNegative(info, actual);
079 return this;
080 }
081
082 /** {@inheritDoc} */
083 public FloatAssert isNotNegative(){
084 floats.assertIsNotNegative(info, actual);
085 return this;
086 }
087
088 /** {@inheritDoc} */
089 public FloatAssert isNotPositive(){
090 floats.assertIsNotPositive(info, actual);
091 return this;
092 }
093
094 /**
095 * Verifies that the actual value is equal to the given one.
096 * @param expected the given value to compare the actual value to.
097 * @return {@code this} assertion object.
098 * @throws AssertionError if the actual value is {@code null}.
099 * @throws AssertionError if the actual value is not equal to the given one.
100 */
101 public FloatAssert isEqualTo(float expected) {
102 floats.assertEqual(info, actual, expected);
103 return this;
104 }
105
106 /** {@inheritDoc} */
107 public FloatAssert isEqualTo(Float expected, Offset<Float> offset) {
108 floats.assertEqual(info, actual, expected, offset);
109 return this;
110 }
111
112 /**
113 * Verifies that the actual value is equal to the given one, within a positive offset.
114 * @param expected the given value to compare the actual value to.
115 * @param offset the given positive offset.
116 * @return {@code this} assertion object.
117 * @throws NullPointerException if the given offset is {@code null}.
118 * @throws AssertionError if the actual value is {@code null}.
119 * @throws AssertionError if the actual value is not equal to the given one.
120 */
121 public FloatAssert isEqualTo(float expected, Offset<Float> offset) {
122 floats.assertEqual(info, actual, expected, offset);
123 return this;
124 }
125
126 /**
127 * Verifies that the actual value is not equal to the given one.
128 * @param other the given value to compare the actual value to.
129 * @return {@code this} assertion object.
130 * @throws AssertionError if the actual value is {@code null}.
131 * @throws AssertionError if the actual value is equal to the given one.
132 */
133 public FloatAssert isNotEqualTo(float other) {
134 floats.assertNotEqual(info, actual, other);
135 return this;
136 }
137
138 /**
139 * Verifies that the actual value is less than the given one.
140 * @param other the given value to compare the actual value to.
141 * @return {@code this} assertion object.
142 * @throws AssertionError if the actual value is {@code null}.
143 * @throws AssertionError if the actual value is equal to or greater than the given one.
144 */
145 public FloatAssert isLessThan(float other) {
146 floats.assertLessThan(info, actual, other);
147 return this;
148 }
149
150 /**
151 * Verifies that the actual value is less than or equal to the given one.
152 * @param other the given value to compare the actual value to.
153 * @return {@code this} assertion object.
154 * @throws AssertionError if the actual value is {@code null}.
155 * @throws AssertionError if the actual value is greater than the given one.
156 */
157 public FloatAssert isLessThanOrEqualTo(float other) {
158 floats.assertLessThanOrEqualTo(info, actual, other);
159 return this;
160 }
161
162 /**
163 * Verifies that the actual value is greater than the given one.
164 * @param other the given value to compare the actual value to.
165 * @return {@code this} assertion object.
166 * @throws AssertionError if the actual value is {@code null}.
167 * @throws AssertionError if the actual value is equal to or less than the given one.
168 */
169 public FloatAssert isGreaterThan(float other) {
170 floats.assertGreaterThan(info, actual, other);
171 return this;
172 }
173
174 /**
175 * Verifies that the actual value is greater than or equal to the given one.
176 * @param other the given value to compare the actual value to.
177 * @return {@code this} assertion object.
178 * @throws AssertionError if the actual value is {@code null}.
179 * @throws AssertionError if the actual value is less than the given one.
180 */
181 public FloatAssert isGreaterThanOrEqualTo(float other) {
182 floats.assertGreaterThanOrEqualTo(info, actual, other);
183 return this;
184 }
185
186 @Override
187 public FloatAssert usingComparator(Comparator<? super Float> customComparator) {
188 super.usingComparator(customComparator);
189 this.floats = new Floats(new ComparatorBasedComparisonStrategy(customComparator));
190 return myself;
191 }
192
193 @Override
194 public FloatAssert usingDefaultComparator() {
195 super.usingDefaultComparator();
196 this.floats = Floats.instance();
197 return myself;
198 }
199 }