track current cursor for first scope detection test

This commit is contained in:
Lulu13022002 2024-04-14 20:28:01 +02:00
parent 018d91b76b
commit 801495f8f1
No known key found for this signature in database
GPG Key ID: 491C8F0B8ACDEB01
14 changed files with 57 additions and 72 deletions

View File

@ -21,7 +21,7 @@ public final class ImportSteps implements StepHolder {
private final IterativeStep skipUntilSemicolonStep = IterativeStep.createUntil(this::skipUntilSemicolon);
private final ImportCollector collector;
private boolean isStatic, isGlobal;
private boolean isStatic;
private @MonotonicNonNull ProtoTypeName name;
public ImportSteps(ImportCollector collector) {
@ -69,6 +69,7 @@ public final class ImportSteps implements StepHolder {
}
public boolean skipUntilSemicolon(StringReader line, LineParser parser) {
// this is only executed once for star imports!
parser.skipCommentOrWhitespace(line);
if (!line.canRead()) {
return true;
@ -100,10 +101,10 @@ public final class ImportSteps implements StepHolder {
}
String name = line.getPartNameUntil(';', parser::skipCommentOrWhitespace, this.name);
boolean isGlobal = false;
if (line.canRead()) {
if (line.peek() == '*') {
this.isGlobal = true;
isGlobal = true;
line.skip();
} else if (parser.nextSingleLineComment(line)) {
// ignore single line comment at the end of the name
@ -113,7 +114,7 @@ public final class ImportSteps implements StepHolder {
this.pushToImportName(name);
if (this.isGlobal) {
if (isGlobal) {
parser.getSteps().addPriority(this.skipUntilSemicolonStep);
return false;
}

View File

@ -8,7 +8,6 @@ import io.papermc.generator.rewriter.utils.Annotations;
import io.papermc.paper.generated.GeneratedFrom;
import net.minecraft.SharedConstants;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;
import java.io.BufferedReader;
import java.io.IOException;
@ -21,7 +20,6 @@ import static io.papermc.generator.rewriter.replace.SearchReplaceRewriter.INDENT
import static io.papermc.generator.rewriter.replace.SearchReplaceRewriter.INDENT_SIZE;
import static org.junit.jupiter.api.Assertions.assertEquals;
@Disabled
public class OldGeneratedCodeTest {
private static final String API_CONTAINER = System.getProperty("paper.generator.rewriter.container.api");

View File

@ -1,6 +1,6 @@
package io.papermc.generator.rewriter.data.sample.parser.area;//{
public @interface AnnotationClass {
public @interface AnnotationClass { // << 34
// @interface should be invalidated and not detected as an annotation
// via its keyword: interface
}

View File

@ -1,6 +1,6 @@
package io.papermc.generator.rewriter.data.sample.parser.area;
@SuppressWarnings({"DeprecatedIsStillUsed", "deprecation"}) // Paper
public class AnnotationPresentClass {
public class AnnotationPresentClass { // << 36
}

View File

@ -9,6 +9,6 @@ package io.papermc.generator.rewriter.data.sample.parser.area;
}
)
// Paper
public class FancyNewlineAnnotationPresentClass {
public class FancyNewlineAnnotationPresentClass { // << 48
}

View File

@ -1,5 +1,5 @@
package io.papermc.generator.rewriter.data.sample.parser.area;//{
public/* d */class/* d */FancyScopeClass/* d */
/* d */{//d
/* d */{//d // << 11
}

View File

@ -1,4 +1,4 @@
package io.papermc.generator.rewriter.data.sample.parser.area;//{
public/* d */class/* d */FancyScopeClass2/* d */ /* d */{//d
public/* d */class/* d */FancyScopeClass2/* d */ /* d */{//d // << 56
}

View File

@ -18,6 +18,6 @@ import org.jetbrains.annotations.ApiStatus;
@/* a*/
ApiStatus/* a*/./* a*/
Experimental/* a*///a/* a*/
public class MixedAnnotationPresentClass {
public class MixedAnnotationPresentClass { // << 41
}

View File

@ -1,4 +1,4 @@
package io.papermc.generator.rewriter.data.sample.parser.area;//{
public class NearScopeClass{
public class NearScopeClass{ // << 27
}

View File

@ -1,5 +1,5 @@
package io.papermc.generator.rewriter.data.sample.parser.area;//{
public class NewlineScopedClass
{
{ // << 0
}

View File

@ -11,5 +11,6 @@ package io.papermc.generator.rewriter.data.sample.parser.area;//{
* }
* {@linkplain SimpleTrapClass}
*/
public class SimpleTrapClass {
public/*{*/class/*{*/SimpleTrapClass/*{*//*
{*/{ // << 3
}

View File

@ -3,7 +3,7 @@ package io.papermc.generator.rewriter.data.sample.parser.imports;
import// discard?
org /* hi */
/* hi */ .
bukkit /* hi */ /*/
bukkit /* hi */ // /*/
/* hi */ .
NamespacedKey// discard?

View File

@ -16,77 +16,59 @@ import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.MethodSource;
import java.io.IOException;
import java.nio.file.Path;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Stream;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.fail;
public class ParserMetadataAreaTest extends ParserTest {
public class ParserFirstClassScopeAreaTest extends ParserTest {
private static Arguments file(Class<?> sampleClass, String expectedLastLine) {
private static Arguments fileToArgs(Class<?> sampleClass) {
String name = sampleClass.getSimpleName();
return Arguments.of(
CONTAINER.resolve(sampleClass.getCanonicalName().replace('.', '/') + ".java"),
sampleClass,
name,
expectedLastLine
name
);
}
private static Stream<Arguments> fileProvider() {
return Stream.of(
file(
SimpleTrapClass.class,
"public class SimpleTrapClass {"
),
file(
AnnotationClass.class,
"public @interface AnnotationClass {"
),
file(
AnnotationPresentClass.class,
"public class AnnotationPresentClass {"
),
file(
FancyNewlineAnnotationPresentClass.class,
"public class FancyNewlineAnnotationPresentClass {"
),
file(
MixedAnnotationPresentClass.class,
"public class MixedAnnotationPresentClass {"
),
file(
NewlineScopedClass.class,
"{"
),
file(
NearScopeClass.class,
"public class NearScopeClass{"
),
file(
FancyScopeClass.class,
" /* d */{//d"
),
file(
FancyScopeClass2.class,
"public/* d */class/* d */FancyScopeClass2/* d */ /* d */{//d"
)
);
SimpleTrapClass.class,
AnnotationClass.class,
AnnotationPresentClass.class,
FancyNewlineAnnotationPresentClass.class,
MixedAnnotationPresentClass.class,
NewlineScopedClass.class,
NearScopeClass.class,
FancyScopeClass.class,
FancyScopeClass2.class
).map(ParserFirstClassScopeAreaTest::fileToArgs);
}
private static final Pattern EXPECTED_LINE = Pattern.compile("<< (?<cursor>\\d+?)$");
@ParameterizedTest
@MethodSource("fileProvider")
public void testAreaEnd(Path path,
public void testFirstClassScope(Path path,
Class<?> sampleClass,
String name,
String expectedLastLine) throws IOException {
String name) throws IOException {
final ImportTypeCollector importCollector = new ImportTypeCollector(new ClassNamed(sampleClass));
parseFile(path, importCollector, line -> {
assertEquals(expectedLastLine, line, "Parser didn't stop at the expected line for " + name);
},
() -> {
fail("File is empty or doesn't contains the required top scope needed for this test to run");
String textLine = line.getString();
Matcher matcher = EXPECTED_LINE.matcher(textLine);
if (matcher.find()) {
int cursor = Integer.parseInt(matcher.group("cursor"));
assertEquals(cursor, line.getCursor(), "Parser didn't stop at the expected cursor for " + name);
} else {
fail("Parser didn't stop at the expected line, for " + name + "! found: " + textLine);
}
);
},
() -> {
fail("File is empty or doesn't contains the required top scope needed for this test to run");
});
}
}

View File

@ -19,23 +19,26 @@ public class ParserTest {
parseFile(path, importCollector, null, null);
}
protected void parseFile(Path path, ImportCollector importCollector, @Nullable Consumer<String> enterBodyCallback, @Nullable Runnable eofCallback) throws IOException {
protected void parseFile(Path path, ImportCollector importCollector, @Nullable Consumer<StringReader> enterBodyCallback, @Nullable Runnable eofCallback) throws IOException {
final LineParser lineParser = new LineParser();
try (BufferedReader reader = Files.newBufferedReader(path, StandardCharsets.UTF_8)) {
while (true) {
String line = reader.readLine();
if (line == null) {
String textLine = reader.readLine();
if (textLine == null) {
if (eofCallback != null) {
eofCallback.run();
}
break;
}
if (!line.isEmpty() && lineParser.consumeImports(new StringReader(line), importCollector)) {
if (enterBodyCallback != null) {
enterBodyCallback.accept(line);
if (!textLine.isEmpty()) {
StringReader line = new StringReader(textLine);
if (lineParser.consumeImports(line, importCollector)) {
if (enterBodyCallback != null) {
enterBodyCallback.accept(line);
}
break;
}
break;
}
}
}