From e93dfe9cdc8735bcb79e6d9486809eb35087f8c6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?fn=20=E2=8C=83=20=E2=8C=A5?= <70830482+FnControlOption@users.noreply.github.com> Date: Wed, 27 Oct 2021 16:13:29 -0700 Subject: [PATCH] Convert `Style/VariableNames` spec --- spec/ameba/rule/style/variable_names_spec.cr | 48 ++++++++++++-------- src/ameba/rule/style/variable_names.cr | 12 +++++ 2 files changed, 40 insertions(+), 20 deletions(-) diff --git a/spec/ameba/rule/style/variable_names_spec.cr b/spec/ameba/rule/style/variable_names_spec.cr index 6ec6ae9f..f150e8a9 100644 --- a/spec/ameba/rule/style/variable_names_spec.cr +++ b/spec/ameba/rule/style/variable_names_spec.cr @@ -3,17 +3,19 @@ require "../../../spec_helper" module Ameba subject = Rule::Style::VariableNames.new - private def it_reports_var_name(code, expected) - it "reports method name #{expected}" do - s = Source.new code - Rule::Style::VariableNames.new.catch(s).should_not be_valid - s.issues.first.message.should contain expected + private def it_reports_var_name(name, value, expected) + it "reports variable name #{expected}" do + rule = Rule::Style::VariableNames.new + expect_issue rule, <<-CRYSTAL, name: name + %{name} = #{value} + # ^{name} error: Var name should be underscore-cased: #{expected}, not %{name} + CRYSTAL end end describe Rule::Style::VariableNames do it "passes if var names are underscore-cased" do - s = Source.new %( + expect_no_issues subject, <<-CRYSTAL class Greeting @@default_greeting = "Hello world" @@ -25,25 +27,31 @@ module Ameba puts greeting end end - ) - subject.catch(s).should be_valid + CRYSTAL end - it_reports_var_name %(myBadNamedVar = 1), "my_bad_named_var" - it_reports_var_name %(wrong_Name = 'y'), "wrong_name" + it_reports_var_name "myBadNamedVar", "1", "my_bad_named_var" + it_reports_var_name "wrong_Name", "'y'", "wrong_name" - it_reports_var_name %( - class Greeting - def initialize(@badNamed = nil) + it "reports instance variable name" do + expect_issue subject, <<-CRYSTAL + 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 - ), "bad_named" + CRYSTAL + end - it_reports_var_name %( - class Greeting - @@defaultGreeting = "Hello world" - end - ), "default_greeting" + it "reports class variable name" do + expect_issue subject, <<-CRYSTAL + class Greeting + @@defaultGreeting = "Hello world" + # ^^^^^^^^^^^^^^^^^ error: Var name should be underscore-cased: @@default_greeting, not @@defaultGreeting + end + CRYSTAL + end it "reports rule, pos and message" do s = Source.new %( diff --git a/src/ameba/rule/style/variable_names.cr b/src/ameba/rule/style/variable_names.cr index e8b999c1..48d89805 100644 --- a/src/ameba/rule/style/variable_names.cr +++ b/src/ameba/rule/style/variable_names.cr @@ -35,6 +35,18 @@ module Ameba::Rule::Style issue_for node, MSG % {expected, node.name} 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) check_node source, node end