Skip to content

Commit

Permalink
Improve more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
rrrene committed Feb 5, 2024
1 parent fadfaac commit 38165e4
Show file tree
Hide file tree
Showing 13 changed files with 272 additions and 15 deletions.
5 changes: 0 additions & 5 deletions lib/credo/check/consistency/space_around_operators.ex
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,6 @@ defmodule Credo.Check.Consistency.SpaceAroundOperators do

@collector Credo.Check.Consistency.SpaceAroundOperators.Collector

# TODO: add *ignored* operators, so you can add "|" and still write
# [head|tail] while enforcing 2 + 3 / 1 ...
# FIXME: this seems to be already implemented, but there don't seem to be
# any related test cases around.

@doc false
@impl true
def run_on_all_source_files(exec, source_files, params) do
Expand Down
9 changes: 9 additions & 0 deletions test/credo/check/consistency/line_endings_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -57,4 +57,13 @@ defmodule Credo.Check.Consistency.LineEndingsTest do
assert issue.trigger == "\r\n"
end)
end

test "it should report an issue here when there is no problem, but the :force param specifies the other kind of line ending" do
[@windows_line_endings]
|> to_source_files
|> run_check(@described_check, force: :unix)
|> assert_issue(fn issue ->
assert issue.trigger == "\r\n"
end)
end
end
15 changes: 15 additions & 0 deletions test/credo/check/consistency/parameter_pattern_matching_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -166,4 +166,19 @@ defmodule Credo.Check.Consistency.ParameterPatternMatchingTest do

assert 3 == Enum.count(issues)
end

test "it should report issues when variable declarations are inconsistent throughout sourcefiles (forcing left side)" do
issues =
[
@var_right_map,
@var_right_struct,
@var_right_list,
@var_left_map
]
|> to_source_files
|> run_check(@described_check, force: :before)
|> assert_issues()

assert 3 == Enum.count(issues)
end
end
25 changes: 25 additions & 0 deletions test/credo/check/consistency/space_around_operators_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -436,4 +436,29 @@ defmodule Credo.Check.Consistency.SpaceAroundOperatorsTest do
|> run_check(@described_check)
|> refute_issues()
end

test "it should allow no spaces if specified in :ignore" do
source_files =
[
~S"""
defmodule TestTest do
def test(a, b, c) do
a = fn b, c -> b+c end
a.(-30, 10)
a.(-3.0, 1.0)
end
end
"""
]
|> to_source_files()

source_files
|> run_check(@described_check)
|> assert_issue()

source_files
|> run_check(@described_check, ignore: [:+])
|> refute_issues()
end
end
40 changes: 40 additions & 0 deletions test/credo/check/consistency/unused_variable_names_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -220,4 +220,44 @@ defmodule Credo.Check.Consistency.UnusedVariableNamesTest do
assert 3 == issue.line_no
end)
end

test "it should report a violation for naming schemes other than the forced one" do
[
"""
defmodule Credo.SampleOne do
defmodule Foo do
def bar(name, _) when is_binary(name) do
case name do
"foo" <> _name -> "FOO"
"bar" <> _name -> "BAR"
_name -> "DEFAULT"
end
end
end
end
""",
"""
defmodule Credo.SampleTwo do
defmodule Foo do
def bar(list) do
Enum.map(list, fn _item -> 1 end)
end
end
end
"""
]
|> to_source_files
|> run_check(@described_check, force: :anonymous)
|> assert_issues(fn issues ->
assert Enum.count(issues) == 4

assert Enum.any?(issues, fn issue ->
issue.trigger == "_name"
end)

assert Enum.any?(issues, fn issue ->
issue.trigger == "_item"
end)
end)
end
end
26 changes: 26 additions & 0 deletions test/credo/check/design/alias_usage_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,32 @@ defmodule Credo.Check.Design.AliasUsageTest do
|> refute_issues()
end

