Skip to content

Commit

Permalink
fix param javadoc not working with name proposal (#210)
Browse files Browse the repository at this point in the history
* add test for param javadoc

* fix param javadoc not working with proposal

* test everything else. why not
  • Loading branch information
ix0rai authored Jul 20, 2024
1 parent 8f449b0 commit f5da419
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -311,7 +311,10 @@ private static MappingPair<LocalVariableEntry, RawEntryMapping> parseArgument(@N
}

LocalVariableEntry obfuscatedEntry = new LocalVariableEntry(ownerEntry, Integer.parseInt(tokens[1]));
String mapping = tokens[2];
String mapping = null;
if (tokens.length > 2) {
mapping = tokens[2];
}

return new MappingPair<>(obfuscatedEntry, new RawEntryMapping(mapping));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,7 @@ protected void writeEntry(PrintWriter writer, EntryTree<EntryMapping> mappings,
line = this.writeMethod(methodEntry, mapping);
} else if (entry instanceof FieldEntry fieldEntry) {
line = this.writeField(fieldEntry, mapping);
} else if (entry instanceof LocalVariableEntry varEntry && mapping.targetName() != null) {
} else if (entry instanceof LocalVariableEntry varEntry) {
line = this.writeArgument(varEntry, mapping);
}

Expand Down Expand Up @@ -292,7 +292,13 @@ protected String writeField(FieldEntry entry, @Nonnull EntryMapping mapping) {
}

protected String writeArgument(LocalVariableEntry entry, @Nonnull EntryMapping mapping) {
return EnigmaFormat.PARAMETER + " " + entry.getIndex() + ' ' + mapping.targetName();
StringBuilder builder = new StringBuilder(EnigmaFormat.PARAMETER + " ");
builder.append(entry.getIndex()).append(" ");
if (mapping.targetName() != null) {
builder.append(mapping.targetName()).append(' ');
}

return builder.toString();
}

private void writeMapping(StringBuilder builder, EntryMapping mapping) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package org.quiltmc.enigma.translation.mapping;

import org.quiltmc.enigma.TestEntryFactory;
import org.quiltmc.enigma.api.Enigma;
import org.quiltmc.enigma.api.ProgressListener;
import org.quiltmc.enigma.api.service.ReadWriteService;
Expand All @@ -14,6 +15,7 @@
import org.quiltmc.enigma.api.translation.representation.entry.ClassEntry;
import org.quiltmc.enigma.api.translation.representation.entry.Entry;
import org.quiltmc.enigma.api.translation.representation.entry.FieldEntry;
import org.quiltmc.enigma.api.translation.representation.entry.LocalVariableEntry;
import org.quiltmc.enigma.api.translation.representation.entry.MethodEntry;
import org.quiltmc.enigma.util.Pair;
import org.junit.jupiter.api.Assertions;
Expand All @@ -35,6 +37,11 @@ public class TestReadWriteCycle {
new EntryMapping("alpha/beta/charlie", "this is a test class", TokenType.DEOBFUSCATED, null)
);

private final Pair<ClassEntry, EntryMapping> testProposedClazz = new Pair<>(
new ClassEntry("a/b/c/d"),
new EntryMapping("alpha/beta/charlie/delta", "this is a test class!", TokenType.JAR_PROPOSED, "enigma:fake_plugin")
);

private final Pair<FieldEntry, EntryMapping> testField1 = new Pair<>(
FieldEntry.parse("a/b/c", "field1", "I"),
new EntryMapping("mapped1", "this is field 1", TokenType.DEOBFUSCATED, null)
Expand All @@ -45,6 +52,11 @@ public class TestReadWriteCycle {
new EntryMapping("mapped2", "this is field 2", TokenType.DEOBFUSCATED, null)
);

private final Pair<FieldEntry, EntryMapping> testProposedField = new Pair<>(
FieldEntry.parse("a/b/c", "proposedField", "I"),
new EntryMapping("proposed1", "this is a proposed field", TokenType.JAR_PROPOSED, "enigma:fake_plugin")
);

private final Pair<MethodEntry, EntryMapping> testMethod1 = new Pair<>(
MethodEntry.parse("a/b/c", "method1", "()V"),
new EntryMapping("mapped3", "this is method1", TokenType.DEOBFUSCATED, null)
Expand All @@ -55,6 +67,26 @@ public class TestReadWriteCycle {
new EntryMapping("mapped4", "this is method 2", TokenType.DEOBFUSCATED, null)
);

private final Pair<MethodEntry, EntryMapping> testMethod3 = new Pair<>(
MethodEntry.parse("a/b/c", "method3", "(IZ)V"),
new EntryMapping("mapped5", "this is method 3", TokenType.DEOBFUSCATED, null)
);

private final Pair<MethodEntry, EntryMapping> testProposedMethod = new Pair<>(
MethodEntry.parse("a/b/c", "proposedMethod", "()V"),
new EntryMapping("proposed2", "this is a proposed method", TokenType.JAR_PROPOSED, "enigma:fake_plugin")
);

private final Pair<LocalVariableEntry, EntryMapping> testNormalParameter = new Pair<>(
TestEntryFactory.newParameter(this.testMethod3.a(), 0),
new EntryMapping("mapped6", "this is a normal parameter", TokenType.DEOBFUSCATED, null)
);

private final Pair<LocalVariableEntry, EntryMapping> testProposedParameter = new Pair<>(
TestEntryFactory.newParameter(this.testMethod3.a(), 1),
new EntryMapping("mapped7", "this is a proposed parameter", TokenType.JAR_PROPOSED, "enigma:fake_plugin")
);

private void insertMapping(EntryTree<EntryMapping> mappings, Pair<? extends Entry<?>, EntryMapping> mappingPair) {
mappings.insert(mappingPair.a(), mappingPair.b());
}
Expand All @@ -63,16 +95,28 @@ private void testReadWriteCycle(ReadWriteService readWriteService, String tmpNam
//construct some known mappings to test with
EntryTree<EntryMapping> testMappings = new HashEntryTree<>();
this.insertMapping(testMappings, this.testClazz);
this.insertMapping(testMappings, this.testProposedClazz);
this.insertMapping(testMappings, this.testField1);
this.insertMapping(testMappings, this.testField2);
this.insertMapping(testMappings, this.testProposedField);
this.insertMapping(testMappings, this.testMethod1);
this.insertMapping(testMappings, this.testMethod2);
this.insertMapping(testMappings, this.testMethod3);
this.insertMapping(testMappings, this.testProposedMethod);
this.insertMapping(testMappings, this.testNormalParameter);
this.insertMapping(testMappings, this.testProposedParameter);

Assertions.assertTrue(testMappings.contains(this.testClazz.a()), "Test mapping insertion failed: testClazz");
Assertions.assertTrue(testMappings.contains(this.testProposedClazz.a()), "Test mapping insertion failed: testProposedClazz");
Assertions.assertTrue(testMappings.contains(this.testField1.a()), "Test mapping insertion failed: testField1");
Assertions.assertTrue(testMappings.contains(this.testField2.a()), "Test mapping insertion failed: testField2");
Assertions.assertTrue(testMappings.contains(this.testProposedField.a()), "Test mapping insertion failed: testProposedField");
Assertions.assertTrue(testMappings.contains(this.testMethod1.a()), "Test mapping insertion failed: testMethod1");
Assertions.assertTrue(testMappings.contains(this.testMethod2.a()), "Test mapping insertion failed: testMethod2");
Assertions.assertTrue(testMappings.contains(this.testMethod3.a()), "Test mapping insertion failed: testMethod3");
Assertions.assertTrue(testMappings.contains(this.testProposedMethod.a()), "Test mapping insertion failed: testProposedMethod");
Assertions.assertTrue(testMappings.contains(this.testNormalParameter.a()), "Test mapping insertion failed: testNormalParameter");
Assertions.assertTrue(testMappings.contains(this.testProposedParameter.a()), "Test mapping insertion failed: testProposedParameter");

File tempFile = File.createTempFile("readWriteCycle", tmpNameSuffix);
tempFile.delete(); //remove the auto created file
Expand All @@ -83,22 +127,40 @@ private void testReadWriteCycle(ReadWriteService readWriteService, String tmpNam
EntryTree<EntryMapping> loadedMappings = readWriteService.read(tempFile.toPath(), ProgressListener.createEmpty());

Assertions.assertTrue(loadedMappings.contains(this.testClazz.a()), "Loaded mappings don't contain testClazz");
Assertions.assertTrue(loadedMappings.contains(this.testProposedClazz.a()), "Loaded mappings don't contain testProposedClazz");
Assertions.assertTrue(loadedMappings.contains(this.testField1.a()), "Loaded mappings don't contain testField1");
Assertions.assertTrue(loadedMappings.contains(this.testField2.a()), "Loaded mappings don't contain testField2");
Assertions.assertTrue(loadedMappings.contains(this.testProposedField.a()), "Loaded mappings don't contain testProposedField");
Assertions.assertTrue(loadedMappings.contains(this.testMethod1.a()), "Loaded mappings don't contain testMethod1");
Assertions.assertTrue(loadedMappings.contains(this.testMethod2.a()), "Loaded mappings don't contain testMethod2");
Assertions.assertTrue(loadedMappings.contains(this.testMethod3.a()), "Loaded mappings don't contain testMethod3");
Assertions.assertTrue(loadedMappings.contains(this.testProposedMethod.a()), "Loaded mappings don't contain testProposedMethod");
Assertions.assertTrue(loadedMappings.contains(this.testNormalParameter.a()), "Loaded mappings don't contain testNormalParameter");
Assertions.assertTrue(loadedMappings.contains(this.testProposedParameter.a()), "Loaded mappings don't contain testProposedParameter");

Assertions.assertEquals(this.testClazz.b().targetName(), loadedMappings.get(this.testClazz.a()).targetName(), "Incorrect mapping: testClazz");
// note: proposed class name will not be saved
Assertions.assertEquals(this.testField1.b().targetName(), loadedMappings.get(this.testField1.a()).targetName(), "Incorrect mapping: testField1");
Assertions.assertEquals(this.testField2.b().targetName(), loadedMappings.get(this.testField2.a()).targetName(), "Incorrect mapping: testField2");
// note: proposed field name will not be saved
Assertions.assertEquals(this.testMethod1.b().targetName(), loadedMappings.get(this.testMethod1.a()).targetName(), "Incorrect mapping: testMethod1");
Assertions.assertEquals(this.testMethod2.b().targetName(), loadedMappings.get(this.testMethod2.a()).targetName(), "Incorrect mapping: testMethod2");
Assertions.assertEquals(this.testMethod3.b().targetName(), loadedMappings.get(this.testMethod3.a()).targetName(), "Incorrect mapping: testMethod3");
// note: proposed method name will not be saved
Assertions.assertEquals(this.testNormalParameter.b().targetName(), loadedMappings.get(this.testNormalParameter.a()).targetName(), "Incorrect mapping: testNormalParameter");
// note: proposed parameter name will not be saved

Assertions.assertEquals(this.testClazz.b().javadoc(), loadedMappings.get(this.testClazz.a()).javadoc(), "Incorrect javadoc: testClazz");
Assertions.assertEquals(this.testProposedClazz.b().javadoc(), loadedMappings.get(this.testProposedClazz.a()).javadoc(), "Incorrect javadoc: testProposedClazz");
Assertions.assertEquals(this.testField1.b().javadoc(), loadedMappings.get(this.testField1.a()).javadoc(), "Incorrect javadoc: testField1");
Assertions.assertEquals(this.testField2.b().javadoc(), loadedMappings.get(this.testField2.a()).javadoc(), "Incorrect javadoc: testField2");
Assertions.assertEquals(this.testProposedField.b().javadoc(), loadedMappings.get(this.testProposedField.a()).javadoc(), "Incorrect javadoc: testProposedField");
Assertions.assertEquals(this.testMethod1.b().javadoc(), loadedMappings.get(this.testMethod1.a()).javadoc(), "Incorrect javadoc: testMethod1");
Assertions.assertEquals(this.testMethod2.b().javadoc(), loadedMappings.get(this.testMethod2.a()).javadoc(), "Incorrect javadoc: testMethod2");
Assertions.assertEquals(this.testMethod3.b().javadoc(), loadedMappings.get(this.testMethod3.a()).javadoc(), "Incorrect javadoc: testMethod3");
Assertions.assertEquals(this.testProposedMethod.b().javadoc(), loadedMappings.get(this.testProposedMethod.a()).javadoc(), "Incorrect javadoc: testProposedMethod");
Assertions.assertEquals(this.testNormalParameter.b().javadoc(), loadedMappings.get(this.testNormalParameter.a()).javadoc(), "Incorrect javadoc: testNormalParameter");
Assertions.assertEquals(this.testProposedParameter.b().javadoc(), loadedMappings.get(this.testProposedParameter.a()).javadoc(), "Incorrect javadoc: testProposedParameter");

tempFile.delete();
}
Expand Down

0 comments on commit f5da419

Please sign in to comment.