|
| 1 | +/* |
| 2 | + * The MIT License |
| 3 | + * |
| 4 | + * Copyright (c) 2010 tap4j team (see AUTHORS) |
| 5 | + * |
| 6 | + * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 7 | + * of this software and associated documentation files (the "Software"), to deal |
| 8 | + * in the Software without restriction, including without limitation the rights |
| 9 | + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 10 | + * copies of the Software, and to permit persons to whom the Software is |
| 11 | + * furnished to do so, subject to the following conditions: |
| 12 | + * |
| 13 | + * The above copyright notice and this permission notice shall be included in |
| 14 | + * all copies or substantial portions of the Software. |
| 15 | + * |
| 16 | + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 17 | + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 18 | + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 19 | + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 20 | + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 21 | + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 22 | + * THE SOFTWARE. |
| 23 | + */ |
| 24 | +package com.tupilabs.tap4j; |
| 25 | + |
| 26 | +import org.apache.commons.io.FileUtils; |
| 27 | +import org.junit.Test; |
| 28 | +import org.parboiled.common.Predicates; |
| 29 | +import org.parboiled.support.Filters; |
| 30 | +import org.parboiled.support.ParsingResult; |
| 31 | +import org.parboiled.support.ToStringFormatter; |
| 32 | + |
| 33 | +import java.io.File; |
| 34 | +import java.io.IOException; |
| 35 | +import java.nio.file.Files; |
| 36 | +import java.nio.file.Path; |
| 37 | +import java.util.Collection; |
| 38 | +import java.util.List; |
| 39 | + |
| 40 | +import static org.parboiled.errors.ErrorUtils.printParseErrors; |
| 41 | +import static org.parboiled.support.ParseTreeUtils.printNodeTree; |
| 42 | +import static org.parboiled.trees.GraphUtils.printTree; |
| 43 | + |
| 44 | +/** |
| 45 | + * Test created to verify compatibility of new PEG parser with all |
| 46 | + * existing files in the test area. |
| 47 | + * |
| 48 | + * <p>Some files may have to be skipped if some feature is dropped |
| 49 | + * in the new parser, or if the file is used for bogus testing.</p> |
| 50 | + * |
| 51 | + * @since 5.0.0 |
| 52 | + */ |
| 53 | +public class AllFilesTest { |
| 54 | + |
| 55 | + private static void parse(Path path, boolean verbose) throws IOException { |
| 56 | + final String tapStream = Files.readString(path); |
| 57 | + Tap13Parser parser = new Tap13Parser(); |
| 58 | + ParsingResult<Object> parsingResult = parser.parse(tapStream); |
| 59 | + if (parsingResult.hasErrors()) { |
| 60 | + if (verbose) { |
| 61 | + System.err.println("\n--- ParseErrors ---\n"); |
| 62 | + // System.err.println(StringUtils.join(parsingResult.parseErrors, "---\n")); |
| 63 | + System.err.println(printParseErrors(parsingResult)); |
| 64 | + System.err.println("\n--- ParseTree ---\n"); |
| 65 | + System.err.println(printNodeTree(parsingResult, Filters.SKIP_EMPTY_OPTS_AND_ZOMS, Predicates.alwaysTrue())); |
| 66 | + } |
| 67 | + throw new RuntimeException("Errors parsing TAP file"); |
| 68 | + } else if (!parsingResult.matched) { |
| 69 | + throw new RuntimeException("No match!"); |
| 70 | + } |
| 71 | + if (verbose) { |
| 72 | + System.out.println("\n--- ParseTree ---\n"); |
| 73 | + String tree = printTree( |
| 74 | + parsingResult.parseTreeRoot, |
| 75 | + new ToStringFormatter<>(), |
| 76 | + Filters.SKIP_EMPTY_OPTS_AND_ZOMS, |
| 77 | + Predicates.alwaysTrue() |
| 78 | + ); |
| 79 | + System.out.println(tree); |
| 80 | + } |
| 81 | + } |
| 82 | + |
| 83 | + @Test |
| 84 | + public void testAll() { |
| 85 | + final String directory = AllFilesTest.class.getResource("/org/tap4j/").getFile(); |
| 86 | + Collection<File> files = FileUtils.listFiles(new File(directory), new String[] {"tap", "t"}, true); |
| 87 | + int failed = 0; |
| 88 | + int total = files.size(); |
| 89 | + for (File file: files) { |
| 90 | + try { |
| 91 | + parse(Path.of(file.getAbsolutePath()), false); |
| 92 | + System.out.printf("success [%s]%n", file.getAbsoluteFile()); |
| 93 | + } catch (IOException|RuntimeException e) { |
| 94 | + System.err.printf("failed [%s]%n", file.getAbsoluteFile()); |
| 95 | + failed++; |
| 96 | + } |
| 97 | + } |
| 98 | + System.out.printf("%.2f%% - failed [%d] success [%d] total [%d] %n", ((float) (total - failed) / total) * 100, failed, total - failed, total); |
| 99 | + } |
| 100 | +} |
0 commit comments