Skip to content

Commit

Permalink
Extract helper method
Browse files Browse the repository at this point in the history
  • Loading branch information
luontola committed Apr 30, 2016
1 parent a8a5b9b commit 7ac6ff0
Showing 1 changed file with 18 additions and 23 deletions.
@@ -1,4 +1,4 @@
// Copyright © 2013-2016 Esko Luontola <www.orfjackal.net> // Copyright © 2013-2016 Esko Luontola and other Retrolambda contributors
// This software is released under the Apache License 2.0. // This software is released under the Apache License 2.0.
// The license text is at http://www.apache.org/licenses/LICENSE-2.0 // The license text is at http://www.apache.org/licenses/LICENSE-2.0


Expand All @@ -12,7 +12,7 @@


import static net.orfjackal.retrolambda.test.TestUtil.assertClassExists; import static net.orfjackal.retrolambda.test.TestUtil.assertClassExists;
import static org.hamcrest.MatcherAssert.assertThat; import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.is; import static org.hamcrest.Matchers.*;


public class LambdaClassesTest { public class LambdaClassesTest {


Expand Down Expand Up @@ -47,15 +47,9 @@ private Dummy2() {


@Test @Test
public void capturing_lambda_classes_contain_no_unnecessary_methods() throws ClassNotFoundException { public void capturing_lambda_classes_contain_no_unnecessary_methods() throws ClassNotFoundException {
Set<String> expected = new HashSet<>(Arrays.asList("lambdaFactory$", "run"));

Class<?> lambdaClass = Class.forName(Capturing.class.getName() + "$$Lambda$1"); Class<?> lambdaClass = Class.forName(Capturing.class.getName() + "$$Lambda$1");


Set<String> actual = new HashSet<>(); assertThat(getDeclaredMethodNames(lambdaClass), is(ImmutableSet.of("lambdaFactory$", "run")));
for (Method method : lambdaClass.getDeclaredMethods()) {
actual.add(method.getName());
}
assertThat(actual, is(expected));
} }


@SuppressWarnings("UnusedDeclaration") @SuppressWarnings("UnusedDeclaration")
Expand All @@ -68,15 +62,9 @@ private Capturing() {


@Test @Test
public void non_capturing_lambda_classes_contain_no_unnecessary_methods() throws ClassNotFoundException { public void non_capturing_lambda_classes_contain_no_unnecessary_methods() throws ClassNotFoundException {
Set<String> expected = new HashSet<>(Arrays.asList("lambdaFactory$", "run"));

Class<?> lambdaClass = Class.forName(NonCapturing.class.getName() + "$$Lambda$1"); Class<?> lambdaClass = Class.forName(NonCapturing.class.getName() + "$$Lambda$1");


Set<String> actual = new HashSet<>(); assertThat(getDeclaredMethodNames(lambdaClass), is(ImmutableSet.of("lambdaFactory$", "run")));
for (Method method : lambdaClass.getDeclaredMethods()) {
actual.add(method.getName());
}
assertThat(actual, is(expected));
} }


@SuppressWarnings("UnusedDeclaration") @SuppressWarnings("UnusedDeclaration")
Expand All @@ -90,13 +78,7 @@ private NonCapturing() {


@Test @Test
public void lambda_bodies_contain_no_unnecessary_methods() throws ClassNotFoundException { public void lambda_bodies_contain_no_unnecessary_methods() throws ClassNotFoundException {
Set<String> expected = ImmutableSet.of("lambda$main$0", "main"); assertThat(getDeclaredMethodNames(HasLambdaBody.class), is(ImmutableSet.of("lambda$main$0", "main")));

Set<String> actual = new HashSet<>();
for (Method method : HasLambdaBody.class.getDeclaredMethods()) {
actual.add(method.getName());
}
assertThat(actual, is(expected));
} }


@SuppressWarnings("UnusedDeclaration") @SuppressWarnings("UnusedDeclaration")
Expand All @@ -106,4 +88,17 @@ private void main() {
}; };
} }
} }


// helpers

private static Set<String> getDeclaredMethodNames(Class<?> clazz) {
Method[] methods = clazz.getDeclaredMethods();
Set<String> uniqueNames = new HashSet<>();
for (Method method : methods) {
uniqueNames.add(method.getName());
}
assertThat("unexpected overloaded methods", methods, arrayWithSize(uniqueNames.size()));
return uniqueNames;
}
} }

0 comments on commit 7ac6ff0

Please sign in to comment.