mirror of
				https://gitea.invidious.io/iv-org/shard-ameba.git
				synced 2024-08-15 00:53:29 +00:00 
			
		
		
		
	Group naming-related rules
This commit is contained in:
		
							parent
							
								
									0c6745781e
								
							
						
					
					
						commit
						b25dc402c8
					
				
					 14 changed files with 28 additions and 27 deletions
				
			
		| 
						 | 
				
			
			@ -1,41 +0,0 @@
 | 
			
		|||
require "../../../spec_helper"
 | 
			
		||||
 | 
			
		||||
module Ameba
 | 
			
		||||
  subject = Rule::Style::ConstantNames.new
 | 
			
		||||
 | 
			
		||||
  private def it_reports_constant(name, value, expected, *, file = __FILE__, line = __LINE__)
 | 
			
		||||
    it "reports constant name #{expected}", file, line do
 | 
			
		||||
      rule = Rule::Style::ConstantNames.new
 | 
			
		||||
      expect_issue rule, <<-CRYSTAL, name: name, file: file, line: line
 | 
			
		||||
          %{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
 | 
			
		||||
      expect_no_issues subject, <<-CRYSTAL
 | 
			
		||||
        LUCKY_NUMBERS     = [3, 7, 11]
 | 
			
		||||
        DOCUMENTATION_URL = "https://crystal-lang.org/docs"
 | 
			
		||||
 | 
			
		||||
        Int32
 | 
			
		||||
 | 
			
		||||
        s : String = "str"
 | 
			
		||||
 | 
			
		||||
        def works(n : Int32)
 | 
			
		||||
        end
 | 
			
		||||
 | 
			
		||||
        Log = ::Log.for("db")
 | 
			
		||||
 | 
			
		||||
        a = 1
 | 
			
		||||
        myVar = 2
 | 
			
		||||
        m_var = 3
 | 
			
		||||
        CRYSTAL
 | 
			
		||||
    end
 | 
			
		||||
 | 
			
		||||
    # it_reports_constant "MyBadConstant", "1", "MYBADCONSTANT"
 | 
			
		||||
    it_reports_constant "Wrong_NAME", "2", "WRONG_NAME"
 | 
			
		||||
    it_reports_constant "Wrong_Name", "3", "WRONG_NAME"
 | 
			
		||||
  end
 | 
			
		||||
end
 | 
			
		||||
| 
						 | 
				
			
			@ -1,42 +0,0 @@
 | 
			
		|||
require "../../../spec_helper"
 | 
			
		||||
 | 
			
		||||
module Ameba
 | 
			
		||||
  subject = Rule::Style::MethodNames.new
 | 
			
		||||
 | 
			
		||||
  private def it_reports_method_name(name, expected, *, file = __FILE__, line = __LINE__)
 | 
			
		||||
    it "reports method name #{expected}", file, line do
 | 
			
		||||
      rule = Rule::Style::MethodNames.new
 | 
			
		||||
      expect_issue rule, <<-CRYSTAL, name: name, file: file, line: line
 | 
			
		||||
        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
 | 
			
		||||
      expect_no_issues subject, <<-CRYSTAL
 | 
			
		||||
        class Person
 | 
			
		||||
          def first_name
 | 
			
		||||
          end
 | 
			
		||||
 | 
			
		||||
          def date_of_birth
 | 
			
		||||
          end
 | 
			
		||||
 | 
			
		||||
          def homepage_url
 | 
			
		||||
          end
 | 
			
		||||
 | 
			
		||||
          def valid?
 | 
			
		||||
          end
 | 
			
		||||
 | 
			
		||||
          def name
 | 
			
		||||
          end
 | 
			
		||||
        end
 | 
			
		||||
        CRYSTAL
 | 
			
		||||
    end
 | 
			
		||||
 | 
			
		||||
    it_reports_method_name "firstName", "first_name"
 | 
			
		||||
    it_reports_method_name "date_of_Birth", "date_of_birth"
 | 
			
		||||
    it_reports_method_name "homepageURL", "homepage_url"
 | 
			
		||||
  end
 | 
			
		||||
end
 | 
			
		||||
| 
						 | 
				
			
			@ -1,40 +0,0 @@
 | 
			
		|||
require "../../../spec_helper"
 | 
			
		||||
 | 
			
		||||
module Ameba::Rule::Style
 | 
			
		||||
  subject = PredicateName.new
 | 
			
		||||
 | 
			
		||||
  describe PredicateName do
 | 
			
		||||
    it "passes if predicate name is correct" do
 | 
			
		||||
      expect_no_issues subject, <<-CRYSTAL
 | 
			
		||||
        def valid?(x)
 | 
			
		||||
        end
 | 
			
		||||
 | 
			
		||||
        class Image
 | 
			
		||||
          def picture?(x)
 | 
			
		||||
          end
 | 
			
		||||
        end
 | 
			
		||||
 | 
			
		||||
        def allow_this_picture?
 | 
			
		||||
        end
 | 
			
		||||
        CRYSTAL
 | 
			
		||||
    end
 | 
			
		||||
 | 
			
		||||
    it "fails if predicate name is wrong" do
 | 
			
		||||
      expect_issue subject, <<-CRYSTAL
 | 
			
		||||
        def is_valid?(x)
 | 
			
		||||
        # ^^^^^^^^^^^^^^ error: Favour method name 'valid?' over 'is_valid?'
 | 
			
		||||
        end
 | 
			
		||||
        CRYSTAL
 | 
			
		||||
    end
 | 
			
		||||
 | 
			
		||||
    it "ignores if alternative name isn't valid syntax" do
 | 
			
		||||
      expect_no_issues subject, <<-CRYSTAL
 | 
			
		||||
        class Image
 | 
			
		||||
          def is_404?(x)
 | 
			
		||||
            true
 | 
			
		||||
          end
 | 
			
		||||
        end
 | 
			
		||||
        CRYSTAL
 | 
			
		||||
    end
 | 
			
		||||
  end
 | 
			
		||||
end
 | 
			
		||||
| 
						 | 
				
			
			@ -1,72 +0,0 @@
 | 
			
		|||
require "../../../spec_helper"
 | 
			
		||||
 | 
			
		||||
module Ameba::Rule::Style
 | 
			
		||||
  subject = QueryBoolMethods.new
 | 
			
		||||
 | 
			
		||||
  describe QueryBoolMethods do
 | 
			
		||||
    it "passes for valid cases" do
 | 
			
		||||
      expect_no_issues subject, <<-CRYSTAL
 | 
			
		||||
        class Foo
 | 
			
		||||
          class_property? foo = true
 | 
			
		||||
          property? foo = true
 | 
			
		||||
          property foo2 : Bool? = true
 | 
			
		||||
          setter panda = true
 | 
			
		||||
        end
 | 
			
		||||
 | 
			
		||||
        module Bar
 | 
			
		||||
          class_getter? bar : Bool = true
 | 
			
		||||
          getter? bar : Bool
 | 
			
		||||
          getter bar2 : Bool? = true
 | 
			
		||||
          setter panda : Bool = true
 | 
			
		||||
 | 
			
		||||
          def initialize(@bar = true)
 | 
			
		||||
          end
 | 
			
		||||
        end
 | 
			
		||||
        CRYSTAL
 | 
			
		||||
    end
 | 
			
		||||
 | 
			
		||||
    it "reports only valid properties" do
 | 
			
		||||
      expect_issue subject, <<-CRYSTAL
 | 
			
		||||
        class Foo
 | 
			
		||||
          class_property? foo = true
 | 
			
		||||
          class_property bar = true
 | 
			
		||||
                       # ^^^ error: Consider using 'class_property?' for 'bar'
 | 
			
		||||
          class_property baz = true
 | 
			
		||||
                       # ^^^ error: Consider using 'class_property?' for 'baz'
 | 
			
		||||
        end
 | 
			
		||||
        CRYSTAL
 | 
			
		||||
    end
 | 
			
		||||
 | 
			
		||||
    {% for call in %w[getter class_getter property class_property] %}
 | 
			
		||||
      it "reports `{{ call.id }}` assign with Bool" do
 | 
			
		||||
        expect_issue subject, <<-CRYSTAL, call: {{ call }}
 | 
			
		||||
          class Foo
 | 
			
		||||
            %{call}   foo = true
 | 
			
		||||
            _{call} # ^^^ error: Consider using '%{call}?' for 'foo'
 | 
			
		||||
          end
 | 
			
		||||
          CRYSTAL
 | 
			
		||||
      end
 | 
			
		||||
 | 
			
		||||
      it "reports `{{ call.id }}` type declaration assign with Bool" do
 | 
			
		||||
        expect_issue subject, <<-CRYSTAL, call: {{ call }}
 | 
			
		||||
          class Foo
 | 
			
		||||
            %{call}   foo : Bool = true
 | 
			
		||||
            _{call} # ^^^ error: Consider using '%{call}?' for 'foo'
 | 
			
		||||
          end
 | 
			
		||||
          CRYSTAL
 | 
			
		||||
      end
 | 
			
		||||
 | 
			
		||||
      it "reports `{{ call.id }}` type declaration with Bool" do
 | 
			
		||||
        expect_issue subject, <<-CRYSTAL, call: {{ call }}
 | 
			
		||||
          class Foo
 | 
			
		||||
            %{call}   foo : Bool
 | 
			
		||||
            _{call} # ^^^ error: Consider using '%{call}?' for 'foo'
 | 
			
		||||
 | 
			
		||||
            def initialize(@foo = true)
 | 
			
		||||
            end
 | 
			
		||||
          end
 | 
			
		||||
          CRYSTAL
 | 
			
		||||
      end
 | 
			
		||||
    {% end %}
 | 
			
		||||
  end
 | 
			
		||||
end
 | 
			
		||||
| 
						 | 
				
			
			@ -1,53 +0,0 @@
 | 
			
		|||
require "../../../spec_helper"
 | 
			
		||||
 | 
			
		||||
module Ameba
 | 
			
		||||
  subject = Rule::Style::TypeNames.new
 | 
			
		||||
 | 
			
		||||
  private def it_reports_name(type, name, expected, *, file = __FILE__, line = __LINE__)
 | 
			
		||||
    it "reports type name #{expected}", file, line do
 | 
			
		||||
      rule = Rule::Style::TypeNames.new
 | 
			
		||||
      expect_issue rule, <<-CRYSTAL, type: type, name: name, file: file, line: line
 | 
			
		||||
        %{type} %{name}; end
 | 
			
		||||
        # ^{type}^{name}^^^^ error: Type name should be camelcased: #{expected}, but it was %{name}
 | 
			
		||||
        CRYSTAL
 | 
			
		||||
    end
 | 
			
		||||
  end
 | 
			
		||||
 | 
			
		||||
  describe Rule::Style::TypeNames do
 | 
			
		||||
    it "passes if type names are camelcased" do
 | 
			
		||||
      expect_no_issues subject, <<-CRYSTAL
 | 
			
		||||
        class ParseError < Exception
 | 
			
		||||
        end
 | 
			
		||||
 | 
			
		||||
        module HTTP
 | 
			
		||||
          class RequestHandler
 | 
			
		||||
          end
 | 
			
		||||
        end
 | 
			
		||||
 | 
			
		||||
        alias NumericValue = Float32 | Float64 | Int32 | Int64
 | 
			
		||||
 | 
			
		||||
        lib LibYAML
 | 
			
		||||
        end
 | 
			
		||||
 | 
			
		||||
        struct TagDirective
 | 
			
		||||
        end
 | 
			
		||||
 | 
			
		||||
        enum Time::DayOfWeek
 | 
			
		||||
        end
 | 
			
		||||
        CRYSTAL
 | 
			
		||||
    end
 | 
			
		||||
 | 
			
		||||
    it_reports_name "class", "My_class", "MyClass"
 | 
			
		||||
    it_reports_name "module", "HTT_p", "HTTP"
 | 
			
		||||
    it_reports_name "lib", "Lib_YAML", "LibYAML"
 | 
			
		||||
    it_reports_name "struct", "Tag_directive", "TagDirective"
 | 
			
		||||
    it_reports_name "enum", "Time_enum::Day_of_week", "TimeEnum::DayOfWeek"
 | 
			
		||||
 | 
			
		||||
    it "reports alias name" do
 | 
			
		||||
      expect_issue subject, <<-CRYSTAL
 | 
			
		||||
        alias Numeric_value = Int32
 | 
			
		||||
        # ^{} error: Type name should be camelcased: NumericValue, but it was Numeric_value
 | 
			
		||||
        CRYSTAL
 | 
			
		||||
    end
 | 
			
		||||
  end
 | 
			
		||||
end
 | 
			
		||||
| 
						 | 
				
			
			@ -1,66 +0,0 @@
 | 
			
		|||
require "../../../spec_helper"
 | 
			
		||||
 | 
			
		||||
module Ameba
 | 
			
		||||
  subject = Rule::Style::VariableNames.new
 | 
			
		||||
 | 
			
		||||
  private def it_reports_var_name(name, value, expected, *, file = __FILE__, line = __LINE__)
 | 
			
		||||
    it "reports variable name #{expected}", file, line do
 | 
			
		||||
      rule = Rule::Style::VariableNames.new
 | 
			
		||||
      expect_issue rule, <<-CRYSTAL, name: name, file: file, line: line
 | 
			
		||||
          %{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
 | 
			
		||||
      expect_no_issues subject, <<-CRYSTAL
 | 
			
		||||
        class Greeting
 | 
			
		||||
          @@default_greeting = "Hello world"
 | 
			
		||||
 | 
			
		||||
          def initialize(@custom_greeting = nil)
 | 
			
		||||
          end
 | 
			
		||||
 | 
			
		||||
          def print_greeting
 | 
			
		||||
            greeting = @custom_greeting || @@default_greeting
 | 
			
		||||
            puts greeting
 | 
			
		||||
          end
 | 
			
		||||
        end
 | 
			
		||||
        CRYSTAL
 | 
			
		||||
    end
 | 
			
		||||
 | 
			
		||||
    it_reports_var_name "myBadNamedVar", "1", "my_bad_named_var"
 | 
			
		||||
    it_reports_var_name "wrong_Name", "'y'", "wrong_name"
 | 
			
		||||
 | 
			
		||||
    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
 | 
			
		||||
          end
 | 
			
		||||
        end
 | 
			
		||||
        CRYSTAL
 | 
			
		||||
    end
 | 
			
		||||
 | 
			
		||||
    it "reports method with multiple instance variables" do
 | 
			
		||||
      expect_issue subject, <<-CRYSTAL
 | 
			
		||||
        class Location
 | 
			
		||||
          def at(@startLocation = nil, @endLocation = nil)
 | 
			
		||||
               # ^ error: Var name should be underscore-cased: @start_location, not @startLocation
 | 
			
		||||
                                     # ^ error: Var name should be underscore-cased: @end_location, not @endLocation
 | 
			
		||||
          end
 | 
			
		||||
        end
 | 
			
		||||
        CRYSTAL
 | 
			
		||||
    end
 | 
			
		||||
 | 
			
		||||
    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
 | 
			
		||||
  end
 | 
			
		||||
end
 | 
			
		||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue