-
Notifications
You must be signed in to change notification settings - Fork 36
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
208 additions
and
43 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
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
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
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,114 @@ | ||
defmodule Styler.Speedo.ReadabilityTest do | ||
use Styler.SpeedoCase | ||
|
||
alias Credo.Check.Readability.FunctionNames | ||
alias Credo.Check.Readability.ImplTrue | ||
alias Credo.Check.Readability.ModuleAttributeNames | ||
alias Credo.Check.Readability.ModuleNames | ||
alias Credo.Check.Readability.PredicateFunctionNames | ||
alias Credo.Check.Readability.StringSigils | ||
alias Credo.Check.Readability.VariableNames | ||
alias Credo.Check.Readability.WithCustomTaggedTuple | ||
|
||
describe "Credo.Check.Readability.FunctionNames" do | ||
test "positives" do | ||
for def <- ~w(def defp defmacro defmacrop defguard defguardp) do | ||
assert_error "#{def} ooF", FunctionNames | ||
end | ||
end | ||
|
||
test "negatives" do | ||
for def <- ~w(def defp defmacro defmacrop defguard defguardp) do | ||
refute_errors "#{def} snake(), do: :ok" | ||
refute_errors "#{def} snake_case(), do: :ok" | ||
end | ||
|
||
refute_errors "def unquote(foo)(), do: :ok" | ||
end | ||
end | ||
|
||
describe "Credo.Check.Readability.ImplTrue" do | ||
test "this one's pretty simple ngl" do | ||
assert_error "defmodule Foo do @impl true end", ImplTrue | ||
refute_errors "defmodule Foo do @impl Blue end" | ||
end | ||
end | ||
|
||
describe "Credo.Check.Readability.ModuleAttributeNames" do | ||
test "snake s'il vous plaît" do | ||
assert_error "defmodule Foo do @weEeEe :foo end", ModuleAttributeNames | ||
refute_errors "defmodule Foo do @snake :always_snakey end" | ||
refute_errors "defmodule Foo do bar = @wEeEeE end" | ||
end | ||
end | ||
|
||
describe "Credo.Check.Readability.ModuleNames" do | ||
test "pascal por favor" do | ||
assert_error "defmodule Snake_Kinda do end", ModuleNames | ||
refute_errors "defmodule OkayName do end" | ||
refute_errors "defmodule Okay.Name do end" | ||
end | ||
end | ||
|
||
describe "Credo.Check.Readability.PredicateFunctionNames" do | ||
test "defs dont get `is_` prefix" do | ||
for def <- ~w(def defp) do | ||
assert_error "#{def} is_foo?", PredicateFunctionNames | ||
assert_error "#{def} is_foo?(bar)", PredicateFunctionNames | ||
assert_error "#{def} is_foo?(bar) when bar != :baz, do: :bop", PredicateFunctionNames | ||
refute_errors "#{def} foo?" | ||
refute_errors "#{def} foo?(bar)" | ||
refute_errors "#{def} foo?(bar) when bar != :baz, do: :bop" | ||
end | ||
end | ||
|
||
test "macros and guards dont get `?` suffix" do | ||
for def <- ~w(defmacro defmacrop defguard defguardp) do | ||
assert_error "#{def} is_foo?", PredicateFunctionNames | ||
assert_error "#{def} is_foo?(bar)", PredicateFunctionNames | ||
assert_error "#{def} is_foo?(bar) when bar != :baz, do: :bop", PredicateFunctionNames | ||
refute_errors "#{def} is_foo" | ||
refute_errors "#{def} is_foo(bar)" | ||
refute_errors "#{def} is_foo(bar) when bar != :baz, do: :bop" | ||
end | ||
end | ||
end | ||
|
||
describe "Credo.Check.Readability.StringSigils" do | ||
test "3 escaped quotes tops" do | ||
assert_error ~s|x = "\\"1\\"2\\"3\\"4"|, StringSigils | ||
refute_errors ~s|x = ~s{"1"2"3"4}| | ||
refute_errors ~s|x = "\\"1\\"2\\"3"| | ||
end | ||
end | ||
|
||
describe "Credo.Check.Readability.VariableNames" do | ||
test "reports violations on variable creation only " do | ||
errors = | ||
lint(""" | ||
def foo(badName) do | ||
with {:ok, anotherBadName} <- badName do | ||
[stopCamelCase | camelsTail] = anotherBadName | ||
lovely_var_name = functionNotYourFault() | ||
end | ||
end | ||
""") | ||
|
||
assert Enum.count(errors) == 4 | ||
|
||
errors = errors |> Enum.group_by(& &1.line) |> Map.new() | ||
|
||
assert [%{check: VariableNames, message: "badName"}] = errors[1] | ||
assert [%{check: VariableNames, message: "anotherBadName"}] = errors[2] | ||
assert [one, two] = errors[3] | ||
assert "stopCamelCase" in [one.message, two.message] | ||
assert "camelsTail" in [one.message, two.message] | ||
end | ||
end | ||
|
||
describe "Credo.Check.Readability.WithCustomTaggedTuple" do | ||
test "shames tagged tuples" do | ||
assert_error "with {:ooph, result} <- {:ooph, call()}, do: :ok", WithCustomTaggedTuple | ||
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,50 @@ | ||
# Copyright 2023 Adobe. All rights reserved. | ||
# This file is licensed to you under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. You may obtain a copy | ||
# of the License at http://www.apache.org/licenses/LICENSE-2.0 | ||
|
||
# Unless required by applicable law or agreed to in writing, software distributed under | ||
# the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS | ||
# OF ANY KIND, either express or implied. See the License for the specific language | ||
# governing permissions and limitations under the License. | ||
|
||
defmodule Styler.SpeedoCase do | ||
@moduledoc """ | ||
Helpers around testing Style rules. | ||
""" | ||
use ExUnit.CaseTemplate | ||
|
||
using do | ||
quote do | ||
import unquote(__MODULE__), only: [assert_error: 2, refute_errors: 1, lint: 1] | ||
end | ||
end | ||
|
||
defmacro assert_error(code, check) do | ||
quote location: :keep, bind_quoted: [code: code, check: check] do | ||
errors = lint(code) | ||
|
||
if Enum.empty?(errors) and ExUnit.configuration()[:trace] do | ||
{ast, comments} = Styler.string_to_quoted_with_comments(code) | ||
dbg(ast) | ||
dbg(comments) | ||
end | ||
|
||
assert [%{check: ^check, message: m, line: l, file: f}] = errors | ||
assert m, "message was nil for #{check} in #{inspect(errors)}" | ||
assert l, "line was nil for #{check} in #{inspect(errors)}" | ||
assert f, "file was nil for #{check} in #{inspect(errors)}" | ||
end | ||
end | ||
|
||
def refute_errors(code) do | ||
assert [] = lint(code) | ||
end | ||
|
||
def lint(code) do | ||
code | ||
|> Styler.lint() | ||
|> List.flatten() | ||
|> Enum.reject(&is_nil/1) | ||
end | ||
end |