|
| 1 | +/* |
| 2 | + * MIT License |
| 3 | + * |
| 4 | + * Copyright (c) 2020 NUM Technology Ltd |
| 5 | + * |
| 6 | + * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated |
| 7 | + * documentation files (the "Software"), to deal in the Software without restriction, including without limitation |
| 8 | + * the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and |
| 9 | + * to permit persons to whom the Software is furnished to do so, subject to the following conditions: |
| 10 | + * |
| 11 | + * The above copyright notice and this permission notice shall be included in all copies or substantial portions of |
| 12 | + * the Software. |
| 13 | + * |
| 14 | + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE |
| 15 | + * WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS |
| 16 | + * OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR |
| 17 | + * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
| 18 | + * |
| 19 | + */ |
| 20 | + |
| 21 | +package uk.modl.interpreter; |
| 22 | + |
| 23 | +import static org.junit.Assert.fail; |
| 24 | + |
| 25 | +import java.io.FileInputStream; |
| 26 | +import java.io.IOException; |
| 27 | +import java.io.InputStream; |
| 28 | +import java.util.ArrayList; |
| 29 | +import java.util.Collections; |
| 30 | +import java.util.LinkedList; |
| 31 | +import java.util.List; |
| 32 | +import java.util.function.Function; |
| 33 | + |
| 34 | +import com.fasterxml.jackson.core.type.TypeReference; |
| 35 | +import com.fasterxml.jackson.databind.JsonNode; |
| 36 | +import com.fasterxml.jackson.databind.ObjectMapper; |
| 37 | +import lombok.Data; |
| 38 | +import lombok.extern.log4j.Log4j2; |
| 39 | +import org.junit.Test; |
| 40 | +import uk.modl.interpreter.model.Modl; |
| 41 | + |
| 42 | +@Log4j2 |
| 43 | +public class InterpreterBaseTests { |
| 44 | + |
| 45 | + private final List<String> errors = new ArrayList<>(); |
| 46 | + |
| 47 | + private final ObjectMapper mapper = new ObjectMapper(); |
| 48 | + |
| 49 | + /** |
| 50 | + * Read a set of tests from a file |
| 51 | + */ |
| 52 | + private final Function<String, List<TestInput>> load = (filename) -> { |
| 53 | + try (final InputStream fileStream = new FileInputStream(filename)) { |
| 54 | + return mapper.readValue(fileStream, new TypeReference<LinkedList<TestInput>>() { |
| 55 | + |
| 56 | + }); |
| 57 | + } catch (IOException e) { |
| 58 | + log.error("Error accessing file: {}", filename, e); |
| 59 | + } |
| 60 | + return Collections.emptyList(); |
| 61 | + }; |
| 62 | + |
| 63 | + @Test |
| 64 | + public void testBaseTest() { |
| 65 | + |
| 66 | + final List<TestInput> list = load.apply("../grammar/tests/base_tests.json"); |
| 67 | + int testNumber = 1; |
| 68 | + final int startFromTestNumber = 0;// Use this to skip tests manually to make it easier for debugging a specific |
| 69 | + // test. |
| 70 | + for (final TestInput testInput : list) { |
| 71 | + if (testNumber >= startFromTestNumber && !testInput.input.equals("DELETED")) { |
| 72 | + checkValidTestInput(testInput); |
| 73 | + } |
| 74 | + testNumber++; |
| 75 | + } |
| 76 | + |
| 77 | + if (errors.size() > 0) { |
| 78 | + log.info("--------------- Errors ----------------"); |
| 79 | + for (final String error : errors) { |
| 80 | + log.error(error); |
| 81 | + } |
| 82 | + fail("Errors found."); |
| 83 | + } |
| 84 | + } |
| 85 | + |
| 86 | + private void checkValidTestInput(final TestInput testInput) { |
| 87 | + try { |
| 88 | + final Modl interpreted = new Interpreter().interpret(testInput.input); |
| 89 | + |
| 90 | + if (interpreted != null) { |
| 91 | + final JsonNode jsonResult = ModlToJson.convert(interpreted); |
| 92 | + |
| 93 | + final String output = mapper.writeValueAsString(jsonResult); |
| 94 | + if (output != null) { |
| 95 | + |
| 96 | + final String expected = testInput.expected_output.replace(" ", "") |
| 97 | + .replace("\n", "") |
| 98 | + .replace("\r", ""); |
| 99 | + final String actual = output.replace(" ", "") |
| 100 | + .replace("\n", "") |
| 101 | + .replace("\r", ""); |
| 102 | + |
| 103 | + if (!expected.equals(actual)) { |
| 104 | + log.info("Running test number: " + testInput.id); |
| 105 | + log.info("Input : " + testInput.input); |
| 106 | + log.info("Minimised : " + testInput.minimised_modl); |
| 107 | + log.info("Expected : " + testInput.expected_output); |
| 108 | + log.info("Actual : " + output); |
| 109 | + |
| 110 | + errors.add("Test: " + testInput.id + "\nExpected: " + testInput.expected_output + "\n" |
| 111 | + + "Actual : " |
| 112 | + + output + "\n"); |
| 113 | + } else { |
| 114 | + log.info("Test: " + testInput.id + " - no errors\n"); |
| 115 | + } |
| 116 | + |
| 117 | + } else { |
| 118 | + errors.add("Test: " + testInput.id + "\nExpected: " + testInput.expected_output + "\n" |
| 119 | + + "Actual : null\n"); |
| 120 | + } |
| 121 | + |
| 122 | + } else { |
| 123 | + log.error("Test: " + testInput.id + " - no result\n"); |
| 124 | + |
| 125 | + } |
| 126 | + } catch (final Exception e) { |
| 127 | + log.error("Test: " + testInput.id + " - exception: " + e, e); |
| 128 | + errors.add("Test: " + testInput.id + "\nExpected: " + testInput.expected_output + "\n" + "Actual : " |
| 129 | + + e.getMessage() + "\n"); |
| 130 | + } |
| 131 | + } |
| 132 | + |
| 133 | + @Test |
| 134 | + public void testExtraTests() { |
| 135 | + // Go through grammar_test/error_tests.json making sure all tests raise an error |
| 136 | + // of some kind |
| 137 | + final List<TestInput> list = load.apply("../grammar/tests/extra_tests.json"); |
| 138 | + int testNumber = 1; |
| 139 | + final int startFromTestNumber = 0;// Use this to skip tests manually to make it easier for debugging a specific |
| 140 | + // test. |
| 141 | + for (final TestInput testInput : list) { |
| 142 | + if (testNumber >= startFromTestNumber && !testInput.input.equals("DELETED")) { |
| 143 | + checkValidTestInput(testInput); |
| 144 | + } |
| 145 | + testNumber++; |
| 146 | + } |
| 147 | + |
| 148 | + if (errors.size() > 0) { |
| 149 | + log.info("--------------- Errors ----------------"); |
| 150 | + for (final String error : errors) { |
| 151 | + log.error(error); |
| 152 | + } |
| 153 | + fail("Errors found."); |
| 154 | + } |
| 155 | + } |
| 156 | + |
| 157 | + @Data |
| 158 | + public static class TestInput { |
| 159 | + |
| 160 | + public String input; |
| 161 | + |
| 162 | + public String minimised_modl; |
| 163 | + |
| 164 | + public String expected_output; |
| 165 | + |
| 166 | + public String[] tested_features; |
| 167 | + |
| 168 | + public int id; |
| 169 | + |
| 170 | + } |
| 171 | + |
| 172 | +} |
0 commit comments