Skip to content
Snippets Groups Projects
Commit cd6f3644 authored by René Schöne's avatar René Schöne
Browse files

Prepare to test for missing trailing newline.

parent 51cbd0ef
No related branches found
No related tags found
1 merge request!6Update relast-preprocessor to newest version.
Pipeline #9891 passed
This commit is part of merge request !6. Comments created here will be created in the context of that merge request.
/*.noNewLine.*
# Issue27
Regression test for failing parser when missing newline at end of specification.
receive A.Name ;
A ::= <Name:String> ;
...@@ -2,7 +2,6 @@ package org.jastadd.ragconnect.tests; ...@@ -2,7 +2,6 @@ package org.jastadd.ragconnect.tests;
import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger; import org.apache.logging.log4j.Logger;
import org.jastadd.ragconnect.compiler.Compiler;
import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeAll; import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
...@@ -10,14 +9,14 @@ import org.junit.jupiter.api.Test; ...@@ -10,14 +9,14 @@ import org.junit.jupiter.api.Test;
import java.io.File; import java.io.File;
import java.io.IOException; import java.io.IOException;
import java.nio.charset.Charset; import java.nio.charset.Charset;
import java.util.ArrayList; import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Arrays; import java.util.Arrays;
import java.util.Collections; import java.util.Collections;
import java.util.List; import java.util.List;
import java.util.stream.Collectors; import java.util.stream.Collectors;
import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThat;
import static org.jastadd.ragconnect.tests.TestUtils.exec;
import static org.jastadd.ragconnect.tests.TestUtils.readFile; import static org.jastadd.ragconnect.tests.TestUtils.readFile;
import static org.junit.jupiter.api.Assertions.assertTrue; import static org.junit.jupiter.api.Assertions.assertTrue;
...@@ -25,8 +24,8 @@ public class Errors { ...@@ -25,8 +24,8 @@ public class Errors {
private static final Logger logger = LogManager.getLogger(Errors.class); private static final Logger logger = LogManager.getLogger(Errors.class);
private static final String FILENAME_PATTERN = "$FILENAME"; private static final String FILENAME_PATTERN = "$FILENAME";
private static final String INPUT_DIRECTORY = "./src/test/01-input/errors/"; private static final String ERROR_DIRECTORY = "errors/";
private static final String OUTPUT_DIRECTORY = "./src/test/02-after-ragconnect/errors/"; private static final String OUTPUT_DIRECTORY = TestUtils.OUTPUT_DIRECTORY_PREFIX + ERROR_DIRECTORY;
private static final String DEFAULT_GRAMMAR_NAME = "Errors"; private static final String DEFAULT_GRAMMAR_NAME = "Errors";
...@@ -48,44 +47,21 @@ public class Errors { ...@@ -48,44 +47,21 @@ public class Errors {
@SuppressWarnings("SameParameterValue") @SuppressWarnings("SameParameterValue")
private void test(String expectedName, String rootNode, String... connectNames) throws IOException { private void test(String expectedName, String rootNode, String... connectNames) throws IOException {
String grammarFile = INPUT_DIRECTORY + DEFAULT_GRAMMAR_NAME + ".relast"; String grammarFile = ERROR_DIRECTORY + DEFAULT_GRAMMAR_NAME + ".relast";
// String ragconnectFile = INPUT_DIRECTORY + name + ".connect"; List<String> connectFiles = Arrays.stream(connectNames)
String outFile = OUTPUT_DIRECTORY + expectedName + ".out"; .map(connectName -> ERROR_DIRECTORY + connectName + ".connect")
String expectedFile = INPUT_DIRECTORY + expectedName + ".expected"; .collect(Collectors.toList());
Path outPath = TestUtils.runCompiler(grammarFile, connectFiles, rootNode, ERROR_DIRECTORY, 1);
assertThat(connectNames).isNotEmpty();
try {
logger.debug("user.dir: {}", System.getProperty("user.dir"));
List<String> args = new ArrayList<>() {{
add("--o=" + OUTPUT_DIRECTORY);
add("--rootNode=" + rootNode);
add("--verbose");
add(grammarFile);
}};
for (String connectName : connectNames) {
args.add(INPUT_DIRECTORY + connectName + ".connect");
}
int returnValue = exec(Compiler.class, args.toArray(new String[0]), new File(outFile));
Assertions.assertEquals(1, returnValue, "RagConnect did not return with value 1");
} catch (IOException | InterruptedException e) {
e.printStackTrace();
}
final String startOfErrorsPattern = "SEVERE: Errors:"; final String startOfErrorsPattern = "SEVERE: Errors:";
String out = readFile(outFile, Charset.defaultCharset()); String out = readFile(outPath, Charset.defaultCharset());
assertThat(out).contains(startOfErrorsPattern); assertThat(out).contains(startOfErrorsPattern);
out = out.substring(out.indexOf(startOfErrorsPattern) + 16); out = out.substring(out.indexOf(startOfErrorsPattern) + 16);
String expected = readFile(expectedFile, Charset.defaultCharset()); Path expectedPath = Paths.get(TestUtils.INPUT_DIRECTORY_PREFIX)
// if (inFiles.size() == 1) { .resolve(ERROR_DIRECTORY)
// expected = expected.replace(FILENAME_PATTERN, name + ".connect"); .resolve(expectedName + ".expected");
// } else { String expected = readFile(expectedPath, Charset.defaultCharset());
// for (int i = 0; i < inFiles.size(); i++) {
// expected = expected.replace(FILENAME_PATTERN + (i + 1), inFiles.get(i));
// }
// }
List<String> outList = Arrays.asList(out.split("\n")); List<String> outList = Arrays.asList(out.split("\n"));
Collections.sort(outList); Collections.sort(outList);
List<String> expectedList = Arrays.stream(expected.split("\n")) List<String> expectedList = Arrays.stream(expected.split("\n"))
......
package org.jastadd.ragconnect.tests;
import org.junit.jupiter.api.Test;
import java.io.IOException;
import java.nio.file.*;
import java.util.Collections;
/**
* Regression tests for fixed issues.
*
* @author rschoene - Initial contribution
*/
public class RegressionTests {
private static final String REGRESSION_TEST_OUTPUT_DIRECTORY = "regression-test/";
@Test
public void issue27() throws IOException {
String grammarFile = "regression-tests/issue27/Test.relast";
String connectFile = "regression-tests/issue27/Test.connect";
grammarFile = ensureNoTrailingNewLine(grammarFile);
connectFile = ensureNoTrailingNewLine(connectFile);
// FIXME Once fixed in pre-processor, change expected return value from 1 to 0
TestUtils.runCompiler(grammarFile, Collections.singletonList(connectFile), "A", REGRESSION_TEST_OUTPUT_DIRECTORY, 1);
}
private String ensureNoTrailingNewLine(String inputFileSuffix) throws IOException {
int dotIndex = inputFileSuffix.lastIndexOf('.');
String outFileSuffix = inputFileSuffix.substring(0, dotIndex) + ".noNewLine" + inputFileSuffix.substring(dotIndex);
Path inputPath = Paths.get(TestUtils.INPUT_DIRECTORY_PREFIX).resolve(inputFileSuffix);
Path outputPath = Paths.get(TestUtils.INPUT_DIRECTORY_PREFIX).resolve(outFileSuffix);
String content = Files.readString(inputPath);
Files.writeString(outputPath, content.stripTrailing(), StandardOpenOption.CREATE);
return outFileSuffix;
}
}
package org.jastadd.ragconnect.tests; package org.jastadd.ragconnect.tests;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.jastadd.ragconnect.compiler.Compiler;
import org.junit.jupiter.api.Assertions;
import java.io.File; import java.io.File;
import java.io.IOException; import java.io.IOException;
import java.nio.charset.Charset; import java.nio.charset.Charset;
import java.nio.file.Files; import java.nio.file.Files;
import java.nio.file.Path; import java.nio.file.Path;
import java.nio.file.Paths; import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.junit.jupiter.api.Assertions.fail; import static org.junit.jupiter.api.Assertions.fail;
/** /**
...@@ -17,7 +26,10 @@ import static org.junit.jupiter.api.Assertions.fail; ...@@ -17,7 +26,10 @@ import static org.junit.jupiter.api.Assertions.fail;
*/ */
public class TestUtils { public class TestUtils {
private static final Logger logger = LogManager.getLogger(TestUtils.class);
public static final double DELTA = 0.001d; public static final double DELTA = 0.001d;
public static final String INPUT_DIRECTORY_PREFIX = "./src/test/01-input/";
public static final String OUTPUT_DIRECTORY_PREFIX = "./src/test/02-after-ragconnect/";
public static String getMqttHost() { public static String getMqttHost() {
if (System.getenv("GITLAB_CI") != null) { if (System.getenv("GITLAB_CI") != null) {
...@@ -41,6 +53,41 @@ public class TestUtils { ...@@ -41,6 +53,41 @@ public class TestUtils {
return 1883; return 1883;
} }
public static Path runCompiler(String grammarFile, Iterable<String> connectFiles, String rootNode, String outputDirectory, int expectedReturnValue) {
assertThat(connectFiles).isNotEmpty();
Path outPath = Paths.get(OUTPUT_DIRECTORY_PREFIX)
.resolve(outputDirectory)
.resolve("Compiler.out");
ensureCreated(outPath.getParent());
try {
logger.debug("user.dir: {}", System.getProperty("user.dir"));
List<String> args = new ArrayList<>() {{
add("--o=" + OUTPUT_DIRECTORY_PREFIX + outputDirectory);
add("--rootNode=" + rootNode);
add("--verbose");
add(INPUT_DIRECTORY_PREFIX + grammarFile);
}};
connectFiles.forEach(connectFile -> args.add(INPUT_DIRECTORY_PREFIX + connectFile));
int returnValue = exec(Compiler.class, args.toArray(new String[0]), outPath.toFile());
Assertions.assertEquals(expectedReturnValue, returnValue, "RagConnect did not return with value " + expectedReturnValue);
} catch (IOException | InterruptedException e) {
fail(e);
}
return outPath;
}
private static void ensureCreated(Path directory) {
File directoryFile = directory.toFile();
if (directoryFile.exists() && directoryFile.isDirectory()) {
return;
}
assertTrue(directoryFile.mkdirs());
}
public static int exec(Class<?> klass, String[] args, File err) throws IOException, public static int exec(Class<?> klass, String[] args, File err) throws IOException,
InterruptedException { InterruptedException {
String javaHome = System.getProperty("java.home"); String javaHome = System.getProperty("java.home");
...@@ -79,9 +126,9 @@ public class TestUtils { ...@@ -79,9 +126,9 @@ public class TestUtils {
} }
} }
public static String readFile(String path, Charset encoding) public static String readFile(Path path, Charset encoding)
throws IOException { throws IOException {
byte[] encoded = Files.readAllBytes(Paths.get(path)); byte[] encoded = Files.readAllBytes(path);
return new String(encoded, encoding); return new String(encoded, encoding);
} }
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment