Enhance spec helpers a bit in re: to the spec context

This commit is contained in:
Sijawusz Pur Rahnama 2022-12-21 17:23:56 +01:00
parent e9f3bbaeff
commit 1ba6fcb55c
8 changed files with 47 additions and 47 deletions

View file

@ -3,11 +3,11 @@ require "../../../spec_helper"
module Ameba module Ameba
subject = Rule::Lint::EmptyExpression.new subject = Rule::Lint::EmptyExpression.new
private def it_detects_empty_expression(code) private def it_detects_empty_expression(code, *, file = __FILE__, line = __LINE__)
it %(detects empty expression "#{code}") do it %(detects empty expression "#{code}"), file, line do
s = Source.new code s = Source.new code
rule = Rule::Lint::EmptyExpression.new 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
end end

View file

@ -3,10 +3,10 @@ require "../../../spec_helper"
module Ameba module Ameba
subject = Rule::Style::ConstantNames.new subject = Rule::Style::ConstantNames.new
private def it_reports_constant(name, value, expected) private def it_reports_constant(name, value, expected, *, file = __FILE__, line = __LINE__)
it "reports constant name #{expected}" do it "reports constant name #{expected}", file, line do
rule = Rule::Style::ConstantNames.new rule = Rule::Style::ConstantNames.new
expect_issue rule, <<-CRYSTAL, name: name expect_issue rule, <<-CRYSTAL, name: name, file: file, line: line
%{name} = #{value} %{name} = #{value}
# ^{name} error: Constant name should be screaming-cased: #{expected}, not #{name} # ^{name} error: Constant name should be screaming-cased: #{expected}, not #{name}
CRYSTAL CRYSTAL

View file

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

View file

@ -3,12 +3,12 @@ require "../../../spec_helper"
module Ameba module Ameba
subject = Rule::Style::LargeNumbers.new subject = Rule::Style::LargeNumbers.new
private def it_transforms(number, expected) private def it_transforms(number, expected, *, file = __FILE__, line = __LINE__)
it "transforms large number #{number}" do it "transforms large number #{number}", file, line do
rule = Rule::Style::LargeNumbers.new rule = Rule::Style::LargeNumbers.new
rule.int_min_digits = 5 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 = %{number}
# ^{number} error: Large numbers should be written with underscores: #{expected} # ^{number} error: Large numbers should be written with underscores: #{expected}
CRYSTAL CRYSTAL

View file

@ -3,10 +3,10 @@ require "../../../spec_helper"
module Ameba module Ameba
subject = Rule::Style::MethodNames.new subject = Rule::Style::MethodNames.new
private def it_reports_method_name(name, expected) private def it_reports_method_name(name, expected, *, file = __FILE__, line = __LINE__)
it "reports method name #{expected}" do it "reports method name #{expected}", file, line do
rule = Rule::Style::MethodNames.new rule = Rule::Style::MethodNames.new
expect_issue rule, <<-CRYSTAL, name: name expect_issue rule, <<-CRYSTAL, name: name, file: file, line: line
def %{name}; end def %{name}; end
# ^{name} error: Method name should be underscore-cased: #{expected}, not %{name} # ^{name} error: Method name should be underscore-cased: #{expected}, not %{name}
CRYSTAL CRYSTAL

View file

@ -3,10 +3,10 @@ require "../../../spec_helper"
module Ameba module Ameba
subject = Rule::Style::TypeNames.new subject = Rule::Style::TypeNames.new
private def it_reports_name(type, name, expected) private def it_reports_name(type, name, expected, *, file = __FILE__, line = __LINE__)
it "reports type name #{expected}" do it "reports type name #{expected}", file, line do
rule = Rule::Style::TypeNames.new 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}; end
# ^{type}^{name}^^^^ error: Type name should be camelcased: #{expected}, but it was %{name} # ^{type}^{name}^^^^ error: Type name should be camelcased: #{expected}, but it was %{name}
CRYSTAL CRYSTAL

View file

@ -3,10 +3,10 @@ require "../../../spec_helper"
module Ameba module Ameba
subject = Rule::Style::VariableNames.new subject = Rule::Style::VariableNames.new
private def it_reports_var_name(name, value, expected) private def it_reports_var_name(name, value, expected, *, file = __FILE__, line = __LINE__)
it "reports variable name #{expected}" do it "reports variable name #{expected}", file, line do
rule = Rule::Style::VariableNames.new rule = Rule::Style::VariableNames.new
expect_issue rule, <<-CRYSTAL, name: name expect_issue rule, <<-CRYSTAL, name: name, file: file, line: line
%{name} = #{value} %{name} = #{value}
# ^{name} error: Var name should be underscore-cased: #{expected}, not %{name} # ^{name} error: Var name should be underscore-cased: #{expected}, not %{name}
CRYSTAL CRYSTAL

View file

@ -1,13 +1,13 @@
require "../spec_helper" require "../spec_helper"
module Ameba module Ameba
private def it_tokenizes(str, expected) private def it_tokenizes(str, expected, *, file = __FILE__, line = __LINE__)
it "tokenizes #{str}" do it "tokenizes #{str}", file, line do
([] of String).tap do |token_types| %w[].tap do |token_types|
Tokenizer.new(Source.new str, normalize: false) Tokenizer.new(Source.new str, normalize: false)
.run { |token| token_types << token.type.to_s } .run { |token| token_types << token.type.to_s }
.should be_true .should be_true
end.should eq expected end.should eq(expected), file: file, line: line
end end
end end