Merge pull request #327 from crystal-ameba/Sija/specs-cleanup

This commit is contained in:
Sijawusz Pur Rahnama 2022-12-22 12:08:44 +01:00 committed by GitHub
commit b9bc5aaab2
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
66 changed files with 354 additions and 841 deletions

View file

@ -34,7 +34,7 @@ module Ameba::Rule::Layout
end
context "properties" do
it "allows to configure max length of the line" do
it "#max_length" do
rule = LineLength.new
rule.max_length = long_line.size

View file

@ -5,7 +5,7 @@ module Ameba::Rule::Layout
describe TrailingWhitespace do
it "passes if all lines do not have trailing whitespace" do
expect_no_issues subject, "no-whispace"
expect_no_issues subject, "no-whitespace"
end
it "fails if there is a line with trailing whitespace" do
@ -15,16 +15,5 @@ module Ameba::Rule::Layout
expect_correction source, "whitespace at the end"
end
it "reports rule, pos and message" do
source = Source.new "a = 1\n b = 2 ", "source.cr"
subject.catch(source).should_not be_valid
issue = source.issues.first
issue.rule.should_not be_nil
issue.location.to_s.should eq "source.cr:2:7"
issue.end_location.to_s.should eq "source.cr:2:7"
issue.message.should eq "Trailing whitespace detected"
end
end
end

View file

@ -94,16 +94,6 @@ module Ameba::Rule::Lint
a
CRYSTAL
end
it "reports rule, pos and message" do
source = Source.new "a != true", "source.cr"
subject.catch(source)
issue = source.issues.first
issue.rule.should_not be_nil
issue.location.to_s.should eq "source.cr:1:1"
issue.message.should eq "Comparison to a boolean is pointless"
end
end
context "boolean on the left" do
@ -165,17 +155,6 @@ module Ameba::Rule::Lint
a
CRYSTAL
end
it "reports rule, pos and message" do
source = Source.new "true != a", "source.cr"
subject.catch(source).should_not be_valid
issue = source.issues.first
issue.rule.should_not be_nil
issue.location.to_s.should eq "source.cr:1:1"
issue.end_location.to_s.should eq "source.cr:1:9"
issue.message.should eq "Comparison to a boolean is pointless"
end
end
end
end

View file

@ -28,16 +28,5 @@ module Ameba::Rule::Lint
CRYSTAL
end
end
it "reports rule, pos and message" do
s = Source.new "pp! :foo", "source.cr"
subject.catch(s).should_not be_valid
issue = s.issues.first
issue.rule.should_not be_nil
issue.location.to_s.should eq "source.cr:1:1"
issue.end_location.to_s.should eq "source.cr:1:8"
issue.message.should eq "Possibly forgotten debug-related `pp!` call detected"
end
end
end

View file

@ -31,16 +31,5 @@ module Ameba::Rule::Lint
expect_no_corrections source
end
it "reports rule, pos and message" do
s = Source.new "debugger", "source.cr"
subject.catch(s).should_not be_valid
issue = s.issues.first
issue.rule.should_not be_nil
issue.location.to_s.should eq "source.cr:1:1"
issue.end_location.to_s.should eq "source.cr:1:8"
issue.message.should eq "Possible forgotten debugger statement detected"
end
end
end

View file

@ -22,28 +22,5 @@ module Ameba::Rule::Lint
expect_no_corrections source
end
it "reports rule, pos and message" do
source = Source.new %(
require "./thing"
require "./thing"
require "./another_thing"
require "./another_thing"
), "source.cr"
subject.catch(source).should_not be_valid
issue = source.issues.first
issue.rule.should_not be_nil
issue.location.to_s.should eq "source.cr:2:1"
issue.end_location.to_s.should eq ""
issue.message.should eq "Duplicated require of `./thing`"
issue = source.issues.last
issue.rule.should_not be_nil
issue.location.to_s.should eq "source.cr:4:1"
issue.end_location.to_s.should eq ""
issue.message.should eq "Duplicated require of `./another_thing`"
end
end
end

View file

@ -3,17 +3,17 @@ require "../../../spec_helper"
module Ameba
subject = Rule::Lint::EmptyExpression.new
def it_detects_empty_expression(code)
it "detects empty expression" do
private def it_detects_empty_expression(code, *, file = __FILE__, line = __LINE__)
it %(detects empty expression "#{code}"), file, line do
s = Source.new code
rule = Rule::Lint::EmptyExpression.new
rule.catch(s).should_not be_valid
rule.catch(s).should_not be_valid, file: file, line: line
end
end
describe Rule::Lint::EmptyExpression do
it "passes if there is no empty expression" do
s = Source.new %(
s = Source.new <<-CRYSTAL
def method()
end
@ -30,7 +30,7 @@ module Ameba
begin "" end
[nil] << nil
)
CRYSTAL
subject.catch(s).should be_valid
end
@ -110,18 +110,5 @@ module Ameba
)
subject.catch(s).should be_valid
end
it "reports rule, location and message" do
s = Source.new %(
if ()
end
), "source.cr"
subject.catch(s).should_not be_valid
issue = s.issues.first
issue.rule.should_not be_nil
issue.location.to_s.should eq "source.cr:1:4"
issue.end_location.to_s.should eq "source.cr:1:5"
issue.message.should eq "Avoid empty expressions"
end
end
end

