Skip to content

Commit b4bd87b

Browse files
committed
fix day 5 kotlin
1 parent 0d38a94 commit b4bd87b

File tree

2 files changed

+35
-35
lines changed

2 files changed

+35
-35
lines changed

exercise/kotlin/day05/src/test/kotlin/PopulationTests.kt

Lines changed: 22 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,29 @@ class PopulationTests : FunSpec({
2525
Person("Glenn", "Quagmire")
2626
)
2727

28+
fun formatPopulation(): String {
29+
var response = ""
30+
31+
for (person in population) {
32+
response += "${person.firstName} ${person.lastName}"
33+
34+
if (person.pets.isNotEmpty()) {
35+
response += " who owns : "
36+
}
37+
for (pet in person.pets) {
38+
response += "${pet.name} "
39+
}
40+
if (population.last() != person) {
41+
response += lineSeparator()
42+
}
43+
}
44+
return response
45+
}
46+
2847
test("people with their pets") {
29-
formatPopulation(population) shouldBe """Peter Griffin who owns : Tabby
48+
val response = formatPopulation()
49+
50+
response shouldBe """Peter Griffin who owns : Tabby
3051
|Stewie Griffin who owns : Dolly Brian
3152
|Joe Swanson who owns : Spike
3253
|Lois Griffin who owns : Serpy
@@ -36,15 +57,3 @@ class PopulationTests : FunSpec({
3657
|Glenn Quagmire""".trimMargin()
3758
}
3859
})
39-
40-
fun formatPopulation(population: List<Person>): String =
41-
population.joinToString(lineSeparator()) { formatPerson(it) }
42-
43-
fun Person.formatPets(): String {
44-
return when {
45-
pets.isNotEmpty() -> return pets.joinToString(" ", " who owns : ", " ") { it.name }
46-
else -> ""
47-
}
48-
}
49-
50-
fun formatPerson(person: Person): String = "${person.firstName} ${person.lastName}" + person.formatPets()

solution/kotlin/day05/src/test/kotlin/PopulationTests.kt

Lines changed: 13 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -25,29 +25,8 @@ class PopulationTests : FunSpec({
2525
Person("Glenn", "Quagmire")
2626
)
2727

28-
fun formatPopulation(): String {
29-
var response = ""
30-
31-
for (person in population) {
32-
response += "${person.firstName} ${person.lastName}"
33-
34-
if (person.pets.isNotEmpty()) {
35-
response += " who owns : "
36-
}
37-
for (pet in person.pets) {
38-
response += "${pet.name} "
39-
}
40-
if (population.last() != person) {
41-
response += lineSeparator()
42-
}
43-
}
44-
return response
45-
}
46-
4728
test("people with their pets") {
48-
val response = formatPopulation()
49-
50-
response shouldBe """Peter Griffin who owns : Tabby
29+
formatPopulation(population) shouldBe """Peter Griffin who owns : Tabby
5130
|Stewie Griffin who owns : Dolly Brian
5231
|Joe Swanson who owns : Spike
5332
|Lois Griffin who owns : Serpy
@@ -57,3 +36,15 @@ class PopulationTests : FunSpec({
5736
|Glenn Quagmire""".trimMargin()
5837
}
5938
})
39+
40+
fun formatPopulation(population: List<Person>): String =
41+
population.joinToString(lineSeparator()) { formatPerson(it) }
42+
43+
fun Person.formatPets(): String {
44+
return when {
45+
pets.isNotEmpty() -> return pets.joinToString(" ", " who owns : ", " ") { it.name }
46+
else -> ""
47+
}
48+
}
49+
50+
fun formatPerson(person: Person): String = "${person.firstName} ${person.lastName}" + person.formatPets()

0 commit comments

Comments
 (0)