Skip to content

Commit

Permalink
Grep: sync expected test results and input data with problem-specific…
Browse files Browse the repository at this point in the history
…ations (#1795)

* grep: sync expected test results and input data with problem-specifications.
  • Loading branch information
BethanyG authored and cmccandless committed May 28, 2019
1 parent 7c6981b commit 09b499c
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 70 deletions.
6 changes: 3 additions & 3 deletions exercises/grep/example.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ def format_files(matched_lines):
return result


def format_lines(matched_lines, files, flags):
def format_lines(matched_lines, flags, files):
result = []

for file_name, line_number, line in matched_lines:
Expand All @@ -42,7 +42,7 @@ def format_lines(matched_lines, files, flags):
return ''.join(result)


def grep(pattern, files, flags=''):
def grep(pattern, flags, files):
matched_lines = []

for file_name in files:
Expand All @@ -54,4 +54,4 @@ def grep(pattern, files, flags=''):
if '-l' in flags:
return format_files(matched_lines)

return format_lines(matched_lines, files, flags)
return format_lines(matched_lines, flags, files)
2 changes: 1 addition & 1 deletion exercises/grep/grep.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
def grep(pattern, files, flags=''):
def grep(pattern, flags, files):
pass
107 changes: 41 additions & 66 deletions exercises/grep/grep_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -115,125 +115,106 @@ def tearDownClass(self):

def test_one_file_one_match_no_flags(self):
self.assertMultiLineEqual(
grep("Agamemnon", [ILIADFILENAME]),
grep("Agamemnon", "", [ILIADFILENAME]),
"Of Atreus, Agamemnon, King of men.\n"
)

def test_one_file_one_match_print_line_numbers_flag(self):
self.assertMultiLineEqual(
grep("Forbidden", [PARADISELOSTFILENAME], "-n"),
grep("Forbidden", "-n", [PARADISELOSTFILENAME]),
"2:Of that Forbidden Tree, whose mortal tast\n"
)

def test_one_file_one_match_case_insensitive_flag(self):
self.assertMultiLineEqual(
grep("FORBIDDEN", [PARADISELOSTFILENAME], "-i"),
grep("FORBIDDEN", "-i", [PARADISELOSTFILENAME]),
"Of that Forbidden Tree, whose mortal tast\n"
)

def test_one_file_one_match_print_file_names_flag(self):
self.assertMultiLineEqual(
grep("Forbidden", [PARADISELOSTFILENAME], "-l"),
PARADISELOSTFILENAME + '\n'
)
grep("Forbidden", "-l", [PARADISELOSTFILENAME]),
PARADISELOSTFILENAME + '\n')

def test_one_file_one_match_match_entire_lines_flag(self):
self.assertMultiLineEqual(
grep("With loss of Eden, till one greater Man",
[PARADISELOSTFILENAME], "-x"),
"With loss of Eden, till one greater Man\n"
)
"-x", [PARADISELOSTFILENAME]),
"With loss of Eden, till one greater Man\n")

def test_one_file_one_match_multiple_flags(self):
self.assertMultiLineEqual(
grep(
"OF ATREUS, Agamemnon, KIng of MEN.",
[ILIADFILENAME],
"-n -i -x"
),
"9:Of Atreus, Agamemnon, King of men.\n"
)
grep("OF ATREUS, Agamemnon, KIng of MEN.",
"-n -i -x", [ILIADFILENAME]),
"9:Of Atreus, Agamemnon, King of men.\n")

def test_one_file_several_matches_no_flags(self):
self.assertMultiLineEqual(
grep("may", [MIDSUMMERNIGHTFILENAME]),
grep("may", "", [MIDSUMMERNIGHTFILENAME]),
"Nor how it may concern my modesty,\n"
"But I beseech your grace that I may know\n"
"The worst that may befall me in this case,\n"
)
"The worst that may befall me in this case,\n")

def test_one_file_several_matches_print_line_numbers_flag(self):
self.assertMultiLineEqual(
grep("may", [MIDSUMMERNIGHTFILENAME], "-n"),
grep("may", "-n", [MIDSUMMERNIGHTFILENAME]),
"3:Nor how it may concern my modesty,\n"
"5:But I beseech your grace that I may know\n"
"6:The worst that may befall me in this case,\n"
)
"6:The worst that may befall me in this case,\n")

def test_one_file_several_matches_match_entire_lines_flag(self):
self.assertMultiLineEqual(
grep("may", [MIDSUMMERNIGHTFILENAME], "-x"),
""
)
grep("may", "-x", [MIDSUMMERNIGHTFILENAME]),
"")

def test_one_file_several_matches_case_insensitive_flag(self):
self.assertMultiLineEqual(
grep("ACHILLES", [ILIADFILENAME], "-i"),
grep("ACHILLES", "-i", [ILIADFILENAME]),
"Achilles sing, O Goddess! Peleus' son;\n"
"The noble Chief Achilles from the son\n"
)
"The noble Chief Achilles from the son\n")

def test_one_file_several_matches_inverted_flag(self):
self.assertMultiLineEqual(
grep("Of", [PARADISELOSTFILENAME], "-v"),
grep("Of", "-v", [PARADISELOSTFILENAME]),
"Brought Death into the World, and all our woe,\n"
"With loss of Eden, till one greater Man\n"
"Restore us, and regain the blissful Seat,\n"
"Sing Heav'nly Muse, that on the secret top\n"
"That Shepherd, who first taught the chosen Seed\n"
)
"That Shepherd, who first taught the chosen Seed\n")

