Skip to content

Commit

Permalink
clean up
Browse files Browse the repository at this point in the history
  • Loading branch information
fglock committed Oct 15, 2024
1 parent 61b7124 commit 9190597
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 14 deletions.
2 changes: 1 addition & 1 deletion src/main/java/org/perlonjava/runtime/RuntimeScalar.java
Original file line number Diff line number Diff line change
Expand Up @@ -368,7 +368,7 @@ public long getLong() {
case UNDEF:
return 0L;
default:
return (long) ((RuntimeScalarReference) value).getIntRef();
return ((RuntimeScalarReference) value).getIntRef();
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,19 @@

import static org.junit.jupiter.api.Assertions.*;

public class PerlLanguageProviderTest {
/**
* Test class for PerlLanguageProvider.
* This class tests the execution of Perl code using the PerlLanguageProvider.
*/
public class PerlLanguageProviderExecutionTest {

private PrintStream originalOut;
private ByteArrayOutputStream outputStream;

/**
* Set up the test environment before each test.
* This method redirects System.out to a ByteArrayOutputStream for capturing output.
*/
@BeforeEach
void setUp() {
originalOut = System.out;
Expand All @@ -25,26 +33,36 @@ void setUp() {
RuntimeIO.setCustomOutputStream(outputStream); // Set custom OutputStream in RuntimeIO
}

/**
* Clean up the test environment after each test.
* This method restores the original System.out.
*/
@AfterEach
void tearDown() {
System.setOut(originalOut);
RuntimeIO.setCustomOutputStream(System.out); // Reset to original System.out
}

/**
* Test the execution of valid Perl code.
* This test verifies that the PerlLanguageProvider can execute a simple Perl script
* and produce the expected output.
*/
@Test
public void testExecutePerlCode() {
// Set up test options
ArgumentParser.CompilerOptions options = new ArgumentParser.CompilerOptions();
options.code = "print 'Hello, World!';";
options.fileName = "test.pl";

try {
// Execute the Perl code
RuntimeList result = PerlLanguageProvider.executePerlCode(options);

// Add assertions to verify the result
// Verify the result
assertNotNull(result, "Result should not be null");
// Add more specific assertions based on the expected behavior of executePerlCode
// For example, if the result should contain certain elements:
// assertTrue(result.contains(expectedElement), "Result should contain the expected element");
// TODO: Add more specific assertions based on the expected behavior of executePerlCode
// For example: assertTrue(result.contains(expectedElement), "Result should contain the expected element");

// Check the captured output
String output = outputStream.toString();
Expand All @@ -54,14 +72,19 @@ public void testExecutePerlCode() {
}
}

/**
* Test the execution of invalid Perl code.
* This test verifies that the PerlLanguageProvider throws an exception
* when trying to execute Perl code with syntax errors.
*/
@Test
public void testExecutePerlCodeWithError() {
// Set up test options with invalid Perl code
ArgumentParser.CompilerOptions options = new ArgumentParser.CompilerOptions();
options.code = "print 'Hello, World!' 123;"; // There is an error in Perl code
options.code = "print 'Hello, World!' 123;"; // Syntax error in Perl code
options.fileName = "test_error.pl";

assertThrows(Throwable.class, () -> {
PerlLanguageProvider.executePerlCode(options);
}, "Expected an exception to be thrown");
// Verify that an exception is thrown
assertThrows(Throwable.class, () -> PerlLanguageProvider.executePerlCode(options), "Expected an exception to be thrown");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,30 @@

import static org.junit.jupiter.api.Assertions.*;

public class PerlExecutionTest {
/**
* This class contains tests for executing Perl scripts using the PerlLanguageProvider.
* It uses JUnit 5 for testing and includes a parameterized test that runs all .pl files
* found in the resources directory.
*/
public class PerlScriptExecutionTest {

private PrintStream originalOut;
private ByteArrayOutputStream outputStream;

/**
* Provides a stream of Perl script filenames found in the resources directory.
* This method is used as a MethodSource for the parameterized test.
*
* @return A stream of Perl script filenames
* @throws URISyntaxException If there's an issue with the URI syntax
* @throws IOException If there's an I/O error
*/
static Stream<String> providePerlScripts() throws URISyntaxException, IOException {
URI uri = PerlExecutionTest.class.getResource("/").toURI();
URI uri = PerlScriptExecutionTest.class.getResource("/").toURI();
Path myPath;
// Interesting: This code handles both JAR and file system resources
if (uri.getScheme().equals("jar")) {
FileSystem fileSystem = FileSystems.newFileSystem(uri, Collections.<String, Object>emptyMap());
FileSystem fileSystem = FileSystems.newFileSystem(uri, Collections.emptyMap());
myPath = fileSystem.getPath("/");
} else {
myPath = Paths.get(uri);
Expand All @@ -38,6 +52,10 @@ static Stream<String> providePerlScripts() throws URISyntaxException, IOExceptio
.map(path -> path.getFileName().toString());
}

/**
* Sets up the test environment before each test.
* This method redirects System.out to a ByteArrayOutputStream for output capturing.
*/
@BeforeEach
void setUp() {
originalOut = System.out;
Expand All @@ -46,18 +64,28 @@ void setUp() {
RuntimeIO.setCustomOutputStream(outputStream); // Set custom OutputStream in RuntimeIO
}

/**
* Tears down the test environment after each test.
* This method restores the original System.out.
*/
@AfterEach
void tearDown() {
System.setOut(originalOut);
RuntimeIO.setCustomOutputStream(System.out); // Reset to original System.out
}

/**
* Parameterized test that executes each Perl script found in the resources directory.
* It checks that the script execution doesn't produce any "not ok" output, which would indicate a test failure.
*
* @param filename The name of the Perl script file to test
*/
@ParameterizedTest(name = "Test using resource file: {0}")
@MethodSource("providePerlScripts")
void testUsingResourceFile(String filename) {
// Load the file from the resources folder
InputStream inputStream = getClass().getClassLoader().getResourceAsStream(filename);
assertNotNull(inputStream);
assertNotNull(inputStream, "Resource file not found: " + filename);

try {
// Read the file content
Expand Down

0 comments on commit 9190597

Please sign in to comment.