View file

@ -64,21 +64,5 @@ module Ameba::Rule::Lint
end
CRYSTAL
end
it "reports rule, message and location" do
s = Source.new %(
a = 1
loop do
# comment goes here
end
), "source.cr"
subject.catch(s).should_not be_valid
s.issues.size.should eq 1
issue = s.issues.first
issue.rule.should_not be_nil
issue.location.to_s.should eq "source.cr:2:1"
issue.end_location.to_s.should eq "source.cr:4:3"
issue.message.should eq EmptyLoop::MSG
end
end
end

View file

@ -32,17 +32,5 @@ module Ameba::Rule::Lint
# ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ error: Duplicated keys in hash literal: "key1", "key2"
CRYSTAL
end
it "reports rule, location and message" do
s = Source.new %q(
h = {"a" => 1, "a" => 2}
), "source.cr"
subject.catch(s).should_not be_valid
issue = s.issues.first
issue.rule.should_not be_nil
issue.location.to_s.should eq "source.cr:1:5"
issue.end_location.to_s.should eq "source.cr:1:24"
issue.message.should eq %(Duplicated keys in hash literal: "a")
end
end
end

View file

@ -58,18 +58,5 @@ module Ameba::Rule::Lint
CRYSTAL
end
end
it "reports rule, pos and message" do
s = Source.new %(
"foo" == "foo"
), "source.cr"
subject.catch(s).should_not be_valid
issue = s.issues.first
issue.rule.should_not be_nil
issue.location.to_s.should eq "source.cr:1:1"
issue.end_location.to_s.should eq "source.cr:1:14"
issue.message.should eq "Comparison always evaluates to true"
end
end
end

View file

@ -58,18 +58,5 @@ module Ameba::Rule::Lint
CRYSTAL
end
end
it "reports rule, pos and message" do
s = Source.new %(
(1..3).index(1).not_nil!
), "source.cr"
subject.catch(s).should_not be_valid
issue = s.issues.first
issue.rule.should_not be_nil
issue.location.to_s.should eq "source.cr:1:8"
issue.end_location.to_s.should eq "source.cr:1:24"
issue.message.should eq "Use `index! {...}` instead of `index {...}.not_nil!`"
end
end
end

View file

@ -32,18 +32,5 @@ module Ameba::Rule::Lint
CRYSTAL
end
end
it "reports rule, pos and message" do
s = Source.new %(
(1..3).first?.not_nil!
), "source.cr"
subject.catch(s).should_not be_valid
issue = s.issues.first
issue.rule.should_not be_nil
issue.location.to_s.should eq "source.cr:1:15"
issue.end_location.to_s.should eq "source.cr:1:22"
issue.message.should eq "Avoid using `not_nil!`"
end
end
end

View file

@ -68,14 +68,14 @@ module Ameba::Rule::Lint
end
context "properties" do
it "allows to configure string_array_unwanted_symbols" do
it "#string_array_unwanted_symbols" do
rule = PercentArrays.new
rule.string_array_unwanted_symbols = ","
s = Source.new %( %w("one") )
rule.catch(s).should be_valid
end
it "allows to configure symbol_array_unwanted_symbols" do
it "#symbol_array_unwanted_symbols" do
rule = PercentArrays.new
rule.symbol_array_unwanted_symbols = ","
s = Source.new %( %i(:one) )

View file

@ -25,16 +25,5 @@ module Ameba::Rule::Lint
# ^^^^^ error: rand(1) always returns 0
CRYSTAL
end
it "reports rule, location and a message" do
s = Source.new "rand(1)", "source.cr"
subject.catch(s).should_not be_valid
issue = s.issues.first
issue.rule.should_not be_nil
issue.location.to_s.should eq "source.cr:1:1"
issue.end_location.to_s.should eq "source.cr:1:7"
issue.message.should eq "rand(1) always returns 0"
end
end
end

View file

@ -157,20 +157,6 @@ module Ameba::Rule::Lint
CRYSTAL
end
it "reports rule, location and message" do
source = Source.new %(
foo = 1
3.times { |foo| foo + 1 }
), "source.cr"
subject.catch(source).should_not be_valid
issue = source.issues.first
issue.rule.should_not be_nil
issue.location.to_s.should eq "source.cr:2:12"
issue.end_location.should be_nil
issue.message.should eq "Shadowing outer local variable `foo`"
end
context "macro" do
it "does not report shadowed vars in outer scope" do
expect_no_issues subject, <<-CRYSTAL

View file

