Skip to content

Commit

Permalink
Add a unit test for the code generator.
Browse files Browse the repository at this point in the history
  • Loading branch information
vRallev committed May 24, 2022
1 parent 7d5bc99 commit cc7a00f
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 1 deletion.
7 changes: 6 additions & 1 deletion anvilcodegen/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,18 @@ tasks.withType(org.jetbrains.kotlin.gradle.tasks.KotlinCompile).configureEach {
}

dependencies {
implementation project(':lib.daggerscopes')
api "com.squareup.anvil:compiler-api:2.4.0"
implementation project(':lib.daggerscopes')
implementation "com.squareup.anvil:compiler-utils:2.4.0"
implementation "com.squareup:kotlinpoet:1.10.2"
implementation 'com.google.dagger:dagger:2.42'
implementation 'com.squareup.retrofit2:retrofit:2.9.0'
implementation project(':lib.daggerscopes')

compileOnly "com.google.auto.service:auto-service-annotations:1.0.1"
kapt "com.google.auto.service:auto-service:1.0.1"

testImplementation testFixtures("com.squareup.anvil:compiler-utils:2.4.0")
testImplementation "com.google.truth:truth:1.1.3"
testImplementation "junit:junit:4.13.2"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package com.gpeal.droidconanvilsample.anvilcompilers

import com.google.common.truth.Truth.assertThat
import com.squareup.anvil.compiler.internal.testing.compileAnvil
import com.tschuchort.compiletesting.KotlinCompilation.ExitCode.OK
import dagger.Provides
import dagger.Reusable
import org.junit.Test

class ContributesApiCodeGeneratorTest {

@Test fun `a dagger module is generated`() {
compileAnvil(
"""
package com.test
import com.gpeal.droidconanvilsample.lib.daggerscopes.ContributesApi
@ContributesApi
interface TestApi
"""
) {
assertThat(exitCode).isEqualTo(OK)

val clazz = classLoader.loadClass("com.test.TestApi_Module")
val providerMethod = clazz.declaredMethods.single()

assertThat(providerMethod.returnType).isEqualTo(classLoader.loadClass("com.test.TestApi"))
assertThat(providerMethod.annotations.map { it.annotationClass }).containsExactly(Provides::class, Reusable::class)
}
}
}

0 comments on commit cc7a00f

Please sign in to comment.