diff --git a/exercises/food-chain/example.py b/exercises/food-chain/example.py index 5deb43a94f..b5a701e694 100644 --- a/exercises/food-chain/example.py +++ b/exercises/food-chain/example.py @@ -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] diff --git a/exercises/food-chain/food_chain.py b/exercises/food-chain/food_chain.py index fe6bd97378..ed87e69c4e 100644 --- a/exercises/food-chain/food_chain.py +++ b/exercises/food-chain/food_chain.py @@ -1,2 +1,2 @@ -def chain(): +def recite(start_verse, end_verse): pass diff --git a/exercises/food-chain/food_chain_test.py b/exercises/food-chain/food_chain_test.py index 100e37ae33..19afcacc57 100644 --- a/exercises/food-chain/food_chain_test.py +++ b/exercises/food-chain/food_chain_test.py @@ -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." + "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__':