Convert `Style/VariableNames` spec

This commit is contained in:
fn ⌃ ⌥ 2021-10-27 16:13:29 -07:00
parent 265bc51481
commit e93dfe9cdc
2 changed files with 40 additions and 20 deletions

View File

@ -3,17 +3,19 @@ require "../../../spec_helper"
module Ameba module Ameba
subject = Rule::Style::VariableNames.new subject = Rule::Style::VariableNames.new
private def it_reports_var_name(code, expected) private def it_reports_var_name(name, value, expected)
it "reports method name #{expected}" do it "reports variable name #{expected}" do
s = Source.new code rule = Rule::Style::VariableNames.new
Rule::Style::VariableNames.new.catch(s).should_not be_valid expect_issue rule, <<-CRYSTAL, name: name
s.issues.first.message.should contain expected %{name} = #{value}
# ^{name} error: Var name should be underscore-cased: #{expected}, not %{name}
CRYSTAL
end end
end end
describe Rule::Style::VariableNames do describe Rule::Style::VariableNames do
it "passes if var names are underscore-cased" do it "passes if var names are underscore-cased" do
s = Source.new %( expect_no_issues subject, <<-CRYSTAL
class Greeting class Greeting
@@default_greeting = "Hello world" @@default_greeting = "Hello world"
@ -25,25 +27,31 @@ module Ameba
puts greeting puts greeting
end end
end end
) CRYSTAL
subject.catch(s).should be_valid
end end
it_reports_var_name %(myBadNamedVar = 1), "my_bad_named_var" it_reports_var_name "myBadNamedVar", "1", "my_bad_named_var"
it_reports_var_name %(wrong_Name = 'y'), "wrong_name" it_reports_var_name "wrong_Name", "'y'", "wrong_name"
it_reports_var_name %( it "reports instance variable name" do
class Greeting expect_issue subject, <<-CRYSTAL
def initialize(@badNamed = nil) class Greeting
def initialize(@badNamed = nil)
# ^ error: Var name should be underscore-cased: @bad_named, not @badNamed
# ^ error: Var name should be underscore-cased: bad_named, not badNamed
end
end end
end CRYSTAL
), "bad_named" end
it_reports_var_name %( it "reports class variable name" do
class Greeting expect_issue subject, <<-CRYSTAL
@@defaultGreeting = "Hello world" class Greeting
end @@defaultGreeting = "Hello world"
), "default_greeting" # ^^^^^^^^^^^^^^^^^ error: Var name should be underscore-cased: @@default_greeting, not @@defaultGreeting
end
CRYSTAL
end
it "reports rule, pos and message" do it "reports rule, pos and message" do
s = Source.new %( s = Source.new %(

View File

@ -35,6 +35,18 @@ module Ameba::Rule::Style
issue_for node, MSG % {expected, node.name} issue_for node, MSG % {expected, node.name}
end end
# TODO: Handle special case where instance variable is method parameter.
# For example, this:
#
# def initialize(@foo)
# end
#
# is represented in the AST as:
#
# def initialize(foo)
# @foo = foo
# end
def test(source, node : Crystal::Var) def test(source, node : Crystal::Var)
check_node source, node check_node source, node
end end