Skip to content

Commit

Permalink
Html5libTest: Add copyright; match project style
Browse files Browse the repository at this point in the history
  • Loading branch information
sideshowbarker committed Aug 26, 2020
1 parent c1c1035 commit 2317de9
Showing 1 changed file with 47 additions and 12 deletions.
59 changes: 47 additions & 12 deletions test-src/nu/validator/htmlparser/test/Html5libTest.java
Original file line number Diff line number Diff line change
@@ -1,3 +1,25 @@
/*
* Copyright (c) 2020 Mozilla Foundation
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS IN THE SOFTWARE.
*/

package nu.validator.htmlparser.test;

import java.io.IOException;
Expand All @@ -14,26 +36,33 @@ public class Html5libTest {
private final Path testDir;

public Html5libTest() throws URISyntaxException {
this.testDir = Path.of(Html5libTest.class.getResource("/html5lib-tests").toURI());
this.testDir = Path.of(
Html5libTest.class.getResource("/html5lib-tests").toURI());
}

public void testEncoding() throws Exception {
Files.walkFileTree(testDir.resolve("encoding"), new TestVisitor(true, false, file -> new EncodingTester(Files.newInputStream(file)).runTests()));
if(EncodingTester.exitStatus != 0) {
Files.walkFileTree(testDir.resolve("encoding"), //
new TestVisitor(true, false, file -> //
new EncodingTester(Files.newInputStream(file)).runTests()));
if (EncodingTester.exitStatus != 0) {
assert false : "Encoding test failed";
}
}

public void testTokenizer() throws Exception {
Files.walkFileTree(testDir.resolve("tokenizer"), new TestVisitor(true, true, file -> new TokenizerTester(Files.newInputStream(file)).runTests()));
if(TokenizerTester.exitStatus != 0) {
Files.walkFileTree(testDir.resolve("tokenizer"),
new TestVisitor(true, true, file -> //
new TokenizerTester(Files.newInputStream(file)).runTests()));
if (TokenizerTester.exitStatus != 0) {
assert false : "Tokenizer test failed";
}
}

public void testTree() throws Exception {
Files.walkFileTree(testDir.resolve("tree-construction"), new TestVisitor(true, false, file -> new TreeTester(Files.newInputStream(file)).runTests()));
if(TreeTester.exitStatus != 0) {
Files.walkFileTree(testDir.resolve("tree-construction"),
new TestVisitor(true, false, file -> //
new TreeTester(Files.newInputStream(file)).runTests()));
if (TreeTester.exitStatus != 0) {
assert false : "Tree test failed";
}
}
Expand All @@ -44,7 +73,7 @@ private interface TestConsumer extends Consumer<Path> {
default void accept(Path t) {
try {
acceptTest(t);
} catch(Throwable e) {
} catch (Throwable e) {
throw new AssertionError(e);
}
}
Expand All @@ -56,17 +85,21 @@ default void accept(Path t) {
private static class TestVisitor extends SimpleFileVisitor<Path> {

private final boolean skipScripted;

private final boolean requireTestExtension;

private final TestConsumer runner;

private TestVisitor(boolean skipScripted, boolean requireTestExtension, TestConsumer runner) {
private TestVisitor(boolean skipScripted, boolean requireTestExtension,
TestConsumer runner) {
this.skipScripted = skipScripted;
this.requireTestExtension = requireTestExtension;
this.runner = runner;
}

@Override
public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) throws IOException {
public FileVisitResult preVisitDirectory(Path dir,
BasicFileAttributes attrs) throws IOException {
if (skipScripted && dir.getFileName().equals(Path.of("scripted"))) {
return FileVisitResult.SKIP_SUBTREE;
}
Expand All @@ -75,8 +108,10 @@ public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) th
}

@Override
public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
if (!requireTestExtension || file.getFileName().toString().endsWith(".test")) {
public FileVisitResult visitFile(Path file, BasicFileAttributes attrs)
throws IOException {
if (!requireTestExtension
|| file.getFileName().toString().endsWith(".test")) {
runner.accept(file);
}
return FileVisitResult.CONTINUE;
Expand Down

0 comments on commit 2317de9

Please sign in to comment.