test "it should NOT report violation when module is included in only parameter but is also in excluded_lastnames" do
"""
defmodule Test do
def just_an_example do
Credo.Foo.Bar.call
end
end
"""
|> to_source_file
|> run_check(@described_check, only: ~r/^Credo.+$/, excluded_lastnames: ["Bar"])
|> refute_issues()
end

test "it should NOT report violation when module is covered in excluded_lastnames" do
"""
defmodule Test do
def just_an_example do
Credo.Foo.Bar.call
end
end
"""
|> to_source_file
|> run_check(@described_check, excluded_lastnames: ["Bar"])
|> refute_issues()
end

test "it should NOT report with __MODULE__" do
"""
defmodule Test do
Expand Down
34 changes: 29 additions & 5 deletions test/credo/check/design/tag_fixme_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -61,12 +61,12 @@ defmodule Credo.Check.Design.TagFIXMETest do
end

test "it should report a couple of issues" do
"""
~S'''
defmodule CredoSampleModule do
use ExUnit.Case # FIXME: this is the first
@moduledoc \"\"\"
@moduledoc """
this is an example # FIXME: and this is no actual comment
\"\"\"
"""
def some_fun do # FIXME this is the second
x = ~s{also: # FIXME: no comment here}
Expand All @@ -76,11 +76,35 @@ defmodule Credo.Check.Design.TagFIXMETest do
"also: # FIXME: no comment here as well"
end
end
"""
'''
|> to_source_file
|> run_check(@described_check)
|> assert_issues(fn issues ->
assert 3 == Enum.count(issues)
assert Enum.count(issues) == 3
end)
end

test "it should report a couple of issues when including docstrings" do
~S'''
defmodule CredoSampleModule do
use ExUnit.Case # FIXME: this is the first
@doc """
FIXME: and this is no actual comment
"""
def some_fun do # FIXME this is the second
x = ~s{also: # FIXME: no comment here}
assert 2 == x
?" # FIXME: this is the third
"also: # FIXME: no comment here as well"
end
end
'''
|> to_source_file
|> run_check(@described_check, include_doc: true)
|> assert_issues(fn issues ->
assert Enum.count(issues) == 4
end)
end
end
26 changes: 25 additions & 1 deletion test/credo/check/design/tag_todo_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -247,7 +247,31 @@ defmodule Credo.Check.Design.TagTODOTest do
|> to_source_file
|> run_check(@described_check)
|> assert_issues(fn issues ->
assert 3 == Enum.count(issues)
assert Enum.count(issues) == 3
end)
end

test "it should report a couple of issues when including docs" do
~S'''
defmodule CredoSampleModule do
use ExUnit.Case # TODO: this is the first
@doc """
TODO: and this is an actual TODO
"""
def some_fun do # TODO this is the second
x = ~s{also: # TODO: no comment here}
assert 2 == x
?" # TODO: this is the third
"also: # TODO: no comment here as well"
end
end
'''
|> to_source_file
|> run_check(@described_check, include_doc: true)
|> assert_issues(fn issues ->
assert Enum.count(issues) == 4
end)
end
end
11 changes: 7 additions & 4 deletions test/credo/check/housekeeping_params.exs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ defmodule Ast do
end
end

defmodule Credo.Check.HousekeepingHeredocsInTestsTest do
defmodule Credo.Check.HousekeepingParamsTest do
use Credo.Test.Case

