Skip to content

Commit

Permalink
Issue luontola#109: Add unit test to make sure child lambdas don't hi…
Browse files Browse the repository at this point in the history
…de parent lambdas.
  • Loading branch information
caarmen committed Sep 17, 2016
1 parent 93ff6ad commit b7cae74
Showing 1 changed file with 43 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,16 @@
import org.junit.Test;

import java.lang.reflect.Method;
import java.util.*;
import java.util.HashSet;
import java.util.Set;

import static net.orfjackal.retrolambda.test.TestUtil.assertClassExists;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.*;
import static org.hamcrest.Matchers.arrayWithSize;
import static org.hamcrest.Matchers.contains;
import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.startsWith;
import static org.junit.Assert.assertNotEquals;

public class LambdaClassesTest {

Expand Down Expand Up @@ -80,13 +85,49 @@ public void enclosing_classes_contain_no_unnecessary_methods_in_addition_to_the_
assertThat("capturing lambda", getMethodsNames(Capturing.class), contains(startsWith("lambda$new$")));
}

private class Parent {
protected void foo() {
Runnable lambda = () -> {
System.out.println("parent");
};
}
}

private class Child extends Parent {
@Override
protected void foo() {
super.foo();
Runnable lambda = () -> {
System.out.println("child");
};
}
}

@Test
public void child_class_lambda_doesnt_hide_parent_class_lambda() throws ClassNotFoundException {
Set<String> parentLambdas = getLambdaMethodNames(Parent.class);
Set<String> childLambdas = getLambdaMethodNames(Child.class);
assertNotEquals("child lambda hides parent lambda", parentLambdas, childLambdas);
}


// helpers

private static Class<?> findLambdaClass(Class<?> clazz) throws ClassNotFoundException {
return Class.forName(clazz.getName() + "$$Lambda$1");
}

private static Set<String> getLambdaMethodNames(Class<?> clazz) {
Method[] methods = clazz.getDeclaredMethods();
Set<String> uniqueNames = new HashSet<>();
for (Method method : methods) {
if (method.getName().startsWith("lambda$")) {
uniqueNames.add(method.getName());
}
}
return uniqueNames;
}

private static Set<String> getMethodsNames(Class<?> clazz) {
Method[] methods = clazz.getDeclaredMethods();
Set<String> uniqueNames = new HashSet<>();
Expand Down

0 comments on commit b7cae74

Please sign in to comment.