Skip to content

food-chain: update tests to v2.1.0 #1347

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Feb 26, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions exercises/food-chain/example.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,3 +43,12 @@ def chain():
song += verse

return song


def verses(letter):
return letter.replace('die.', 'die.slice').split('slice')


def recite(start_verse, end_verse):
generated = [verse.replace("\n", "") for verse in verses(chain())]
return generated[start_verse-1:end_verse]
2 changes: 1 addition & 1 deletion exercises/food-chain/food_chain.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
def chain():
def recite(start_verse, end_verse):
pass
159 changes: 79 additions & 80 deletions exercises/food-chain/food_chain_test.py
Original file line number Diff line number Diff line change
@@ -1,108 +1,107 @@
import unittest

from food_chain import chain

# Tests adapted from `problem-specifications//canonical-data.json` @ v1.0.0

song = """I know an old lady who swallowed a fly.
I don't know why she swallowed the fly. Perhaps she'll die.

I know an old lady who swallowed a spider.
It wriggled and jiggled and tickled inside her.
She swallowed the spider to catch the fly.
I don't know why she swallowed the fly. Perhaps she'll die.

I know an old lady who swallowed a bird.
How absurd to swallow a bird!
She swallowed the bird to catch the spider that wriggled
and jiggled and tickled inside her.
She swallowed the spider to catch the fly.
I don't know why she swallowed the fly. Perhaps she'll die.

I know an old lady who swallowed a cat.
Imagine that, to swallow a cat!
She swallowed the cat to catch the bird.
She swallowed the bird to catch the spider that wriggled
and jiggled and tickled inside her.
She swallowed the spider to catch the fly.
I don't know why she swallowed the fly. Perhaps she'll die.

I know an old lady who swallowed a dog.
What a hog, to swallow a dog!
She swallowed the dog to catch the cat.
She swallowed the cat to catch the bird.
She swallowed the bird to catch the spider that wriggled
and jiggled and tickled inside her.
She swallowed the spider to catch the fly.
I don't know why she swallowed the fly. Perhaps she'll die.

I know an old lady who swallowed a goat.
Just opened her throat and swallowed a goat!
She swallowed the goat to catch the dog.
She swallowed the dog to catch the cat.
She swallowed the cat to catch the bird.
She swallowed the bird to catch the spider that wriggled
and jiggled and tickled inside her.
She swallowed the spider to catch the fly.
I don't know why she swallowed the fly. Perhaps she'll die.

I know an old lady who swallowed a cow.
I don't know how she swallowed a cow!
She swallowed the cow to catch the goat.
She swallowed the goat to catch the dog.
She swallowed the dog to catch the cat.
She swallowed the cat to catch the bird.
She swallowed the bird to catch the spider that wriggled
and jiggled and tickled inside her.
She swallowed the spider to catch the fly.
I don't know why she swallowed the fly. Perhaps she'll die.

I know an old lady who swallowed a horse.
She's dead, of course!"""


def verses(letter):
return letter.replace('die.', 'die.slice').split('slice')


original = [verse.replace("\n", "").replace(" ", "").lower()
for verse in verses(song)]

generated = [verse.replace("\n", "").replace(" ", "").lower()
for verse in verses(chain())]
from food_chain import recite


# Tests adapted from `problem-specifications//canonical-data.json` @ v2.1.0

class FoodChainTest(unittest.TestCase):

def test_fly(self):
self.assertEqual(original[0], generated[0])
expected = [
"I know an old lady who swallowed a fly."
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think within each verse, we ought to keep the newlines (\n).

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But we seem to have reached a consensus in beer-song, house and twelve-days, where there is no newline at the end of each verse.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm... I obviously didn't pay as much attention to beer-song and house as I thought. In the case of twelve-days, each verse is a single line, is this was not a relevant point.

After considering the format used in the canonical data, I have changed my mind and agree that it should remain this way to be consistent both with the other exercises and with the canonical data. However, this means that house is not actually consistent after all, as each verse contains multiple element in the returned list. This should be corrected.

"I don't know why she swallowed the fly. Perhaps she'll die."
]
self.assertEqual(recite(1, 1), expected)

def test_spider(self):
self.assertEqual(original[1], generated[1])
expected = [
"I know an old lady who swallowed a spider."
"It wriggled and jiggled and tickled inside her."
"She swallowed the spider to catch the fly."
"I don't know why she swallowed the fly. Perhaps she'll die."
]
self.assertEqual(recite(2, 2), expected)

def test_bird(self):
self.assertEqual(original[2], generated[2])
expected = [
"I know an old lady who swallowed a bird."
"How absurd to swallow a bird!"
"She swallowed the bird to catch the spider that "
"wriggled and jiggled and tickled inside her."
"She swallowed the spider to catch the fly."
"I don't know why she swallowed the fly. Perhaps she'll die."
]
self.assertEqual(recite(3, 3), expected)

def test_cat(self):
self.assertEqual(original[3], generated[3])
expected = [
"I know an old lady who swallowed a cat."
"Imagine that, to swallow a cat!"
"She swallowed the cat to catch the bird."
"She swallowed the bird to catch the spider that "
"wriggled and jiggled and tickled inside her."
"She swallowed the spider to catch the fly."
"I don't know why she swallowed the fly. Perhaps she'll die."
]
self.assertEqual(recite(4, 4), expected)

def test_dog(self):
self.assertEqual(original[4], generated[4])
expected = [
"I know an old lady who swallowed a dog."
"What a hog, to swallow a dog!"
"She swallowed the dog to catch the cat."
"She swallowed the cat to catch the bird."
"She swallowed the bird to catch the spider that wriggled "
"and jiggled and tickled inside her."
"She swallowed the spider to catch the fly."
"I don't know why she swallowed the fly. Perhaps she'll die."
]
self.assertEqual(recite(5, 5), expected)

def test_goat(self):
self.assertEqual(original[5], generated[5])
expected = [
"I know an old lady who swallowed a goat."
"Just opened her throat and swallowed a goat!"
"She swallowed the goat to catch the dog."
"She swallowed the dog to catch the cat."
"She swallowed the cat to catch the bird."
"She swallowed the bird to catch the spider that "
"wriggled and jiggled and tickled inside her."
"She swallowed the spider to catch the fly."
"I don't know why she swallowed the fly. Perhaps she'll die."
]
self.assertEqual(recite(6, 6), expected)

def test_cow(self):
self.assertEqual(original[6], generated[6])
expected = [
"I know an old lady who swallowed a cow."
"I don't know how she swallowed a cow!"
"She swallowed the cow to catch the goat."
"She swallowed the goat to catch the dog."
"She swallowed the dog to catch the cat."
"She swallowed the cat to catch the bird."
"She swallowed the bird to catch the spider that "
"wriggled and jiggled and tickled inside her."
"She swallowed the spider to catch the fly."
"I don't know why she swallowed the fly. Perhaps she'll die."
]
self.assertEqual(recite(7, 7), expected)

def test_horse(self):
self.assertEqual(original[7], generated[7])
expected = [
"I know an old lady who swallowed a horse."
"She's dead, of course!"
]
self.assertEqual(recite(8, 8), expected)

def test_multiple_verses(self):
self.assertEqual(original[0:3], generated[0:3])
expected = [recite(n, n)[0] for n in range(1, 4)]
self.assertEqual(recite(1, 3), expected)

def test_full_song(self):
self.assertEqual(original, generated)
expected = [recite(n, n)[0] for n in range(1, 9)]
self.assertEqual(recite(1, 8), expected)


if __name__ == '__main__':
Expand Down