Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add a shouldFail parameter to compile and auto-check #72

Merged
merged 1 commit into from
Jul 24, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package tangle.inject.tests

import com.tschuchort.compiletesting.KotlinCompilation.ExitCode.COMPILATION_ERROR
import io.kotest.matchers.shouldBe
import io.kotest.matchers.string.shouldContain
import org.junit.jupiter.api.Disabled
Expand All @@ -22,9 +21,9 @@ class FragmentInjectGeneratorTest : BaseTest() {

@ContributesFragment(Unit::class)
class MyFragment @FragmentInject constructor() : Fragment()
"""
""",
shouldFail = true
) {
exitCode shouldBe COMPILATION_ERROR

messages shouldContain "@FragmentInject must only be applied to the constructor " +
"of a Fragment, and that fragment must have a corresponding " +
Expand All @@ -50,9 +49,9 @@ class FragmentInjectGeneratorTest : BaseTest() {
fun create(@TangleParam("name") name: String): MyFragment
}
}
"""
""",
shouldFail = true
) {
exitCode shouldBe COMPILATION_ERROR

messages shouldContain "The @FragmentInjectFactory-annotated interface " +
"`tangle.inject.tests.MyFragment.Factory` must be defined inside a Fragment " +
Expand All @@ -76,9 +75,9 @@ class FragmentInjectGeneratorTest : BaseTest() {
@FragmentInjectFactory
interface Factory
}
"""
""",
shouldFail = true
) {
exitCode shouldBe COMPILATION_ERROR

messages shouldContain "@FragmentInjectFactory-annotated types must have exactly one " +
"abstract function -- without a default implementation -- " +
Expand Down Expand Up @@ -106,9 +105,9 @@ class FragmentInjectGeneratorTest : BaseTest() {
fun create2(@TangleParam("name") name: String): MyFragment
}
}
"""
""",
shouldFail = true
) {
exitCode shouldBe COMPILATION_ERROR

messages shouldContain "@FragmentInjectFactory-annotated types must have exactly one " +
"abstract function -- without a default implementation -- " +
Expand All @@ -135,9 +134,9 @@ class FragmentInjectGeneratorTest : BaseTest() {
fun create(@TangleParam("name") name: String)
}
}
"""
""",
shouldFail = true
) {
exitCode shouldBe COMPILATION_ERROR

messages shouldContain "Return type of 'create' is not a subtype of the return type " +
"of the overridden member 'public abstract fun create(name: String): Unit " +
Expand Down Expand Up @@ -235,9 +234,9 @@ class FragmentInjectGeneratorTest : BaseTest() {
fun create(@TangleParam("name") name: String): MyFragment
}
}
"""
""",
shouldFail = true
) {
exitCode shouldBe COMPILATION_ERROR

messages shouldContain "@FragmentInject-annotated Fragments must also have " +
"a `tangle.fragment.ContributesFragment` class annotation."
Expand All @@ -261,9 +260,9 @@ class FragmentInjectGeneratorTest : BaseTest() {
fun create(name: String): MyFragment
}
}
"""
""",
shouldFail = true
) {
exitCode shouldBe COMPILATION_ERROR

messages shouldContain "could not find a @TangleParam annotation for parameter `name`"
}
Expand Down Expand Up @@ -705,9 +704,9 @@ class FragmentInjectGeneratorTest : BaseTest() {
): MyFragment
}
}
"""
""",
shouldFail = true
) {
exitCode shouldBe COMPILATION_ERROR

messages shouldContain "Tangle found Fragment runtime arguments which cannot " +
"be inserted into a Bundle: [name: tangle.inject.tests.Illegal]"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,10 @@ package tangle.inject.test.utils

import com.tschuchort.compiletesting.KotlinCompilation
import com.tschuchort.compiletesting.KotlinCompilation.ExitCode.COMPILATION_ERROR
import com.tschuchort.compiletesting.KotlinCompilation.ExitCode.OK
import hermit.test.junit.HermitJUnit5
import io.kotest.assertions.asClue
import io.kotest.matchers.shouldBe
import io.kotest.matchers.string.shouldContain
import org.junit.jupiter.api.BeforeEach
import org.junit.jupiter.api.DynamicTest
import org.junit.jupiter.api.TestInfo
Expand All @@ -44,9 +45,38 @@ abstract class BaseTest : HermitJUnit5() {
}
}

@Suppress("NewApi")
protected fun compileWithDagger(
vararg sources: String,
shouldFail: Boolean = false,
block: KotlinCompilation.Result.() -> Unit = { }
) {
fun String.clean() = replace("[^a-zA-Z0-9]".toRegex(), "_")

val className = testInfo.testClass.get().simpleName

val testName = testInfo.displayName
.clean()
.replace("_{2,}".toRegex(), "_")
.removeSuffix("_")

val workingDir = File("build/test-builds/$className/$testName")

compileAnvil(
sources = sources,
enableDaggerAnnotationProcessor = true,
generateDaggerFactories = false,
// Many constructor parameters are unused.
allWarningsAsErrors = false,
block = { checkExitCode(shouldFail).block() },
workingDir = workingDir
)
}

@Suppress("NewApi")
protected fun TestScope.compile(
vararg sources: String,
shouldFail: Boolean = false,
block: KotlinCompilation.Result.() -> Unit = { }
): KotlinCompilation.Result {
fun String.clean() = replace("[^a-zA-Z0-9]".toRegex(), "_")
Expand All @@ -68,15 +98,21 @@ abstract class BaseTest : HermitJUnit5() {
generateDaggerFactories = useAnvilFactories,
// Many constructor parameters are unused.
allWarningsAsErrors = false,
block = block,
block = { checkExitCode(shouldFail).block() },
workingDir = workingDir
)
}

infix fun KotlinCompilation.Result.shouldFailWithMessage(message: String) {
exitCode shouldBe COMPILATION_ERROR
fun KotlinCompilation.Result.checkExitCode(shouldFail: Boolean) = apply {
val expectedCode = if (shouldFail) {
COMPILATION_ERROR
} else {
OK
}

messages shouldContain message
messages.asClue {
exitCode shouldBe expectedCode
}
}

data class TestScope(val useAnvilFactories: Boolean)
Expand Down