/
MyTests.kt
109 lines (95 loc) · 3 KB
/
MyTests.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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
package what.the.rxkotlin.android
import io.kotest.core.spec.style.BehaviorSpec
import io.kotest.core.spec.style.FunSpec
import io.kotest.core.spec.style.ShouldSpec
import io.kotest.matchers.shouldBe
import io.kotest.matchers.string.startWith
import kotlin.math.roundToInt
class MyTests : FunSpec({
test("String length should return the length of the string") {
"sammy".length shouldBe 5
"".length shouldBe 0
}
// val sample = "abcd"
// sample should startWith("ab")
})
class MyTests2 : ShouldSpec({
context("String.length") {
should("return the length of the string") {
"sammy".length shouldBe 5
"".length shouldBe 0
}
}
context("sample") {
val sample = "abcd"
sample should startWith("ab")
}
})
class MyTests3 : BehaviorSpec({
given("a broomstick") {
`when`("I sit on it") {
then("I should be able to fly") {
// test code
val list = listOf("a", "b", "c")
list.contains("b")
}
}
`when`("I throw it away") {
then("it should come back") {
// test code
val list = listOf("a", "b", "c")
list.contains("b")
}
}
}
})
class MyTests4 : BehaviorSpec({
given("100점이 만점인 상황에서") {
val totalMarks = 100
`when`("학생의 점수가") {
and("90점 이상이라면") {
val obtainedMarks = 92
then("등급은 A") {
getGrade(obtainedMarks, totalMarks) shouldBe "A"
}
}
and("80점 이상 90점 미만이라면") {
val obtainedMarks = 88
then("등급은 B") {
getGrade(obtainedMarks, totalMarks) shouldBe "B"
}
}
and("70점 이상 80점 미만이라면") {
val obtainedMarks = 77
then("등급은 C") {
getGrade(obtainedMarks, totalMarks) shouldBe "C"
}
}
and("60점 이상 70점 미만이라면") {
val obtainedMarks = 66
then("등급은 D") {
getGrade(obtainedMarks, totalMarks) shouldBe "D"
}
}
and("60점 미만이라면") {
val obtainedMarks = 34
then("등급은 F") {
getGrade(obtainedMarks, totalMarks) shouldBe "F"
}
}
}
}
})
fun getGrade(obtainedMarks: Int, totalMarks: Int): String {
val percentage = getPercentage(obtainedMarks, totalMarks)
return when {
percentage >= 90 -> "A"
percentage in 80..89 -> "B"
percentage in 70..79 -> "C"
percentage in 60..69 -> "D"
else -> "F"
}
}
private fun getPercentage(obtainedMarks: Int, totalMarks: Int): Int {
return (obtainedMarks / totalMarks.toFloat() * 100).roundToInt()
}