@ -194,24 +194,5 @@ module Ameba::Rule::Lint
end
CRYSTAL
end
it "reports rule, location and message" do
s = Source.new %(
i = 0
while true
i += 1
spawn { i }
end
), "source.cr"
subject.catch(s).should_not be_valid
s.issues.size.should eq 1
issue = s.issues.first
issue.rule.should_not be_nil
issue.location.to_s.should eq "source.cr:4:11"
issue.end_location.to_s.should eq "source.cr:4:11"
issue.message.should eq "Shared variable `i` is used in fiber"
end
end
end

View file

@ -23,16 +23,6 @@ module Ameba::Rule::Lint
CRYSTAL
end
it "reports rule, location and message" do
s = Source.new "def hello end", "source.cr"
subject.catch(s).should_not be_valid
issue = s.issues.first
issue.rule.should_not be_nil
issue.location.to_s.should eq "source.cr:1:11"
issue.message.should match /unexpected token: "?end"? \(expected ["'];["'] or newline\)/
end
it "has highest severity" do
subject.severity.should eq Severity::Error
end

View file

@ -24,22 +24,5 @@ module Ameba::Rule::Lint
end
CRYSTAL
end
it "reports rule, location and message" do
s = Source.new %(
case
when String
puts "hello"
when can_generate?
generate if can_generate?
end
), "source.cr"
subject.catch(s).should_not be_valid
issue = s.issues.first
issue.rule.should_not be_nil
issue.location.to_s.should eq "source.cr:5:15"
issue.end_location.to_s.should eq "source.cr:5:27"
issue.message.should eq "Useless condition in when detected"
end
end
end

View file

@ -46,8 +46,8 @@ module Ameba::Rule::Performance
end
context "properties" do
it "allows to configure object_call_names" do
rule = Rule::Performance::AnyAfterFilter.new
it "#filter_names" do
rule = AnyAfterFilter.new
rule.filter_names = %w(select)
expect_no_issues rule, <<-CRYSTAL
@ -66,14 +66,5 @@ module Ameba::Rule::Performance
expect_no_corrections source
end
end
it "reports rule, pos and message" do
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

@ -42,18 +42,5 @@ module Ameba::Rule::Performance
CRYSTAL
end
end
it "reports rule, pos and message" do
source = Source.new path: "source.cr", code: %(
[1, 2, 3].any?
)
subject.catch(source).should_not be_valid
issue = source.issues.first
issue.rule.should_not be_nil
issue.location.to_s.should eq "source.cr:1:11"
issue.end_location.to_s.should eq "source.cr:1:14"
issue.message.should eq "Use `!{...}.empty?` instead of `{...}.any?`"
end
end
end

View file

@ -44,7 +44,7 @@ module Ameba::Rule::Performance
end
context "properties" do
it "allows to configure `call_names`" do
it "#call_names" do
rule = ChainedCallWithNoBang.new
rule.call_names = %w(uniq)
@ -54,22 +54,6 @@ module Ameba::Rule::Performance
end
end
it "reports rule, pos and message" do
source = Source.new path: "source.cr", code: <<-CODE
[1, 2, 3].select { |e| e > 1 }.reverse
CODE
subject.catch(source).should_not be_valid
source.issues.size.should eq 1
issue = source.issues.first
issue.rule.should_not be_nil
issue.location.to_s.should eq "source.cr:1:32"
issue.end_location.to_s.should eq "source.cr:1:38"
issue.message.should eq "Use bang method variant `reverse!` after chained `select` call"
end
context "macro" do
it "doesn't report in macro scope" do
expect_no_issues subject, <<-CRYSTAL

View file

@ -36,18 +36,5 @@ module Ameba::Rule::Performance
CRYSTAL
end
end
it "reports rule, pos and message" do
s = Source.new %(
(1..3).map(&.itself).compact
), "source.cr"
subject.catch(s).should_not be_valid
issue = s.issues.first
issue.rule.should_not be_nil
issue.location.to_s.should eq "source.cr:1:8"
issue.end_location.to_s.should eq "source.cr:1:29"
issue.message.should eq "Use `compact_map {...}` instead of `map {...}.compact`"
end
end
end

View file

@ -62,8 +62,8 @@ module Ameba::Rule::Performance
end
context "properties" do
it "allows to configure object_call_names" do
rule = Rule::Performance::FirstLastAfterFilter.new
it "#filter_names" do
rule = FirstLastAfterFilter.new
rule.filter_names = %w(reject)
expect_no_issues rule, <<-CRYSTAL
@ -72,21 +72,6 @@ module Ameba::Rule::Performance
end
end
it "reports rule, pos and message" do
s = Source.new %(
[1, 2, 3].select { |e| e > 2 }.first
), "source.cr"
subject.catch(s).should_not be_valid
s.issues.size.should eq 1
issue = s.issues.first
issue.rule.should_not be_nil
issue.location.to_s.should eq "source.cr:1:11"
issue.end_location.to_s.should eq "source.cr:1:37"
issue.message.should eq "Use `find {...}` instead of `select {...}.first`"
end
context "macro" do
it "doesn't report in macro scope" do
expect_no_issues subject, <<-CRYSTAL

View file

@ -30,18 +30,5 @@ module Ameba::Rule::Performance
CRYSTAL
end
end
it "reports rule, pos and message" do
s = Source.new %(
%w[Alice Bob].map(&.chars).flatten
), "source.cr"
subject.catch(s).should_not be_valid
issue = s.issues.first
issue.rule.should_not be_nil
issue.location.to_s.should eq "source.cr:1:15"
issue.end_location.to_s.should eq "source.cr:1:35"
issue.message.should eq "Use `flat_map {...}` instead of `map {...}.flatten`"
end
end
end

View file

@ -45,18 +45,5 @@ module Ameba::Rule::Performance
CRYSTAL
end
end
it "reports rule, pos and message" do
s = Source.new %(
(1..3).map(&.to_u64).sum
), "source.cr"
subject.catch(s).should_not be_valid
issue = s.issues.first
issue.rule.should_not be_nil
issue.location.to_s.should eq "source.cr:1:8"
issue.end_location.to_s.should eq "source.cr:1:25"
issue.message.should eq "Use `sum {...}` instead of `map {...}.sum`"
end
end
end

View file

@ -44,8 +44,8 @@ module Ameba::Rule::Performance
end
context "properties" do
it "allows to configure object caller names" do
rule = Rule::Performance::SizeAfterFilter.new
it "#filter_names" do
rule = SizeAfterFilter.new
rule.filter_names = %w(select)
expect_no_issues rule, <<-CRYSTAL
@ -61,18 +61,5 @@ module Ameba::Rule::Performance
CRYSTAL
end
end
it "reports rule, pos and message" do
s = Source.new %(
lines.split("\n").reject(&.empty?).size
), "source.cr"
subject.catch(s).should_not be_valid
issue = s.issues.first
issue.rule.should_not be_nil
issue.location.to_s.should eq "source.cr:2:4"
issue.end_location.to_s.should eq "source.cr:2:25"
issue.message.should eq "Use `count {...}` instead of `reject {...}.size`."
end
end
end

View file

@ -3,10 +3,10 @@ require "../../../spec_helper"
module Ameba
subject = Rule::Style::ConstantNames.new
private def it_reports_constant(name, value, expected)
it "reports constant name #{expected}" do
private def it_reports_constant(name, value, expected, *, file = __FILE__, line = __LINE__)
it "reports constant name #{expected}", file, line do
rule = Rule::Style::ConstantNames.new
expect_issue rule, <<-CRYSTAL, name: name
expect_issue rule, <<-CRYSTAL, name: name, file: file, line: line
%{name} = #{value}
# ^{name} error: Constant name should be screaming-cased: #{expected}, not #{name}
CRYSTAL
@ -17,7 +17,7 @@ module Ameba
it "passes if type names are screaming-cased" do
expect_no_issues subject, <<-CRYSTAL
LUCKY_NUMBERS = [3, 7, 11]
DOCUMENTATION_URL = "http://crystal-lang.org/docs"
DOCUMENTATION_URL = "https://crystal-lang.org/docs"
Int32
@ -37,19 +37,5 @@ module Ameba
# it_reports_constant "MyBadConstant", "1", "MYBADCONSTANT"
it_reports_constant "Wrong_NAME", "2", "WRONG_NAME"
it_reports_constant "Wrong_Name", "3", "WRONG_NAME"
it "reports rule, pos and message" do
s = Source.new %(
Const_Name = 1
), "source.cr"
subject.catch(s).should_not be_valid
issue = s.issues.first
issue.rule.should_not be_nil
issue.location.to_s.should eq "source.cr:1:1"
issue.end_location.to_s.should eq "source.cr:1:10"
issue.message.should eq(
"Constant name should be screaming-cased: CONST_NAME, not Const_Name"
)
end
end
end

View file

@ -3,11 +3,11 @@ require "../../../spec_helper"
module Ameba
subject = Rule::Style::GuardClause.new
def it_reports_body(body, *, line = __LINE__)
private def it_reports_body(body, *, file = __FILE__, line = __LINE__)
rule = Rule::Style::GuardClause.new
it "reports an issue if method body is if / unless without else" do
source = expect_issue rule, <<-CRYSTAL, line: line
it "reports an issue if method body is if / unless without else", file, line do
source = expect_issue rule, <<-CRYSTAL, file: file, line: line
def func
if something
# ^^ error: Use a guard clause (`return unless something`) instead of wrapping the code inside a conditional expression.
@ -23,7 +23,7 @@ module Ameba
end
CRYSTAL
expect_correction source, <<-CRYSTAL, line: line
expect_correction source, <<-CRYSTAL, file: file, line: line
def func
return unless something
#{body}
@ -38,8 +38,8 @@ module Ameba
CRYSTAL
end
it "reports an issue if method body ends with if / unless without else" do
source = expect_issue rule, <<-CRYSTAL, line: line
it "reports an issue if method body ends with if / unless without else", file, line do
source = expect_issue rule, <<-CRYSTAL, file: file, line: line
def func
test
if something
@ -57,7 +57,7 @@ module Ameba
end
CRYSTAL
expect_correction source, <<-CRYSTAL, line: line
expect_correction source, <<-CRYSTAL, file: file, line: line
def func
test
return unless something
@ -75,11 +75,11 @@ module Ameba
end
end
def it_reports_control_expression(kw, *, line = __LINE__)
private def it_reports_control_expression(kw, *, file = __FILE__, line = __LINE__)
rule = Rule::Style::GuardClause.new
it "reports an issue with #{kw} in the if branch" do
source = expect_issue rule, <<-CRYSTAL, line: line
it "reports an issue with #{kw} in the if branch", file, line do
source = expect_issue rule, <<-CRYSTAL, file: file, line: line
def func
if something
# ^^ error: Use a guard clause (`#{kw} if something`) instead of wrapping the code inside a conditional expression.
@ -90,11 +90,11 @@ module Ameba
end
CRYSTAL
expect_no_corrections source, line: line
expect_no_corrections source, file: file, line: line
end
it "reports an issue with #{kw} in the else branch" do
source = expect_issue rule, <<-CRYSTAL, line: line
it "reports an issue with #{kw} in the else branch", file, line do
source = expect_issue rule, <<-CRYSTAL, file: file, line: line
def func
if something
# ^^ error: Use a guard clause (`#{kw} unless something`) instead of wrapping the code inside a conditional expression.
@ -105,11 +105,11 @@ module Ameba
end
CRYSTAL
expect_no_corrections source, line: line
expect_no_corrections source, file: file, line: line
end
it "doesn't report an issue if condition has multiple lines" do
expect_no_issues rule, <<-CRYSTAL, line: line
it "doesn't report an issue if condition has multiple lines", file, line do
expect_no_issues rule, <<-CRYSTAL, file: file, line: line
def func
if something &&
something_else
@ -121,8 +121,8 @@ module Ameba
CRYSTAL
end
it "does not report an issue if #{kw} is inside elsif" do
expect_no_issues rule, <<-CRYSTAL, line: line
it "does not report an issue if #{kw} is inside elsif", file, line do
expect_no_issues rule, <<-CRYSTAL, file: file, line: line
def func
if something
a
@ -133,8 +133,8 @@ module Ameba
CRYSTAL
end
it "does not report an issue if #{kw} is inside if..elsif..else..end" do
expect_no_issues rule, <<-CRYSTAL, line: line
it "does not report an issue if #{kw} is inside if..elsif..else..end", file, line do
expect_no_issues rule, <<-CRYSTAL, file: file, line: line
def func
if something
a
@ -147,8 +147,8 @@ module Ameba
CRYSTAL
end
it "doesn't report an issue if control flow expr has multiple lines" do
expect_no_issues rule, <<-CRYSTAL, line: line
it "doesn't report an issue if control flow expr has multiple lines", file, line do
expect_no_issues rule, <<-CRYSTAL, file: file, line: line
def func
if something
#{kw} \\
@ -161,8 +161,8 @@ module Ameba
CRYSTAL
end
it "reports an issue if non-control-flow branch has multiple lines" do
source = expect_issue rule, <<-CRYSTAL, line: line
it "reports an issue if non-control-flow branch has multiple lines", file, line do
source = expect_issue rule, <<-CRYSTAL, file: file, line: line
def func
if something
# ^^ error: Use a guard clause (`#{kw} if something`) instead of wrapping the code inside a conditional expression.
@ -174,7 +174,7 @@ module Ameba
end
CRYSTAL
expect_no_corrections source, line: line
expect_no_corrections source, file: file, line: line
end
end

View file

@ -42,9 +42,10 @@ module Ameba::Rule::Style
end
context "properties" do
it "allows to configure filter_names" do
it "#filter_names" do
rule = IsAFilter.new
rule.filter_names = %w(select)
expect_no_issues rule, <<-CRYSTAL
[1, 2, nil].reject(&.nil?)
CRYSTAL
@ -58,20 +59,5 @@ module Ameba::Rule::Style
CRYSTAL
end
end
it "reports rule, pos and message" do
source = Source.new path: "source.cr", code: %(
[1, 2, nil].reject(&.nil?)
)
subject.catch(source).should_not be_valid
source.issues.size.should eq 1
issue = source.issues.first
issue.rule.should_not be_nil
issue.location.to_s.should eq "source.cr:1:13"
issue.end_location.to_s.should eq "source.cr:1:26"
issue.message.should eq "Use `reject(Nil)` instead of `reject {...}`"
end
end
end

View file

@ -34,19 +34,5 @@ module Ameba::Rule::Style
a.nil?
CRYSTAL
end
it "reports rule, location and message" do
s = Source.new %(
nil.is_a? Nil
), "source.cr"
subject.catch(s).should_not be_valid
s.issues.size.should eq 1
issue = s.issues.first
issue.rule.should_not be_nil
issue.location.to_s.should eq "source.cr:1:11"
issue.end_location.to_s.should eq "source.cr:1:13"
issue.message.should eq IsANil::MSG
end
end
end

View file

@ -3,12 +3,12 @@ require "../../../spec_helper"
module Ameba
subject = Rule::Style::LargeNumbers.new
private def it_transforms(number, expected)
it "transforms large number #{number}" do
private def it_transforms(number, expected, *, file = __FILE__, line = __LINE__)
it "transforms large number #{number}", file, line do
rule = Rule::Style::LargeNumbers.new
rule.int_min_digits = 5
source = expect_issue rule, <<-CRYSTAL, number: number
source = expect_issue rule, <<-CRYSTAL, number: number, file: file, line: line
number = %{number}
# ^{number} error: Large numbers should be written with underscores: #{expected}
CRYSTAL
@ -97,10 +97,12 @@ module Ameba
it_transforms "10000_i16", "10_000_i16"
it_transforms "10000_i32", "10_000_i32"
it_transforms "10000_i64", "10_000_i64"
it_transforms "10000_i128", "10_000_i128"
it_transforms "10000_u16", "10_000_u16"
it_transforms "10000_u32", "10_000_u32"
it_transforms "10000_u64", "10_000_u64"
it_transforms "10000_u128", "10_000_u128"
it_transforms "123456_f32", "123_456_f32"
it_transforms "123456_f64", "123_456_f64"
@ -117,20 +119,8 @@ module Ameba
it_transforms "3.001234", "3.001_234"
it_transforms "3.0012345", "3.001_234_5"
it "reports rule, pos and message" do
s = Source.new %q(
1200000
), "source.cr"
subject.catch(s).should_not be_valid
issue = s.issues.first
issue.rule.should_not be_nil
issue.location.to_s.should eq "source.cr:1:1"
issue.end_location.to_s.should eq "source.cr:1:7"
issue.message.should match /1_200_000/
end
context "properties" do
it "allows to configure integer min digits" do
it "#int_min_digits" do
rule = Rule::Style::LargeNumbers.new
rule.int_min_digits = 10
expect_no_issues rule, %q(1200000)

View file

@ -3,10 +3,10 @@ require "../../../spec_helper"
module Ameba
subject = Rule::Style::MethodNames.new
private def it_reports_method_name(name, expected)
it "reports method name #{expected}" do
private def it_reports_method_name(name, expected, *, file = __FILE__, line = __LINE__)
it "reports method name #{expected}", file, line do
rule = Rule::Style::MethodNames.new
expect_issue rule, <<-CRYSTAL, name: name
expect_issue rule, <<-CRYSTAL, name: name, file: file, line: line
def %{name}; end
# ^{name} error: Method name should be underscore-cased: #{expected}, not %{name}
CRYSTAL
@ -38,20 +38,5 @@ module Ameba
it_reports_method_name "firstName", "first_name"
it_reports_method_name "date_of_Birth", "date_of_birth"
it_reports_method_name "homepageURL", "homepage_url"
it "reports rule, pos and message" do
s = Source.new %(
def bad_Name(a)
end
), "source.cr"
subject.catch(s).should_not be_valid
issue = s.issues.first
issue.rule.should_not be_nil
issue.location.to_s.should eq "source.cr:1:5"
issue.end_location.to_s.should eq "source.cr:1:12"
issue.message.should eq(
"Method name should be underscore-cased: bad_name, not bad_Name"
)
end
end
end

View file

@ -53,16 +53,5 @@ module Ameba::Rule::Style
end
CRYSTAL
end
it "reports rule, pos and message" do
s = Source.new ":nok unless !s.empty?", "source.cr"
subject.catch(s).should_not be_valid
issue = s.issues.first
issue.rule.should_not be_nil
issue.location.to_s.should eq "source.cr:1:1"
issue.end_location.to_s.should eq "source.cr:1:21"
issue.message.should eq "Avoid negated conditions in unless blocks"
end
end
end

View file

@ -43,7 +43,7 @@ module Ameba::Rule::Style
end
context "properties" do
context "#exclude_ternary=" do
context "#exclude_ternary" do
it "skips ternary control expressions by default" do
expect_no_issues subject, <<-CRYSTAL
(foo > bar) ? true : false
@ -51,7 +51,7 @@ module Ameba::Rule::Style
end
it "allows to configure assignments" do
rule = Rule::Style::ParenthesesAroundCondition.new
rule = ParenthesesAroundCondition.new
rule.exclude_ternary = false
expect_issue rule, <<-CRYSTAL
@ -75,7 +75,7 @@ module Ameba::Rule::Style
end
end
context "#allow_safe_assignment=" do
context "#allow_safe_assignment" do
it "reports assignments by default" do
expect_issue subject, <<-CRYSTAL
if (foo = @foo)
@ -98,7 +98,7 @@ module Ameba::Rule::Style
end
it "allows to configure assignments" do
rule = Rule::Style::ParenthesesAroundCondition.new
rule = ParenthesesAroundCondition.new
rule.allow_safe_assignment = true
expect_issue rule, <<-CRYSTAL

View file

@ -27,24 +27,6 @@ module Ameba::Rule::Style
CRYSTAL
end
it "reports rule, pos and message" do
s = Source.new %q(
class Image
def is_valid?(x)
true
end
end
), "source.cr"
subject.catch(s).should_not be_valid
issue = s.issues.first
issue.rule.should_not be_nil
issue.location.to_s.should eq "source.cr:2:3"
issue.end_location.to_s.should eq "source.cr:4:5"
issue.message.should eq(
"Favour method name 'valid?' over 'is_valid?'")
end
it "ignores if alternative name isn't valid syntax" do
expect_no_issues subject, <<-CRYSTAL
class Image

View file

@ -294,24 +294,5 @@ module Ameba::Rule::Style
}
CRYSTAL
end
it "reports rule, pos and message" do
s = Source.new %q(
def method
begin
open_connection
ensure
close_connection
end
end
), "source.cr"
subject.catch(s).should_not be_valid
issue = s.issues.first
issue.rule.should_not be_nil
issue.location.to_s.should eq "source.cr:2:3"
issue.end_location.to_s.should eq "source.cr:2:7"
issue.message.should eq "Redundant `begin` block detected"
end
end
end

View file

@ -201,7 +201,7 @@ module Ameba::Rule::Style
end
context "properties" do
context "#allow_multi_next=" do
context "#allow_multi_next" do
it "allows multi next statements by default" do
expect_no_issues subject, <<-CRYSTAL
block do |a, b|
@ -211,7 +211,7 @@ module Ameba::Rule::Style
end
it "allows to configure multi next statements" do
rule = Rule::Style::RedundantNext.new
rule = RedundantNext.new
rule.allow_multi_next = false
source = expect_issue rule, <<-CRYSTAL
block do |a, b|
@ -238,7 +238,7 @@ module Ameba::Rule::Style
end
it "allows to configure empty next statements" do
rule = Rule::Style::RedundantNext.new
rule = RedundantNext.new
rule.allow_empty_next = false
source = expect_issue rule, <<-CRYSTAL
block do

View file

@ -284,7 +284,7 @@ module Ameba::Rule::Style
end
context "properties" do
context "#allow_multi_return=" do
context "#allow_multi_return" do
it "allows multi returns by default" do
expect_no_issues subject, <<-CRYSTAL
def method(a, b)
@ -294,7 +294,7 @@ module Ameba::Rule::Style
end
it "allows to configure multi returns" do
rule = Rule::Style::RedundantReturn.new
rule = RedundantReturn.new
rule.allow_multi_return = false
source = expect_issue rule, <<-CRYSTAL
def method(a, b)
@ -321,7 +321,7 @@ module Ameba::Rule::Style
end
it "allows to configure empty returns" do
rule = Rule::Style::RedundantReturn.new
rule = RedundantReturn.new
rule.allow_empty_return = false
source = expect_issue rule, <<-CRYSTAL
def method

View file

@ -3,10 +3,10 @@ require "../../../spec_helper"
module Ameba
subject = Rule::Style::TypeNames.new
private def it_reports_name(type, name, expected)
it "reports type name #{expected}" do
private def it_reports_name(type, name, expected, *, file = __FILE__, line = __LINE__)
it "reports type name #{expected}", file, line do
rule = Rule::Style::TypeNames.new
expect_issue rule, <<-CRYSTAL, type: type, name: name
expect_issue rule, <<-CRYSTAL, type: type, name: name, file: file, line: line
%{type} %{name}; end
# ^{type}^{name}^^^^ error: Type name should be camelcased: #{expected}, but it was %{name}
CRYSTAL
@ -49,20 +49,5 @@ module Ameba
# ^{} error: Type name should be camelcased: NumericValue, but it was Numeric_value
CRYSTAL
end
it "reports rule, pos and message" do
s = Source.new %(
class My_class
end
), "source.cr"
subject.catch(s).should_not be_valid
issue = s.issues.first
issue.rule.should_not be_nil
issue.location.to_s.should eq "source.cr:1:1"
issue.end_location.to_s.should eq "source.cr:2:3"
issue.message.should eq(
"Type name should be camelcased: MyClass, but it was My_class"
)
end
end
end

View file

@ -22,23 +22,5 @@ module Ameba::Rule::Style
end
CRYSTAL
end
it "reports rule, pos and message" do
s = Source.new %(
unless something
:one
else
:two
end
), "source.cr"
subject.catch(s).should_not be_valid
issue = s.issues.first
issue.should_not be_nil
issue.rule.should_not be_nil
issue.location.to_s.should eq "source.cr:1:1"
issue.end_location.to_s.should eq "source.cr:5:3"
issue.message.should eq "Favour if over unless with else"
end
end
end

View file

@ -3,10 +3,10 @@ require "../../../spec_helper"
module Ameba
subject = Rule::Style::VariableNames.new
private def it_reports_var_name(name, value, expected)
it "reports variable name #{expected}" do
private def it_reports_var_name(name, value, expected, *, file = __FILE__, line = __LINE__)
it "reports variable name #{expected}", file, line do
rule = Rule::Style::VariableNames.new
expect_issue rule, <<-CRYSTAL, name: name
expect_issue rule, <<-CRYSTAL, name: name, file: file, line: line
%{name} = #{value}
# ^{name} error: Var name should be underscore-cased: #{expected}, not %{name}
CRYSTAL
@ -62,19 +62,5 @@ module Ameba
end
CRYSTAL
end
it "reports rule, pos and message" do
s = Source.new %(
badName = "Yeah"
), "source.cr"
subject.catch(s).should_not be_valid
issue = s.issues.first
issue.rule.should_not be_nil
issue.location.to_s.should eq "source.cr:1:1"
issue.end_location.to_s.should eq "source.cr:1:7"
issue.message.should eq(
"Var name should be underscore-cased: bad_name, not badName"
)
end
end
end

View file

@ -64,10 +64,12 @@ module Ameba::Rule::Style
context "properties" do
it "#exclude_calls_with_block" do
rule = VerboseBlock.new
rule.exclude_calls_with_block = true
expect_no_issues rule, <<-CRYSTAL
(1..3).in_groups_of(1) { |i| i.map(&.to_s) }
CRYSTAL
rule.exclude_calls_with_block = false
source = expect_issue rule, <<-CRYSTAL
(1..3).in_groups_of(1) { |i| i.map(&.to_s) }
@ -81,12 +83,14 @@ module Ameba::Rule::Style
it "#exclude_multiple_line_blocks" do
rule = VerboseBlock.new
rule.exclude_multiple_line_blocks = true
expect_no_issues rule, <<-CRYSTAL
(1..3).any? do |i|
i.odd?
end
CRYSTAL
rule.exclude_multiple_line_blocks = false
source = expect_issue rule, <<-CRYSTAL
(1..3).any? do |i|
@ -102,12 +106,14 @@ module Ameba::Rule::Style
it "#exclude_prefix_operators" do
rule = VerboseBlock.new
rule.exclude_prefix_operators = true
expect_no_issues rule, <<-CRYSTAL
(1..3).sum { |i| +i }
(1..3).sum { |i| -i }
(1..3).sum { |i| ~i }
CRYSTAL
rule.exclude_prefix_operators = false
rule.exclude_operators = false
source = expect_issue rule, <<-CRYSTAL
@ -128,10 +134,12 @@ module Ameba::Rule::Style
it "#exclude_operators" do
rule = VerboseBlock.new
rule.exclude_operators = true
expect_no_issues rule, <<-CRYSTAL
(1..3).sum { |i| i * 2 }
CRYSTAL
rule.exclude_operators = false
source = expect_issue rule, <<-CRYSTAL
(1..3).sum { |i| i * 2 }
@ -145,10 +153,12 @@ module Ameba::Rule::Style
it "#exclude_setters" do
rule = VerboseBlock.new
rule.exclude_setters = true
expect_no_issues rule, <<-CRYSTAL
Char::Reader.new("abc").tap { |reader| reader.pos = 0 }
CRYSTAL
rule.exclude_setters = false
source = expect_issue rule, <<-CRYSTAL
Char::Reader.new("abc").tap { |reader| reader.pos = 0 }
@ -163,12 +173,14 @@ module Ameba::Rule::Style
it "#max_line_length" do
rule = VerboseBlock.new
rule.exclude_multiple_line_blocks = false
rule.max_line_length = 60
expect_no_issues rule, <<-CRYSTAL
(1..3).tap &.tap &.tap &.tap &.tap &.tap &.tap do |i|
i.to_s.reverse.strip.blank?
end
CRYSTAL
rule.max_line_length = nil
source = expect_issue rule, <<-CRYSTAL
(1..3).tap &.tap &.tap &.tap &.tap &.tap &.tap do |i|
@ -184,10 +196,12 @@ module Ameba::Rule::Style
it "#max_length" do
rule = VerboseBlock.new
rule.max_length = 30
expect_no_issues rule, <<-CRYSTAL
(1..3).tap { |i| i.to_s.split.reverse.join.strip.blank? }
CRYSTAL
rule.max_length = nil
source = expect_issue rule, <<-CRYSTAL
(1..3).tap { |i| i.to_s.split.reverse.join.strip.blank? }
@ -216,6 +230,7 @@ module Ameba::Rule::Style
it "reports call args and named_args" do
rule = VerboseBlock.new
rule.exclude_operators = false
source = expect_issue rule, <<-CRYSTAL
(1..3).map { |i| i.to_s[start: 0.to_i64, count: 3]? }
# ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ error: [...] `map(&.to_s.[start: 0.to_i64, count: 3]?)`
@ -258,20 +273,5 @@ module Ameba::Rule::Style
(1..3).join(separator: '.', &.to_s)
CRYSTAL
end
it "reports rule, pos and message" do
source = Source.new path: "source.cr", code: %(
(1..3).any? { |i| i.odd? }
)
subject.catch(source).should_not be_valid
source.issues.size.should eq 1
issue = source.issues.first
issue.rule.should_not be_nil
issue.location.to_s.should eq "source.cr:1:8"
issue.end_location.to_s.should eq "source.cr:1:26"
issue.message.should eq "Use short block notation instead: `any?(&.odd?)`"
end
end
end