|
| 1 | +package uvl; |
| 2 | + |
| 3 | +import org.antlr.v4.runtime.CharStream; |
| 4 | +import org.antlr.v4.runtime.CharStreams; |
| 5 | +import org.antlr.v4.runtime.CommonTokenStream; |
| 6 | +import org.junit.jupiter.params.ParameterizedTest; |
| 7 | +import org.junit.jupiter.params.provider.MethodSource; |
| 8 | + |
| 9 | +import java.io.IOException; |
| 10 | +import java.nio.file.Files; |
| 11 | +import java.nio.file.Path; |
| 12 | +import java.nio.file.Paths; |
| 13 | +import java.util.stream.Stream; |
| 14 | + |
| 15 | +import static org.junit.jupiter.api.Assertions.assertDoesNotThrow; |
| 16 | +import static org.junit.jupiter.api.Assertions.assertThrows; |
| 17 | + |
| 18 | +class UVLParserTest { |
| 19 | + |
| 20 | + private static final Path TEST_MODELS_DIR = Paths.get("../test_models"); |
| 21 | + |
| 22 | + static Stream<Path> validModels() throws IOException { |
| 23 | + return Files.walk(TEST_MODELS_DIR) |
| 24 | + .filter(p -> p.toString().endsWith(".uvl")) |
| 25 | + .filter(p -> !p.toString().contains("faulty")); |
| 26 | + } |
| 27 | + |
| 28 | + static Stream<Path> faultyModels() throws IOException { |
| 29 | + return Files.list(TEST_MODELS_DIR.resolve("faulty")) |
| 30 | + .filter(p -> p.toString().endsWith(".uvl")); |
| 31 | + } |
| 32 | + |
| 33 | + private void parseFile(Path file) throws IOException { |
| 34 | + CharStream input = CharStreams.fromPath(file); |
| 35 | + UVLJavaLexer lexer = new UVLJavaLexer(input); |
| 36 | + lexer.removeErrorListeners(); |
| 37 | + lexer.addErrorListener(ThrowingErrorListener.INSTANCE); |
| 38 | + CommonTokenStream tokens = new CommonTokenStream(lexer); |
| 39 | + UVLJavaParser parser = new UVLJavaParser(tokens); |
| 40 | + parser.removeErrorListeners(); |
| 41 | + parser.addErrorListener(ThrowingErrorListener.INSTANCE); |
| 42 | + parser.featureModel(); |
| 43 | + } |
| 44 | + |
| 45 | + @ParameterizedTest(name = "{0}") |
| 46 | + @MethodSource("validModels") |
| 47 | + void testValidModel(Path file) { |
| 48 | + assertDoesNotThrow(() -> parseFile(file), "Failed to parse: " + file); |
| 49 | + } |
| 50 | + |
| 51 | + @ParameterizedTest(name = "{0}") |
| 52 | + @MethodSource("faultyModels") |
| 53 | + void testFaultyModel(Path file) { |
| 54 | + assertThrows(RuntimeException.class, () -> parseFile(file), "Expected parse error for: " + file); |
| 55 | + } |
| 56 | +} |
0 commit comments