mirror of
				https://gitea.invidious.io/iv-org/shard-ameba.git
				synced 2024-08-15 00:53:29 +00:00 
			
		
		
		
	Add rule namespaces: style, lint, layout (#63)
This commit is contained in:
		
							parent
							
								
									d9f04af057
								
							
						
					
					
						commit
						4cb5328513
					
				
					 71 changed files with 128 additions and 128 deletions
				
			
		
							
								
								
									
										51
									
								
								spec/ameba/rule/style/constant_names_spec.cr
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										51
									
								
								spec/ameba/rule/style/constant_names_spec.cr
									
										
									
									
									
										Normal file
									
								
							| 
						 | 
				
			
			@ -0,0 +1,51 @@
 | 
			
		|||
require "../../../spec_helper"
 | 
			
		||||
 | 
			
		||||
module Ameba
 | 
			
		||||
  subject = Rule::Style::ConstantNames.new
 | 
			
		||||
 | 
			
		||||
  private def it_reports_constant(code, 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
 | 
			
		||||
    end
 | 
			
		||||
  end
 | 
			
		||||
 | 
			
		||||
  describe Rule::Style::ConstantNames do
 | 
			
		||||
    it "passes if type names are screaming-cased" do
 | 
			
		||||
      s = Source.new %(
 | 
			
		||||
        LUCKY_NUMBERS     = [3, 7, 11]
 | 
			
		||||
        DOCUMENTATION_URL = "http://crystal-lang.org/docs"
 | 
			
		||||
 | 
			
		||||
        Int32
 | 
			
		||||
 | 
			
		||||
        s : String = "str"
 | 
			
		||||
 | 
			
		||||
        def works(n : Int32)
 | 
			
		||||
        end
 | 
			
		||||
 | 
			
		||||
        a = 1
 | 
			
		||||
        myVar = 2
 | 
			
		||||
        m_var = 3
 | 
			
		||||
      )
 | 
			
		||||
      subject.catch(s).should be_valid
 | 
			
		||||
    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 rule, pos and message" do
 | 
			
		||||
      s = Source.new %(
 | 
			
		||||
        Const = 1
 | 
			
		||||
      ), "source.cr"
 | 
			
		||||
      subject.catch(s).should_not be_valid
 | 
			
		||||
      issue = s.issues.first
 | 
			
		||||
      issue.rule.should_not be_nil
 | 
			
		||||
      issue.location.to_s.should eq "source.cr:2:9"
 | 
			
		||||
      issue.message.should eq(
 | 
			
		||||
        "Constant name should be screaming-cased: CONST, not Const"
 | 
			
		||||
      )
 | 
			
		||||
    end
 | 
			
		||||
  end
 | 
			
		||||
end
 | 
			
		||||
							
								
								
									
										133
									
								
								spec/ameba/rule/style/large_numbers_spec.cr
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										133
									
								
								spec/ameba/rule/style/large_numbers_spec.cr
									
										
									
									
									
										Normal file
									
								
							| 
						 | 
				
			
			@ -0,0 +1,133 @@
 | 
			
		|||
require "../../../spec_helper"
 | 
			
		||||
 | 
			
		||||
module Ameba
 | 
			
		||||
  subject = Rule::Style::LargeNumbers.new
 | 
			
		||||
 | 
			
		||||
  private def it_transforms(number, expected)
 | 
			
		||||
    it "transforms large number #{number}" do
 | 
			
		||||
      s = Source.new number
 | 
			
		||||
      Rule::Style::LargeNumbers.new.catch(s).should_not be_valid
 | 
			
		||||
      s.issues.first.message.should contain expected
 | 
			
		||||
    end
 | 
			
		||||
  end
 | 
			
		||||
 | 
			
		||||
  describe Rule::Style::LargeNumbers do
 | 
			
		||||
    it "passes if large number does not require underscore" do
 | 
			
		||||
      s = Source.new %q(
 | 
			
		||||
        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
 | 
			
		||||
        999
 | 
			
		||||
        1000
 | 
			
		||||
        1_000
 | 
			
		||||
        9999
 | 
			
		||||
        9_999
 | 
			
		||||
        10_000
 | 
			
		||||
        100_000
 | 
			
		||||
        200_000
 | 
			
		||||
        300_000
 | 
			
		||||
        400_000
 | 
			
		||||
        500_000
 | 
			
		||||
        600_000
 | 
			
		||||
        700_000
 | 
			
		||||
        800_000
 | 
			
		||||
        900_000
 | 
			
		||||
        1_000_000
 | 
			
		||||
 | 
			
		||||
       -9_223_372_036_854_775_808
 | 
			
		||||
        9_223_372_036_854_775_807
 | 
			
		||||
 | 
			
		||||
        141_592_654
 | 
			
		||||
        141_592_654.0
 | 
			
		||||
        141_592_654.001
 | 
			
		||||
        141_592_654.001_2
 | 
			
		||||
        141_592_654.001_23
 | 
			
		||||
        141_592_654.001_234
 | 
			
		||||
        141_592_654.001_234_5
 | 
			
		||||
 | 
			
		||||
        0b1101
 | 
			
		||||
        0o123
 | 
			
		||||
        0xFE012D
 | 
			
		||||
        0xfe012d
 | 
			
		||||
        0xfe012dd11
 | 
			
		||||
 | 
			
		||||
        1_i8
 | 
			
		||||
        12_i16
 | 
			
		||||
        123_i32
 | 
			
		||||
        1_234_i64
 | 
			
		||||
 | 
			
		||||
        12_u8
 | 
			
		||||
        123_u16
 | 
			
		||||
        1_234_u32
 | 
			
		||||
        9_223_372_036_854_775_808_u64
 | 
			
		||||
        9_223_372_036_854_775_808.000_123_456_789_f64
 | 
			
		||||
 | 
			
		||||
        +100_u32
 | 
			
		||||
        -900_000_i32
 | 
			
		||||
 | 
			
		||||
        1_234.5e-7
 | 
			
		||||
        11_234e10_f32
 | 
			
		||||
        +1.123
 | 
			
		||||
        -0.000_5
 | 
			
		||||
 | 
			
		||||
        1200.0
 | 
			
		||||
        1200.01
 | 
			
		||||
        1200.012
 | 
			
		||||
      )
 | 
			
		||||
      subject.catch(s).should be_valid
 | 
			
		||||
    end
 | 
			
		||||
 | 
			
		||||
    it_transforms "10000", "10_000"
 | 
			
		||||
    it_transforms "+10000", "+10_000"
 | 
			
		||||
    it_transforms "-10000", "-10_000"
 | 
			
		||||
 | 
			
		||||
    it_transforms "9223372036854775808", "9_223_372_036_854_775_808"
 | 
			
		||||
    it_transforms "-9223372036854775808", "-9_223_372_036_854_775_808"
 | 
			
		||||
    it_transforms "+9223372036854775808", "+9_223_372_036_854_775_808"
 | 
			
		||||
 | 
			
		||||
    it_transforms "1_00000", "100_000"
 | 
			
		||||
 | 
			
		||||
    it_transforms "10000_i16", "10_000_i16"
 | 
			
		||||
    it_transforms "10000_i32", "10_000_i32"
 | 
			
		||||
    it_transforms "10000_i64", "10_000_i64"
 | 
			
		||||
 | 
			
		||||
    it_transforms "10000_u16", "10_000_u16"
 | 
			
		||||
    it_transforms "10000_u32", "10_000_u32"
 | 
			
		||||
    it_transforms "10000_u64", "10_000_u64"
 | 
			
		||||
 | 
			
		||||
    it_transforms "123456_f32", "123_456_f32"
 | 
			
		||||
    it_transforms "123456_f64", "123_456_f64"
 | 
			
		||||
 | 
			
		||||
    it_transforms "123456.5e-7_f32", "123_456.5e-7_f32"
 | 
			
		||||
    it_transforms "123456e10_f64", "123_456e10_f64"
 | 
			
		||||
 | 
			
		||||
    it_transforms "123456.5e-7", "123_456.5e-7"
 | 
			
		||||
    it_transforms "123456e10", "123_456e10"
 | 
			
		||||
 | 
			
		||||
    it_transforms "3.00_1", "3.001"
 | 
			
		||||
    it_transforms "3.0012", "3.001_2"
 | 
			
		||||
    it_transforms "3.00123", "3.001_23"
 | 
			
		||||
    it_transforms "3.001234", "3.001_234"
 | 
			
		||||
    it_transforms "3.0012345", "3.001_234_5"
 | 
			
		||||
 | 
			
		||||
    it "reports rule, pos and message" do
 | 
			
		||||
      s = Source.new %q(
 | 
			
		||||
         1200000
 | 
			
		||||
      ), "source.cr"
 | 
			
		||||
      subject.catch(s).should_not be_valid
 | 
			
		||||
      issue = s.issues.first
 | 
			
		||||
      issue.rule.should_not be_nil
 | 
			
		||||
      issue.location.to_s.should eq "source.cr:2:10"
 | 
			
		||||
      issue.message.should match /1_200_000/
 | 
			
		||||
    end
 | 
			
		||||
 | 
			
		||||
    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
 | 
			
		||||
      end
 | 
			
		||||
    end
 | 
			
		||||
  end
 | 
			
		||||
end
 | 
			
		||||
							
								
								
									
										55
									
								
								spec/ameba/rule/style/method_names_spec.cr
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										55
									
								
								spec/ameba/rule/style/method_names_spec.cr
									
										
									
									
									
										Normal file
									
								
							| 
						 | 
				
			
			@ -0,0 +1,55 @@
 | 
			
		|||
require "../../../spec_helper"
 | 
			
		||||
 | 
			
		||||
module Ameba
 | 
			
		||||
  subject = Rule::Style::MethodNames.new
 | 
			
		||||
 | 
			
		||||
  private def it_reports_method_name(code, 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
 | 
			
		||||
    end
 | 
			
		||||
  end
 | 
			
		||||
 | 
			
		||||
  describe Rule::Style::MethodNames do
 | 
			
		||||
    it "passes if method names are underscore-cased" do
 | 
			
		||||
      s = Source.new %(
 | 
			
		||||
        class Person
 | 
			
		||||
          def first_name
 | 
			
		||||
          end
 | 
			
		||||
 | 
			
		||||
          def date_of_birth
 | 
			
		||||
          end
 | 
			
		||||
 | 
			
		||||
          def homepage_url
 | 
			
		||||
          end
 | 
			
		||||
 | 
			
		||||
          def valid?
 | 
			
		||||
          end
 | 
			
		||||
 | 
			
		||||
          def name
 | 
			
		||||
          end
 | 
			
		||||
        end
 | 
			
		||||
      )
 | 
			
		||||
      subject.catch(s).should be_valid
 | 
			
		||||
    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 rule, pos and message" do
 | 
			
		||||
      s = Source.new %(
 | 
			
		||||
        def bad_Name
 | 
			
		||||
        end
 | 
			
		||||
      ), "source.cr"
 | 
			
		||||
      subject.catch(s).should_not be_valid
 | 
			
		||||
      issue = s.issues.first
 | 
			
		||||
      issue.rule.should_not be_nil
 | 
			
		||||
      issue.location.to_s.should eq "source.cr:2:9"
 | 
			
		||||
      issue.message.should eq(
 | 
			
		||||
        "Method name should be underscore-cased: bad_name, not bad_Name"
 | 
			
		||||
      )
 | 
			
		||||
    end
 | 
			
		||||
  end
 | 
			
		||||
end
 | 
			
		||||
							
								
								
									
										68
									
								
								spec/ameba/rule/style/negated_conditions_in_unless_spec.cr
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										68
									
								
								spec/ameba/rule/style/negated_conditions_in_unless_spec.cr
									
										
									
									
									
										Normal file
									
								
							| 
						 | 
				
			
			@ -0,0 +1,68 @@
 | 
			
		|||
require "../../../spec_helper"
 | 
			
		||||
 | 
			
		||||
module Ameba::Rule::Style
 | 
			
		||||
  subject = NegatedConditionsInUnless.new
 | 
			
		||||
 | 
			
		||||
  describe NegatedConditionsInUnless do
 | 
			
		||||
    it "passes with a unless without negated condition" do
 | 
			
		||||
      s = Source.new %(
 | 
			
		||||
        unless a
 | 
			
		||||
          :ok
 | 
			
		||||
        end
 | 
			
		||||
 | 
			
		||||
        :ok unless b
 | 
			
		||||
 | 
			
		||||
        unless s.empty?
 | 
			
		||||
          :ok
 | 
			
		||||
        end
 | 
			
		||||
      )
 | 
			
		||||
      subject.catch(s).should be_valid
 | 
			
		||||
    end
 | 
			
		||||
 | 
			
		||||
    it "fails if there is a negated condition in unless" do
 | 
			
		||||
      s = Source.new %(
 | 
			
		||||
        unless !a
 | 
			
		||||
          :nok
 | 
			
		||||
        end
 | 
			
		||||
      )
 | 
			
		||||
      subject.catch(s).should_not be_valid
 | 
			
		||||
    end
 | 
			
		||||
 | 
			
		||||
    it "fails if one of AND conditions is negated" do
 | 
			
		||||
      s = Source.new %(
 | 
			
		||||
        unless a && !b
 | 
			
		||||
          :nok
 | 
			
		||||
        end
 | 
			
		||||
      )
 | 
			
		||||
      subject.catch(s).should_not be_valid
 | 
			
		||||
    end
 | 
			
		||||
 | 
			
		||||
    it "fails if one of OR conditions is negated" do
 | 
			
		||||
      s = Source.new %(
 | 
			
		||||
        unless a || !b
 | 
			
		||||
          :nok
 | 
			
		||||
        end
 | 
			
		||||
      )
 | 
			
		||||
      subject.catch(s).should_not be_valid
 | 
			
		||||
    end
 | 
			
		||||
 | 
			
		||||
    it "fails if one of inner conditions is negated" do
 | 
			
		||||
      s = Source.new %(
 | 
			
		||||
        unless a && (b || !c)
 | 
			
		||||
          :nok
 | 
			
		||||
        end
 | 
			
		||||
      )
 | 
			
		||||
      subject.catch(s).should_not be_valid
 | 
			
		||||
    end
 | 
			
		||||
 | 
			
		||||
    it "reports rule, pos and message" do
 | 
			
		||||
      s = Source.new ":nok unless !s.empty?", "source.cr"
 | 
			
		||||
      subject.catch(s).should_not be_valid
 | 
			
		||||
 | 
			
		||||
      issue = s.issues.first
 | 
			
		||||
      issue.rule.should_not be_nil
 | 
			
		||||
      issue.location.to_s.should eq "source.cr:1:1"
 | 
			
		||||
      issue.message.should eq "Avoid negated conditions in unless blocks"
 | 
			
		||||
    end
 | 
			
		||||
  end
 | 
			
		||||
end
 | 
			
		||||
							
								
								
									
										59
									
								
								spec/ameba/rule/style/predicate_name_spec.cr
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										59
									
								
								spec/ameba/rule/style/predicate_name_spec.cr
									
										
									
									
									
										Normal file
									
								
							| 
						 | 
				
			
			@ -0,0 +1,59 @@
 | 
			
		|||
require "../../../spec_helper"
 | 
			
		||||
 | 
			
		||||
module Ameba::Rule::Style
 | 
			
		||||
  subject = PredicateName.new
 | 
			
		||||
 | 
			
		||||
  describe PredicateName do
 | 
			
		||||
    it "passes if predicate name is correct" do
 | 
			
		||||
      s = Source.new %q(
 | 
			
		||||
        def valid?(x)
 | 
			
		||||
        end
 | 
			
		||||
 | 
			
		||||
        class Image
 | 
			
		||||
          def picture?(x)
 | 
			
		||||
          end
 | 
			
		||||
        end
 | 
			
		||||
 | 
			
		||||
        def allow_this_picture?
 | 
			
		||||
        end
 | 
			
		||||
      )
 | 
			
		||||
      subject.catch(s).should be_valid
 | 
			
		||||
    end
 | 
			
		||||
 | 
			
		||||
    it "fails if predicate name is wrong" do
 | 
			
		||||
      s = Source.new %q(
 | 
			
		||||
        def is_valid?(x)
 | 
			
		||||
        end
 | 
			
		||||
      )
 | 
			
		||||
      subject.catch(s).should_not be_valid
 | 
			
		||||
    end
 | 
			
		||||
 | 
			
		||||
    it "reports rule, pos and message" do
 | 
			
		||||
      s = Source.new %q(
 | 
			
		||||
        class Image
 | 
			
		||||
          def has_picture?(x)
 | 
			
		||||
            true
 | 
			
		||||
          end
 | 
			
		||||
        end
 | 
			
		||||
      ), "source.cr"
 | 
			
		||||
      subject.catch(s).should_not be_valid
 | 
			
		||||
 | 
			
		||||
      issue = s.issues.first
 | 
			
		||||
      issue.rule.should_not be_nil
 | 
			
		||||
      issue.location.to_s.should eq "source.cr:3:11"
 | 
			
		||||
      issue.message.should eq(
 | 
			
		||||
        "Favour method name 'picture?' over 'has_picture?'")
 | 
			
		||||
    end
 | 
			
		||||
 | 
			
		||||
    it "ignores if alternative name isn't valid syntax" do
 | 
			
		||||
      s = Source.new %q(
 | 
			
		||||
        class Image
 | 
			
		||||
          def is_404?(x)
 | 
			
		||||
            true
 | 
			
		||||
          end
 | 
			
		||||
        end
 | 
			
		||||
      )
 | 
			
		||||
      subject.catch(s).should be_valid
 | 
			
		||||
    end
 | 
			
		||||
  end
 | 
			
		||||
end
 | 
			
		||||
							
								
								
									
										213
									
								
								spec/ameba/rule/style/redundant_begin_spec.cr
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										213
									
								
								spec/ameba/rule/style/redundant_begin_spec.cr
									
										
									
									
									
										Normal file
									
								
							| 
						 | 
				
			
			@ -0,0 +1,213 @@
 | 
			
		|||
require "../../../spec_helper"
 | 
			
		||||
 | 
			
		||||
module Ameba::Rule::Style
 | 
			
		||||
  describe RedundantBegin do
 | 
			
		||||
    subject = RedundantBegin.new
 | 
			
		||||
 | 
			
		||||
    it "passes if there is no redundant begin blocks" do
 | 
			
		||||
      s = Source.new %(
 | 
			
		||||
        def method
 | 
			
		||||
          do_something
 | 
			
		||||
        rescue
 | 
			
		||||
          do_something_else
 | 
			
		||||
        end
 | 
			
		||||
 | 
			
		||||
        def method
 | 
			
		||||
          do_something
 | 
			
		||||
          do_something_else
 | 
			
		||||
        ensure
 | 
			
		||||
          handle_something
 | 
			
		||||
        end
 | 
			
		||||
 | 
			
		||||
        def method
 | 
			
		||||
          yield
 | 
			
		||||
        rescue
 | 
			
		||||
        end
 | 
			
		||||
 | 
			
		||||
        def method; end
 | 
			
		||||
        def method; a = 1; rescue; end
 | 
			
		||||
        def method; begin; rescue; end; end
 | 
			
		||||
      )
 | 
			
		||||
      subject.catch(s).should be_valid
 | 
			
		||||
    end
 | 
			
		||||
 | 
			
		||||
    it "passes if there is a correct begin block in a handler" do
 | 
			
		||||
      s = Source.new %q(
 | 
			
		||||
        def handler_and_expression
 | 
			
		||||
          begin
 | 
			
		||||
            open_file
 | 
			
		||||
          rescue
 | 
			
		||||
            close_file
 | 
			
		||||
          end
 | 
			
		||||
          do_some_stuff
 | 
			
		||||
        end
 | 
			
		||||
 | 
			
		||||
        def multiple_handlers
 | 
			
		||||
          begin
 | 
			
		||||
            begin1
 | 
			
		||||
          rescue
 | 
			
		||||
          end
 | 
			
		||||
 | 
			
		||||
          begin
 | 
			
		||||
            begin2
 | 
			
		||||
          rescue
 | 
			
		||||
          end
 | 
			
		||||
        rescue
 | 
			
		||||
          do_something_else
 | 
			
		||||
        end
 | 
			
		||||
 | 
			
		||||
        def assign_and_begin
 | 
			
		||||
          @result ||=
 | 
			
		||||
            begin
 | 
			
		||||
              do_something
 | 
			
		||||
              do_something_else
 | 
			
		||||
              returnit
 | 
			
		||||
            end
 | 
			
		||||
        rescue
 | 
			
		||||
        end
 | 
			
		||||
 | 
			
		||||
        def inner_handler
 | 
			
		||||
          s = begin
 | 
			
		||||
              rescue
 | 
			
		||||
              end
 | 
			
		||||
        rescue
 | 
			
		||||
        end
 | 
			
		||||
 | 
			
		||||
        def begin_and_expression
 | 
			
		||||
          begin
 | 
			
		||||
            a = 1
 | 
			
		||||
            b = 2
 | 
			
		||||
          end
 | 
			
		||||
          expr
 | 
			
		||||
        end
 | 
			
		||||
      )
 | 
			
		||||
      subject.catch(s).should be_valid
 | 
			
		||||
    end
 | 
			
		||||
 | 
			
		||||
    it "fails if there is a redundant begin block" do
 | 
			
		||||
      s = Source.new %q(
 | 
			
		||||
        def method(a : String) : String
 | 
			
		||||
          begin
 | 
			
		||||
            open_file
 | 
			
		||||
            do_some_stuff
 | 
			
		||||
          ensure
 | 
			
		||||
            close_file
 | 
			
		||||
          end
 | 
			
		||||
        end
 | 
			
		||||
      )
 | 
			
		||||
      subject.catch(s).should_not be_valid
 | 
			
		||||
    end
 | 
			
		||||
 | 
			
		||||
    it "fails if there is a redundant begin block in a method without args" do
 | 
			
		||||
      s = Source.new %q(
 | 
			
		||||
        def method
 | 
			
		||||
          begin
 | 
			
		||||
            open_file
 | 
			
		||||
          ensure
 | 
			
		||||
            close_file
 | 
			
		||||
          end
 | 
			
		||||
        end
 | 
			
		||||
      )
 | 
			
		||||
      subject.catch(s).should_not be_valid
 | 
			
		||||
    end
 | 
			
		||||
 | 
			
		||||
    it "fails if there is a redundant block in a method with return type" do
 | 
			
		||||
      s = Source.new %q(
 | 
			
		||||
        def method : String
 | 
			
		||||
          begin
 | 
			
		||||
            open_file
 | 
			
		||||
          ensure
 | 
			
		||||
            close_file
 | 
			
		||||
          end
 | 
			
		||||
        end
 | 
			
		||||
      )
 | 
			
		||||
      subject.catch(s).should_not be_valid
 | 
			
		||||
    end
 | 
			
		||||
 | 
			
		||||
    it "fails if there is a redundant block in a method with multiple args" do
 | 
			
		||||
      s = Source.new %q(
 | 
			
		||||
        def method(a : String,
 | 
			
		||||
                  b : String)
 | 
			
		||||
          begin
 | 
			
		||||
            open_file
 | 
			
		||||
          ensure
 | 
			
		||||
            close_file
 | 
			
		||||
          end
 | 
			
		||||
        end
 | 
			
		||||
      )
 | 
			
		||||
      subject.catch(s).should_not be_valid
 | 
			
		||||
    end
 | 
			
		||||
 | 
			
		||||
    it "fails if there is a redundant block in a method with multiple args" do
 | 
			
		||||
      s = Source.new %q(
 | 
			
		||||
        def method(a : String,
 | 
			
		||||
                  b : String
 | 
			
		||||
        )
 | 
			
		||||
          begin
 | 
			
		||||
            open_file
 | 
			
		||||
          ensure
 | 
			
		||||
            close_file
 | 
			
		||||
          end
 | 
			
		||||
        end
 | 
			
		||||
      )
 | 
			
		||||
      subject.catch(s).should_not be_valid
 | 
			
		||||
    end
 | 
			
		||||
 | 
			
		||||
    it "doesn't report if there is an inner redundant block" do
 | 
			
		||||
      s = Source.new %q(
 | 
			
		||||
        def method
 | 
			
		||||
          begin
 | 
			
		||||
            open_file
 | 
			
		||||
          ensure
 | 
			
		||||
            close_file
 | 
			
		||||
          end
 | 
			
		||||
        rescue
 | 
			
		||||
        end
 | 
			
		||||
      )
 | 
			
		||||
      subject.catch(s).should be_valid
 | 
			
		||||
    end
 | 
			
		||||
 | 
			
		||||
    it "fails if there is a redundant block with yield" do
 | 
			
		||||
      s = Source.new %q(
 | 
			
		||||
        def method
 | 
			
		||||
          begin
 | 
			
		||||
            yield
 | 
			
		||||
          ensure
 | 
			
		||||
            close_file
 | 
			
		||||
          end
 | 
			
		||||
        end
 | 
			
		||||
      )
 | 
			
		||||
      subject.catch(s).should_not be_valid
 | 
			
		||||
    end
 | 
			
		||||
 | 
			
		||||
    it "fails if there is top level redundant block in a method" do
 | 
			
		||||
      s = Source.new %q(
 | 
			
		||||
        def method
 | 
			
		||||
          begin
 | 
			
		||||
            a = 1
 | 
			
		||||
            b = 2
 | 
			
		||||
          end
 | 
			
		||||
        end
 | 
			
		||||
      )
 | 
			
		||||
      subject.catch(s).should_not be_valid
 | 
			
		||||
    end
 | 
			
		||||
 | 
			
		||||
    it "reports rule, pos and message" do
 | 
			
		||||
      s = Source.new %q(
 | 
			
		||||
        def method
 | 
			
		||||
          begin
 | 
			
		||||
            open_connection
 | 
			
		||||
          ensure
 | 
			
		||||
            close_connection
 | 
			
		||||
          end
 | 
			
		||||
        end
 | 
			
		||||
      ), "source.cr"
 | 
			
		||||
      subject.catch(s).should_not be_valid
 | 
			
		||||
 | 
			
		||||
      issue = s.issues.first
 | 
			
		||||
      issue.rule.should_not be_nil
 | 
			
		||||
      issue.location.to_s.should eq "source.cr:2:9"
 | 
			
		||||
      issue.message.should eq "Redundant `begin` block detected"
 | 
			
		||||
    end
 | 
			
		||||
  end
 | 
			
		||||
end
 | 
			
		||||
							
								
								
									
										60
									
								
								spec/ameba/rule/style/type_names_spec.cr
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										60
									
								
								spec/ameba/rule/style/type_names_spec.cr
									
										
									
									
									
										Normal file
									
								
							| 
						 | 
				
			
			@ -0,0 +1,60 @@
 | 
			
		|||
require "../../../spec_helper"
 | 
			
		||||
 | 
			
		||||
module Ameba
 | 
			
		||||
  subject = Rule::Style::TypeNames.new
 | 
			
		||||
 | 
			
		||||
  private def it_reports_name(code, expected)
 | 
			
		||||
    it "reports type name #{expected}" do
 | 
			
		||||
      s = Source.new code
 | 
			
		||||
      Rule::Style::TypeNames.new.catch(s).should_not be_valid
 | 
			
		||||
      s.issues.first.message.should contain expected
 | 
			
		||||
    end
 | 
			
		||||
  end
 | 
			
		||||
 | 
			
		||||
  describe Rule::Style::TypeNames do
 | 
			
		||||
    it "passes if type names are camelcased" do
 | 
			
		||||
      s = Source.new %(
 | 
			
		||||
        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
 | 
			
		||||
      )
 | 
			
		||||
      subject.catch(s).should be_valid
 | 
			
		||||
    end
 | 
			
		||||
 | 
			
		||||
    it_reports_name "class My_class; end", "MyClass"
 | 
			
		||||
    it_reports_name "module HTT_p; end", "HTTP"
 | 
			
		||||
    it_reports_name "alias Numeric_value = Int32", "NumericValue"
 | 
			
		||||
    it_reports_name "lib Lib_YAML; end", "LibYAML"
 | 
			
		||||
    it_reports_name "struct Tag_directive; end", "TagDirective"
 | 
			
		||||
    it_reports_name "enum Time_enum::Day_of_week; end", "TimeEnum::DayOfWeek"
 | 
			
		||||
 | 
			
		||||
    it "reports rule, pos and message" do
 | 
			
		||||
      s = Source.new %(
 | 
			
		||||
        class My_class
 | 
			
		||||
        end
 | 
			
		||||
      ), "source.cr"
 | 
			
		||||
      subject.catch(s).should_not be_valid
 | 
			
		||||
      issue = s.issues.first
 | 
			
		||||
      issue.rule.should_not be_nil
 | 
			
		||||
      issue.location.to_s.should eq "source.cr:2:9"
 | 
			
		||||
      issue.message.should eq(
 | 
			
		||||
        "Type name should be camelcased: MyClass, but it was My_class"
 | 
			
		||||
      )
 | 
			
		||||
    end
 | 
			
		||||
  end
 | 
			
		||||
end
 | 
			
		||||
							
								
								
									
										44
									
								
								spec/ameba/rule/style/unless_else_spec.cr
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										44
									
								
								spec/ameba/rule/style/unless_else_spec.cr
									
										
									
									
									
										Normal file
									
								
							| 
						 | 
				
			
			@ -0,0 +1,44 @@
 | 
			
		|||
require "../../../spec_helper"
 | 
			
		||||
 | 
			
		||||
module Ameba::Rule::Style
 | 
			
		||||
  subject = UnlessElse.new
 | 
			
		||||
 | 
			
		||||
  describe UnlessElse do
 | 
			
		||||
    it "passes if unless hasn't else" do
 | 
			
		||||
      s = Source.new %(
 | 
			
		||||
        unless something
 | 
			
		||||
          :ok
 | 
			
		||||
        end
 | 
			
		||||
      )
 | 
			
		||||
      subject.catch(s).should be_valid
 | 
			
		||||
    end
 | 
			
		||||
 | 
			
		||||
    it "fails if unless has else" do
 | 
			
		||||
      s = Source.new %(
 | 
			
		||||
        unless something
 | 
			
		||||
          :one
 | 
			
		||||
        else
 | 
			
		||||
          :two
 | 
			
		||||
        end
 | 
			
		||||
      )
 | 
			
		||||
      subject.catch(s).should_not be_valid
 | 
			
		||||
    end
 | 
			
		||||
 | 
			
		||||
    it "reports rule, pos and message" do
 | 
			
		||||
      s = Source.new %(
 | 
			
		||||
        unless something
 | 
			
		||||
          :one
 | 
			
		||||
        else
 | 
			
		||||
          :two
 | 
			
		||||
        end
 | 
			
		||||
      ), "source.cr"
 | 
			
		||||
      subject.catch(s).should_not be_valid
 | 
			
		||||
 | 
			
		||||
      issue = s.issues.first
 | 
			
		||||
      issue.should_not be_nil
 | 
			
		||||
      issue.rule.should_not be_nil
 | 
			
		||||
      issue.location.to_s.should eq "source.cr:2:9"
 | 
			
		||||
      issue.message.should eq "Favour if over unless with else"
 | 
			
		||||
    end
 | 
			
		||||
  end
 | 
			
		||||
end
 | 
			
		||||
							
								
								
									
										61
									
								
								spec/ameba/rule/style/variable_names_spec.cr
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										61
									
								
								spec/ameba/rule/style/variable_names_spec.cr
									
										
									
									
									
										Normal file
									
								
							| 
						 | 
				
			
			@ -0,0 +1,61 @@
 | 
			
		|||
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
 | 
			
		||||
    end
 | 
			
		||||
  end
 | 
			
		||||
 | 
			
		||||
  describe Rule::Style::VariableNames do
 | 
			
		||||
    it "passes if var names are underscore-cased" do
 | 
			
		||||
      s = Source.new %(
 | 
			
		||||
        class Greeting
 | 
			
		||||
          @@default_greeting = "Hello world"
 | 
			
		||||
 | 
			
		||||
          def initialize(@custom_greeting = nil)
 | 
			
		||||
          end
 | 
			
		||||
 | 
			
		||||
          def print_greeting
 | 
			
		||||
            greeting = @custom_greeting || @@default_greeting
 | 
			
		||||
            puts greeting
 | 
			
		||||
          end
 | 
			
		||||
        end
 | 
			
		||||
      )
 | 
			
		||||
      subject.catch(s).should be_valid
 | 
			
		||||
    end
 | 
			
		||||
 | 
			
		||||
    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)
 | 
			
		||||
        end
 | 
			
		||||
      end
 | 
			
		||||
    ), "bad_named"
 | 
			
		||||
 | 
			
		||||
    it_reports_var_name %(
 | 
			
		||||
      class Greeting
 | 
			
		||||
        @@defaultGreeting = "Hello world"
 | 
			
		||||
      end
 | 
			
		||||
    ), "default_greeting"
 | 
			
		||||
 | 
			
		||||
    it "reports rule, pos and message" do
 | 
			
		||||
      s = Source.new %(
 | 
			
		||||
        badName = "Yeah"
 | 
			
		||||
      ), "source.cr"
 | 
			
		||||
      subject.catch(s).should_not be_valid
 | 
			
		||||
      issue = s.issues.first
 | 
			
		||||
      issue.rule.should_not be_nil
 | 
			
		||||
      issue.location.to_s.should eq "source.cr:2:9"
 | 
			
		||||
      issue.message.should eq(
 | 
			
		||||
        "Var name should be underscore-cased: bad_name, not badName"
 | 
			
		||||
      )
 | 
			
		||||
    end
 | 
			
		||||
  end
 | 
			
		||||
end
 | 
			
		||||
							
								
								
									
										42
									
								
								spec/ameba/rule/style/while_true_spec.cr
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										42
									
								
								spec/ameba/rule/style/while_true_spec.cr
									
										
									
									
									
										Normal file
									
								
							| 
						 | 
				
			
			@ -0,0 +1,42 @@
 | 
			
		|||
require "../../../spec_helper"
 | 
			
		||||
 | 
			
		||||
valid_source = <<-EOF
 | 
			
		||||
a = 1
 | 
			
		||||
loop do
 | 
			
		||||
  a += 1
 | 
			
		||||
  break if a > 5
 | 
			
		||||
end
 | 
			
		||||
EOF
 | 
			
		||||
 | 
			
		||||
invalid_source = <<-EOF
 | 
			
		||||
a = 1
 | 
			
		||||
while true
 | 
			
		||||
  a += 1
 | 
			
		||||
  break if a > 5
 | 
			
		||||
end
 | 
			
		||||
EOF
 | 
			
		||||
 | 
			
		||||
module Ameba::Rule::Style
 | 
			
		||||
  subject = WhileTrue.new
 | 
			
		||||
 | 
			
		||||
  describe WhileTrue do
 | 
			
		||||
    it "passes if there is no `while true`" do
 | 
			
		||||
      source = Source.new valid_source
 | 
			
		||||
      subject.catch(source).should be_valid
 | 
			
		||||
    end
 | 
			
		||||
 | 
			
		||||
    it "fails if there is `while true`" do
 | 
			
		||||
      source = Source.new invalid_source
 | 
			
		||||
      subject.catch(source).should_not be_valid
 | 
			
		||||
    end
 | 
			
		||||
 | 
			
		||||
    it "reports rule, pos and message" do
 | 
			
		||||
      source = Source.new invalid_source, "source.cr"
 | 
			
		||||
      subject.catch(source).should_not be_valid
 | 
			
		||||
 | 
			
		||||
      issue = source.issues.first
 | 
			
		||||
      issue.location.to_s.should eq "source.cr:2:1"
 | 
			
		||||
      issue.message.should eq "While statement using true literal as condition"
 | 
			
		||||
    end
 | 
			
		||||
  end
 | 
			
		||||
end
 | 
			
		||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue