Remove normalize parameter from expect_issue (#249)

* Add `normalize` parameter to `expect_correction`

* Convert Style/IsAFilter spec

* Revert "Add `normalize` parameter to `expect_correction`"

This reverts commit 4b67e4b900.

* Remove `normalize` parameter from `expect_issue`

* Require indentation if multiple issues on a single line

* Update `Style/IsAFilter` spec

* Update `ExpectIssue` documentation

* Add missing `expect_no_corrections`

* Use carets and space with issues at column 1 or 2

* Update `expect_issue` docs
This commit is contained in:
fn ⌃ ⌥ 2021-11-06 06:15:19 -07:00 committed by GitHub
parent 7cb0c15747
commit 7b437fbd2f
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
9 changed files with 182 additions and 103 deletions

View file

@ -5,7 +5,7 @@ module Ameba::Rule::Layout
describe TrailingBlankLines do
it "passes if there is a blank line at the end of a source" do
expect_no_issues subject, "a = 1\n", normalize: false
expect_no_issues subject, "a = 1\n"
end
it "passes if source is empty" do
@ -18,12 +18,12 @@ module Ameba::Rule::Layout
end
it "fails if there more then one blank line at the end of a source" do
source = expect_issue subject, "a = 1\n \n # error: Excessive trailing newline detected", normalize: false
source = expect_issue subject, "a = 1\n \n # error: Excessive trailing newline detected"
expect_no_corrections source
end
it "fails if last line is not blank" do
source = expect_issue subject, "\n\n\n puts 22 # error: Trailing newline missing", normalize: false
source = expect_issue subject, "\n\n\n puts 22 # error: Trailing newline missing"
expect_correction source, "\n\n\n puts 22\n"
end

View file

@ -5,7 +5,7 @@ module Ameba::Rule::Lint
describe DebuggerStatement do
it "passes if there is no debugger statement" do
expect_no_issues subject, %(
expect_no_issues subject, <<-CRYSTAL
"this is not a debugger statement"
s = "debugger"
@ -18,16 +18,18 @@ module Ameba::Rule::Lint
end
end
A.new.debugger
)
CRYSTAL
end
it "fails if there is a debugger statement" do
expect_issue subject, %(
source = expect_issue subject, <<-CRYSTAL
a = 2
debugger
# ^{} error: Possible forgotten debugger statement detected
# ^^^^^^ error: Possible forgotten debugger statement detected
a = a + 1
)
CRYSTAL
expect_no_corrections source
end
it "reports rule, pos and message" do

View file

@ -5,20 +5,22 @@ module Ameba::Rule::Lint
describe DuplicatedRequire do
it "passes if there are no duplicated requires" do
expect_no_issues subject, %(
expect_no_issues subject, <<-CRYSTAL
require "math"
require "big"
require "big/big_decimal"
)
CRYSTAL
end
it "reports if there are a duplicated requires" do
expect_issue subject, %(
source = expect_issue subject, <<-CRYSTAL
require "big"
require "math"
require "big"
# ^{} error: Duplicated require of `big`
)
CRYSTAL
expect_no_corrections source
end
it "reports rule, pos and message" do

View file

@ -5,18 +5,18 @@ module Ameba::Rule::Lint
subject = SharedVarInFiber.new
it "doesn't report if there is only local shared var in fiber" do
expect_no_issues subject, %(
expect_no_issues subject, <<-CRYSTAL
spawn do
i = 1
puts i
end
Fiber.yield
)
CRYSTAL
end
it "doesn't report if there is only block shared var in fiber" do
expect_no_issues subject, %(
expect_no_issues subject, <<-CRYSTAL
10.times do |i|
spawn do
puts i
@ -24,11 +24,11 @@ module Ameba::Rule::Lint
end
Fiber.yield
)
CRYSTAL
end
it "doesn't report if there a spawn macro is used" do
expect_no_issues subject, %(
expect_no_issues subject, <<-CRYSTAL
i = 0
while i < 10
spawn puts(i)
@ -36,11 +36,11 @@ module Ameba::Rule::Lint
end
Fiber.yield
)
CRYSTAL
end
it "reports if there is a shared var in spawn" do
expect_issue subject, %(
source = expect_issue subject, <<-CRYSTAL
i = 0
while i < 10
spawn do
@ -51,11 +51,13 @@ module Ameba::Rule::Lint
end
Fiber.yield
)
CRYSTAL
expect_no_corrections source
end
it "reports reassigned reference to shared var in spawn" do
expect_issue subject, %(
source = expect_issue subject, <<-CRYSTAL
channel = Channel(String).new
n = 0
@ -67,11 +69,13 @@ module Ameba::Rule::Lint
channel.send m
end
end
)
CRYSTAL
expect_no_corrections source
end
it "doesn't report reassigned reference to shared var in block" do
expect_no_issues subject, %(
expect_no_issues subject, <<-CRYSTAL
channel = Channel(String).new
n = 0
@ -82,21 +86,21 @@ module Ameba::Rule::Lint
channel.send m
end
end
)
CRYSTAL
end
it "does not report block is called in a spawn" do
expect_no_issues subject, %(
expect_no_issues subject, <<-CRYSTAL
def method(block)
spawn do
block.call(10)
end
end
)
CRYSTAL
end
it "reports multiple shared variables in spawn" do
expect_issue subject, %(
source = expect_issue subject, <<-CRYSTAL
foo, bar, baz = 0, 0, 0
while foo < 10
baz += 1
@ -109,11 +113,13 @@ module Ameba::Rule::Lint
end
foo += 1
end
)
CRYSTAL
expect_no_corrections source
end
it "doesn't report if variable is passed to the proc" do
expect_no_issues subject, %(
expect_no_issues subject, <<-CRYSTAL
i = 0
while i < 10
proc = ->(x : Int32) do
@ -124,19 +130,19 @@ module Ameba::Rule::Lint
proc.call(i)
i += 1
end
)
CRYSTAL
end
it "doesn't report if a channel is declared in outer scope" do
expect_no_issues subject, %(
expect_no_issues subject, <<-CRYSTAL
channel = Channel(Nil).new
spawn { channel.send(nil) }
channel.receive
)
CRYSTAL
end
it "doesn't report if there is a loop in spawn" do
expect_no_issues subject, %(
expect_no_issues subject, <<-CRYSTAL
channel = Channel(String).new
spawn do
@ -146,47 +152,47 @@ module Ameba::Rule::Lint
channel.send(line)
end
end
)
CRYSTAL
end
it "doesn't report if a var is mutated in spawn and referenced outside" do
expect_no_issues subject, %(
expect_no_issues subject, <<-CRYSTAL
def method
foo = 1
spawn { foo = 2 }
foo
end
)
CRYSTAL
end
it "doesn't report if variable is changed without iterations" do
expect_no_issues subject, %(
expect_no_issues subject, <<-CRYSTAL
def foo
i = 0
i += 1
spawn { i }
end
)
CRYSTAL
end
it "doesn't report if variable is in a loop inside spawn" do
expect_no_issues subject, %(
expect_no_issues subject, <<-CRYSTAL
i = 0
spawn do
while i < 10
i += 1
end
end
)
CRYSTAL
end
it "doesn't report if variable declared inside loop" do
expect_no_issues subject, %(
expect_no_issues subject, <<-CRYSTAL
while true
i = 0
spawn { i += 1 }
end
)
CRYSTAL
end
it "reports rule, location and message" do

View file

@ -5,66 +5,74 @@ module Ameba::Rule::Performance
describe AnyAfterFilter do
it "passes if there is no potential performance improvements" do
expect_no_issues subject, %(
expect_no_issues subject, <<-CRYSTAL
[1, 2, 3].select { |e| e > 1 }.any?(&.zero?)
[1, 2, 3].reject { |e| e > 1 }.any?(&.zero?)
[1, 2, 3].select { |e| e > 1 }
[1, 2, 3].reject { |e| e > 1 }
[1, 2, 3].any? { |e| e > 1 }
)
CRYSTAL
end
it "reports if there is select followed by any? without a block" do
expect_issue subject, %(
source = expect_issue subject, <<-CRYSTAL
[1, 2, 3].select { |e| e > 2 }.any?
# ^^^^^^^^^^^^^^^^^^^^^^^^^^ error: Use `any? {...}` instead of `select {...}.any?`
)
CRYSTAL
expect_no_corrections source
end
it "does not report if source is a spec" do
expect_no_issues subject, %(
expect_no_issues subject, <<-CRYSTAL, "source_spec.cr"
[1, 2, 3].select { |e| e > 2 }.any?
), "source_spec.cr"
CRYSTAL
end
it "reports if there is reject followed by any? without a block" do
expect_issue subject, %(
source = expect_issue subject, <<-CRYSTAL
[1, 2, 3].reject { |e| e > 2 }.any?
# ^^^^^^^^^^^^^^^^^^^^^^^^^^ error: Use `any? {...}` instead of `reject {...}.any?`
)
CRYSTAL
expect_no_corrections source
end
it "does not report if any? calls contains a block" do
expect_no_issues subject, %(
expect_no_issues subject, <<-CRYSTAL
[1, 2, 3].select { |e| e > 2 }.any?(&.zero?)
[1, 2, 3].reject { |e| e > 2 }.any?(&.zero?)
)
CRYSTAL
end
context "properties" do
it "allows to configure object_call_names" do
rule = Rule::Performance::AnyAfterFilter.new
rule.filter_names = %w(select)
expect_no_issues rule, %(
expect_no_issues rule, <<-CRYSTAL
[1, 2, 3].reject { |e| e > 2 }.any?
)
CRYSTAL
end
end
context "macro" do
it "reports in macro scope" do
expect_issue subject, %(
source = expect_issue subject, <<-CRYSTAL
{{ [1, 2, 3].reject { |e| e > 2 }.any? }}
# ^^^^^^^^^^^^^^^^^^^^^^^^^^^ error: Use `any? {...}` instead of `reject {...}.any?`
)
CRYSTAL
expect_no_corrections source
end
end
it "reports rule, pos and message" do
expect_issue subject, %(
source = expect_issue subject, <<-CRYSTAL
[1, 2, 3].reject { |e| e > 2 }.any?
# ^^^^^^^^^^^^^^^^^^^^^^^^^^ error: Use `any? {...}` instead of `reject {...}.any?`
)
CRYSTAL
expect_no_corrections source
end
end
end

View file

@ -5,53 +5,57 @@ module Ameba::Rule::Style
describe IsAFilter do
it "passes if there is no potential performance improvements" do
source = Source.new %(
expect_no_issues subject, <<-CRYSTAL
[1, 2, nil].select(Int32)
[1, 2, nil].reject(Nil)
)
subject.catch(source).should be_valid
CRYSTAL
end
it "reports if there is .is_a? call within select" do
source = Source.new %(
source = expect_issue subject, <<-CRYSTAL
[1, 2, nil].select(&.is_a?(Int32))
)
subject.catch(source).should_not be_valid
# ^^^^^^^^^^^^^^^^^^^^^^ error: Use `select(Int32)` instead of `select {...}`
CRYSTAL
expect_correction source, <<-CRYSTAL
[1, 2, nil].select(Int32)
CRYSTAL
end
it "reports if there is .nil? call within reject" do
source = Source.new %(
source = expect_issue subject, <<-CRYSTAL
[1, 2, nil].reject(&.nil?)
)
subject.catch(source).should_not be_valid
# ^^^^^^^^^^^^^^ error: Use `reject(Nil)` instead of `reject {...}`
CRYSTAL
expect_correction source, <<-CRYSTAL
[1, 2, nil].reject(Nil)
CRYSTAL
end
it "does not report if there .is_a? call within block with multiple arguments" do
source = Source.new %(
expect_no_issues subject, <<-CRYSTAL
t.all? { |_, v| v.is_a?(String) }
t.all? { |foo, bar| foo.is_a?(String) }
t.all? { |foo, bar| bar.is_a?(String) }
)
subject.catch(source).should be_valid
CRYSTAL
end
context "properties" do
it "allows to configure filter_names" do
source = Source.new %(
[1, 2, nil].reject(&.nil?)
)
rule = IsAFilter.new
rule.filter_names = %w(select)
rule.catch(source).should be_valid
expect_no_issues rule, <<-CRYSTAL
[1, 2, nil].reject(&.nil?)
CRYSTAL
end
end
context "macro" do
it "doesn't report in macro scope" do
source = Source.new %(
expect_no_issues subject, <<-CRYSTAL
{{ [1, 2, nil].reject(&.nil?) }}
)
subject.catch(source).should be_valid
CRYSTAL
end
end