-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
231 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
defmodule Mix.Tasks.TypoKillerTest do | ||
use TypoKiller.Case, async: true | ||
|
||
alias Mix.Tasks.TypoKiller | ||
|
||
describe "run/1" do | ||
test "should find typos from given directory" do | ||
find_typos = fn -> | ||
TypoKiller.run([@file_with_typo]) | ||
end | ||
|
||
assert capture_io(find_typos) == @file_with_typo_output | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
defmodule TypoKiller.CLITest do | ||
use TypoKiller.Case, async: true | ||
|
||
alias TypoKiller.CLI | ||
|
||
@no_action_defined """ | ||
Error: no action provided. | ||
Available actions: run | ||
""" | ||
|
||
@avaliable_actions """ | ||
Error: unknown action 'foobar'. | ||
Available: run | ||
""" | ||
|
||
@help_action """ | ||
TypoKiller - qu'est que ce | ||
--------------------- | ||
Usage: | ||
typokiller <command> <options> | ||
Available commands: run | ||
Options | ||
-p, --path <path> Run TypoKiller in the given path | ||
Example: | ||
typokiller run --path ~/my_project | ||
""" | ||
|
||
describe "main/0" do | ||
test "without action defined" do | ||
main = fn -> CLI.main() end | ||
|
||
assert capture_io(main) == @no_action_defined | ||
end | ||
end | ||
|
||
describe "main/1" do | ||
test "using help command" do | ||
main = fn -> CLI.main(["--help"]) end | ||
|
||
assert capture_io(main) == @help_action | ||
end | ||
|
||
test "using valid arguments" do | ||
main = fn -> CLI.main(["run", "-p", @file_with_typo]) end | ||
|
||
assert capture_io(main) == @file_with_typo_output | ||
end | ||
|
||
test "using invalid arguments" do | ||
invalid_arg = "-x" | ||
|
||
main = fn -> CLI.main(["run", invalid_arg, @file_with_typo]) end | ||
|
||
output = """ | ||
Error - invalid args: | ||
#{invalid_arg} | ||
""" | ||
|
||
assert capture_io(main) == output | ||
end | ||
|
||
test "using unknown command" do | ||
main = fn -> CLI.main(["foobar"]) end | ||
|
||
assert capture_io(main) == @avaliable_actions | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
defmodule TypoKiller.OutputTest do | ||
use TypoKiller.Case | ||
|
||
alias TypoKiller.Candidate | ||
alias TypoKiller.Output | ||
|
||
describe "print_candidates/1" do | ||
test "should print middle" do | ||
file = "./test/fixtures/output.txt" | ||
|
||
candidates = [ | ||
%Candidate{ | ||
occurrences: [ | ||
{file, [1, 2]}, | ||
{file, [2, 3]} | ||
], | ||
similar_word: nil, | ||
score: nil, | ||
word: "mnigrations" | ||
} | ||
] | ||
|
||
io_write = fn -> | ||
Output.print_candidates(candidates) | ||
end | ||
|
||
assert capture_io(io_write) == build_output(candidates) | ||
end | ||
end | ||
|
||
defp build_output(candidates) when is_list(candidates) do | ||
output = | ||
Enum.map(candidates, fn candidate = %Candidate{} -> | ||
{[], occurrences} = | ||
Enum.reduce(candidate.occurrences, {candidate.occurrences, []}, fn _occ, | ||
{acc, occurrences} -> | ||
occurrences = occurrences ++ [build_occurrences(acc)] | ||
[_ | acc] = acc | ||
{acc, occurrences} | ||
end) | ||
|
||
""" | ||
┌ #{candidate.word} | ||
├──┬─ Similar to: #{candidate.similar_word} | ||
│ └─ Score: #{candidate.score} | ||
│ | ||
""" <> Enum.join(occurrences) | ||
end) | ||
|
||
Enum.join(output, "\n") <> "\n" | ||
end | ||
|
||
defp build_occurrences([{path, lines}]) do | ||
""" | ||
│ | ||
└──┬─ File: #{path} | ||
└─ Lines: #{Enum.join(lines, ", ")} | ||
""" | ||
end | ||
|
||
defp build_occurrences([{path, lines} | _remaining]) do | ||
""" | ||
├──┬─ File: #{path} | ||
│ └─ Lines: #{Enum.join(lines, ", ")} | ||
""" | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
defmodule TypoKiller.WordsTest do | ||
use TypoKiller.Case | ||
|
||
alias TypoKiller.Words | ||
|
||
@files ~w( | ||
./test/fixtures/words.txt | ||
) | ||
|
||
describe "files_to_words/1" do | ||
test "should merge file results" do | ||
assert {[%TypoKiller.Candidate{}, %TypoKiller.Candidate{}], _} = | ||
Words.files_to_words(@files) | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters