我尝试为具有多个具有默认值的参数的kotlin函数实施数据驱动测试。
在我的测试定义中,我希望能够省略函数声明中具有默认参数的任何参数组合。 我看不到它如何工作(对于默认值的每种组合都没有单独的分支)。
也许最好用代码来解释:
import kotlin.test.assertEquals
fun foobalize(start: Int = 0, separator: String = "\t", convert: Boolean = false): Int {
return 0 // implementation omitted
}
data class TestSpec(
val start: Int? = null, // null should mean: Don't pass this argument to foobalize(), but use its default value
val separator: String? = null, // dito
val convert: Boolean? = null, // dito
val expectedResult: Int
)
fun testFoobalize(testSpec: TestSpec) {
// How to call foobalize here with values from TestSpec, but leave out parameters that are null,
// so that the defaults from the fopobalize() function declaration are used???
val actualResult = foobalize(start = testSpec.start)
assertEquals(testSpec.expectedResult, actualResult)
}
是否有完全不同的方式来做到这一点?
答案 0 :(得分:1)
默认参数是kotlin编译器的功能,因此没有简单的方法可以在运行时使用它。 reflection应该可以。
IMO,如果您真的希望这样做,最好使用另一种方法来增强API,该方法将数据类作为参数。
public void AdicionarImagem_Pasta() {
for (int i : arrayID) {
new GetImages(getString(R.string.link), i + ".jpg").execute();
}
}