Skip to content

Commit

Permalink
Improve tests to increase coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
aleDsz committed Nov 28, 2020
1 parent 26ba9d1 commit ce7348c
Show file tree
Hide file tree
Showing 5 changed files with 231 additions and 6 deletions.
15 changes: 15 additions & 0 deletions test/mix/typo_killer_test.exs
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
80 changes: 80 additions & 0 deletions test/typo_killer/cli_test.exs
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
67 changes: 67 additions & 0 deletions test/typo_killer/output_test.exs
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
16 changes: 16 additions & 0 deletions test/typo_killer/words_test.exs
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
59 changes: 53 additions & 6 deletions test/typo_killer_test.exs
Original file line number Diff line number Diff line change
@@ -1,10 +1,5 @@
defmodule TypoKillerTest do
use ExUnit.Case, async: true

import ExUnit.CaptureIO

@file_with_typo "./test/fixtures/file_with_typo.txt"
@file_with_typo_output File.read!("./test/fixtures/file_with_typo_output.txt")
use TypoKiller.Case, async: true

describe "find_typos/2" do
test "find typos and print them" do
Expand All @@ -14,5 +9,57 @@ defmodule TypoKillerTest do

assert capture_io(find_typos) == @file_with_typo_output
end

test "find typos using options and print them" do
options = [ignore_dot_files: false]

find_typos = fn ->
TypoKiller.find_typos(@file_with_typo, options)
end

assert capture_io(find_typos) ==
format_options(@file_with_typo_output_using_options, options)
end

test "find typos using options with array values and print them" do
options = [allowed_extensions: [".ex", ".exs"]]

find_typos = fn ->
TypoKiller.find_typos(@file_with_typo, options)
end

output = """
Path: "./test/fixtures/file_with_typo.txt\"
Options:
{{options}}
---
Running...
"""

assert capture_io(find_typos) == format_options(output, options)
end
end

describe "benchmark_find_typos/1" do
test "generate benchmark for given path" do
find_typos = fn ->
TypoKiller.benchmark_find_typos(@file_with_typo)
end

assert String.contains?(capture_io(find_typos), @file_with_typo_output)
end
end

defp format_options(output, options) do
options =
Enum.map(options, fn
{k, v} when is_list(v) ->
" #{Atom.to_string(k)} -> #{Enum.join(v, ", ")}"

{k, v} ->
" #{Atom.to_string(k)} -> #{inspect(v)}"
end)

String.replace(output, "{{options}}", Enum.join(options, "\n"))
end
end

0 comments on commit ce7348c

Please sign in to comment.