|
| 1 | +package nu.validator.htmlparser.test; |
| 2 | + |
| 3 | +import java.io.IOException; |
| 4 | +import java.net.URISyntaxException; |
| 5 | +import java.nio.file.FileVisitResult; |
| 6 | +import java.nio.file.Files; |
| 7 | +import java.nio.file.Path; |
| 8 | +import java.nio.file.SimpleFileVisitor; |
| 9 | +import java.nio.file.attribute.BasicFileAttributes; |
| 10 | +import java.util.function.Consumer; |
| 11 | + |
| 12 | +public class Html5libTest { |
| 13 | + |
| 14 | + private final Path testDir; |
| 15 | + |
| 16 | + public Html5libTest() throws URISyntaxException { |
| 17 | + this.testDir = Path.of(Html5libTest.class.getResource("/html5lib-tests").toURI()); |
| 18 | + } |
| 19 | + |
| 20 | + public void testEncoding() throws Exception { |
| 21 | + Files.walkFileTree(testDir.resolve("encoding"), new TestVisitor(true, false, file -> new EncodingTester(Files.newInputStream(file)).runTests())); |
| 22 | + if(EncodingTester.exitStatus != 0) { |
| 23 | + assert false : "Encoding test failed"; |
| 24 | + } |
| 25 | + } |
| 26 | + |
| 27 | + public void testTokenizer() throws Exception { |
| 28 | + Files.walkFileTree(testDir.resolve("tokenizer"), new TestVisitor(true, true, file -> new TokenizerTester(Files.newInputStream(file)).runTests())); |
| 29 | + if(TokenizerTester.exitStatus != 0) { |
| 30 | + assert false : "Tokenizer test failed"; |
| 31 | + } |
| 32 | + } |
| 33 | + |
| 34 | + public void testTree() throws Exception { |
| 35 | + Files.walkFileTree(testDir.resolve("tree-construction"), new TestVisitor(true, false, file -> new TreeTester(Files.newInputStream(file)).runTests())); |
| 36 | + if(TreeTester.exitStatus != 0) { |
| 37 | + assert false : "Tree test failed"; |
| 38 | + } |
| 39 | + } |
| 40 | + |
| 41 | + private interface TestConsumer extends Consumer<Path> { |
| 42 | + |
| 43 | + @Override |
| 44 | + default void accept(Path t) { |
| 45 | + try { |
| 46 | + acceptTest(t); |
| 47 | + } catch(Throwable e) { |
| 48 | + throw new AssertionError(e); |
| 49 | + } |
| 50 | + } |
| 51 | + |
| 52 | + void acceptTest(Path t) throws Throwable; |
| 53 | + |
| 54 | + } |
| 55 | + |
| 56 | + private static class TestVisitor extends SimpleFileVisitor<Path> { |
| 57 | + |
| 58 | + private final boolean skipScripted; |
| 59 | + private final boolean requireTestExtension; |
| 60 | + private final TestConsumer runner; |
| 61 | + |
| 62 | + private TestVisitor(boolean skipScripted, boolean requireTestExtension, TestConsumer runner) { |
| 63 | + this.skipScripted = skipScripted; |
| 64 | + this.requireTestExtension = requireTestExtension; |
| 65 | + this.runner = runner; |
| 66 | + } |
| 67 | + |
| 68 | + @Override |
| 69 | + public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) throws IOException { |
| 70 | + if (skipScripted && dir.getFileName().equals(Path.of("scripted"))) { |
| 71 | + return FileVisitResult.SKIP_SUBTREE; |
| 72 | + } |
| 73 | + |
| 74 | + return FileVisitResult.CONTINUE; |
| 75 | + } |
| 76 | + |
| 77 | + @Override |
| 78 | + public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException { |
| 79 | + if (!requireTestExtension || file.getFileName().toString().endsWith(".test")) { |
| 80 | + runner.accept(file); |
| 81 | + } |
| 82 | + return FileVisitResult.CONTINUE; |
| 83 | + } |
| 84 | + } |
| 85 | + |
| 86 | +} |
0 commit comments