Convert remaining Style specs

This commit is contained in:
fn ⌃ ⌥ 2021-10-27 15:58:58 -07:00
parent 72a3487bb6
commit b32b00adad
7 changed files with 85 additions and 93 deletions

View file

@ -3,17 +3,19 @@ require "../../../spec_helper"
module Ameba module Ameba
subject = Rule::Style::ConstantNames.new subject = Rule::Style::ConstantNames.new
private def it_reports_constant(code, expected) private def it_reports_constant(name, value, expected)
it "reports constant name #{expected}" do it "reports constant name #{expected}" do
s = Source.new code rule = Rule::Style::ConstantNames.new
Rule::Style::ConstantNames.new.catch(s).should_not be_valid expect_issue rule, <<-CRYSTAL, name: name
s.issues.first.message.should contain expected %{name} = #{value}
# ^{name} error: Constant name should be screaming-cased: #{expected}, not #{name}
CRYSTAL
end end
end end
describe Rule::Style::ConstantNames do describe Rule::Style::ConstantNames do
it "passes if type names are screaming-cased" do it "passes if type names are screaming-cased" do
s = Source.new %( expect_no_issues subject, <<-CRYSTAL
LUCKY_NUMBERS = [3, 7, 11] LUCKY_NUMBERS = [3, 7, 11]
DOCUMENTATION_URL = "http://crystal-lang.org/docs" DOCUMENTATION_URL = "http://crystal-lang.org/docs"
@ -29,13 +31,12 @@ module Ameba
a = 1 a = 1
myVar = 2 myVar = 2
m_var = 3 m_var = 3
) CRYSTAL
subject.catch(s).should be_valid
end end
# it_reports_constant "MyBadConstant=1", "MYBADCONSTANT" # it_reports_constant "MyBadConstant", "1", "MYBADCONSTANT"
it_reports_constant "Wrong_NAME=2", "WRONG_NAME" it_reports_constant "Wrong_NAME", "2", "WRONG_NAME"
it_reports_constant "Wrong_Name=3", "WRONG_NAME" it_reports_constant "Wrong_Name", "3", "WRONG_NAME"
it "reports rule, pos and message" do it "reports rule, pos and message" do
s = Source.new %( s = Source.new %(

View file

@ -5,27 +5,26 @@ module Ameba::Rule::Style
subject = IsANil.new subject = IsANil.new
it "doesn't report if there are no is_a?(Nil) calls" do it "doesn't report if there are no is_a?(Nil) calls" do
s = Source.new %( expect_no_issues subject, <<-CRYSTAL
a = 1 a = 1
a.nil? a.nil?
a.is_a?(NilLiteral) a.is_a?(NilLiteral)
a.is_a?(Custom::Nil) a.is_a?(Custom::Nil)
) CRYSTAL
subject.catch(s).should be_valid
end end
it "reports if there is a call to is_a?(Nil) without receiver" do it "reports if there is a call to is_a?(Nil) without receiver" do
s = Source.new %( expect_issue subject, <<-CRYSTAL
is_a?(Nil) a = is_a?(Nil)
) # ^^^ error: Use `nil?` instead of `is_a?(Nil)`
subject.catch(s).should_not be_valid CRYSTAL
end end
it "reports if there is a call to is_a?(Nil) with receiver" do it "reports if there is a call to is_a?(Nil) with receiver" do
s = Source.new %( expect_issue subject, <<-CRYSTAL
a.is_a?(Nil) a.is_a?(Nil)
) # ^^^ error: Use `nil?` instead of `is_a?(Nil)`
subject.catch(s).should_not be_valid CRYSTAL
end end
it "reports rule, location and message" do it "reports rule, location and message" do

View file

@ -20,7 +20,7 @@ module Ameba
describe Rule::Style::LargeNumbers do describe Rule::Style::LargeNumbers do
it "passes if large number does not require underscore" do it "passes if large number does not require underscore" do
s = Source.new %q( expect_no_issues subject, <<-CRYSTAL
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
16 17 18 19 20 30 40 50 60 70 80 90 16 17 18 19 20 30 40 50 60 70 80 90
100 100
@ -41,7 +41,7 @@ module Ameba
900_000 900_000
1_000_000 1_000_000
-9_223_372_036_854_775_808 -9_223_372_036_854_775_808
9_223_372_036_854_775_807 9_223_372_036_854_775_807
141_592_654 141_592_654
@ -80,8 +80,7 @@ module Ameba
1200.0 1200.0
1200.01 1200.01
1200.012 1200.012
) CRYSTAL
subject.catch(s).should be_valid
end end
it_transforms "10000", "10_000" it_transforms "10000", "10_000"
@ -131,10 +130,9 @@ module Ameba
context "properties" do context "properties" do
it "allows to configure integer min digits" do it "allows to configure integer min digits" do
s = Source.new %q(1200000)
rule = Rule::Style::LargeNumbers.new rule = Rule::Style::LargeNumbers.new
rule.int_min_digits = 10 rule.int_min_digits = 10
rule.catch(s).should be_valid expect_no_issues rule, %q(1200000)
end end
end end
end end

View file

@ -3,17 +3,19 @@ require "../../../spec_helper"
module Ameba module Ameba
subject = Rule::Style::MethodNames.new subject = Rule::Style::MethodNames.new
private def it_reports_method_name(code, expected) private def it_reports_method_name(name, expected)
it "reports method name #{expected}" do it "reports method name #{expected}" do
s = Source.new code rule = Rule::Style::MethodNames.new
Rule::Style::MethodNames.new.catch(s).should_not be_valid expect_issue rule, <<-CRYSTAL, name: name
s.issues.first.message.should contain expected def %{name}; end
# ^{name} error: Method name should be underscore-cased: #{expected}, not %{name}
CRYSTAL
end end
end end
describe Rule::Style::MethodNames do describe Rule::Style::MethodNames do
it "passes if method names are underscore-cased" do it "passes if method names are underscore-cased" do
s = Source.new %( expect_no_issues subject, <<-CRYSTAL
class Person class Person
def first_name def first_name
end end
@ -30,13 +32,12 @@ module Ameba
def name def name
end end
end end
) CRYSTAL
subject.catch(s).should be_valid
end end
it_reports_method_name %(def firstName; end), "first_name" it_reports_method_name "firstName", "first_name"
it_reports_method_name %(def date_of_Birth; end), "date_of_birth" it_reports_method_name "date_of_Birth", "date_of_birth"
it_reports_method_name %(def homepageURL; end), "homepage_url" it_reports_method_name "homepageURL", "homepage_url"
it "reports rule, pos and message" do it "reports rule, pos and message" do
s = Source.new %( s = Source.new %(

View file

@ -5,7 +5,7 @@ module Ameba::Rule::Style
describe NegatedConditionsInUnless do describe NegatedConditionsInUnless do
it "passes with a unless without negated condition" do it "passes with a unless without negated condition" do
s = Source.new %( expect_no_issues subject, <<-CRYSTAL
unless a unless a
:ok :ok
end end
@ -15,44 +15,43 @@ module Ameba::Rule::Style
unless s.empty? unless s.empty?
:ok :ok
end end
) CRYSTAL
subject.catch(s).should be_valid
end end
it "fails if there is a negated condition in unless" do it "fails if there is a negated condition in unless" do
s = Source.new %( expect_issue subject, <<-CRYSTAL
unless !a unless !a
# ^^^^^^^ error: Avoid negated conditions in unless blocks
:nok :nok
end end
) CRYSTAL
subject.catch(s).should_not be_valid
end end
it "fails if one of AND conditions is negated" do it "fails if one of AND conditions is negated" do
s = Source.new %( expect_issue subject, <<-CRYSTAL
unless a && !b unless a && !b
# ^^^^^^^^^^^^ error: Avoid negated conditions in unless blocks
:nok :nok
end end
) CRYSTAL
subject.catch(s).should_not be_valid
end end
it "fails if one of OR conditions is negated" do it "fails if one of OR conditions is negated" do
s = Source.new %( expect_issue subject, <<-CRYSTAL
unless a || !b unless a || !b
# ^^^^^^^^^^^^ error: Avoid negated conditions in unless blocks
:nok :nok
end end
) CRYSTAL
subject.catch(s).should_not be_valid
end end
it "fails if one of inner conditions is negated" do it "fails if one of inner conditions is negated" do
s = Source.new %( expect_issue subject, <<-CRYSTAL
unless a && (b || !c) unless a && (b || !c)
# ^^^^^^^^^^^^^^^^^^^ error: Avoid negated conditions in unless blocks
:nok :nok
end end
) CRYSTAL
subject.catch(s).should_not be_valid
end end
it "reports rule, pos and message" do it "reports rule, pos and message" do

View file

@ -5,7 +5,7 @@ module Ameba::Rule::Style
describe PredicateName do describe PredicateName do
it "passes if predicate name is correct" do it "passes if predicate name is correct" do
s = Source.new %q( expect_no_issues subject, <<-CRYSTAL
def valid?(x) def valid?(x)
end end
@ -16,16 +16,15 @@ module Ameba::Rule::Style
def allow_this_picture? def allow_this_picture?
end end
) CRYSTAL
subject.catch(s).should be_valid
end end
it "fails if predicate name is wrong" do it "fails if predicate name is wrong" do
s = Source.new %q( expect_issue subject, <<-CRYSTAL
def is_valid?(x) def is_valid?(x)
# ^^^^^^^^^^^^^^ error: Favour method name 'valid?' over 'is_valid?'
end end
) CRYSTAL
subject.catch(s).should_not be_valid
end end
it "reports rule, pos and message" do it "reports rule, pos and message" do
@ -47,14 +46,13 @@ module Ameba::Rule::Style
end end
it "ignores if alternative name isn't valid syntax" do it "ignores if alternative name isn't valid syntax" do
s = Source.new %q( expect_no_issues subject, <<-CRYSTAL
class Image class Image
def is_404?(x) def is_404?(x)
true true
end end
end end
) CRYSTAL
subject.catch(s).should be_valid
end end
end end
end end

View file

@ -5,7 +5,7 @@ module Ameba::Rule::Style
subject = RedundantBegin.new subject = RedundantBegin.new
it "passes if there is no redundant begin blocks" do it "passes if there is no redundant begin blocks" do
s = Source.new %( expect_no_issues subject, <<-CRYSTAL
def method def method
do_something do_something
rescue rescue
@ -27,12 +27,11 @@ module Ameba::Rule::Style
def method; end def method; end
def method; a = 1; rescue; end def method; a = 1; rescue; end
def method; begin; rescue; end; end def method; begin; rescue; end; end
) CRYSTAL
subject.catch(s).should be_valid
end end
it "passes if there is a correct begin block in a handler" do it "passes if there is a correct begin block in a handler" do
s = Source.new %q( expect_no_issues subject, <<-CRYSTAL
def handler_and_expression def handler_and_expression
begin begin
open_file open_file
@ -80,13 +79,13 @@ module Ameba::Rule::Style
end end
expr expr
end end
) CRYSTAL
subject.catch(s).should be_valid
end end
it "fails if there is a redundant begin block" do it "fails if there is a redundant begin block" do
s = Source.new %q( expect_issue subject, <<-CRYSTAL
def method(a : String) : String def method(a : String) : String
# ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ error: Redundant `begin` block detected
begin begin
open_file open_file
do_some_stuff do_some_stuff
@ -94,39 +93,39 @@ module Ameba::Rule::Style
close_file close_file
end end
end end
) CRYSTAL
subject.catch(s).should_not be_valid
end end
it "fails if there is a redundant begin block in a method without args" do it "fails if there is a redundant begin block in a method without args" do
s = Source.new %q( expect_issue subject, <<-CRYSTAL
def method def method
# ^^^^^^^^ error: Redundant `begin` block detected
begin begin
open_file open_file
ensure ensure
close_file close_file
end end
end end
) CRYSTAL
subject.catch(s).should_not be_valid
end end
it "fails if there is a redundant block in a method with return type" do it "fails if there is a redundant block in a method with return type" do
s = Source.new %q( expect_issue subject, <<-CRYSTAL
def method : String def method : String
# ^^^^^^^^^^^^^^^^^ error: Redundant `begin` block detected
begin begin
open_file open_file
ensure ensure
close_file close_file
end end
end end
) CRYSTAL
subject.catch(s).should_not be_valid
end end
it "fails if there is a redundant block in a method with multiple args" do it "fails if there is a redundant block in a method with multiple args" do
s = Source.new %q( expect_issue subject, <<-CRYSTAL
def method(a : String, def method(a : String,
# ^^^^^^^^^^^^^^^^^^^^ error: Redundant `begin` block detected
b : String) b : String)
begin begin
open_file open_file
@ -134,13 +133,13 @@ module Ameba::Rule::Style
close_file close_file
end end
end end
) CRYSTAL
subject.catch(s).should_not be_valid
end end
it "fails if there is a redundant block in a method with multiple args" do it "fails if there is a redundant block in a method with multiple args" do
s = Source.new %q( expect_issue subject, <<-CRYSTAL
def method(a : String, def method(a : String,
# ^^^^^^^^^^^^^^^^^^^^ error: Redundant `begin` block detected
b : String b : String
) )
begin begin
@ -149,12 +148,11 @@ module Ameba::Rule::Style
close_file close_file
end end
end end
) CRYSTAL
subject.catch(s).should_not be_valid
end end
it "doesn't report if there is an inner redundant block" do it "doesn't report if there is an inner redundant block" do
s = Source.new %q( expect_no_issues subject, <<-CRYSTAL
def method def method
begin begin
open_file open_file
@ -163,37 +161,36 @@ module Ameba::Rule::Style
end end
rescue rescue
end end
) CRYSTAL
subject.catch(s).should be_valid
end end
it "fails if there is a redundant block with yield" do it "fails if there is a redundant block with yield" do
s = Source.new %q( expect_issue subject, <<-CRYSTAL
def method def method
# ^^^^^^^^ error: Redundant `begin` block detected
begin begin
yield yield
ensure ensure
close_file close_file
end end
end end
) CRYSTAL
subject.catch(s).should_not be_valid
end end
it "fails if there is top level redundant block in a method" do it "fails if there is top level redundant block in a method" do
s = Source.new %q( expect_issue subject, <<-CRYSTAL
def method def method
# ^^^^^^^^ error: Redundant `begin` block detected
begin begin
a = 1 a = 1
b = 2 b = 2
end end
end end
) CRYSTAL
subject.catch(s).should_not be_valid
end end
it "doesn't report if begin-end block in a proc literal" do it "doesn't report if begin-end block in a proc literal" do
s = Source.new %q( expect_no_issues subject, <<-CRYSTAL
foo = ->{ foo = ->{
begin begin
raise "Foo!" raise "Foo!"
@ -201,8 +198,7 @@ module Ameba::Rule::Style
pp ex pp ex
end end
} }
) CRYSTAL
subject.catch(s).should be_valid
end end
it "reports rule, pos and message" do it "reports rule, pos and message" do