generated from jetbrains-academy/kotlin-course-template
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Tests.kt
58 lines (53 loc) · 2.12 KB
/
Tests.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
import org.jetbrains.academy.kotlin.template.main
import org.jetbrains.academy.kotlin.template.newLineSeparator
import org.jetbrains.academy.kotlin.template.runMainFunction
import org.jetbrains.academy.kotlin.template.throwInternalCourseError
import org.jetbrains.academy.test.system.core.invokeWithArgs
import org.jetbrains.academy.test.system.core.models.classes.findClassSafe
import org.junit.jupiter.api.Assertions
import org.junit.jupiter.api.BeforeAll
import org.junit.jupiter.api.Test
import org.junit.jupiter.params.ParameterizedTest
import org.junit.jupiter.params.provider.Arguments
import org.junit.jupiter.params.provider.MethodSource
class Test {
companion object {
private lateinit var mainClazz: Class<*>
@JvmStatic
@BeforeAll
fun beforeAll() {
mainClazz = mainClass.findClassSafe() ?: throwInternalCourseError()
}
private const val HELLO = "Hello"
@JvmStatic
fun invokeSayHelloArguments() = listOf(
Arguments.of(1, List(1) { HELLO }.joinToString(System.lineSeparator())),
Arguments.of(2, List(2) { HELLO }.joinToString(System.lineSeparator())),
Arguments.of(3, List(3) { HELLO }.joinToString(System.lineSeparator()))
)
}
@Test
fun invokeSayHelloFunction() {
mainClass.checkMethod(mainClazz, invokeSayHelloFunction)
}
@ParameterizedTest
@MethodSource("invokeSayHelloArguments")
fun invokeSayHelloImplementation(
howManyTimes: Int,
output: String,
) {
val userMethod = mainClass.findMethod(mainClazz, invokeSayHelloFunction)
Assertions.assertEquals(
output,
userMethod.invokeWithArgs(howManyTimes, clazz = mainClazz)?.toString()?.trimIndent(),
"For howManyTimes = $howManyTimes the function ${invokeSayHelloFunction.name} should return $output"
)
}
@Test
fun testSolution() {
Assertions.assertEquals(
"How many times should I print $HELLO?${newLineSeparator}$HELLO${newLineSeparator}$HELLO".trimIndent(),
runMainFunction(::main, "2").trimIndent()
)
}
}