Skip to content

Commit f1c4eb8

Browse files
anthonyvdotbesideshowbarker
authored andcommitted
Add Html5libTest
1 parent 41b60f7 commit f1c4eb8

File tree

4 files changed

+94
-7
lines changed

4 files changed

+94
-7
lines changed

test-src/nu/validator/htmlparser/test/EncodingTester.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ public class EncodingTester {
3939

4040
protected static int SNIFFING_LIMIT = 16384;
4141

42-
private static int exitStatus = 0;
42+
static int exitStatus = 0;
4343

4444
private final InputStream aggregateStream;
4545

@@ -59,7 +59,7 @@ public EncodingTester() {
5959
this.aggregateStream = null;
6060
}
6161

62-
private void runTests() throws IOException, SAXException {
62+
void runTests() throws IOException, SAXException {
6363
while (runTest()) {
6464
// spin
6565
}
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
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(null).test(file.toString())));
29+
if(TokenizerTester.exitStatus != 0) {
30+
assert false : "Tokenizer test failed";
31+
}
32+
}
33+
34+
public void testTree() throws Exception {
35+
TreeTester tester = new TreeTester();
36+
Files.walkFileTree(testDir.resolve("tree-construction"), new TestVisitor(true, false, file -> tester.runTests(file.toString())));
37+
if(TreeTester.exitStatus != 0) {
38+
assert false : "Tree test failed";
39+
}
40+
}
41+
42+
private interface TestConsumer extends Consumer<Path> {
43+
44+
@Override
45+
default void accept(Path t) {
46+
try {
47+
acceptTest(t);
48+
} catch(Throwable e) {
49+
throw new AssertionError(e);
50+
}
51+
}
52+
53+
void acceptTest(Path t) throws Throwable;
54+
55+
}
56+
57+
private static class TestVisitor extends SimpleFileVisitor<Path> {
58+
59+
private final boolean skipScripted;
60+
private final boolean requireTestExtension;
61+
private final TestConsumer runner;
62+
63+
private TestVisitor(boolean skipScripted, boolean requireTestExtension, TestConsumer runner) {
64+
this.skipScripted = skipScripted;
65+
this.requireTestExtension = requireTestExtension;
66+
this.runner = runner;
67+
}
68+
69+
@Override
70+
public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) throws IOException {
71+
if (skipScripted && dir.getFileName().equals(Path.of("scripted"))) {
72+
return FileVisitResult.SKIP_SUBTREE;
73+
}
74+
75+
return FileVisitResult.CONTINUE;
76+
}
77+
78+
@Override
79+
public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
80+
if (!requireTestExtension || file.getFileName().toString().endsWith(".test")) {
81+
runner.accept(file);
82+
}
83+
return FileVisitResult.CONTINUE;
84+
}
85+
}
86+
87+
}

test-src/nu/validator/htmlparser/test/TokenizerTester.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@
5656

5757
public class TokenizerTester {
5858

59-
private static int exitStatus = 0;
59+
static int exitStatus = 0;
6060

6161
private static JSONString PLAINTEXT = new JSONString("PLAINTEXT state");
6262

@@ -102,7 +102,7 @@ private static boolean jsonDeepEquals(JSONValue one, JSONValue other) {
102102

103103
private final Writer writer;
104104

105-
private TokenizerTester(InputStream stream) throws TokenStreamException,
105+
TokenizerTester(InputStream stream) throws TokenStreamException,
106106
RecognitionException, UnsupportedEncodingException {
107107
tests = null;
108108
tokenHandler = new JSONArrayTokenHandler();
@@ -226,7 +226,7 @@ private void recurseDirectory(File directory) throws Throwable {
226226
}
227227
}
228228

229-
private void test(String file)
229+
void test(String file)
230230
throws IOException, TokenStreamException, RecognitionException,
231231
SAXException {
232232
if (!file.endsWith(".test")) {

test-src/nu/validator/htmlparser/test/TreeTester.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ public class TreeTester {
4444

4545
private boolean streaming = false;
4646

47-
private static int exitStatus = 0;
47+
static int exitStatus = 0;
4848

4949
/**
5050
* @param aggregateStream
@@ -53,7 +53,7 @@ public TreeTester() {
5353
this.aggregateStream = null;
5454
}
5555

56-
private void runTests(String filename) throws Throwable {
56+
void runTests(String filename) throws Throwable {
5757
if (!filename.endsWith(".dat")) {
5858
return;
5959
}

0 commit comments

Comments
 (0)