Added tests for 'show' exercise in 'String templates'
This commit is contained in:
parent
775e10208e
commit
4d9c710a3d
|
@ -0,0 +1,18 @@
|
|||
package stringTemplates3
|
||||
|
||||
fun show(i: Int, s: String, c: Char, d: Double) {
|
||||
println("i: $i")
|
||||
println("""s: "$s"""")
|
||||
println("c: '$c'")
|
||||
println("d: $d")
|
||||
}
|
||||
|
||||
fun main() {
|
||||
show(1, "abc", 'd', 2.0)
|
||||
}
|
||||
/* Expected output:
|
||||
i: 1
|
||||
s: "abc"
|
||||
c: 'd'
|
||||
d: 2.0
|
||||
*/
|
|
@ -0,0 +1,6 @@
|
|||
type: edu
|
||||
files:
|
||||
- name: src/Task.kt
|
||||
visible: true
|
||||
- name: test/Tests.kt
|
||||
visible: false
|
|
@ -0,0 +1,16 @@
|
|||
## String Templates (#3)
|
||||
|
||||
Write a function `show()` with parameters `i: Int`, `s: String`, `c: Char`
|
||||
and `d: Double`. The function should display on the console the value of each
|
||||
parameter in its own line: first the name of the identifier, a colon and space,
|
||||
and then the value of that identifier. Surround the string value with double
|
||||
quotes and the char value with single quotes.
|
||||
|
||||
Sample output:
|
||||
|
||||
```
|
||||
i: 10
|
||||
s: "abc"
|
||||
c: 'a'
|
||||
d: 1.0
|
||||
```
|
|
@ -0,0 +1,45 @@
|
|||
import org.junit.Assert
|
||||
import org.junit.Test
|
||||
import stringTemplates3.show
|
||||
import java.io.ByteArrayOutputStream
|
||||
import java.io.PrintStream
|
||||
import kotlin.reflect.KFunction
|
||||
import kotlin.reflect.KParameter
|
||||
|
||||
class TestShowFunction {
|
||||
@Test
|
||||
fun testShow() {
|
||||
val showMethod = ::show
|
||||
Assert.assertEquals("'show' method should have 4 parameters", 4, showMethod.parameters.size)
|
||||
val (i, s, c, d) = showMethod.parameters
|
||||
checkParameter(i, "i", "kotlin.Int")
|
||||
checkParameter(s, "s", "kotlin.String")
|
||||
checkParameter(c, "c", "kotlin.Char")
|
||||
checkParameter(d, "d", "kotlin.Double")
|
||||
|
||||
testArguments(showMethod, 1, "abc", 'd', 2.0)
|
||||
testArguments(showMethod, 193, "string", '&', -7.51)
|
||||
}
|
||||
|
||||
private fun testArguments(showMethod: KFunction<*>, i: Int, s: String, c: Char, d: Double) {
|
||||
val byteArrayOutputStream = ByteArrayOutputStream()
|
||||
System.setOut(PrintStream(byteArrayOutputStream))
|
||||
|
||||
val message = "Incorrect output for 'show' function call"
|
||||
val expectedOutput = """
|
||||
i: $i
|
||||
s: "$s"
|
||||
c: '$c'
|
||||
d: $d
|
||||
""".trimIndent()
|
||||
showMethod.call(i, s, c, d)
|
||||
|
||||
val output = byteArrayOutputStream.toString().trim()
|
||||
Assert.assertEquals(message, expectedOutput, output)
|
||||
}
|
||||
|
||||
private fun checkParameter(i: KParameter, expectedName: String, expectedType: String) {
|
||||
Assert.assertEquals("The name of the first parameter should be '$expectedName'", expectedName, i.name)
|
||||
Assert.assertEquals("The type of the first parameter should be '$expectedType'", expectedType, i.type.toString())
|
||||
}
|
||||
}
|
|
@ -2,5 +2,6 @@ content:
|
|||
- Examples
|
||||
- Exercise 1
|
||||
- Exercise 2
|
||||
- Exercise 3
|
||||
- Exercise 4
|
||||
- Exercise 5
|
||||
|
|
Loading…
Reference in New Issue