From 4d9c710a3daa8f3374f0d7fafb4b8d7d23fac6a6 Mon Sep 17 00:00:00 2001 From: Svetlana Isakova Date: Wed, 9 Oct 2019 15:58:18 +0200 Subject: [PATCH] Added tests for 'show' exercise in 'String templates' --- .../String Templates/Exercise 3/src/Task.kt | 18 ++++++++ .../Exercise 3/task-info.yaml | 6 +++ .../String Templates/Exercise 3/task.md | 16 +++++++ .../String Templates/Exercise 3/test/Tests.kt | 45 +++++++++++++++++++ .../String Templates/lesson-info.yaml | 1 + 5 files changed, 86 insertions(+) create mode 100644 Programming Basics/String Templates/Exercise 3/src/Task.kt create mode 100644 Programming Basics/String Templates/Exercise 3/task-info.yaml create mode 100644 Programming Basics/String Templates/Exercise 3/task.md create mode 100644 Programming Basics/String Templates/Exercise 3/test/Tests.kt diff --git a/Programming Basics/String Templates/Exercise 3/src/Task.kt b/Programming Basics/String Templates/Exercise 3/src/Task.kt new file mode 100644 index 00000000..2d394b0d --- /dev/null +++ b/Programming Basics/String Templates/Exercise 3/src/Task.kt @@ -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 +*/ \ No newline at end of file diff --git a/Programming Basics/String Templates/Exercise 3/task-info.yaml b/Programming Basics/String Templates/Exercise 3/task-info.yaml new file mode 100644 index 00000000..7ad1b827 --- /dev/null +++ b/Programming Basics/String Templates/Exercise 3/task-info.yaml @@ -0,0 +1,6 @@ +type: edu +files: +- name: src/Task.kt + visible: true +- name: test/Tests.kt + visible: false diff --git a/Programming Basics/String Templates/Exercise 3/task.md b/Programming Basics/String Templates/Exercise 3/task.md new file mode 100644 index 00000000..d49ccdb0 --- /dev/null +++ b/Programming Basics/String Templates/Exercise 3/task.md @@ -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 +``` \ No newline at end of file diff --git a/Programming Basics/String Templates/Exercise 3/test/Tests.kt b/Programming Basics/String Templates/Exercise 3/test/Tests.kt new file mode 100644 index 00000000..48e57923 --- /dev/null +++ b/Programming Basics/String Templates/Exercise 3/test/Tests.kt @@ -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()) + } +} \ No newline at end of file diff --git a/Programming Basics/String Templates/lesson-info.yaml b/Programming Basics/String Templates/lesson-info.yaml index 0a407cf0..536bd6af 100644 --- a/Programming Basics/String Templates/lesson-info.yaml +++ b/Programming Basics/String Templates/lesson-info.yaml @@ -2,5 +2,6 @@ content: - Examples - Exercise 1 - Exercise 2 +- Exercise 3 - Exercise 4 - Exercise 5