Skip to content

Commit

Permalink
preliminary support for android unit tests, enforce compiling plugin …
Browse files Browse the repository at this point in the history
…with java 6 to reduce chances of errors applying the plugin
  • Loading branch information
evant committed Mar 29, 2015
1 parent 68e2929 commit 6d697c7
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 3 deletions.
2 changes: 2 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ repositories {
group = 'me.tatarka'
version = '2.6.0-SNAPSHOT'

sourceCompatibility = '1.6'

dependencies {
compile gradleApi()
compile localGroovy()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,12 @@
import org.junit.Test;
import org.junit.runner.RunWith;

import me.tatarka.retrolambda.sample.app.Function;
import me.tatarka.retrolambda.sample.app.MainActivity;
import me.tatarka.sample.app.R;
import me.tatarka.retrolambda.sample.app.R;

import static org.assertj.android.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThat;

/**
* Created by evan on 3/29/15.
Expand All @@ -39,4 +41,10 @@ public void testHelloRetrolambdaLib() {
TextView textLib = (TextView) getActivity().findViewById(R.id.text_lib);
assertThat(textLib).hasText("Hello, retrolambda (from lib)!");
}

@Test
public void testLambdaInTest() {
Function lambda = () -> "test";
assertThat(lambda.run()).isEqualTo("test");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;

import me.tatarka.retrolambda.sample.app.Function;
import me.tatarka.retrolambda.sample.app.MainActivity;
import me.tatarka.retrolambda.sample.lib.Lib;

Expand All @@ -18,8 +19,15 @@ public class FunctionTest {
public void testGetHello() {
assertThat(MainActivity.getHello().run()).isEqualTo("Hello, retrolambda!");
}

@Test
public void testGetHelloLib() {
assertThat(Lib.getHello().run()).isEqualTo("Hello, retrolambda (from lib)!");
}

@Test
public void testLambdaInTest() {
Function lambda = () -> "test";
assertThat(lambda.run()).isEqualTo("test");
}
}
31 changes: 29 additions & 2 deletions src/main/groovy/me/tatarka/RetrolambdaPluginAndroid.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -96,17 +96,44 @@ public class RetrolambdaPluginAndroid implements Plugin<Project> {
// I hope gradle doesn't change the class name!
def taskActions = var.javaCompile.taskActions
def taskRemoved = false
def afterActions = []
for (int i = taskActions.size() - 1; i >= 0; i--) {
if (taskActions[i].class.name == "org.gradle.api.internal.project.taskfactory.AnnotationProcessingTaskFactory\$IncrementalTaskAction") {
taskActions.remove(i)
taskRemoved = true
break
taskRemoved = true
} else if (taskRemoved) {
afterActions.add(taskActions[i])
taskActions.remove(i)
}
}

if (!taskRemoved) {
throw new ProjectConfigurationException("Unable to delete old javaCompile action, maybe the class name has changed? Please submit a bug report with what version of gradle you are using.", null)
}

// Move any after to the retrolambda task to that they run after retrolambda
afterActions.each {
retrolambdaTask.doLast(it)
}

// Ensure retrolamba runs before compiling tests
def compileTestTaskName = "compile${var.name.capitalize()}UnitTestJava"
def compileTestTask = project.tasks.findByName(compileTestTaskName)
if (compileTestTask != null) {
compileTestTask.mustRunAfter(retrolambdaTask)
// We need to add the rt to the classpath to support lambdas in the tests themselves
compileTestTask.classpath += project.files("$project.retrolambda.jdk/jre/lib/rt.jar")

if (!project.retrolambda.onJava8) {
// Set JDK 8 for the compiler task
compileTestTask.doFirst {
it.options.fork = true
def javac = "${project.retrolambda.tryGetJdk()}/bin/javac"
if (!checkIfExecutableExists(javac)) throw new ProjectConfigurationException("Cannot find executable: $javac", null)
it.options.forkOptions.executable = javac
}
}
}

def extractTaskName = "extract${var.name.capitalize()}Annotations"
def extractTask = project.tasks.findByName(extractTaskName)
Expand Down

0 comments on commit 6d697c7

Please sign in to comment.