View Javadoc
1   package io.jawk.gawk;
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 static org.junit.Assume.assumeTrue;
26  import static org.junit.Assert.assertTrue;
27  
28  import java.io.IOException;
29  import java.nio.charset.StandardCharsets;
30  import java.nio.file.Files;
31  import java.nio.file.Path;
32  import io.jawk.AwkTestSupport;
33  import io.jawk.CompatibilityTestResources;
34  import io.jawk.jrt.AwkRuntimeException;
35  
36  /**
37   * Shared helpers for the explicit gawk compatibility integration suites
38   * transcribed from the vendored GNU Awk test suite.
39   */
40  abstract class AbstractGawkSuite {
41  
42  	protected static final Path GAWK_DIRECTORY = CompatibilityTestResources
43  			.resourceDirectory(AbstractGawkSuite.class, "gawk");
44  	protected static final String NON_ZERO_TRANSCRIPT_REASON = "This case compares a non-zero gawk CLI transcript, which is intentionally skipped in the explicit AwkTestSupport.cliTest suite.";
45  	protected static final String NON_UTF8_STDIN_REASON = "This case redirects stdin that is not valid UTF-8, which is intentionally skipped in the explicit AwkTestSupport.cliTest suite.";
46  	protected static final String NON_UTF8_EXPECTED_REASON = "This case uses an expected .ok file that is not valid UTF-8, which is intentionally skipped in the explicit AwkTestSupport.cliTest suite.";
47  	protected static final String MANUAL_SKIP_REASON = "Handwritten gawk case from Makefile.am not yet expressed as an AwkTestSupport.cliTest case.";
48  
49  	protected static Path gawkPath(String fileName) {
50  		return GAWK_DIRECTORY.resolve(fileName);
51  	}
52  
53  	protected static String gawkFile(String fileName) {
54  		return gawkPath(fileName).toString();
55  	}
56  
57  	protected static String gawkText(String fileName) throws IOException {
58  		return new String(Files.readAllBytes(gawkPath(fileName)), StandardCharsets.UTF_8);
59  	}
60  
61  	protected static void assertGawkRuntimeFailure(String testName) throws Exception {
62  		AwkTestSupport.TestResult result = AwkTestSupport
63  				.awkTest("GAWK " + testName)
64  				.script(gawkText(testName + ".awk"))
65  				.expectThrow(AwkRuntimeException.class)
66  				.run();
67  		result.assertExpected();
68  
69  		String expectedTranscript = gawkText(testName + ".ok");
70  		String expectedMessage = expectedTranscript.contains(" as an array") ?
71  				"scalar as an array" : "array in a scalar context";
72  		String actualMessage = result.thrownException().getMessage();
73  		assertTrue(actualMessage, actualMessage.contains(expectedMessage));
74  	}
75  
76  	protected static void skip(String reason) {
77  		assumeTrue(reason, false);
78  	}
79  }