require Ast
Expand All @@ -34,7 +34,9 @@ defmodule Credo.Check.HousekeepingHeredocsInTestsTest do
errors =
Path.join(__DIR__, "*/**/*_test.exs")
|> Path.wildcard()
|> Enum.reject(&String.match?(&1, ~r/(collector|helper)/))
|> Enum.reject(
&String.match?(&1, ~r/(collector|helper|duplicated_code|perceived_complexity)/)
)
|> Enum.map(&{&1, File.read!(&1)})
|> Enum.map(fn {filename, test_source} ->
check_filename =
Expand Down Expand Up @@ -68,10 +70,11 @@ defmodule Credo.Check.HousekeepingHeredocsInTestsTest do
end
end

untested_params = all_param_names -- tested_params
untested_params =
(all_param_names -- tested_params) -- Credo.Check.Params.builtin_param_names()

if check_source != "" && untested_params != [] do
"- #{Credo.Code.Module.name(check_ast)} - untested params: #{inspect(untested_params)}"
"- #{Path.relative_to_cwd(filename)}:1 - untested params: #{inspect(untested_params)}"
end
end)
|> Enum.reject(&is_nil/1)
Expand Down
36 changes: 36 additions & 0 deletions test/credo/check/readability/max_line_length_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,42 @@ defmodule Credo.Check.Readability.MaxLineLengthTest do
|> refute_issues()
end

test "it should NOT report a violation if heredocs are excluded" do
~S'''
defmodule CredoSampleModule do
use ExUnit.Case
def some_fun do
IO.puts 1
"""
long string, right? 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 == 2
"""
IO.puts 2
end
end
'''
|> to_source_file
|> run_check(@described_check, max_length: 80, ignore_heredocs: true)
|> refute_issues()
end

test "it should NOT report a violation if sigils are excluded" do
~S'''
defmodule CredoSampleModule do
use ExUnit.Case
def some_fun do
IO.puts 1
~s(long string, right? 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 == 2)
IO.puts 2
end
end
'''
|> to_source_file
|> run_check(@described_check, max_length: 80, ignore_sigils: true)
|> refute_issues()
end

test "it should NOT report a violation if strings are excluded for heredocs" do
"""
defmodule CredoSampleModule do
Expand Down
12 changes: 12 additions & 0 deletions test/credo/check/readability/module_doc_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -108,4 +108,16 @@ defmodule Credo.Check.Readability.ModuleDocTest do
assert issue.trigger == "Person"
end)
end

test "it should report controller submodules when the :ignore_names param says so" do
"""
defmodule MyApp.SomePhoenixController do
defmodule SubModule do
end
end
"""
|> to_source_file
|> run_check(@described_check, ignore_names: [])
|> assert_issues()
end
end
25 changes: 25 additions & 0 deletions test/credo/check/refactor/abc_size_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,31 @@ defmodule Credo.Check.Refactor.ABCSizeTest do
end)
end

test "it should NOT report count ecto functions when ecto functions are excluded via :excluded_functions" do
"""
defmodule CredoEctoQueryModule do
def foobar() do
Favorite
|> where(user_id: ^user.id)
|> join(:left, [f], t in Template, f.entity_id == t.id and f.entity_type == "template")
|> join(:left, [f, t], d in Document, f.entity_id == d.id and f.entity_type == "document")
|> join(:left, [f, t, d], dt in Template, dt.id == d.template_id)
|> join(:left, [f, t, d, dt], c in Category, c.id == t.category_id or c.id == dt.category_id)
|> select([f, t, d, dt, c], c)
|> distinct(true)
|> Repo.all()
end
end
"""
|> to_source_file
|> run_check(@described_check,
max_size: 3,
excluded_functions: ["where", "from", "select", "join"]
)
|> refute_issues()
end

#
# ABC size unit tests
#
Expand Down
23 changes: 23 additions & 0 deletions test/credo/check/refactor/nesting_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,29 @@ defmodule Credo.Check.Refactor.NestingTest do
|> refute_issues()
end

test "it should NOT report when :max_nesting is used properly" do
"""
defmodule CredoSampleModule do
defp foo() do
if true do
"flat"
else
if true do
if true do
if true do
"nested"
end
end
end
end
end
end
"""
|> to_source_file
|> run_check(@described_check, max_nesting: 4)
|> refute_issues()
end

#
# cases raising issues
#
Expand Down

0 comments on commit 38165e4

Please sign in to comment.