|
| 1 | +package com.iluwatar.interpreter; |
| 2 | + |
| 3 | +import org.junit.Test; |
| 4 | + |
| 5 | +import java.util.ArrayList; |
| 6 | +import java.util.List; |
| 7 | +import java.util.function.BiFunction; |
| 8 | + |
| 9 | +import static org.junit.Assert.assertEquals; |
| 10 | +import static org.junit.Assert.assertNotNull; |
| 11 | + |
| 12 | +/** |
| 13 | + * Date: 12/14/15 - 11:48 AM |
| 14 | + * |
| 15 | + * @author Jeroen Meulemeester |
| 16 | + */ |
| 17 | +public abstract class ExpressionTest<E extends Expression> { |
| 18 | + |
| 19 | + /** |
| 20 | + * Generate inputs ranging from -10 to 10 for both input params and calculate the expected result |
| 21 | + * |
| 22 | + * @param resultCalc The function used to calculate the expected result |
| 23 | + * @return A data set with test entries |
| 24 | + */ |
| 25 | + static List<Object[]> prepareParameters(final BiFunction<Integer, Integer, Integer> resultCalc) { |
| 26 | + final List<Object[]> testData = new ArrayList<>(); |
| 27 | + for (int i = -10; i < 10; i++) { |
| 28 | + for (int j = -10; j < 10; j++) { |
| 29 | + testData.add(new Object[]{ |
| 30 | + new NumberExpression(i), |
| 31 | + new NumberExpression(j), |
| 32 | + resultCalc.apply(i, j) |
| 33 | + }); |
| 34 | + } |
| 35 | + } |
| 36 | + return testData; |
| 37 | + } |
| 38 | + |
| 39 | + /** |
| 40 | + * The input used as first parameter during the test |
| 41 | + */ |
| 42 | + private final NumberExpression first; |
| 43 | + |
| 44 | + /** |
| 45 | + * The input used as second parameter during the test |
| 46 | + */ |
| 47 | + private final NumberExpression second; |
| 48 | + |
| 49 | + /** |
| 50 | + * The expected result of the calculation, taking the first and second parameter in account |
| 51 | + */ |
| 52 | + private final int result; |
| 53 | + |
| 54 | + /** |
| 55 | + * The expected {@link E#toString()} response |
| 56 | + */ |
| 57 | + private final String expectedToString; |
| 58 | + |
| 59 | + /** |
| 60 | + * Factory, used to create a new test object instance with the correct first and second parameter |
| 61 | + */ |
| 62 | + private final BiFunction<NumberExpression, NumberExpression, E> factory; |
| 63 | + |
| 64 | + /** |
| 65 | + * Create a new test instance with the given parameters and expected results |
| 66 | + * |
| 67 | + * @param first The input used as first parameter during the test |
| 68 | + * @param second The input used as second parameter during the test |
| 69 | + * @param result The expected result of the tested expression |
| 70 | + * @param expectedToString The expected {@link E#toString()} response |
| 71 | + * @param factory Factory, used to create a new test object instance |
| 72 | + */ |
| 73 | + ExpressionTest(final NumberExpression first, final NumberExpression second, final int result, |
| 74 | + final String expectedToString, final BiFunction<NumberExpression, NumberExpression, E> factory) { |
| 75 | + |
| 76 | + this.first = first; |
| 77 | + this.second = second; |
| 78 | + this.result = result; |
| 79 | + this.expectedToString = expectedToString; |
| 80 | + this.factory = factory; |
| 81 | + } |
| 82 | + |
| 83 | + /** |
| 84 | + * Get the first parameter |
| 85 | + * |
| 86 | + * @return The first parameter |
| 87 | + */ |
| 88 | + final NumberExpression getFirst() { |
| 89 | + return this.first; |
| 90 | + } |
| 91 | + |
| 92 | + /** |
| 93 | + * Verify if the expression calculates the correct result when calling {@link E#interpret()} |
| 94 | + */ |
| 95 | + @Test |
| 96 | + public void testInterpret() { |
| 97 | + final E expression = this.factory.apply(this.first, this.second); |
| 98 | + assertNotNull(expression); |
| 99 | + assertEquals(this.result, expression.interpret()); |
| 100 | + } |
| 101 | + |
| 102 | + /** |
| 103 | + * Verify if the expression has the expected {@link E#toString()} value |
| 104 | + */ |
| 105 | + @Test |
| 106 | + public void testToString() { |
| 107 | + final E expression = this.factory.apply(this.first, this.second); |
| 108 | + assertNotNull(expression); |
| 109 | + assertEquals(expectedToString, expression.toString()); |
| 110 | + } |
| 111 | +} |
0 commit comments