|
| 1 | +package de.oliver.fancylib.tests; |
| 2 | + |
| 3 | +/** |
| 4 | + * A generic class for making assertions on the expected values. |
| 5 | + * |
| 6 | + * @param <T> the type of the value to be asserted. |
| 7 | + */ |
| 8 | +public class Expectable<T> { |
| 9 | + |
| 10 | + /** |
| 11 | + * The value that is being wrapped by this Expectable instance. |
| 12 | + * This is the object against which all expectations will be verified. |
| 13 | + */ |
| 14 | + private final T t; |
| 15 | + |
| 16 | + private Expectable(T t) { |
| 17 | + this.t = t; |
| 18 | + } |
| 19 | + |
| 20 | + /** |
| 21 | + * Creates a new instance of Expectable for the given value. |
| 22 | + * |
| 23 | + * @param <T> the type of the value being tested |
| 24 | + * @param t the actual value to create an expectation for |
| 25 | + * @return a new Expectable instance for the given value |
| 26 | + */ |
| 27 | + public static <T> Expectable<T> expect(T t) { |
| 28 | + return new Expectable<>(t); |
| 29 | + } |
| 30 | + |
| 31 | + /** |
| 32 | + * Ensures that the actual value is not null. |
| 33 | + * <p> |
| 34 | + * Throws an AssertionError if the value of the field 't' is null, |
| 35 | + * indicating that the actual value is expected to be non-null. |
| 36 | + * |
| 37 | + * @throws AssertionError if the value of the field 't' is null |
| 38 | + */ |
| 39 | + public void toBeDefined() { |
| 40 | + if (t == null) { |
| 41 | + throw new AssertionError("Expected not null but got null"); |
| 42 | + } |
| 43 | + } |
| 44 | + |
| 45 | + /** |
| 46 | + * Asserts that the value of the field 't' is null. |
| 47 | + * <p> |
| 48 | + * Throws an AssertionError if the value of 't' is not null, |
| 49 | + * indicating the expectation that the value should be null. |
| 50 | + * |
| 51 | + * @throws AssertionError if the value of 't' is not null |
| 52 | + */ |
| 53 | + public void toBeNull() { |
| 54 | + if (t != null) { |
| 55 | + throw new AssertionError("Expected null but got not null"); |
| 56 | + } |
| 57 | + } |
| 58 | + |
| 59 | + /** |
| 60 | + * Asserts that the actual value is equal to the expected value. |
| 61 | + * |
| 62 | + * @param expected the value that the actual value is expected to be equal to |
| 63 | + * @throws AssertionError if the actual value is not equal to the expected value |
| 64 | + */ |
| 65 | + public void toBe(T expected) { |
| 66 | + if (t != expected) { |
| 67 | + throw new AssertionError("Expected " + expected + " but got " + t); |
| 68 | + } |
| 69 | + } |
| 70 | + |
| 71 | + /** |
| 72 | + * Asserts that the actual value is equal to the expected value using the {@code equals} method. |
| 73 | + * |
| 74 | + * @param expected the value that the actual value is expected to be equal to |
| 75 | + * @throws AssertionError if the actual value is not equal to the expected value |
| 76 | + */ |
| 77 | + public void toEqual(T expected) { |
| 78 | + if (!t.equals(expected)) { |
| 79 | + throw new AssertionError("Expected " + expected + " but got " + t); |
| 80 | + } |
| 81 | + } |
| 82 | + |
| 83 | + /** |
| 84 | + * Asserts that the actual value is greater than the expected value. |
| 85 | + * |
| 86 | + * @param expected the value that the actual value is expected to be greater than |
| 87 | + * @throws AssertionError if the actual value is not greater than the expected value, |
| 88 | + * or if the type of the actual value is not one of Integer, Long, Float, or Double |
| 89 | + */ |
| 90 | + public void toBeGreaterThan(T expected) { |
| 91 | + if (t instanceof Integer) { |
| 92 | + if ((Integer) t <= (Integer) expected) { |
| 93 | + throw new AssertionError("Expected " + t + " to be greater than " + expected); |
| 94 | + } else { |
| 95 | + return; |
| 96 | + } |
| 97 | + } else if (t instanceof Long) { |
| 98 | + if ((Long) t <= (Long) expected) { |
| 99 | + throw new AssertionError("Expected " + t + " to be greater than " + expected); |
| 100 | + } else { |
| 101 | + return; |
| 102 | + } |
| 103 | + } else if (t instanceof Float) { |
| 104 | + if ((Float) t <= (Float) expected) { |
| 105 | + throw new AssertionError("Expected " + t + " to be greater than " + expected); |
| 106 | + } else { |
| 107 | + return; |
| 108 | + } |
| 109 | + } else if (t instanceof Double) { |
| 110 | + if ((Double) t <= (Double) expected) { |
| 111 | + throw new AssertionError("Expected " + t + " to be greater than " + expected); |
| 112 | + } else { |
| 113 | + return; |
| 114 | + } |
| 115 | + } |
| 116 | + |
| 117 | + throw new AssertionError("toBeGreaterThan can only be used on Integers, Longs, Floats, and Doubles"); |
| 118 | + } |
| 119 | + |
| 120 | + /** |
| 121 | + * Asserts that the actual value is less than the expected value. |
| 122 | + * |
| 123 | + * @param expected the value that the actual value is expected to be less than |
| 124 | + * @throws AssertionError if the actual value is not less than the expected value, |
| 125 | + * or if the type of the actual value is not one of Integer, Long, Float, or Double |
| 126 | + */ |
| 127 | + public void toBeLessThan(T expected) { |
| 128 | + if (t instanceof Integer) { |
| 129 | + if ((Integer) t >= (Integer) expected) { |
| 130 | + throw new AssertionError("Expected " + t + " to be less than " + expected); |
| 131 | + } else { |
| 132 | + return; |
| 133 | + } |
| 134 | + } else if (t instanceof Long) { |
| 135 | + if ((Long) t >= (Long) expected) { |
| 136 | + throw new AssertionError("Expected " + t + " to be less than " + expected); |
| 137 | + } else { |
| 138 | + return; |
| 139 | + } |
| 140 | + } else if (t instanceof Float) { |
| 141 | + if ((Float) t >= (Float) expected) { |
| 142 | + throw new AssertionError("Expected " + t + " to be less than " + expected); |
| 143 | + } else { |
| 144 | + return; |
| 145 | + } |
| 146 | + } else if (t instanceof Double) { |
| 147 | + if ((Double) t >= (Double) expected) { |
| 148 | + throw new AssertionError("Expected " + t + " to be less than " + expected); |
| 149 | + } else { |
| 150 | + return; |
| 151 | + } |
| 152 | + } |
| 153 | + |
| 154 | + throw new AssertionError("toBeLessThan can only be used on Integers, Longs, Floats, and Doubles"); |
| 155 | + } |
| 156 | + |
| 157 | + /** |
| 158 | + * Asserts that the actual value is an instance of the expected class. |
| 159 | + * This method checks whether the value held in the field 't' is an instance of the provided Class<?>. |
| 160 | + * |
| 161 | + * @param expected the Class object that the actual value is expected to be an instance of |
| 162 | + * @throws AssertionError if the actual value is not an instance of the expected class |
| 163 | + */ |
| 164 | + public void toBeInstanceOf(Class<?> expected) { |
| 165 | + if (!expected.isInstance(t)) { |
| 166 | + throw new AssertionError("Expected " + t + " to be an instance of " + expected); |
| 167 | + } |
| 168 | + } |
| 169 | + |
| 170 | + /** |
| 171 | + * Asserts that the given expected value is contained within the actual value. |
| 172 | + * <p> |
| 173 | + * This method checks if the expected value is present in a String, Iterable, or Array. |
| 174 | + * If the actual value is a String, it uses the contains method to check if the expected value |
| 175 | + * is a substring. If the actual value is an Iterable, it checks if the expected value is an element. |
| 176 | + * If the actual value is an Array, it checks if the expected value is present in the array. |
| 177 | + * |
| 178 | + * @param expected the value that is expected to be contained within the actual value |
| 179 | + * @throws AssertionError if the expected value is not contained within the actual value |
| 180 | + */ |
| 181 | + public void toContain(T expected) { |
| 182 | + if (t instanceof String) { |
| 183 | + if (!((String) t).contains((String) expected)) { |
| 184 | + throw new AssertionError("Expected " + expected + " to be contained in " + t); |
| 185 | + } else { |
| 186 | + return; |
| 187 | + } |
| 188 | + } else if (t instanceof Iterable) { |
| 189 | + if (!((Iterable<?>) t).spliterator().tryAdvance(o -> { |
| 190 | + if (o.equals(expected)) { |
| 191 | + return; |
| 192 | + } |
| 193 | + throw new AssertionError("Expected " + expected + " to be contained in " + t); |
| 194 | + })) { |
| 195 | + throw new AssertionError("Expected " + expected + " to be contained in " + t); |
| 196 | + } else { |
| 197 | + return; |
| 198 | + } |
| 199 | + } else if (t instanceof Object[]) { |
| 200 | + for (Object o : (Object[]) t) { |
| 201 | + if (o.equals(expected)) { |
| 202 | + return; |
| 203 | + } |
| 204 | + } |
| 205 | + throw new AssertionError("Expected " + expected + " to be contained in " + t); |
| 206 | + } |
| 207 | + |
| 208 | + throw new AssertionError("toContain can only be used on Strings, Iterables and Arrays"); |
| 209 | + } |
| 210 | + |
| 211 | + /** |
| 212 | + * Asserts that the actual value has the expected length. |
| 213 | + * This method checks if the actual value is a String, Iterable, or Array, |
| 214 | + * and compares their length or size to the given expected length. |
| 215 | + * |
| 216 | + * @param expected the expected length of the actual value |
| 217 | + * @throws AssertionError if the actual value does not have the expected length, |
| 218 | + * or if the actual value is not of type String, Iterable, or Array |
| 219 | + */ |
| 220 | + public void toHaveLength(int expected) { |
| 221 | + if (t instanceof String) { |
| 222 | + if (((String) t).length() != expected) { |
| 223 | + throw new AssertionError("Expected " + expected + " but got " + ((String) t).length()); |
| 224 | + } else { |
| 225 | + return; |
| 226 | + } |
| 227 | + } else if (t instanceof Iterable) { |
| 228 | + if (((Iterable<?>) t).spliterator().getExactSizeIfKnown() != expected) { |
| 229 | + throw new AssertionError("Expected " + expected + " but got " + ((Iterable<?>) t).spliterator().getExactSizeIfKnown()); |
| 230 | + } else { |
| 231 | + return; |
| 232 | + } |
| 233 | + } else if (t instanceof Object[]) { |
| 234 | + if (((Object[]) t).length != expected) { |
| 235 | + throw new AssertionError("Expected " + expected + " but got " + ((Object[]) t).length); |
| 236 | + } else { |
| 237 | + return; |
| 238 | + } |
| 239 | + } |
| 240 | + |
| 241 | + throw new AssertionError("toHaveLength can only be used on Strings"); |
| 242 | + } |
| 243 | +} |
0 commit comments