object House {
val verses: String =
"""This is the house that Jack built.
This is the malt
that lay in the house that Jack built.
This is the rat
that ate the malt
that lay in the house that Jack built.
This is the cat
that killed the rat
that ate the malt
that lay in the house that Jack built.
This is the dog
that worried the cat
that killed the rat
that ate the malt
that lay in the house that Jack built.
This is the cow with the crumpled horn
that tossed the dog
that worried the cat
that killed the rat
that ate the malt
that lay in the house that Jack built.
This is the maiden all forlorn
that milked the cow with the crumpled horn
that tossed the dog
that worried the cat
that killed the rat
that ate the malt
that lay in the house that Jack built.
This is the man all tattered and torn
that kissed the maiden all forlorn
that milked the cow with the crumpled horn
that tossed the dog
that worried the cat
that killed the rat
that ate the malt
that lay in the house that Jack built.
This is the priest all shaven and shorn
that married the man all tattered and torn
that kissed the maiden all forlorn
that milked the cow with the crumpled horn
that tossed the dog
that worried the cat
that killed the rat
that ate the malt
that lay in the house that Jack built.
This is the rooster that crowed in the morn
that woke the priest all shaven and shorn
that married the man all tattered and torn
that kissed the maiden all forlorn
that milked the cow with the crumpled horn
that tossed the dog
that worried the cat
that killed the rat
that ate the malt
that lay in the house that Jack built.
This is the farmer sowing his corn
that kept the rooster that crowed in the morn
that woke the priest all shaven and shorn
that married the man all tattered and torn
that kissed the maiden all forlorn
that milked the cow with the crumpled horn
that tossed the dog
that worried the cat
that killed the rat
that ate the malt
that lay in the house that Jack built.
This is the horse and the hound and the horn
that belonged to the farmer sowing his corn
that kept the rooster that crowed in the morn
that woke the priest all shaven and shorn
that married the man all tattered and torn
that kissed the maiden all forlorn
that milked the cow with the crumpled horn
that tossed the dog
that worried the cat
that killed the rat
that ate the malt
that lay in the house that Jack built."""
private val splitVerses = verses.trim.split("""\R\R""").map(_.trim)
def verse(n: Int): String = splitVerses(n - 1)
def recite(startVerse: Int, endVerse: Int): String =
splitVerses.slice(startVerse-1, endVerse).mkString("\n\n")
}
Have you run the tests? What do the tests say is the issue?
org.scalatest.exceptions.TestFailedException: "...use that Jack built.[]" was not equal to "...use that Jack built.[
]"
Looks like there are some missing newlines after “that Jack built.” somewhere. I don’t know Scala, so I can’t way what the error is.
1 Like
The tests expect every recite result to end with a blank line. That means the returned string must end with two newline characters.
Your current version joins verses with blank lines between them:
.mkString("\n\n")
but it does not add the final blank line after the last verse. That is why the failure shows:
actual: Jack built.
expected: Jack built.\n\n