Skip to content

Commit

Permalink
Test: Add test for reportDuplicateAliasesAsErrors.
Browse files Browse the repository at this point in the history
  • Loading branch information
Fedor Ihnatkevich committed Apr 6, 2023
1 parent c081199 commit 208f738
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 7 deletions.
3 changes: 3 additions & 0 deletions testing/tests/src/main/kotlin/CompileTestDriver.kt
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

package com.yandex.yatagan.testing.tests

import com.yandex.yatagan.processor.common.Option
import com.yandex.yatagan.testing.source_set.SourceSet
import org.junit.Rule
import org.junit.rules.TestWatcher
Expand All @@ -29,6 +30,8 @@ interface CompileTestDriver : SourceSet {
sources: SourceSet,
)

fun <V : Any> givenOption(option: Option<V>, value: V)

/**
* Runs the test and validates the output against the golden output file.
* The golden output file resource path is computed from the junit test name.
Expand Down
14 changes: 10 additions & 4 deletions testing/tests/src/main/kotlin/CompileTestDriverBase.kt
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import com.yandex.yatagan.generated.CompiledApiClasspath
import com.yandex.yatagan.generated.DynamicApiClasspath
import com.yandex.yatagan.processor.common.IntOption
import com.yandex.yatagan.processor.common.LoggerDecorator
import com.yandex.yatagan.processor.common.Option
import com.yandex.yatagan.testing.source_set.SourceFile
import com.yandex.yatagan.testing.source_set.SourceSet
import org.junit.Assert
Expand All @@ -39,6 +40,10 @@ abstract class CompileTestDriverBase private constructor(
private val mainSourceSet: SourceSet,
) : CompileTestDriver, SourceSet by mainSourceSet {
private var precompiledModuleOutputDirs: List<File>? = null
private val options = mutableMapOf(
IntOption.MaxIssueEncounterPaths.key to "100",
IntOption.MaxSlotsPerSwitch.key to "100",
)

protected constructor(
apiType: ApiType = ApiType.Compiled,
Expand Down Expand Up @@ -95,6 +100,10 @@ abstract class CompileTestDriverBase private constructor(

abstract fun generatedFilesSubDir(): String?

override fun <V : Any> givenOption(option: Option<V>, value: V) {
options[option.key] = value.toString()
}

override fun compileRunAndValidate() {
val goldenResourcePath = "golden/${testNameRule.testClassSimpleName}/${testNameRule.testMethodName}.golden.txt"

Expand Down Expand Up @@ -162,10 +171,7 @@ abstract class CompileTestDriverBase private constructor(
"-opt-in=com.yandex.yatagan.VariantApi",
"-P", "plugin:org.jetbrains.kotlin.kapt3:correctErrorTypes=true",
),
processorOptions = mapOf(
IntOption.MaxIssueEncounterPaths.key to "100",
IntOption.MaxSlotsPerSwitch.key to "100",
),
processorOptions = options,
)

protected open fun createCompilationArguments() = createBaseCompilationArguments()
Expand Down
27 changes: 24 additions & 3 deletions testing/tests/src/main/kotlin/DynamicCompileTestDriver.kt
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,10 @@ import com.squareup.javapoet.ClassName
import com.yandex.yatagan.Component
import com.yandex.yatagan.lang.jap.asTypeElement
import com.yandex.yatagan.lang.jap.isAnnotatedWith
import com.yandex.yatagan.processor.common.IntOption
import com.yandex.yatagan.processor.common.Logger
import com.yandex.yatagan.processor.common.LoggerDecorator
import com.yandex.yatagan.processor.common.Option
import com.yandex.yatagan.testing.source_set.SourceFile
import org.intellij.lang.annotations.Language
import java.io.File
Expand All @@ -39,6 +41,9 @@ class DynamicCompileTestDriver(
apiType: ApiType = ApiType.Dynamic,
) : CompileTestDriverBase(apiType) {
private val accumulator = ComponentBootstrapperGenerator()
private val options = mutableMapOf<Option<*>, Any>(
IntOption.MaxIssueEncounterPaths to 100,
)

private val runnerSource = SourceFile.java("RuntimeTestRunner", """
import com.yandex.yatagan.Yatagan;
Expand All @@ -60,8 +65,25 @@ class DynamicCompileTestDriver(
}
""".trimIndent())

private fun formatOptions() = buildString {
for ((option, value) in options) {
val argument = when(value) {
is Boolean -> value.toString()
is Int -> value.toString()
else -> throw AssertionError("Unexpected option value type, please, support it explicitly")
}
append('.')
append(option.key.substringAfterLast('.'))
append('(').append(argument).append(')')
}
}

override fun generatedFilesSubDir(): String? = null

override fun <V : Any> givenOption(option: Option<V>, value: V) {
options[option] = value
}

override fun doCompile(): TestCompilationResult {
val testCompilationResult = super.doCompile()
check(testCompilationResult.success) {
Expand Down Expand Up @@ -136,7 +158,7 @@ class DynamicCompileTestDriver(
return success
}

private class ComponentBootstrapperGenerator : AbstractProcessor() {
private inner class ComponentBootstrapperGenerator : AbstractProcessor() {
private val _bootstrapperNames: MutableSet<ClassName> = TreeSet()
val bootstrapperNames: Set<ClassName> get() = _bootstrapperNames

Expand Down Expand Up @@ -196,10 +218,9 @@ class DynamicCompileTestDriver(
Yatagan.setupReflectionBackend()
.validation(delegate)
.maxIssueEncounterPaths(100)
.strictMode(true)
.useCompiledImplementationIfAvailable(true)
.logger(logger)
${formatOptions()}
.apply();
try {
Expand Down
26 changes: 26 additions & 0 deletions testing/tests/src/test/kotlin/CoreBindingsFailureTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

package com.yandex.yatagan.testing.tests

import com.yandex.yatagan.processor.common.BooleanOption
import com.yandex.yatagan.testing.source_set.SourceSet
import org.junit.Before
import org.junit.Test
Expand Down Expand Up @@ -576,6 +577,31 @@ class CoreBindingsFailureTest(
compileRunAndValidate()
}

@Test
fun `conflicting aliases with an option`() {
givenOption(BooleanOption.ReportDuplicateAliasesAsErrors, true)
givenKotlinSource("test.TestCase", """
import com.yandex.yatagan.*
import javax.inject.*
interface Api
class Impl1 @Inject constructor(): Api
class Impl2 @Inject constructor(): Api
@Module interface MyModule {
@Binds fun bind1(i: Impl1): Api
@Binds fun bind2(i: Impl2): Api
}
@Component(modules = [MyModule::class])
interface MyComponent {
val api: Api
}
""".trimIndent())

compileRunAndValidate()
}

@Test
fun `manual framework type usage`() {
givenKotlinSource("test.TestCase", """
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
error: Conflicting bindings for `test.Api`
NOTE: Conflicting binding: `alias test.MyModule::bind1(test.Impl1)`
NOTE: Conflicting binding: `alias test.MyModule::bind2(test.Impl2)`
Encountered:
here: graph for root-component test.MyComponent
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

0 comments on commit 208f738

Please sign in to comment.