def test_one_file_one_match_file_flag_takes_precedence_over_line(self):
self.assertMultiLineEqual(
grep("ten", [ILIADFILENAME], "-n -l"),
ILIADFILENAME + '\n'
)
grep("ten", "-n -l", [ILIADFILENAME]),
ILIADFILENAME + '\n')

def test_one_file_no_matches_various_flags(self):
self.assertMultiLineEqual(
grep("Gandalf", [ILIADFILENAME], "-n -l -x -i"),
""
)
grep("Gandalf", "-n -l -x -i", [ILIADFILENAME]),
"")

def test_multiple_files_one_match_no_flags(self):
self.assertMultiLineEqual(
grep("Agamemnon", FILENAMES),
"iliad.txt:Of Atreus, Agamemnon, King of men.\n"
)
grep("Agamemnon", "", FILENAMES),
"iliad.txt:Of Atreus, Agamemnon, King of men.\n")

def test_multiple_files_several_matches_no_flags(self):
self.assertMultiLineEqual(
grep("may", FILENAMES),
grep("may", "", FILENAMES),
"midsummer-night.txt:Nor how it may concern my modesty,\n"
"midsummer-night.txt:But I beseech your grace that I may know\n"
"midsummer-night.txt:The worst that may befall me in this case,\n"
)
"midsummer-night.txt:The worst that may befall me in this case,\n")

def test_multiple_files_several_matches_print_line_numbers_flag(self):
expected = (
"midsummer-night.txt:5:But I beseech your grace that I may know\n"
"midsummer-night.txt:6:The worst that may befall me in this case,"
"\nparadise-lost.txt:2:Of that Forbidden Tree, whose mortal tast\n"
"paradise-lost.txt:6:Sing Heav'nly Muse, that on the secret top\n"
)
self.assertMultiLineEqual(
grep("that", FILENAMES, "-n"),
expected
)
"paradise-lost.txt:6:Sing Heav'nly Muse, that on the secret top\n")
self.assertMultiLineEqual(grep("that", "-n", FILENAMES), expected)

def test_multiple_files_one_match_print_file_names_flag(self):
self.assertMultiLineEqual(
grep("who", FILENAMES, "-l"),
grep("who", "-l", FILENAMES),
ILIADFILENAME + '\n' + PARADISELOSTFILENAME + '\n')

def test_multiple_files_several_matches_case_insensitive_flag(self):
Expand All @@ -247,44 +228,38 @@ def test_multiple_files_several_matches_case_insensitive_flag(self):
"\nmidsummer-night.txt:If I refuse to wed Demetrius.\n"
"paradise-lost.txt:Brought Death into the World, and all our woe,"
"\nparadise-lost.txt:Restore us, and regain the blissful Seat,\n"
"paradise-lost.txt:Sing Heav'nly Muse, that on the secret top\n"
)
self.assertMultiLineEqual(
grep("TO", FILENAMES, "-i"),
expected
)
"paradise-lost.txt:Sing Heav'nly Muse, that on the secret top\n")
self.assertMultiLineEqual(grep("TO", "-i", FILENAMES), expected)

def test_multiple_files_several_matches_inverted_flag(self):
self.assertMultiLineEqual(
grep("a", FILENAMES, "-v"),
grep("a", "-v", FILENAMES),
"iliad.txt:Achilles sing, O Goddess! Peleus' son;\n"
"iliad.txt:The noble Chief Achilles from the son\n"
"midsummer-night.txt:If I refuse to wed Demetrius.\n"
)

def test_multiple_files_one_match_match_entire_lines_flag(self):
self.assertMultiLineEqual(
grep("But I beseech your grace that I may know",
FILENAMES, "-x"),
grep("But I beseech your grace that I may know", "-x", FILENAMES),
"midsummer-night.txt:But I beseech your grace that I may know\n")

def test_multiple_files_one_match_multiple_flags(self):
self.assertMultiLineEqual(
grep("WITH LOSS OF EDEN, TILL ONE GREATER MAN",
FILENAMES, "-n -i -x"),
grep("WITH LOSS OF EDEN, TILL ONE GREATER MAN", "-n -i -x",
FILENAMES),
"paradise-lost.txt:4:With loss of Eden, till one greater Man\n")

def test_multiple_files_no_matches_various_flags(self):
self.assertMultiLineEqual(
grep("Frodo", FILENAMES, "-n -l -x -i"),
grep("Frodo", "-n -l -x -i", FILENAMES),
""
)

def test_multiple_files_several_matches_file_flag_takes_precedence(self):
self.assertMultiLineEqual(
grep("who", FILENAMES, "-n -l"),
ILIADFILENAME + '\n' + PARADISELOSTFILENAME + '\n'
)
grep("who", "-n -l", FILENAMES),
ILIADFILENAME + '\n' + PARADISELOSTFILENAME + '\n')

def test_multiple_files_several_matches_inverted_match_entire_lines(self):
expected = (
Expand Down Expand Up @@ -314,7 +289,7 @@ def test_multiple_files_several_matches_inverted_match_entire_lines(self):
"\n"
)
self.assertMultiLineEqual(
grep("Illustrious into Ades premature,", FILENAMES, "-x -v"),
grep("Illustrious into Ades premature,", "-x -v", FILENAMES),
expected
)

Expand Down

0 comments on commit 09b499c

Please sign in to comment.