rubocop 0.3.0 → 0.6.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/.rubocop.yml +176 -0
- data/.travis.yml +1 -0
- data/CHANGELOG.md +60 -0
- data/CONTRIBUTING.md +13 -6
- data/Gemfile +1 -13
- data/README.md +131 -6
- data/Rakefile +1 -15
- data/bin/rubocop +2 -1
- data/lib/rubocop/cli.rb +133 -21
- data/lib/rubocop/cop/alias.rb +18 -0
- data/lib/rubocop/cop/align_parameters.rb +5 -4
- data/lib/rubocop/cop/array_literal.rb +22 -0
- data/lib/rubocop/cop/ascii_identifiers_and_comments.rb +18 -0
- data/lib/rubocop/cop/avoid_class_vars.rb +20 -0
- data/lib/rubocop/cop/avoid_for.rb +17 -0
- data/lib/rubocop/cop/avoid_perl_backrefs.rb +20 -0
- data/lib/rubocop/cop/avoid_perlisms.rb +46 -0
- data/lib/rubocop/cop/blocks.rb +1 -1
- data/lib/rubocop/cop/brace_after_percent.rb +28 -0
- data/lib/rubocop/cop/class_and_module_camel_case.rb +20 -0
- data/lib/rubocop/cop/collection_methods.rb +29 -0
- data/lib/rubocop/cop/cop.rb +16 -4
- data/lib/rubocop/cop/def_parentheses.rb +4 -1
- data/lib/rubocop/cop/encoding.rb +7 -5
- data/lib/rubocop/cop/end_of_line.rb +1 -3
- data/lib/rubocop/cop/ensure_return.rb +36 -0
- data/lib/rubocop/cop/favor_modifier.rb +82 -0
- data/lib/rubocop/cop/favor_percent_r.rb +19 -0
- data/lib/rubocop/cop/favor_sprintf.rb +37 -0
- data/lib/rubocop/cop/favor_unless_over_negated_if.rb +47 -0
- data/lib/rubocop/cop/grammar.rb +12 -8
- data/lib/rubocop/cop/handle_exceptions.rb +21 -0
- data/lib/rubocop/cop/hash_literal.rb +22 -0
- data/lib/rubocop/cop/indentation.rb +1 -1
- data/lib/rubocop/cop/line_length.rb +7 -3
- data/lib/rubocop/cop/method_and_variable_snake_case.rb +31 -0
- data/lib/rubocop/cop/method_length.rb +66 -0
- data/lib/rubocop/cop/new_lambda_literal.rb +17 -0
- data/lib/rubocop/cop/offence.rb +3 -1
- data/lib/rubocop/cop/op_method.rb +23 -0
- data/lib/rubocop/cop/parentheses_around_condition.rb +24 -0
- data/lib/rubocop/cop/percent_literals.rb +25 -0
- data/lib/rubocop/cop/percent_r.rb +19 -0
- data/lib/rubocop/cop/reduce_arguments.rb +67 -0
- data/lib/rubocop/cop/rescue_exception.rb +39 -0
- data/lib/rubocop/cop/rescue_modifier.rb +20 -0
- data/lib/rubocop/cop/semicolon.rb +18 -0
- data/lib/rubocop/cop/space_after_comma_etc.rb +2 -1
- data/lib/rubocop/cop/string_literals.rb +12 -0
- data/lib/rubocop/cop/surrounding_space.rb +48 -47
- data/lib/rubocop/cop/symbol_snake_case.rb +27 -0
- data/lib/rubocop/cop/syntax.rb +42 -0
- data/lib/rubocop/cop/tab.rb +1 -3
- data/lib/rubocop/cop/variable_interpolation.rb +20 -0
- data/lib/rubocop/cop/when_then.rb +3 -0
- data/lib/rubocop/report/emacs_style.rb +2 -2
- data/lib/rubocop/report/plain_text.rb +1 -1
- data/lib/rubocop/report/report.rb +5 -2
- data/lib/rubocop/version.rb +3 -1
- data/lib/rubocop.rb +37 -0
- data/rubocop.gemspec +36 -134
- data/spec/rubocop/cli_spec.rb +326 -48
- data/spec/rubocop/cops/alias_spec.rb +36 -0
- data/spec/rubocop/cops/align_parameters_spec.rb +43 -34
- data/spec/rubocop/cops/ampersands_pipes_vs_and_or_spec.rb +4 -4
- data/spec/rubocop/cops/array_literal_spec.rb +29 -0
- data/spec/rubocop/cops/ascii_identifiers_and_comments_spec.rb +38 -0
- data/spec/rubocop/cops/avoid_class_vars_spec.rb +20 -0
- data/spec/rubocop/cops/avoid_for_spec.rb +22 -0
- data/spec/rubocop/cops/avoid_perl_backrefs_spec.rb +18 -0
- data/spec/rubocop/cops/avoid_perlisms_spec.rb +44 -0
- data/spec/rubocop/cops/brace_after_percent_spec.rb +27 -0
- data/spec/rubocop/cops/class_and_module_camel_case_spec.rb +34 -0
- data/spec/rubocop/cops/collection_methods_spec.rb +39 -0
- data/spec/rubocop/cops/cop_spec.rb +5 -5
- data/spec/rubocop/cops/def_with_parentheses_spec.rb +5 -5
- data/spec/rubocop/cops/def_without_parentheses_spec.rb +4 -4
- data/spec/rubocop/cops/empty_lines_spec.rb +8 -8
- data/spec/rubocop/cops/encoding_spec.rb +17 -11
- data/spec/rubocop/cops/end_of_line_spec.rb +5 -5
- data/spec/rubocop/cops/ensure_return_spec.rb +37 -0
- data/spec/rubocop/cops/favor_modifier_spec.rb +112 -0
- data/spec/rubocop/cops/favor_percent_r.rb +29 -0
- data/spec/rubocop/cops/favor_sprintf_spec.rb +57 -0
- data/spec/rubocop/cops/favor_unless_over_negated_if_spec.rb +62 -0
- data/spec/rubocop/cops/favor_until_over_negated_while_spec.rb +45 -0
- data/spec/rubocop/cops/grammar_spec.rb +59 -44
- data/spec/rubocop/cops/handle_exceptions_spec.rb +36 -0
- data/spec/rubocop/cops/hash_literal_spec.rb +29 -0
- data/spec/rubocop/cops/hash_syntax_spec.rb +10 -10
- data/spec/rubocop/cops/if_with_semicolon_spec.rb +3 -3
- data/spec/rubocop/cops/indentation_spec.rb +7 -7
- data/spec/rubocop/cops/line_length_spec.rb +4 -4
- data/spec/rubocop/cops/method_and_variable_snake_case_spec.rb +47 -0
- data/spec/rubocop/cops/method_length_spec.rb +150 -0
- data/spec/rubocop/cops/multiline_blocks_spec.rb +4 -4
- data/spec/rubocop/cops/multiline_if_then_spec.rb +7 -7
- data/spec/rubocop/cops/new_lambda_literal_spec.rb +23 -0
- data/spec/rubocop/cops/numeric_literals_spec.rb +10 -10
- data/spec/rubocop/cops/offence_spec.rb +10 -4
- data/spec/rubocop/cops/one_line_conditional_spec.rb +2 -3
- data/spec/rubocop/cops/op_method_spec.rb +58 -0
- data/spec/rubocop/cops/parameter_lists_spec.rb +4 -4
- data/spec/rubocop/cops/parentheses_around_condition_spec.rb +64 -0
- data/spec/rubocop/cops/percent_literals_spec.rb +47 -0
- data/spec/rubocop/cops/percent_r_spec.rb +29 -0
- data/spec/rubocop/cops/reduce_arguments_spec.rb +57 -0
- data/spec/rubocop/cops/rescue_exception_spec.rb +73 -0
- data/spec/rubocop/cops/rescue_modifier.rb +40 -0
- data/spec/rubocop/cops/semicolon_spec.rb +29 -0
- data/spec/rubocop/cops/single_line_blocks_spec.rb +4 -4
- data/spec/rubocop/cops/space_after_colon_spec.rb +5 -5
- data/spec/rubocop/cops/space_after_comma_spec.rb +3 -3
- data/spec/rubocop/cops/space_after_semicolon_spec.rb +3 -3
- data/spec/rubocop/cops/space_around_braces_spec.rb +6 -6
- data/spec/rubocop/cops/space_around_equals_in_default_parameter_spec.rb +22 -0
- data/spec/rubocop/cops/space_around_operators_spec.rb +57 -25
- data/spec/rubocop/cops/space_inside_brackets_spec.rb +8 -8
- data/spec/rubocop/cops/space_inside_parens_spec.rb +4 -4
- data/spec/rubocop/cops/string_literals_spec.rb +9 -6
- data/spec/rubocop/cops/symbol_snake_case_spec.rb +74 -0
- data/spec/rubocop/cops/syntax_spec.rb +34 -0
- data/spec/rubocop/cops/tab_spec.rb +4 -4
- data/spec/rubocop/cops/ternary_operator_spec.rb +8 -8
- data/spec/rubocop/cops/trailing_whitespace_spec.rb +4 -4
- data/spec/rubocop/cops/unless_else_spec.rb +4 -4
- data/spec/rubocop/cops/variable_interpolation_spec.rb +45 -0
- data/spec/rubocop/cops/when_then_spec.rb +12 -5
- data/spec/rubocop/reports/emacs_style_spec.rb +3 -3
- data/spec/rubocop/reports/report_spec.rb +5 -5
- data/spec/spec_helper.rb +6 -0
- metadata +165 -36
- data/.rbenv-version +0 -1
- data/.rvmrc +0 -1
- data/Gemfile.lock +0 -39
- data/VERSION +0 -1
- data/features/rubocop.feature +0 -9
- data/features/step_definitions/rubocop_steps.rb +0 -1
- data/features/support/env.rb +0 -15
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
# encoding: utf-8
|
|
2
|
+
|
|
3
|
+
require 'spec_helper'
|
|
4
|
+
|
|
5
|
+
module Rubocop
|
|
6
|
+
module Cop
|
|
7
|
+
describe HashLiteral do
|
|
8
|
+
let(:a) { HashLiteral.new }
|
|
9
|
+
|
|
10
|
+
it 'registers an offence for Hash.new()' do
|
|
11
|
+
inspect_source(a,
|
|
12
|
+
'file.rb',
|
|
13
|
+
['test = Hash.new()'])
|
|
14
|
+
expect(a.offences.size).to eq(1)
|
|
15
|
+
expect(a.offences.map(&:message))
|
|
16
|
+
.to eq([HashLiteral::ERROR_MESSAGE])
|
|
17
|
+
end
|
|
18
|
+
|
|
19
|
+
it 'registers an offence for Hash.new'
|
|
20
|
+
|
|
21
|
+
it 'does not register an offence for Hash.new(3)' do
|
|
22
|
+
inspect_source(a,
|
|
23
|
+
'file.rb',
|
|
24
|
+
['test = Hash.new(3)'])
|
|
25
|
+
expect(a.offences).to be_empty
|
|
26
|
+
end
|
|
27
|
+
end
|
|
28
|
+
end
|
|
29
|
+
end
|
|
@@ -5,39 +5,39 @@ require 'spec_helper'
|
|
|
5
5
|
module Rubocop
|
|
6
6
|
module Cop
|
|
7
7
|
describe HashSyntax do
|
|
8
|
-
let
|
|
8
|
+
let(:hash_syntax) { HashSyntax.new }
|
|
9
9
|
|
|
10
10
|
it 'registers an offence for hash rocket syntax when new is possible' do
|
|
11
11
|
inspect_source(hash_syntax, '', ['x = { :a => 0 }'])
|
|
12
|
-
hash_syntax.offences.map(&:message).
|
|
13
|
-
['Ruby 1.8 hash syntax detected']
|
|
12
|
+
expect(hash_syntax.offences.map(&:message)).to eq(
|
|
13
|
+
['Ruby 1.8 hash syntax detected'])
|
|
14
14
|
end
|
|
15
15
|
|
|
16
16
|
it 'registers an offence for mixed syntax when new is possible' do
|
|
17
17
|
inspect_source(hash_syntax, '', ['x = { :a => 0, b: 1 }'])
|
|
18
|
-
hash_syntax.offences.map(&:message).
|
|
19
|
-
['Ruby 1.8 hash syntax detected']
|
|
18
|
+
expect(hash_syntax.offences.map(&:message)).to eq(
|
|
19
|
+
['Ruby 1.8 hash syntax detected'])
|
|
20
20
|
end
|
|
21
21
|
|
|
22
22
|
it 'registers an offence for hash rockets in method calls' do
|
|
23
23
|
inspect_source(hash_syntax, '', ['func(3, :a => 0)'])
|
|
24
|
-
hash_syntax.offences.map(&:message).
|
|
25
|
-
['Ruby 1.8 hash syntax detected']
|
|
24
|
+
expect(hash_syntax.offences.map(&:message)).to eq(
|
|
25
|
+
['Ruby 1.8 hash syntax detected'])
|
|
26
26
|
end
|
|
27
27
|
|
|
28
28
|
it 'accepts hash rockets when keys have different types' do
|
|
29
29
|
inspect_source(hash_syntax, '', ['x = { :a => 0, "b" => 1 }'])
|
|
30
|
-
hash_syntax.offences.map(&:message).
|
|
30
|
+
expect(hash_syntax.offences.map(&:message)).to be_empty
|
|
31
31
|
end
|
|
32
32
|
|
|
33
33
|
it 'accepts new syntax in a hash literal' do
|
|
34
34
|
inspect_source(hash_syntax, '', ['x = { a: 0, b: 1 }'])
|
|
35
|
-
hash_syntax.offences.map(&:message).
|
|
35
|
+
expect(hash_syntax.offences.map(&:message)).to be_empty
|
|
36
36
|
end
|
|
37
37
|
|
|
38
38
|
it 'accepts new syntax in method calls' do
|
|
39
39
|
inspect_source(hash_syntax, '', ['func(3, a: 0)'])
|
|
40
|
-
hash_syntax.offences.map(&:message).
|
|
40
|
+
expect(hash_syntax.offences.map(&:message)).to be_empty
|
|
41
41
|
end
|
|
42
42
|
end
|
|
43
43
|
end
|
|
@@ -5,12 +5,12 @@ require 'spec_helper'
|
|
|
5
5
|
module Rubocop
|
|
6
6
|
module Cop
|
|
7
7
|
describe IfWithSemicolon do
|
|
8
|
-
let
|
|
8
|
+
let(:iws) { IfWithSemicolon.new }
|
|
9
9
|
|
|
10
10
|
it 'registers an offence for one line if/;/end' do
|
|
11
11
|
inspect_source(iws, '', ['if cond; run else dont end'])
|
|
12
|
-
iws.offences.map(&:message).
|
|
13
|
-
['Never use if x; Use the ternary operator instead.']
|
|
12
|
+
expect(iws.offences.map(&:message)).to eq(
|
|
13
|
+
['Never use if x; Use the ternary operator instead.'])
|
|
14
14
|
end
|
|
15
15
|
end
|
|
16
16
|
end
|
|
@@ -5,7 +5,7 @@ require 'spec_helper'
|
|
|
5
5
|
module Rubocop
|
|
6
6
|
module Cop
|
|
7
7
|
describe Indentation do
|
|
8
|
-
let
|
|
8
|
+
let(:ind) { Indentation.new }
|
|
9
9
|
|
|
10
10
|
it "registers an offence for a when clause that's deeper than case" do
|
|
11
11
|
source = ['case a',
|
|
@@ -15,8 +15,8 @@ module Rubocop
|
|
|
15
15
|
' end',
|
|
16
16
|
'end']
|
|
17
17
|
inspect_source(ind, 'file.rb', source)
|
|
18
|
-
ind.offences.map(&:message).
|
|
19
|
-
['Indent when as deep as case.'] * 2
|
|
18
|
+
expect(ind.offences.map(&:message)).to eq(
|
|
19
|
+
['Indent when as deep as case.'] * 2)
|
|
20
20
|
end
|
|
21
21
|
|
|
22
22
|
it "accepts a when clause that's equally indented with case" do
|
|
@@ -33,7 +33,7 @@ module Rubocop
|
|
|
33
33
|
'end',
|
|
34
34
|
'']
|
|
35
35
|
inspect_source(ind, 'file.rb', source)
|
|
36
|
-
ind.offences.
|
|
36
|
+
expect(ind.offences).to be_empty
|
|
37
37
|
end
|
|
38
38
|
|
|
39
39
|
it "doesn't get confused by strings with case in them" do
|
|
@@ -43,7 +43,7 @@ module Rubocop
|
|
|
43
43
|
'end',
|
|
44
44
|
'']
|
|
45
45
|
inspect_source(ind, 'file.rb', source)
|
|
46
|
-
ind.offences.map(&:message).
|
|
46
|
+
expect(ind.offences.map(&:message)).to be_empty
|
|
47
47
|
end
|
|
48
48
|
|
|
49
49
|
it "doesn't get confused by symbols named case or when" do
|
|
@@ -56,7 +56,7 @@ module Rubocop
|
|
|
56
56
|
'end',
|
|
57
57
|
'']
|
|
58
58
|
inspect_source(ind, 'file.rb', source)
|
|
59
|
-
ind.offences.map(&:message).
|
|
59
|
+
expect(ind.offences.map(&:message)).to be_empty
|
|
60
60
|
end
|
|
61
61
|
|
|
62
62
|
it 'accepts correctly indented whens in complex combinations' do
|
|
@@ -79,7 +79,7 @@ module Rubocop
|
|
|
79
79
|
'end',
|
|
80
80
|
'']
|
|
81
81
|
inspect_source(ind, 'file.rb', source)
|
|
82
|
-
ind.offences.map(&:message).
|
|
82
|
+
expect(ind.offences.map(&:message)).to be_empty
|
|
83
83
|
end
|
|
84
84
|
end
|
|
85
85
|
end
|
|
@@ -5,17 +5,17 @@ require 'spec_helper'
|
|
|
5
5
|
module Rubocop
|
|
6
6
|
module Cop
|
|
7
7
|
describe LineLength do
|
|
8
|
-
let
|
|
8
|
+
let(:ll) { LineLength.new }
|
|
9
9
|
|
|
10
10
|
it "registers an offence for a line that's 80 characters wide" do
|
|
11
11
|
ll.inspect('file.rb', ['#' * 80], nil, nil)
|
|
12
|
-
ll.offences.size.
|
|
13
|
-
ll.offences.first.message.
|
|
12
|
+
expect(ll.offences.size).to eq(1)
|
|
13
|
+
expect(ll.offences.first.message).to eq('Line is too long. [80/79]')
|
|
14
14
|
end
|
|
15
15
|
|
|
16
16
|
it "accepts a line that's 79 characters wide" do
|
|
17
17
|
ll.inspect('file.rb', ['#' * 79], nil, nil)
|
|
18
|
-
ll.offences.
|
|
18
|
+
expect(ll.offences).to be_empty
|
|
19
19
|
end
|
|
20
20
|
end
|
|
21
21
|
end
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
# encoding: utf-8
|
|
2
|
+
|
|
3
|
+
require 'spec_helper'
|
|
4
|
+
|
|
5
|
+
module Rubocop
|
|
6
|
+
module Cop
|
|
7
|
+
describe MethodAndVariableSnakeCase do
|
|
8
|
+
let(:snake_case) { MethodAndVariableSnakeCase.new }
|
|
9
|
+
|
|
10
|
+
it 'registers an offence for camel case in names' do
|
|
11
|
+
inspect_source(snake_case, 'file.rb',
|
|
12
|
+
['def myMethod',
|
|
13
|
+
' myLocal = 1',
|
|
14
|
+
' self.mySetter = 2',
|
|
15
|
+
' @myAttribute = 3',
|
|
16
|
+
'end',
|
|
17
|
+
])
|
|
18
|
+
expect(snake_case.offences.map(&:message)).to eq(
|
|
19
|
+
['Use snake_case for methods and variables.'] * 4)
|
|
20
|
+
end
|
|
21
|
+
|
|
22
|
+
it 'accepts snake case in names' do
|
|
23
|
+
inspect_source(snake_case, 'file.rb',
|
|
24
|
+
['def my_method',
|
|
25
|
+
' my_local_html = 1',
|
|
26
|
+
' self.my_setter = 2',
|
|
27
|
+
' @my_attribute = 3',
|
|
28
|
+
'end',
|
|
29
|
+
])
|
|
30
|
+
expect(snake_case.offences.map(&:message)).to be_empty
|
|
31
|
+
end
|
|
32
|
+
|
|
33
|
+
it 'registers an offence for mixed snake case and camel case' do
|
|
34
|
+
inspect_source(snake_case, 'file.rb',
|
|
35
|
+
['def visit_Arel_Nodes_SelectStatement',
|
|
36
|
+
'end'])
|
|
37
|
+
expect(snake_case.offences.map(&:message)).to eq(
|
|
38
|
+
['Use snake_case for methods and variables.'])
|
|
39
|
+
end
|
|
40
|
+
|
|
41
|
+
it 'accepts screaming snake case globals' do
|
|
42
|
+
inspect_source(snake_case, 'file.rb', ['$MY_GLOBAL = 0'])
|
|
43
|
+
expect(snake_case.offences.map(&:message)).to be_empty
|
|
44
|
+
end
|
|
45
|
+
end
|
|
46
|
+
end
|
|
47
|
+
end
|
|
@@ -0,0 +1,150 @@
|
|
|
1
|
+
# encoding: utf-8
|
|
2
|
+
|
|
3
|
+
require 'spec_helper'
|
|
4
|
+
|
|
5
|
+
module Rubocop
|
|
6
|
+
module Cop
|
|
7
|
+
describe MethodLength do
|
|
8
|
+
let(:method_length) { MethodLength.new }
|
|
9
|
+
before do
|
|
10
|
+
MethodLength.stub(:max).and_return(5)
|
|
11
|
+
MethodLength.stub(:count_comments?).and_return(false)
|
|
12
|
+
end
|
|
13
|
+
|
|
14
|
+
it 'rejects a method with more than 5 lines' do
|
|
15
|
+
inspect_source(method_length, '', ['def m()',
|
|
16
|
+
' a = 1',
|
|
17
|
+
' a = 2',
|
|
18
|
+
' a = 3',
|
|
19
|
+
' a = 4',
|
|
20
|
+
' a = 5',
|
|
21
|
+
' a = 6',
|
|
22
|
+
'end'])
|
|
23
|
+
expect(method_length.offences.size).to eq(1)
|
|
24
|
+
expect(method_length.offences.map(&:line_number).sort).to eq([1])
|
|
25
|
+
end
|
|
26
|
+
|
|
27
|
+
it 'accepts a method with less than 5 lines' do
|
|
28
|
+
inspect_source(method_length, '', ['def m()',
|
|
29
|
+
' a = 1',
|
|
30
|
+
' a = 2',
|
|
31
|
+
' a = 3',
|
|
32
|
+
' a = 4',
|
|
33
|
+
'end'])
|
|
34
|
+
expect(method_length.offences).to be_empty
|
|
35
|
+
end
|
|
36
|
+
|
|
37
|
+
it 'does not count blank lines' do
|
|
38
|
+
inspect_source(method_length, '', ['def m()',
|
|
39
|
+
' a = 1',
|
|
40
|
+
' a = 2',
|
|
41
|
+
' a = 3',
|
|
42
|
+
' a = 4',
|
|
43
|
+
'',
|
|
44
|
+
'',
|
|
45
|
+
' a = 7',
|
|
46
|
+
'end'])
|
|
47
|
+
expect(method_length.offences).to be_empty
|
|
48
|
+
end
|
|
49
|
+
|
|
50
|
+
it 'accepts empty methods' do
|
|
51
|
+
inspect_source(method_length, '', ['def m()',
|
|
52
|
+
'end'])
|
|
53
|
+
expect(method_length.offences).to be_empty
|
|
54
|
+
end
|
|
55
|
+
|
|
56
|
+
it 'is not fooled by one-liner methods, syntax #1' do
|
|
57
|
+
inspect_source(method_length, '', ['def one_line; 10 end',
|
|
58
|
+
'def self.m()',
|
|
59
|
+
' a = 1',
|
|
60
|
+
' a = 2',
|
|
61
|
+
' a = 4',
|
|
62
|
+
' a = 5',
|
|
63
|
+
' a = 6',
|
|
64
|
+
'end'])
|
|
65
|
+
expect(method_length.offences).to be_empty
|
|
66
|
+
end
|
|
67
|
+
|
|
68
|
+
it 'is not fooled by one-liner methods, syntax #2' do
|
|
69
|
+
inspect_source(method_length, '', ['def one_line(test) 10 end',
|
|
70
|
+
'def self.m()',
|
|
71
|
+
' a = 1',
|
|
72
|
+
' a = 2',
|
|
73
|
+
' a = 4',
|
|
74
|
+
' a = 5',
|
|
75
|
+
' a = 6',
|
|
76
|
+
'end'])
|
|
77
|
+
expect(method_length.offences).to be_empty
|
|
78
|
+
end
|
|
79
|
+
|
|
80
|
+
it 'checks class methods, syntax #1' do
|
|
81
|
+
inspect_source(method_length, '', ['def self.m()',
|
|
82
|
+
' a = 1',
|
|
83
|
+
' a = 2',
|
|
84
|
+
' a = 3',
|
|
85
|
+
' a = 4',
|
|
86
|
+
' a = 5',
|
|
87
|
+
' a = 6',
|
|
88
|
+
'end'])
|
|
89
|
+
expect(method_length.offences.size).to eq(1)
|
|
90
|
+
expect(method_length.offences.map(&:line_number).sort).to eq([1])
|
|
91
|
+
end
|
|
92
|
+
|
|
93
|
+
it 'checks class methods, syntax #2' do
|
|
94
|
+
inspect_source(method_length, '', ['class K',
|
|
95
|
+
' class << self',
|
|
96
|
+
' def m()',
|
|
97
|
+
' a = 1',
|
|
98
|
+
' a = 2',
|
|
99
|
+
' a = 3',
|
|
100
|
+
' a = 4',
|
|
101
|
+
' a = 5',
|
|
102
|
+
' a = 6',
|
|
103
|
+
' end',
|
|
104
|
+
' end',
|
|
105
|
+
'end'])
|
|
106
|
+
expect(method_length.offences.size).to eq(1)
|
|
107
|
+
expect(method_length.offences.map(&:line_number).sort).to eq([3])
|
|
108
|
+
end
|
|
109
|
+
|
|
110
|
+
it 'properly counts lines when method ends with block' do
|
|
111
|
+
inspect_source(method_length, '', ['def m()',
|
|
112
|
+
' do',
|
|
113
|
+
' a = 2',
|
|
114
|
+
' a = 3',
|
|
115
|
+
' a = 4',
|
|
116
|
+
' a = 5',
|
|
117
|
+
' end',
|
|
118
|
+
'end'])
|
|
119
|
+
expect(method_length.offences.size).to eq(1)
|
|
120
|
+
expect(method_length.offences.map(&:line_number).sort).to eq([1])
|
|
121
|
+
end
|
|
122
|
+
|
|
123
|
+
it 'does not count commented lines by default' do
|
|
124
|
+
inspect_source(method_length, '', ['def m()',
|
|
125
|
+
' a = 1',
|
|
126
|
+
' #a = 2',
|
|
127
|
+
' a = 3',
|
|
128
|
+
' #a = 4',
|
|
129
|
+
' a = 5',
|
|
130
|
+
' a = 6',
|
|
131
|
+
'end'])
|
|
132
|
+
expect(method_length.offences).to be_empty
|
|
133
|
+
end
|
|
134
|
+
|
|
135
|
+
it 'has the option of counting commented lines' do
|
|
136
|
+
MethodLength.stub(:count_comments?).and_return(true)
|
|
137
|
+
inspect_source(method_length, '', ['def m()',
|
|
138
|
+
' a = 1',
|
|
139
|
+
' #a = 2',
|
|
140
|
+
' a = 3',
|
|
141
|
+
' #a = 4',
|
|
142
|
+
' a = 5',
|
|
143
|
+
' a = 6',
|
|
144
|
+
'end'])
|
|
145
|
+
expect(method_length.offences.size).to eq(1)
|
|
146
|
+
expect(method_length.offences.map(&:line_number).sort).to eq([1])
|
|
147
|
+
end
|
|
148
|
+
end
|
|
149
|
+
end
|
|
150
|
+
end
|
|
@@ -5,19 +5,19 @@ require 'spec_helper'
|
|
|
5
5
|
module Rubocop
|
|
6
6
|
module Cop
|
|
7
7
|
describe MultilineBlocks do
|
|
8
|
-
let
|
|
8
|
+
let(:blocks) { MultilineBlocks.new }
|
|
9
9
|
|
|
10
10
|
it 'registers an offence for a multiline block with braces' do
|
|
11
11
|
inspect_source(blocks, '', ['each { |x|',
|
|
12
12
|
'}'])
|
|
13
|
-
blocks.offences.map(&:message).
|
|
14
|
-
['Avoid using {...} for multi-line blocks.']
|
|
13
|
+
expect(blocks.offences.map(&:message)).to eq(
|
|
14
|
+
['Avoid using {...} for multi-line blocks.'])
|
|
15
15
|
end
|
|
16
16
|
|
|
17
17
|
it 'accepts a multiline block with do-end' do
|
|
18
18
|
inspect_source(blocks, '', ['each do |x|',
|
|
19
19
|
'end'])
|
|
20
|
-
blocks.offences.map(&:message).
|
|
20
|
+
expect(blocks.offences.map(&:message)).to be_empty
|
|
21
21
|
end
|
|
22
22
|
end
|
|
23
23
|
end
|
|
@@ -5,7 +5,7 @@ require 'spec_helper'
|
|
|
5
5
|
module Rubocop
|
|
6
6
|
module Cop
|
|
7
7
|
describe MultilineIfThen do
|
|
8
|
-
let
|
|
8
|
+
let(:mit) { MultilineIfThen.new }
|
|
9
9
|
|
|
10
10
|
# if
|
|
11
11
|
|
|
@@ -18,13 +18,13 @@ module Rubocop
|
|
|
18
18
|
'end',
|
|
19
19
|
'if cond then # bad',
|
|
20
20
|
'end'])
|
|
21
|
-
mit.offences.map(&:line_number).
|
|
21
|
+
expect(mit.offences.map(&:line_number)).to eq([1, 3, 5, 7])
|
|
22
22
|
end
|
|
23
23
|
|
|
24
24
|
it 'accepts multiline if without then' do
|
|
25
25
|
inspect_source(mit, '', ['if cond',
|
|
26
26
|
'end'])
|
|
27
|
-
mit.offences.map(&:message).
|
|
27
|
+
expect(mit.offences.map(&:message)).to be_empty
|
|
28
28
|
end
|
|
29
29
|
|
|
30
30
|
it 'accepts table style if/then/elsif/ends' do
|
|
@@ -34,7 +34,7 @@ module Rubocop
|
|
|
34
34
|
'elsif @io == $stderr then str << "$stderr"',
|
|
35
35
|
'else str << @io.class.to_s',
|
|
36
36
|
'end'])
|
|
37
|
-
mit.offences.map(&:message).
|
|
37
|
+
expect(mit.offences.map(&:message)).to be_empty
|
|
38
38
|
end
|
|
39
39
|
|
|
40
40
|
# unless
|
|
@@ -42,14 +42,14 @@ module Rubocop
|
|
|
42
42
|
it 'registers an offence for then in multiline unless' do
|
|
43
43
|
inspect_source(mit, '', ['unless cond then',
|
|
44
44
|
'end'])
|
|
45
|
-
mit.offences.map(&:message).
|
|
46
|
-
['Never use then for multi-line if/unless.']
|
|
45
|
+
expect(mit.offences.map(&:message)).to eq(
|
|
46
|
+
['Never use then for multi-line if/unless.'])
|
|
47
47
|
end
|
|
48
48
|
|
|
49
49
|
it 'accepts multiline unless without then' do
|
|
50
50
|
inspect_source(mit, '', ['unless cond',
|
|
51
51
|
'end'])
|
|
52
|
-
mit.offences.map(&:message).
|
|
52
|
+
expect(mit.offences.map(&:message)).to be_empty
|
|
53
53
|
end
|
|
54
54
|
end
|
|
55
55
|
end
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
# encoding: utf-8
|
|
2
|
+
|
|
3
|
+
require 'spec_helper'
|
|
4
|
+
|
|
5
|
+
module Rubocop
|
|
6
|
+
module Cop
|
|
7
|
+
describe NewLambdaLiteral do
|
|
8
|
+
let(:lambda_literal) { NewLambdaLiteral.new }
|
|
9
|
+
|
|
10
|
+
it 'registers an offence for an old lambda call' do
|
|
11
|
+
inspect_source(lambda_literal, 'file.rb', ['f = lambda { |x| x }'])
|
|
12
|
+
expect(lambda_literal.offences.map(&:message)).to eq(
|
|
13
|
+
['The new lambda literal syntax is preferred in Ruby 1.9.'])
|
|
14
|
+
end
|
|
15
|
+
|
|
16
|
+
it 'accepts the new lambda literal' do
|
|
17
|
+
inspect_source(lambda_literal, 'file.rb', ['lambda = ->(x) { x }',
|
|
18
|
+
'lambda.(1)'])
|
|
19
|
+
expect(lambda_literal.offences.map(&:message)).to be_empty
|
|
20
|
+
end
|
|
21
|
+
end
|
|
22
|
+
end
|
|
23
|
+
end
|
|
@@ -5,44 +5,44 @@ require 'spec_helper'
|
|
|
5
5
|
module Rubocop
|
|
6
6
|
module Cop
|
|
7
7
|
describe NumericLiterals do
|
|
8
|
-
let
|
|
8
|
+
let(:num) { NumericLiterals.new }
|
|
9
9
|
|
|
10
10
|
it 'registers an offence for a long integer without underscores' do
|
|
11
11
|
inspect_source(num, 'file.rb', ['a = 123456'])
|
|
12
|
-
num.offences.map(&:message).
|
|
12
|
+
expect(num.offences.map(&:message)).to eq(
|
|
13
13
|
['Add underscores to large numeric literals to improve their ' +
|
|
14
|
-
'readability.']
|
|
14
|
+
'readability.'])
|
|
15
15
|
end
|
|
16
16
|
|
|
17
17
|
it 'registers an offence for an integer with not enough underscores' do
|
|
18
18
|
inspect_source(num, 'file.rb', ['a = 123456_789000'])
|
|
19
|
-
num.offences.map(&:message).
|
|
19
|
+
expect(num.offences.map(&:message)).to eq(
|
|
20
20
|
['Add underscores to large numeric literals to improve their ' +
|
|
21
|
-
'readability.']
|
|
21
|
+
'readability.'])
|
|
22
22
|
end
|
|
23
23
|
|
|
24
24
|
it 'registers an offence for a long float without underscores' do
|
|
25
25
|
inspect_source(num, 'file.rb', ['a = 1.234567'])
|
|
26
|
-
num.offences.map(&:message).
|
|
26
|
+
expect(num.offences.map(&:message)).to eq(
|
|
27
27
|
['Add underscores to large numeric literals to improve their ' +
|
|
28
|
-
'readability.']
|
|
28
|
+
'readability.'])
|
|
29
29
|
end
|
|
30
30
|
|
|
31
31
|
it 'accepts long numbers with underscore' do
|
|
32
32
|
inspect_source(num, 'file.rb', ['a = 123_456',
|
|
33
33
|
'b = 1.234_56'])
|
|
34
|
-
num.offences.map(&:message).
|
|
34
|
+
expect(num.offences.map(&:message)).to be_empty
|
|
35
35
|
end
|
|
36
36
|
|
|
37
37
|
it 'accepts a short integer without underscore' do
|
|
38
38
|
inspect_source(num, 'file.rb', ['a = 123'])
|
|
39
|
-
num.offences.map(&:message).
|
|
39
|
+
expect(num.offences.map(&:message)).to be_empty
|
|
40
40
|
end
|
|
41
41
|
|
|
42
42
|
it 'accepts short numbers without underscore' do
|
|
43
43
|
inspect_source(num, 'file.rb', ['a = 123',
|
|
44
44
|
'b = 123.456'])
|
|
45
|
-
num.offences.map(&:message).
|
|
45
|
+
expect(num.offences.map(&:message)).to be_empty
|
|
46
46
|
end
|
|
47
47
|
end
|
|
48
48
|
end
|
|
@@ -8,15 +8,21 @@ module Rubocop
|
|
|
8
8
|
it 'has a few required attributes' do
|
|
9
9
|
offence = Offence.new(:convention, 1, 'message')
|
|
10
10
|
|
|
11
|
-
offence.severity.
|
|
12
|
-
offence.line_number.
|
|
13
|
-
offence.message.
|
|
11
|
+
expect(offence.severity).to eq(:convention)
|
|
12
|
+
expect(offence.line_number).to eq(1)
|
|
13
|
+
expect(offence.message).to eq('message')
|
|
14
14
|
end
|
|
15
15
|
|
|
16
16
|
it 'overrides #to_s' do
|
|
17
17
|
offence = Offence.new(:convention, 1, 'message')
|
|
18
18
|
|
|
19
|
-
offence.to_s.
|
|
19
|
+
expect(offence.to_s).to eq('C: 1: message')
|
|
20
|
+
end
|
|
21
|
+
|
|
22
|
+
it 'does not blow up if a message contains %' do
|
|
23
|
+
offence = Offence.new(:convention, 1, 'message % test')
|
|
24
|
+
|
|
25
|
+
expect(offence.to_s).to eq('C: 1: message % test')
|
|
20
26
|
end
|
|
21
27
|
end
|
|
22
28
|
end
|
|
@@ -5,12 +5,11 @@ require 'spec_helper'
|
|
|
5
5
|
module Rubocop
|
|
6
6
|
module Cop
|
|
7
7
|
describe OneLineConditional do
|
|
8
|
-
let
|
|
8
|
+
let(:olc) { OneLineConditional.new }
|
|
9
9
|
|
|
10
10
|
it 'registers an offence for one line if/then/end' do
|
|
11
11
|
inspect_source(olc, '', ['if cond then run else dont end'])
|
|
12
|
-
olc.offences.map(&:message).
|
|
13
|
-
['Favor the ternary operator (?:) over if/then/else/end constructs.']
|
|
12
|
+
expect(olc.offences.map(&:message)).to eq([olc.error_message])
|
|
14
13
|
end
|
|
15
14
|
end
|
|
16
15
|
end
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
# encoding: utf-8
|
|
2
|
+
|
|
3
|
+
require 'spec_helper'
|
|
4
|
+
|
|
5
|
+
module Rubocop
|
|
6
|
+
module Cop
|
|
7
|
+
describe OpMethod do
|
|
8
|
+
let(:om) { OpMethod.new }
|
|
9
|
+
|
|
10
|
+
it 'registers an offence for arg not named other' do
|
|
11
|
+
inspect_source(om,
|
|
12
|
+
'file.rb',
|
|
13
|
+
['def +(another)',
|
|
14
|
+
' another',
|
|
15
|
+
'end'])
|
|
16
|
+
expect(om.offences.size).to eq(1)
|
|
17
|
+
expect(om.offences.map(&:message))
|
|
18
|
+
.to eq([sprintf(OpMethod::ERROR_MESSAGE, '+')])
|
|
19
|
+
end
|
|
20
|
+
|
|
21
|
+
it 'does not register an offence for arg named other' do
|
|
22
|
+
inspect_source(om,
|
|
23
|
+
'file.rb',
|
|
24
|
+
['def +(other)',
|
|
25
|
+
' other',
|
|
26
|
+
'end'])
|
|
27
|
+
expect(om.offences).to be_empty
|
|
28
|
+
end
|
|
29
|
+
|
|
30
|
+
it 'does not register an offence for []' do
|
|
31
|
+
inspect_source(om,
|
|
32
|
+
'file.rb',
|
|
33
|
+
['def [](index)',
|
|
34
|
+
' other',
|
|
35
|
+
'end'])
|
|
36
|
+
expect(om.offences).to be_empty
|
|
37
|
+
end
|
|
38
|
+
|
|
39
|
+
it 'does not register an offence for []=' do
|
|
40
|
+
inspect_source(om,
|
|
41
|
+
'file.rb',
|
|
42
|
+
['def []=(index, value)',
|
|
43
|
+
' other',
|
|
44
|
+
'end'])
|
|
45
|
+
expect(om.offences).to be_empty
|
|
46
|
+
end
|
|
47
|
+
|
|
48
|
+
it 'does not register an offence for <<' do
|
|
49
|
+
inspect_source(om,
|
|
50
|
+
'file.rb',
|
|
51
|
+
['def <<(cop)',
|
|
52
|
+
' other',
|
|
53
|
+
'end'])
|
|
54
|
+
expect(om.offences).to be_empty
|
|
55
|
+
end
|
|
56
|
+
end
|
|
57
|
+
end
|
|
58
|
+
end
|
|
@@ -5,19 +5,19 @@ require 'spec_helper'
|
|
|
5
5
|
module Rubocop
|
|
6
6
|
module Cop
|
|
7
7
|
describe ParameterLists do
|
|
8
|
-
let
|
|
8
|
+
let(:list) { ParameterLists.new }
|
|
9
9
|
|
|
10
10
|
it 'registers an offence for a method def with 5 parameters' do
|
|
11
11
|
inspect_source(list, 'file.rb', ['def meth(a, b, c, d, e)',
|
|
12
12
|
'end'])
|
|
13
|
-
list.offences.map(&:message).
|
|
14
|
-
['Avoid parameter lists longer than four parameters.']
|
|
13
|
+
expect(list.offences.map(&:message)).to eq(
|
|
14
|
+
['Avoid parameter lists longer than four parameters.'])
|
|
15
15
|
end
|
|
16
16
|
|
|
17
17
|
it 'accepts a method def with 4 parameters' do
|
|
18
18
|
inspect_source(list, 'file.rb', ['def meth(a, b, c, d)',
|
|
19
19
|
'end'])
|
|
20
|
-
list.offences.map(&:message).
|
|
20
|
+
expect(list.offences.map(&:message)).to be_empty
|
|
21
21
|
end
|
|
22
22
|
end
|
|
23
23
|
end
|