1 package io.jawk.gawk;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
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
38
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 }