-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathPerlExitExceptionTest.java
More file actions
104 lines (83 loc) · 3.31 KB
/
PerlExitExceptionTest.java
File metadata and controls
104 lines (83 loc) · 3.31 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
package org.perlonjava;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Tag;
import org.junit.jupiter.api.Test;
import org.perlonjava.app.cli.CompilerOptions;
import org.perlonjava.app.scriptengine.PerlLanguageProvider;
import org.perlonjava.runtime.runtimetypes.PerlExitException;
import static org.junit.jupiter.api.Assertions.*;
/**
* Test that PerlExitException is thrown when Perl code calls exit(),
* allowing library users to catch it and continue execution.
*
* This addresses GitHub issue #291 where executePerlCode() was
* terminating the JVM instead of returning.
*/
@Tag("unit")
public class PerlExitExceptionTest {
@BeforeEach
void setUp() {
PerlLanguageProvider.resetAll();
}
@Test
void testExitZeroThrowsException() {
CompilerOptions options = new CompilerOptions();
options.fileName = "<test>";
options.code = "exit 0;";
PerlExitException thrown = assertThrows(PerlExitException.class, () -> {
PerlLanguageProvider.executePerlCode(options, true);
});
assertEquals(0, thrown.getExitCode(), "exit 0 should have exit code 0");
}
@Test
void testExitNonZeroThrowsException() {
CompilerOptions options = new CompilerOptions();
options.fileName = "<test>";
options.code = "exit 42;";
PerlExitException thrown = assertThrows(PerlExitException.class, () -> {
PerlLanguageProvider.executePerlCode(options, true);
});
assertEquals(42, thrown.getExitCode(), "exit 42 should have exit code 42");
}
@Test
void testExitAfterOutputThrowsException() {
CompilerOptions options = new CompilerOptions();
options.fileName = "<test>";
options.code = "print 'hello'; exit 0;";
PerlExitException thrown = assertThrows(PerlExitException.class, () -> {
PerlLanguageProvider.executePerlCode(options, true);
});
assertEquals(0, thrown.getExitCode());
}
@Test
void testScriptWithoutExitReturnsNormally() throws Exception {
CompilerOptions options = new CompilerOptions();
options.fileName = "<test>";
options.code = "my $x = 1 + 1;";
// Should not throw - script completes without calling exit()
assertDoesNotThrow(() -> {
PerlLanguageProvider.executePerlCode(options, true);
});
}
@Test
void testExceptionMessage() {
CompilerOptions options = new CompilerOptions();
options.fileName = "<test>";
options.code = "exit 123;";
PerlExitException thrown = assertThrows(PerlExitException.class, () -> {
PerlLanguageProvider.executePerlCode(options, true);
});
assertEquals("exit 123", thrown.getMessage());
}
@Test
void testExitInsideEvalNotCaught() {
// In Perl, exit() should NOT be caught by eval{} - it should always exit
CompilerOptions options = new CompilerOptions();
options.fileName = "<test>";
options.code = "eval { exit 99; }; print 'should not reach here';";
PerlExitException thrown = assertThrows(PerlExitException.class, () -> {
PerlLanguageProvider.executePerlCode(options, true);
});
assertEquals(99, thrown.getExitCode(), "exit inside eval should still throw PerlExitException");
}
}