diff --git a/javatests/jflex/testcase/eof/BUILD.bazel b/javatests/jflex/testcase/eof/BUILD.bazel new file mode 100644 index 000000000..5eb225a90 --- /dev/null +++ b/javatests/jflex/testcase/eof/BUILD.bazel @@ -0,0 +1,43 @@ +# This build file was generated automatically, but won't be re-generated. +# Feel free to improve. + +load("@jflex_rules//jflex:jflex.bzl", "jflex") + +# eof + +jflex( + name = "gen_eof_scanner", + srcs = ["eof.flex"], + jflex_bin = "//jflex:jflex_bin", + outputs = ["Eof.java"], +) + +java_library( + name = "eof_scanner", + srcs = [ + ":gen_eof_scanner", + ], + deps = [ + "//java/jflex/testing/testsuite/golden", + "//third_party/com/google/guava", + ], +) + +java_test( + name = "EofGoldenTest", + srcs = [ + "EofGoldenTest.java", + ], + data = [ + "eof-0.input", + "eof-0.output", + ], + deps = [ + ":eof_scanner", + "//java/jflex/testing/diff", + "//java/jflex/testing/testsuite/golden", + "//java/jflex/util/scanner:scanner_factory", + "//third_party/com/google/guava", + "//third_party/com/google/truth", + ], +) diff --git a/javatests/jflex/testcase/eof/EofGoldenTest.java b/javatests/jflex/testcase/eof/EofGoldenTest.java new file mode 100644 index 000000000..7fc1f1e29 --- /dev/null +++ b/javatests/jflex/testcase/eof/EofGoldenTest.java @@ -0,0 +1,40 @@ +// test: eof + +package jflex.testcase.eof; + +import java.io.File; +import jflex.testing.testsuite.golden.AbstractGoldenTest; +import jflex.testing.testsuite.golden.GoldenInOutFilePair; +import jflex.util.scanner.ScannerFactory; +import org.junit.Test; + +/** + * test %eof and %eofthrows directives https://github.com/jflex-de/jflex/issues/743 + * + *

Note: This test was generated from {@code jflex-testsuite-maven-plugin} test cases. The test + * relies on golden files for testing, expecting the scanner to output logs on the {@code + * System.out}. Please migrate to proper unit tests, as describe in + * //javatest/jflex/testcase. + */ +// TODO Migrate this test to proper unit tests. +public class EofGoldenTest extends AbstractGoldenTest { + + /** Creates a scanner conforming to the {@code eof.flex} specification. */ + private final ScannerFactory scannerFactory = ScannerFactory.of(Eof::new); + + private File testRuntimeDir = new File("javatests/jflex/testcase/eof"); + + @Test + public void goldenTest0() throws Exception { + GoldenInOutFilePair golden = + new GoldenInOutFilePair( + new File(testRuntimeDir, "eof-0.input"), new File(testRuntimeDir, "eof-0.output")); + compareSystemOutWith(golden); + + Eof scanner = scannerFactory.createScannerForFile(golden.inputFile); + while (!scanner.yyatEOF()) { + System.out.println(scanner.yylex()); + } + } +} diff --git a/javatests/jflex/testcase/eof/eof-0.input b/javatests/jflex/testcase/eof/eof-0.input new file mode 100644 index 000000000..6ddadea14 --- /dev/null +++ b/javatests/jflex/testcase/eof/eof-0.input @@ -0,0 +1,2 @@ +bla +blub diff --git a/javatests/jflex/testcase/eof/eof-0.output b/javatests/jflex/testcase/eof/eof-0.output new file mode 100644 index 000000000..44a430ca7 --- /dev/null +++ b/javatests/jflex/testcase/eof/eof-0.output @@ -0,0 +1,2 @@ +EOF +<> diff --git a/javatests/jflex/testcase/eof/eof.flex b/javatests/jflex/testcase/eof/eof.flex new file mode 100644 index 000000000..5e40d8e47 --- /dev/null +++ b/javatests/jflex/testcase/eof/eof.flex @@ -0,0 +1,29 @@ +package jflex.testcase.eof; + +import java.io.*; + +%% + +%public +%class Eof +%type String + +%eof{ + System.out.println("EOF"); + + // should never throw, but the compiler won't know + if (System.out == null) { + throw new java.io.IOException("testing"); + } +%eof} + +%eofthrow{ + java.io.IOException +%eofthrow} + +%% + +.+ { /* ignore */ } +\n { /* that too */ } + +<> { System.out.println("<>"); return ""; } \ No newline at end of file