Skip to content

Commit

Permalink
logic cleanup and new test
Browse files Browse the repository at this point in the history
  • Loading branch information
ix0rai committed Dec 5, 2024
1 parent 7058b27 commit eddc324
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,23 @@
import java.nio.file.Path;

public class DropInvalidMappingsTest extends CommandTest {
private static final Path JAR = TestUtil.obfJar("lone_class");
private static final Path LONE_JAR = TestUtil.obfJar("lone_class");
private static final Path INNER_JAR = TestUtil.obfJar("inner_classes");
private static final Path INPUT_DIR = getResource("/drop_invalid_mappings/input/");
private static final Path EXPECTED_DIR = getResource("/drop_invalid_mappings/expected/");

private static final Path INVALID_MAPPINGS_INPUT = INPUT_DIR.resolve("InvalidMappings.mapping");
private static final Path INVALID_MAPPINGS_EXPECTED = EXPECTED_DIR.resolve("InvalidMappings.mapping");
private static final Path EMPTY_MAPPINGS_INPUT = INPUT_DIR.resolve("EmptyMappings.mapping");
private static final Path EMPTY_MAPPINGS_EXPECTED = EXPECTED_DIR.resolve("EmptyMappings.mapping");
private static final Path MAPPING_SAVE_INPUT = INPUT_DIR.resolve("MappingSave.mapping");
private static final Path MAPPING_SAVE_EXPECTED = EXPECTED_DIR.resolve("MappingSave.mapping");

@Test
public void testInvalidMappings() throws Exception {
Path resultFile = Files.createTempFile("invalidMappingsResult", ".mapping");

DropInvalidMappingsCommand.run(JAR, INVALID_MAPPINGS_INPUT, resultFile);
DropInvalidMappingsCommand.run(LONE_JAR, INVALID_MAPPINGS_INPUT, resultFile);

String expectedLines = Files.readString(INVALID_MAPPINGS_EXPECTED);
String actualLines = Files.readString(resultFile);
Expand All @@ -32,11 +36,23 @@ public void testInvalidMappings() throws Exception {
public void testEmptyMappings() throws Exception {
Path resultFile = Files.createTempFile("emptyMappingsResult", ".mapping");

DropInvalidMappingsCommand.run(JAR, EMPTY_MAPPINGS_INPUT, resultFile);
DropInvalidMappingsCommand.run(LONE_JAR, EMPTY_MAPPINGS_INPUT, resultFile);

String expectedLines = Files.readString(EMPTY_MAPPINGS_EXPECTED);
String actualLines = Files.readString(resultFile);

Assertions.assertEquals(expectedLines, actualLines);
}

@Test
public void testMappingSave() throws Exception {
Path resultFile = Files.createTempFile("mappingSaveResult", ".mapping");

DropInvalidMappingsCommand.run(INNER_JAR, MAPPING_SAVE_INPUT, resultFile);

String expectedLines = Files.readString(MAPPING_SAVE_EXPECTED);
String actualLines = Files.readString(resultFile);

Assertions.assertEquals(expectedLines, actualLines);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
CLASS a
CLASS b
METHOD b (Ljava/lang/String;)V
ARG 2 nonEmpty
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
CLASS c
CLASS a
METHOD <init> (Lc;I)V
ARG 2 x
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,3 @@ CLASS a
METHOD <init> (Ljava/lang/String;)V
ARG 1
METHOD a ()Ljava/lang/String;
CLASS b
METHOD b (Ljava/lang/String;)V
ARG 2
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
CLASS c
METHOD a ()V
ARG 34 invalid
ARG 23423 invalid
CLASS a
FIELD a I
METHOD <init> (Lc;I)V
ARG 2 x
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import org.quiltmc.enigma.api.ProgressListener;
import org.quiltmc.enigma.api.analysis.index.jar.EntryIndex;
import org.quiltmc.enigma.api.analysis.index.jar.JarIndex;
import org.quiltmc.enigma.api.source.TokenType;
import org.quiltmc.enigma.api.translation.mapping.EntryMapping;
import org.quiltmc.enigma.api.translation.mapping.ResolutionStrategy;
import org.quiltmc.enigma.api.translation.mapping.tree.EntryTree;
Expand Down Expand Up @@ -79,7 +80,7 @@ private boolean shouldDropBrokenEntry(Dropped dropped, Entry<?> entry) {
}

// Method entry has parameter names, keep it even though it's not the root.
return !(entry instanceof MethodEntry) || this.hasNoChildren(entry, dropped);
return !(entry instanceof MethodEntry) || this.hasNoMappedChildren(entry, dropped);

// Entry is not the root, and is not a method with params
}
Expand All @@ -103,24 +104,24 @@ private boolean shouldDropEmptyMapping(Dropped dropped, Entry<?> entry) {
boolean isEmpty = (mapping.targetName() == null && mapping.javadoc() == null) || !this.project.isRenamable(entry);

if (isEmpty) {
return this.hasNoChildren(entry, dropped);
return this.hasNoMappedChildren(entry, dropped);
}
}

return false;
}

private boolean hasNoChildren(Entry<?> entry, Dropped dropped) {
private boolean hasNoMappedChildren(Entry<?> entry, Dropped dropped) {
var children = this.mappings.getChildren(entry);

// account for child mappings that have been dropped already
if (!children.isEmpty()) {
for (Entry<?> child : children) {
var mapping = this.mappings.get(child);
if (mapping != null && !(mapping.targetName() == null && mapping.javadoc() == null)) {
if (!dropped.getDroppedMappings().containsKey(child)
&& mapping != null && mapping.tokenType() != TokenType.OBFUSCATED
|| !this.hasNoMappedChildren(child, dropped)) {
return false;
} else if (!dropped.getDroppedMappings().containsKey(child) && this.hasNoChildren(child, dropped)) {
return true;
}
}
}
Expand Down

0 comments on commit eddc324

Please sign in to comment.