Skip to content

Commit

Permalink
Unit test NONE access transforms
Browse files Browse the repository at this point in the history
  • Loading branch information
lynxplay committed Mar 30, 2024
1 parent 481504c commit b7639f7
Show file tree
Hide file tree
Showing 6 changed files with 50 additions and 20 deletions.
6 changes: 2 additions & 4 deletions src/main/java/io/papermc/restamp/at/ModifierTransformer.java
Original file line number Diff line number Diff line change
Expand Up @@ -78,11 +78,9 @@ public ModifierTransformationResult transformModifiers(@NotNull final AccessTran
continue;
}

// Do not touch access modifiers if no change was requested.
if (accessTransform.getAccess() == AccessChange.NONE) continue;

// Drop unwanted visibility modifiers and record potential position for new modifier.
if (modifier.getType() != accessTypeToKeep) {
// Do not touch access modifiers if no change was requested.
if (accessTransform.getAccess() != AccessChange.NONE && modifier.getType() != accessTypeToKeep) {
transformationProgress.dropModifier(modifier);
transformationProgress.proposeValidVisibilitySpot();
continue;
Expand Down
34 changes: 31 additions & 3 deletions src/test/java/io/papermc/restamp/RestampFunctionTestHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,23 @@ public static String accessChangeToModifierString(@NotNull final AccessChange ac
return stringBuilder.toString();
}

/**
* Replaces all none changes in the base access transformer with the respective values found in the replaceFrom transform.
*
* @param base the base access transform instance which should be returned but with its NONE values replaced.
* @param replaceFrom the access transform instance to source the replacement values from.
*
* @return the replaced/filled in access transform.
*/
@NotNull
public static AccessTransform replaceNoneFrom(@NotNull final AccessTransform base,
@NotNull final AccessTransform replaceFrom) {
return AccessTransform.of(
base.getAccess() == AccessChange.NONE ? replaceFrom.getAccess() : base.getAccess(),
base.getFinal() == ModifierChange.NONE ? replaceFrom.getFinal() : base.getFinal()
);
}

/**
* An argument provider that provides all combinations of known visibility modifiers (private, public, protected and nothing (package private)).
*/
Expand All @@ -105,19 +122,30 @@ public static final class CartesianVisibilityArgumentProvider implements Argumen
*/
private static final List<AccessTransform> MODIFIERS = List.of(
AccessTransform.of(AccessChange.PRIVATE, ModifierChange.ADD),
AccessTransform.of(AccessChange.PRIVATE, ModifierChange.NONE),
AccessTransform.of(AccessChange.PRIVATE, ModifierChange.REMOVE),
AccessTransform.of(AccessChange.PUBLIC, ModifierChange.ADD),
AccessTransform.of(AccessChange.PUBLIC, ModifierChange.NONE),
AccessTransform.of(AccessChange.PUBLIC, ModifierChange.REMOVE),
AccessTransform.of(AccessChange.PROTECTED, ModifierChange.ADD),
AccessTransform.of(AccessChange.PROTECTED, ModifierChange.NONE),
AccessTransform.of(AccessChange.PROTECTED, ModifierChange.REMOVE),
AccessTransform.of(AccessChange.PACKAGE_PRIVATE, ModifierChange.ADD),
AccessTransform.of(AccessChange.PACKAGE_PRIVATE, ModifierChange.REMOVE)
AccessTransform.of(AccessChange.PACKAGE_PRIVATE, ModifierChange.NONE),
AccessTransform.of(AccessChange.PACKAGE_PRIVATE, ModifierChange.REMOVE),
AccessTransform.of(AccessChange.NONE, ModifierChange.ADD),
AccessTransform.of(AccessChange.NONE, ModifierChange.NONE),
AccessTransform.of(AccessChange.NONE, ModifierChange.REMOVE)
);

@Override
public Stream<? extends Arguments> provideArguments(final ExtensionContext context) {
return MODIFIERS.stream().flatMap(given ->
return MODIFIERS.stream()
.filter(a -> a.getAccess() != AccessChange.NONE && a.getFinal() != ModifierChange.NONE) // Do not start with a given none value.
.flatMap(given ->
MODIFIERS.stream().flatMap(target ->
Stream.of("static", null).map(staticModifier -> Arguments.of(
given, target, staticModifier
given, replaceNoneFrom(target, given), target, staticModifier
))
)
);
Expand Down
21 changes: 11 additions & 10 deletions src/test/java/io/papermc/restamp/at/ModifierTransformerTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -69,11 +69,12 @@ public void testSkipModifyTransformation(@NotNull final AccessTransform transfor

@ParameterizedTest
@ArgumentsSource(RestampFunctionTestHelper.CartesianVisibilityArgumentProvider.class)
public void testModifyVisibilityTransformation(@NotNull final AccessTransform current,
@NotNull final AccessTransform wanted,
public void testModifyVisibilityTransformation(@NotNull final AccessTransform currentState,
@NotNull final AccessTransform targetState,
@NotNull final AccessTransform instruction,
@Nullable final String staticModifierExisting) {
final List<J.Modifier> modifiers = new ArrayList<>();
final J.Modifier.Type type = switch (current.getAccess()) {
final J.Modifier.Type type = switch (currentState.getAccess()) {
case PRIVATE -> J.Modifier.Type.Private;
case PROTECTED -> J.Modifier.Type.Protected;
case PUBLIC -> J.Modifier.Type.Public;
Expand All @@ -83,23 +84,23 @@ public void testModifyVisibilityTransformation(@NotNull final AccessTransform cu
modifiers.add(modifierFrom(Space.EMPTY, type));
}
if (staticModifierExisting != null) modifiers.add(modifierFrom(Space.SINGLE_SPACE, J.Modifier.Type.Static));
if (current.getFinal() == ModifierChange.ADD) modifiers.add(modifierFrom(Space.SINGLE_SPACE, J.Modifier.Type.Final));
if (currentState.getFinal() == ModifierChange.ADD) modifiers.add(modifierFrom(Space.SINGLE_SPACE, J.Modifier.Type.Final));

final ModifierTransformationResult result = transformer.transformModifiers(wanted, modifiers, Space.SINGLE_SPACE);
final ModifierTransformationResult result = transformer.transformModifiers(instruction, modifiers, Space.SINGLE_SPACE);

final List<J.Modifier> newModifiers = result.newModifiers();

int expectedModifierSize = 0;
if (wanted.getAccess() != AccessChange.PACKAGE_PRIVATE) expectedModifierSize++;
if (wanted.getFinal() == ModifierChange.ADD) expectedModifierSize++;
if (targetState.getAccess() != AccessChange.PACKAGE_PRIVATE) expectedModifierSize++;
if (targetState.getFinal() == ModifierChange.ADD) expectedModifierSize++;
if (staticModifierExisting != null) expectedModifierSize++;

Assertions.assertEquals(expectedModifierSize, newModifiers.size());
if (wanted.getAccess() != AccessChange.PACKAGE_PRIVATE) {
Assertions.assertEquals(RecipeHelper.typeFromAccessChange(wanted.getAccess()), newModifiers.remove(0).getType());
if (targetState.getAccess() != AccessChange.PACKAGE_PRIVATE) {
Assertions.assertEquals(RecipeHelper.typeFromAccessChange(targetState.getAccess()), newModifiers.remove(0).getType());
}
if (staticModifierExisting != null) Assertions.assertEquals(J.Modifier.Type.Static, newModifiers.remove(0).getType());
if (wanted.getFinal() == ModifierChange.ADD) Assertions.assertEquals(J.Modifier.Type.Final, newModifiers.remove(0).getType());
if (targetState.getFinal() == ModifierChange.ADD) Assertions.assertEquals(J.Modifier.Type.Final, newModifiers.remove(0).getType());
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,10 @@ public class RestampClassFunctionTest {
@ArgumentsSource(RestampFunctionTestHelper.CartesianVisibilityArgumentProvider.class)
public void testAccessTransformerOnClass(@NotNull final AccessTransform given,
@NotNull final AccessTransform target,
@NotNull final AccessTransform instruction,
@Nullable final String staticModifier) {
final AccessTransformSet accessTransformSet = AccessTransformSet.create();
accessTransformSet.getOrCreateClass("io.papermc.test.Test").replace(target);
accessTransformSet.getOrCreateClass("io.papermc.test.Test").replace(instruction);

final RestampInput input = RestampFunctionTestHelper.inputFromSourceString(accessTransformSet, constructClassTest(
accessChangeToModifierString(given.getAccess(), staticModifier, given.getFinal() == ModifierChange.ADD ? "final" : null)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,10 @@ public class RestampFieldFunctionTest {
@ArgumentsSource(RestampFunctionTestHelper.CartesianVisibilityArgumentProvider.class)
public void testAccessTransformerOnField(@NotNull final AccessTransform given,
@NotNull final AccessTransform target,
@NotNull final AccessTransform instruction,
@Nullable final String staticModifier) {
final AccessTransformSet accessTransformSet = AccessTransformSet.create();
accessTransformSet.getOrCreateClass("io.papermc.test.Test").replaceField("passphrase", target);
accessTransformSet.getOrCreateClass("io.papermc.test.Test").replaceField("passphrase", instruction);

final RestampInput input = RestampFunctionTestHelper.inputFromSourceString(accessTransformSet, constructFieldTest(
accessChangeToModifierString(given.getAccess(), staticModifier, given.getFinal() == ModifierChange.ADD ? "final" : null)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,11 @@ public class RestampMethodFunctionTest {
@ArgumentsSource(RestampFunctionTestHelper.CartesianVisibilityArgumentProvider.class)
public void testAccessTransformerOnMethod(@NotNull final AccessTransform given,
@NotNull final AccessTransform target,
@NotNull final AccessTransform instruction,
@Nullable final String staticModifier) {
final AccessTransformSet accessTransformSet = AccessTransformSet.create();
accessTransformSet.getOrCreateClass("io.papermc.test.Test").replaceMethod(
MethodSignature.of("test", "(Ljava.lang.Object;)Ljava.lang.String;"), target
MethodSignature.of("test", "(Ljava.lang.Object;)Ljava.lang.String;"), instruction
);

final RestampInput input = RestampFunctionTestHelper.inputFromSourceString(accessTransformSet, constructMethodTest(
Expand Down

0 comments on commit b7639f7

Please sign in to comment.