Hello world, I have a few problems with the Grep exercise in the Elixir track.
Print only file names
The task description for the -l
flag reads:
-l
Output only the names of the files that contain at least one matching line.
The “real” grep -l
prints the name of a file containing a match only once.
There is no test case for this like the following:
# test for "real grep" behaviour when printing file names
test "several matches, print file names flag" do
assert Grep.grep("may", ["-l"], ["midsummer-night.txt"]) ==
"""
midsummer-night.txt
"""
end
Multiple flags
Also, I managed to do flag handling wrong in exactly the right way so that this test passes
test "one match, multiple flags" do
assert Grep.grep("OF ATREUS, Agamemnon, KIng of MEN.", ["-n", "-i", "-x"], ["iliad.txt"]) ==
"""
9:Of Atreus, Agamemnon, King of men.
"""
end
although my implementation ignores -x
after finding -i
.
filter =
cond do
"-i" in flags ->
fn {line, _} -> String.contains?(String.downcase(line), String.downcase(pattern)) end
"-x" in flags ->
fn {line, _} -> "#{pattern}\n" == line end
true ->
fn {line, _} -> String.contains?(line, pattern) end
end
There should be another test that makes sure that -x
is actually respected:
test "one match, -i -x flags" do
assert Grep.grep("OF ATREUS", ["-i", "-x"], ["iliad.txt"]) ==
"""
"""
end
Let me know what you think.
Regards,
Stefan