Skip to content

Commit

Permalink
Move epoxy-modelfactory tests to their own module (#834)
Browse files Browse the repository at this point in the history
To avoid shipping test code.
  • Loading branch information
ngsilverman authored and elihart committed Dec 17, 2019
1 parent d98658a commit 9a51d5b
Show file tree
Hide file tree
Showing 36 changed files with 202 additions and 81 deletions.
56 changes: 0 additions & 56 deletions epoxy-modelfactory/build.gradle
Original file line number Diff line number Diff line change
@@ -1,8 +1,4 @@
import org.gradle.internal.jvm.Jvm

apply plugin: 'com.android.library'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-kapt'
apply plugin: "com.vanniktech.maven.publish"

android {
Expand All @@ -13,63 +9,11 @@ android {
minSdkVersion 16
targetSdkVersion rootProject.TARGET_SDK_VERSION
}

testOptions.unitTests.includeAndroidResources = true
}

configurations.all { strategy ->
strategy.resolutionStrategy.force rootProject.deps.junit, rootProject.deps.robolectric,
rootProject.deps.mockito
}

// Java files in the "resources" folder are not included in the build for some reason (seems like source files are skipped?)
// This files are needed to test the annotation processor, so we manually copy them into the build.
task('copyDebugTestResources', type: Copy) {
from("${projectDir}/src/test/resources")
into("${buildDir}/intermediates/sourceFolderJavaResources/debug")
}

task('copyReleaseTestResources', type: Copy) {
from("${projectDir}/src/test/resources")
into("${buildDir}/intermediates/sourceFolderJavaResources/release")
}

preBuild.dependsOn copyReleaseTestResources
preBuild.dependsOn copyDebugTestResources

dependencies {
implementation project(':epoxy-adapter')
implementation rootProject.deps.paris
implementation rootProject.deps.kotlin
implementation rootProject.deps.androidRecyclerView

kapt project(':epoxy-processor')

// Need to include the processors directly since we create an instance of it in code for testing
testImplementation project(':epoxy-processor')
testImplementation rootProject.deps.parisProcessor

testImplementation project(':epoxy-processortest')
testImplementation rootProject.deps.googleTestingCompile
testImplementation rootProject.deps.junit
testImplementation rootProject.deps.robolectric
testImplementation files(getRuntimeJar())
testImplementation files(Jvm.current().getToolsJar())
}

// Javadoc isn't working well with Kotlin :(
tasks.withType(Javadoc).all { enabled = false }

static def getRuntimeJar() {
try {
final File javaBase = new File(System.getProperty("java.home")).getCanonicalFile()
File runtimeJar = new File(javaBase, "lib/rt.jar")
if (runtimeJar.exists()) {
return runtimeJar
}
runtimeJar = new File(javaBase, "jre/lib/rt.jar")
return runtimeJar.exists() ? runtimeJar : null
} catch (IOException e) {
throw new RuntimeException(e)
}
}
1 change: 1 addition & 0 deletions epoxy-modelfactorytest/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/build
72 changes: 72 additions & 0 deletions epoxy-modelfactorytest/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
import org.gradle.internal.jvm.Jvm

apply plugin: 'com.android.library'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-kapt'

android {
compileSdkVersion rootProject.COMPILE_SDK_VERSION

defaultConfig {
// Using 16 here as it is the minimum version of Paris, which we are restricted to.
minSdkVersion 16
targetSdkVersion rootProject.TARGET_SDK_VERSION
}

testOptions.unitTests.includeAndroidResources = true
}

configurations.all { strategy ->
strategy.resolutionStrategy.force rootProject.deps.junit, rootProject.deps.robolectric,
rootProject.deps.mockito
}

// Java files in the "resources" folder are not included in the build for some reason (seems like source files are skipped?)
// This files are needed to test the annotation processor, so we manually copy them into the build.
task('copyDebugTestResources', type: Copy) {
from("${projectDir}/src/test/resources")
into("${buildDir}/intermediates/sourceFolderJavaResources/debug")
}

task('copyReleaseTestResources', type: Copy) {
from("${projectDir}/src/test/resources")
into("${buildDir}/intermediates/sourceFolderJavaResources/release")
}

preBuild.dependsOn copyReleaseTestResources
preBuild.dependsOn copyDebugTestResources

dependencies {
implementation project(':epoxy-adapter')
implementation project(':epoxy-modelfactory')
implementation rootProject.deps.paris
implementation rootProject.deps.kotlin
implementation rootProject.deps.androidRecyclerView

kapt project(':epoxy-processor')

// Need to include the processors directly since we create an instance of it in code for testing
testImplementation project(':epoxy-processor')
testImplementation rootProject.deps.parisProcessor

testImplementation project(':epoxy-processortest')
testImplementation rootProject.deps.googleTestingCompile
testImplementation rootProject.deps.junit
testImplementation rootProject.deps.robolectric
testImplementation files(getRuntimeJar())
testImplementation files(Jvm.current().getToolsJar())
}

static def getRuntimeJar() {
try {
final File javaBase = new File(System.getProperty("java.home")).getCanonicalFile()
File runtimeJar = new File(javaBase, "lib/rt.jar")
if (runtimeJar.exists()) {
return runtimeJar
}
runtimeJar = new File(javaBase, "jre/lib/rt.jar")
return runtimeJar.exists() ? runtimeJar : null
} catch (IOException e) {
throw new RuntimeException(e)
}
}
2 changes: 2 additions & 0 deletions epoxy-modelfactorytest/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
<manifest package="com.airbnb.epoxy.modelfactorytest"
xmlns:android="http://schemas.android.com/apk/res/android" />
3 changes: 3 additions & 0 deletions epoxy-modelfactorytest/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<resources>
<string name="app_name">Epoxy Model Factory Test</string>
</resources>
Original file line number Diff line number Diff line change
Expand Up @@ -15,21 +15,33 @@ class FromModelPropertiesKotlinTest {

@Test
fun getId() {
val model = TestModelPropertiesKotlinViewModel_.from(TestModelProperties(id = "100"))
val model = TestModelPropertiesKotlinViewModel_.from(
TestModelProperties(
id = "100"
)
)
assertFalse(model.hasDefaultId())
}

@Test
fun getBoolean() {
val model =
TestModelPropertiesKotlinViewModel_.from(TestModelProperties(booleanValue = true))
TestModelPropertiesKotlinViewModel_.from(
TestModelProperties(
booleanValue = true
)
)
assertEquals(true, model.booleanValue())
}

@Test
fun getDouble() {
val model =
TestModelPropertiesKotlinViewModel_.from(TestModelProperties(doubleValue = 42.0))
TestModelPropertiesKotlinViewModel_.from(
TestModelProperties(
doubleValue = 42.0
)
)
assertEquals(42.0, model.doubleValue(), 0.0)
}

Expand Down Expand Up @@ -59,14 +71,22 @@ class FromModelPropertiesKotlinTest {

@Test
fun getInt() {
val model = TestModelPropertiesKotlinViewModel_.from(TestModelProperties(intValue = 51))
val model = TestModelPropertiesKotlinViewModel_.from(
TestModelProperties(
intValue = 51
)
)
assertEquals(51, model.intValue())
}

@Test
fun getLong() {
val model =
TestModelPropertiesKotlinViewModel_.from(TestModelProperties(longValue = 3000000L))
TestModelPropertiesKotlinViewModel_.from(
TestModelProperties(
longValue = 3000000L
)
)
assertEquals(3000000L, model.longValue())
}

Expand All @@ -84,23 +104,33 @@ class FromModelPropertiesKotlinTest {
@Test
fun getNullOnClickListener() {
val model =
TestModelPropertiesKotlinViewModel_.from(TestModelProperties(onClickListener = null))
TestModelPropertiesKotlinViewModel_.from(
TestModelProperties(
onClickListener = null
)
)
assertNull(model.onClickListener())
}

@Test
fun getRawRes() {
// We use an arbitrary int rather than adding a test-only raw resource, which isn't easy
val model =
TestModelPropertiesKotlinViewModel_.from(TestModelProperties(rawRes = 42))
TestModelPropertiesKotlinViewModel_.from(
TestModelProperties(
rawRes = 42
)
)
assertEquals(42, model.rawRes())
}

@Test
fun getString() {
val model =
TestModelPropertiesKotlinViewModel_.from(
TestModelProperties(stringValue = "ModelFactory")
TestModelProperties(
stringValue = "ModelFactory"
)
)
assertEquals("ModelFactory", model.stringValue())
}
Expand All @@ -109,7 +139,11 @@ class FromModelPropertiesKotlinTest {
fun getStringList() {
val stringList = listOf("Model", "Factory")
val model =
TestModelPropertiesKotlinViewModel_.from(TestModelProperties(stringList = stringList))
TestModelPropertiesKotlinViewModel_.from(
TestModelProperties(
stringList = stringList
)
)
assertEquals(stringList, model.stringList())
}

Expand Down
Loading

0 comments on commit 9a51d5b

Please sign in to comment.