Skip to content

Commit 15955c0

Browse files
anthonyvdotbesideshowbarker
authored andcommitted
Add Html5libTest
1 parent e6abe04 commit 15955c0

File tree

4 files changed

+92
-6
lines changed

4 files changed

+92
-6
lines changed

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@
3636

3737
public class EncodingTester {
3838

39-
private static int exitStatus = 0;
39+
static int exitStatus = 0;
4040

4141
protected static int SNIFFING_LIMIT = 16384;
4242

@@ -51,7 +51,7 @@ public EncodingTester(InputStream aggregateStream) {
5151
this.aggregateStream = aggregateStream;
5252
}
5353

54-
private void runTests() throws IOException, SAXException {
54+
void runTests() throws IOException, SAXException {
5555
while (runTest()) {
5656
// spin
5757
}
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
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+
}

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@
5151

5252
public class TokenizerTester {
5353

54-
private static int exitStatus = 0;
54+
static int exitStatus = 0;
5555

5656
private static JSONString PLAINTEXT = new JSONString("PLAINTEXT state");
5757

@@ -121,7 +121,7 @@ public TokenizerTester(InputStream stream) throws TokenStreamException,
121121
}
122122
}
123123

124-
private void runTests() throws SAXException, IOException {
124+
void runTests() throws SAXException, IOException {
125125
for (JSONValue val : tests.getValue()) {
126126
runTest((JSONObject) val);
127127
}

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

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

4444
private boolean streaming = false;
4545

46-
private static int exitStatus = 0;
46+
static int exitStatus = 0;
4747

4848
/**
4949
* @param aggregateStream
@@ -52,7 +52,7 @@ public TreeTester(InputStream aggregateStream) {
5252
this.aggregateStream = new BufferedInputStream(aggregateStream);
5353
}
5454

55-
private void runTests() throws Throwable {
55+
void runTests() throws Throwable {
5656
if (aggregateStream.read() != '#') {
5757
System.err.println("No hash at start!");
5858
return;

0 commit comments

Comments
 (0)