Skip to content

Commit f686fd3

Browse files
authored
Merge pull request gregmalcolm#183 from tracybporter/clarify-iteration-tests
Show filter applies the rule to all items iSorry this sat out for so long, maintaining a repo for 10+ years is hard... :D But I like what I'm seeing, and the indent fixes are in, lets add this! Also thanks to @kjc for advancing this while I wasn't paying attention!
2 parents e59bea4 + 15c13ed commit f686fd3

1 file changed

Lines changed: 18 additions & 23 deletions

File tree

python3/koans/about_iteration.py

Lines changed: 18 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -64,21 +64,23 @@ def is_even(item):
6464

6565
self.assertEqual(__, even_numbers)
6666

67-
def test_just_return_first_item_found(self):
67+
def test_filter_returns_all_items_matching_criterion(self):
6868
def is_big_name(item):
69-
return len(item) > 4
70-
71-
names = ["Jim", "Bill", "Clarence", "Doug", "Eli"]
72-
name = None
69+
return len(item) > 4
7370

71+
names = ["Jim", "Bill", "Clarence", "Doug", "Eli", "Elizabeth"]
7472
iterator = filter(is_big_name, names)
73+
74+
self.assertEqual(__, next(iterator))
75+
self.assertEqual(__, next(iterator))
76+
7577
try:
76-
name = next(iterator)
78+
next(iterator)
79+
pass
7780
except StopIteration:
7881
msg = 'Ran out of big names'
7982

80-
self.assertEqual(__, name)
81-
83+
self.assertEquals(__, msg)
8284

8385
# ------------------------------------------------------------------
8486

@@ -120,18 +122,11 @@ def test_all_iteration_methods_work_on_any_sequence_not_just_lists(self):
120122
result = map(self.add_ten, range(1,4))
121123
self.assertEqual(__, list(result))
122124

123-
try:
124-
file = open("example_file.txt")
125-
126-
try:
127-
def make_upcase(line):
128-
return line.strip().upper()
129-
upcase_lines = map(make_upcase, file.readlines())
130-
self.assertEqual(__, list(upcase_lines))
131-
finally:
132-
# Arg, this is ugly.
133-
# We will figure out how to fix this later.
134-
file.close()
135-
except IOError:
136-
# should never happen
137-
self.fail()
125+
def test_lines_in_a_file_are_iterable_sequences_too(self):
126+
def make_upcase(line):
127+
return line.strip().title()
128+
129+
file = open("example_file.txt")
130+
upcase_lines = map(make_upcase, file.readlines())
131+
self.assertEqual(__, list(upcase_lines))
132+
file.close()

0 commit comments

Comments
 (0)