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

View File

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

View File

@ -20,7 +20,7 @@ module Ameba
describe Rule::Style::LargeNumbers 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
16 17 18 19 20 30 40 50 60 70 80 90
100
@ -41,7 +41,7 @@ module Ameba
900_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
141_592_654
@ -80,8 +80,7 @@ module Ameba
1200.0
1200.01
1200.012
)
subject.catch(s).should be_valid
CRYSTAL
end
it_transforms "10000", "10_000"
@ -131,10 +130,9 @@ module Ameba
context "properties" do
it "allows to configure integer min digits" do
s = Source.new %q(1200000)
rule = Rule::Style::LargeNumbers.new
rule.int_min_digits = 10
rule.catch(s).should be_valid
expect_no_issues rule, %q(1200000)
end
end
end

View File

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

View File

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

View File

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

View File

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