View Javadoc
1   package io.jawk.onetrueawk;
2   
3   /*-
4    * ╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲
5    * Jawk
6    * ჻჻჻჻჻჻
7    * Copyright (C) 2006 - 2026 MetricsHub
8    * ჻჻჻჻჻჻
9    * This program is free software: you can redistribute it and/or modify
10   * it under the terms of the GNU Lesser General Public License as
11   * published by the Free Software Foundation, either version 3 of the
12   * License, or (at your option) any later version.
13   *
14   * This program is distributed in the hope that it will be useful,
15   * but WITHOUT ANY WARRANTY; without even the implied warranty of
16   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17   * GNU General Lesser Public License for more details.
18   *
19   * You should have received a copy of the GNU General Lesser Public
20   * License along with this program.  If not, see
21   * <http://www.gnu.org/licenses/lgpl-3.0.html>.
22   * ╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱
23   */
24  
25  import java.io.File;
26  import java.io.IOException;
27  import java.nio.file.Path;
28  import java.util.Arrays;
29  import java.util.stream.Collectors;
30  import io.jawk.AwkTestSupport;
31  import io.jawk.CompatibilityTestResources;
32  import org.junit.AfterClass;
33  import org.junit.BeforeClass;
34  import org.junit.Test;
35  import org.junit.runner.RunWith;
36  import org.junit.runners.Parameterized;
37  import org.junit.runners.Parameterized.Parameter;
38  import org.junit.runners.Parameterized.Parameters;
39  
40  /**
41   * Integration suite based on the BWK miscellaneous compatibility tests vendored
42   * from the One True Awk upstream repository. Each AWK script executes against
43   * its corresponding input file and its output is compared with the recorded
44   * result.
45   *
46   * @see <a href="https://github.com/onetrueawk/awk">BWK / One True Awk upstream repository</a>
47   */
48  @RunWith(Parameterized.class)
49  public class BwkMiscIT {
50  
51  	private static Path bwkMiscDirectory;
52  	private static Path scriptsDirectory;
53  
54  	/**
55  	 * Initializes the BWK miscellaneous integration suite.
56  	 *
57  	 * @throws Exception when resource discovery fails
58  	 */
59  	@BeforeClass
60  	public static void beforeAll() throws Exception {}
61  
62  	/**
63  	 * Returns the BWK miscellaneous script names discovered from the
64  	 * integration-test resources.
65  	 *
66  	 * @return the parameter values for this suite
67  	 * @throws Exception when resource discovery fails
68  	 */
69  	@Parameters(name = "BWK.misc {0}")
70  	public static Iterable<String> awkList() throws Exception {
71  		bwkMiscDirectory = CompatibilityTestResources.resourceDirectory(BwkMiscIT.class, "bwk", "misc");
72  		if (!bwkMiscDirectory.toFile().isDirectory()) {
73  			throw new IOException(bwkMiscDirectory + " is not a directory");
74  		}
75  		scriptsDirectory = bwkMiscDirectory.resolve("scripts");
76  		if (!scriptsDirectory.toFile().isDirectory()) {
77  			throw new IOException("scripts is not a directory");
78  		}
79  		File[] scriptFiles = scriptsDirectory.toFile().listFiles();
80  		if (scriptFiles == null) {
81  			throw new IOException("Couldn't list files in " + scriptsDirectory);
82  		}
83  
84  		return Arrays
85  				.stream(scriptFiles)
86  				.filter(scriptFile -> scriptFile.getName().endsWith(".awk"))
87  				.map(File::getName)
88  				.sorted()
89  				.collect(Collectors.toList());
90  	}
91  
92  	/** Path to the AWK test script to execute. */
93  	@Parameter
94  	public String awkName;
95  
96  	/**
97  	 * Executes one BWK miscellaneous script and compares its output with the
98  	 * expected result.
99  	 *
100 	 * @throws Exception when the test setup or execution fails unexpectedly
101 	 */
102 	@Test
103 	public void test() throws Exception {
104 		Path awkFile = scriptsDirectory.resolve(awkName);
105 		String shortName = awkName.substring(0, awkName.length() - 4);
106 		Path inputFile = bwkMiscDirectory.resolve("inputs/" + shortName + ".in");
107 		Path okFile = bwkMiscDirectory.resolve("results/" + shortName + ".ok");
108 
109 		AwkTestSupport
110 				.cliTest("BWK.misc " + awkName)
111 				.argument("-f", awkFile.toString())
112 				.operand(inputFile.toString())
113 				.expectLines(okFile)
114 				.build()
115 				.runAndAssert();
116 	}
117 
118 	/**
119 	 * Finalizes the BWK miscellaneous integration suite.
120 	 *
121 	 * @throws Exception unused hook retained for suite symmetry
122 	 */
123 	@AfterClass
124 	public static void afterAll() throws Exception {}
125 }