rubocop 0.28.0 → 0.29.0
Sign up to get free protection for your applications and to get access to all the features.
Potentially problematic release.
This version of rubocop might be problematic. Click here for more details.
- checksums.yaml +4 -4
- data/.rubocop_todo.yml +7 -7
- data/.travis.yml +4 -0
- data/CHANGELOG.md +59 -0
- data/README.md +62 -28
- data/config/default.yml +31 -0
- data/config/disabled.yml +1 -1
- data/config/enabled.yml +17 -0
- data/lib/rubocop.rb +4 -0
- data/lib/rubocop/cli.rb +1 -1
- data/lib/rubocop/config.rb +12 -8
- data/lib/rubocop/config_loader.rb +20 -10
- data/lib/rubocop/cop/cop.rb +13 -7
- data/lib/rubocop/cop/corrector.rb +10 -10
- data/lib/rubocop/cop/lint/assignment_in_condition.rb +6 -1
- data/lib/rubocop/cop/lint/block_alignment.rb +9 -2
- data/lib/rubocop/cop/lint/debugger.rb +13 -1
- data/lib/rubocop/cop/lint/duplicate_methods.rb +104 -0
- data/lib/rubocop/cop/lint/parentheses_as_grouped_expression.rb +1 -1
- data/lib/rubocop/cop/lint/require_parentheses.rb +3 -3
- data/lib/rubocop/cop/mixin/access_modifier_node.rb +27 -0
- data/lib/rubocop/cop/mixin/on_normal_if_unless.rb +0 -4
- data/lib/rubocop/cop/rails/delegate.rb +4 -5
- data/lib/rubocop/cop/rails/read_write_attribute.rb +33 -0
- data/lib/rubocop/cop/style/access_modifier_indentation.rb +0 -7
- data/lib/rubocop/cop/style/braces_around_hash_parameters.rb +4 -2
- data/lib/rubocop/cop/style/class_methods.rb +25 -9
- data/lib/rubocop/cop/style/empty_lines_around_access_modifier.rb +7 -1
- data/lib/rubocop/cop/style/empty_literal.rb +25 -10
- data/lib/rubocop/cop/style/even_odd.rb +32 -14
- data/lib/rubocop/cop/style/first_parameter_indentation.rb +106 -0
- data/lib/rubocop/cop/style/format_string.rb +2 -2
- data/lib/rubocop/cop/style/global_vars.rb +1 -5
- data/lib/rubocop/cop/style/hash_syntax.rb +0 -4
- data/lib/rubocop/cop/style/indentation_consistency.rb +5 -5
- data/lib/rubocop/cop/style/indentation_width.rb +13 -14
- data/lib/rubocop/cop/style/lambda.rb +48 -2
- data/lib/rubocop/cop/style/line_end_concatenation.rb +43 -47
- data/lib/rubocop/cop/style/method_called_on_do_end_block.rb +3 -3
- data/lib/rubocop/cop/style/module_function.rb +3 -3
- data/lib/rubocop/cop/style/multiline_block_layout.rb +65 -17
- data/lib/rubocop/cop/style/multiline_operation_indentation.rb +2 -1
- data/lib/rubocop/cop/style/perl_backrefs.rb +2 -2
- data/lib/rubocop/cop/style/redundant_exception.rb +12 -0
- data/lib/rubocop/cop/style/self_assignment.rb +27 -0
- data/lib/rubocop/cop/style/semicolon.rb +2 -1
- data/lib/rubocop/cop/style/space_around_block_parameters.rb +92 -0
- data/lib/rubocop/cop/style/string_literals_in_interpolation.rb +0 -8
- data/lib/rubocop/cop/style/struct_inheritance.rb +42 -0
- data/lib/rubocop/cop/style/trivial_accessors.rb +10 -4
- data/lib/rubocop/cop/util.rb +32 -7
- data/lib/rubocop/formatter/simple_text_formatter.rb +0 -15
- data/lib/rubocop/options.rb +28 -41
- data/lib/rubocop/processed_source.rb +6 -0
- data/lib/rubocop/rake_task.rb +6 -3
- data/lib/rubocop/runner.rb +55 -15
- data/lib/rubocop/version.rb +1 -1
- data/relnotes/v0.29.0.md +116 -0
- data/rubocop.gemspec +3 -3
- data/spec/rubocop/cli_spec.rb +329 -25
- data/spec/rubocop/config_loader_spec.rb +20 -0
- data/spec/rubocop/cop/lint/assignment_in_condition_spec.rb +27 -3
- data/spec/rubocop/cop/lint/block_alignment_spec.rb +3 -4
- data/spec/rubocop/cop/lint/condition_position_spec.rb +1 -1
- data/spec/rubocop/cop/lint/debugger_spec.rb +20 -7
- data/spec/rubocop/cop/lint/duplicate_methods_spec.rb +189 -0
- data/spec/rubocop/cop/lint/empty_interpolation_spec.rb +1 -1
- data/spec/rubocop/cop/lint/end_in_method_spec.rb +1 -1
- data/spec/rubocop/cop/lint/eval_spec.rb +4 -4
- data/spec/rubocop/cop/lint/literal_in_condition_spec.rb +18 -0
- data/spec/rubocop/cop/lint/literal_in_interpolation_spec.rb +2 -2
- data/spec/rubocop/cop/lint/loop_spec.rb +4 -4
- data/spec/rubocop/cop/lint/parentheses_as_grouped_expression_spec.rb +7 -7
- data/spec/rubocop/cop/lint/space_before_first_arg_spec.rb +5 -4
- data/spec/rubocop/cop/lint/useless_assignment_spec.rb +3 -9
- data/spec/rubocop/cop/lint/useless_comparison_spec.rb +1 -1
- data/spec/rubocop/cop/lint/void_spec.rb +1 -1
- data/spec/rubocop/cop/metrics/abc_size_spec.rb +10 -0
- data/spec/rubocop/cop/metrics/line_length_spec.rb +2 -2
- data/spec/rubocop/cop/metrics/parameter_lists_spec.rb +2 -2
- data/spec/rubocop/cop/rails/action_filter_spec.rb +6 -12
- data/spec/rubocop/cop/rails/default_scope_spec.rb +5 -5
- data/spec/rubocop/cop/rails/delegate_spec.rb +8 -0
- data/spec/rubocop/cop/rails/has_and_belongs_to_many_spec.rb +1 -1
- data/spec/rubocop/cop/rails/read_write_attribute_spec.rb +119 -8
- data/spec/rubocop/cop/rails/scope_args_spec.rb +3 -3
- data/spec/rubocop/cop/rails/validation_spec.rb +3 -3
- data/spec/rubocop/cop/style/alias_spec.rb +5 -5
- data/spec/rubocop/cop/style/align_hash_spec.rb +1 -1
- data/spec/rubocop/cop/style/align_parameters_spec.rb +8 -8
- data/spec/rubocop/cop/style/and_or_spec.rb +15 -30
- data/spec/rubocop/cop/style/array_join_spec.rb +4 -4
- data/spec/rubocop/cop/style/ascii_comments_spec.rb +1 -2
- data/spec/rubocop/cop/style/ascii_identifiers_spec.rb +2 -2
- data/spec/rubocop/cop/style/begin_block_spec.rb +1 -1
- data/spec/rubocop/cop/style/block_comments_spec.rb +1 -1
- data/spec/rubocop/cop/style/block_end_newline_spec.rb +1 -1
- data/spec/rubocop/cop/style/blocks_spec.rb +2 -2
- data/spec/rubocop/cop/style/braces_around_hash_parameters_spec.rb +38 -33
- data/spec/rubocop/cop/style/case_equality_spec.rb +1 -1
- data/spec/rubocop/cop/style/character_literal_spec.rb +4 -4
- data/spec/rubocop/cop/style/class_and_module_children_spec.rb +4 -2
- data/spec/rubocop/cop/style/class_methods_spec.rb +12 -0
- data/spec/rubocop/cop/style/class_vars_spec.rb +2 -2
- data/spec/rubocop/cop/style/collection_methods_spec.rb +4 -4
- data/spec/rubocop/cop/style/colon_method_call_spec.rb +8 -8
- data/spec/rubocop/cop/style/comment_annotation_spec.rb +10 -10
- data/spec/rubocop/cop/style/constant_name_spec.rb +7 -7
- data/spec/rubocop/cop/style/def_with_parentheses_spec.rb +1 -1
- data/spec/rubocop/cop/style/deprecated_hash_methods_spec.rb +4 -4
- data/spec/rubocop/cop/style/dot_position_spec.rb +8 -6
- data/spec/rubocop/cop/style/each_with_object_spec.rb +2 -2
- data/spec/rubocop/cop/style/else_alignment_spec.rb +2 -4
- data/spec/rubocop/cop/style/empty_lines_around_access_modifier_spec.rb +78 -0
- data/spec/rubocop/cop/style/empty_lines_around_class_body_spec.rb +60 -0
- data/spec/rubocop/cop/style/empty_lines_spec.rb +3 -3
- data/spec/rubocop/cop/style/empty_literal_spec.rb +29 -12
- data/spec/rubocop/cop/style/encoding_spec.rb +3 -3
- data/spec/rubocop/cop/style/end_block_spec.rb +1 -1
- data/spec/rubocop/cop/style/end_of_line_spec.rb +2 -2
- data/spec/rubocop/cop/style/even_odd_spec.rb +109 -20
- data/spec/rubocop/cop/style/extra_spacing_spec.rb +3 -3
- data/spec/rubocop/cop/style/first_parameter_indentation_spec.rb +293 -0
- data/spec/rubocop/cop/style/for_spec.rb +2 -2
- data/spec/rubocop/cop/style/format_string_spec.rb +45 -21
- data/spec/rubocop/cop/style/global_vars_spec.rb +4 -4
- data/spec/rubocop/cop/style/guard_clause_spec.rb +17 -0
- data/spec/rubocop/cop/style/hash_syntax_spec.rb +15 -15
- data/spec/rubocop/cop/{metrics → style}/if_unless_modifier_spec.rb +2 -2
- data/spec/rubocop/cop/style/if_with_semicolon_spec.rb +2 -2
- data/spec/rubocop/cop/style/indent_array_spec.rb +3 -6
- data/spec/rubocop/cop/style/indent_hash_spec.rb +4 -4
- data/spec/rubocop/cop/style/indentation_consistency_spec.rb +1 -2
- data/spec/rubocop/cop/style/indentation_width_spec.rb +1 -2
- data/spec/rubocop/cop/style/infinite_loop_spec.rb +1 -1
- data/spec/rubocop/cop/style/lambda_call_spec.rb +4 -4
- data/spec/rubocop/cop/style/lambda_spec.rb +37 -2
- data/spec/rubocop/cop/style/leading_comment_space_spec.rb +7 -12
- data/spec/rubocop/cop/style/line_end_concatenation_spec.rb +41 -1
- data/spec/rubocop/cop/style/method_call_parentheses_spec.rb +4 -4
- data/spec/rubocop/cop/style/method_called_on_do_end_block_spec.rb +3 -3
- data/spec/rubocop/cop/style/method_name_spec.rb +1 -1
- data/spec/rubocop/cop/style/multiline_block_layout_spec.rb +61 -0
- data/spec/rubocop/cop/style/multiline_if_then_spec.rb +1 -3
- data/spec/rubocop/cop/style/multiline_operation_indentation_spec.rb +8 -0
- data/spec/rubocop/cop/style/multiline_ternary_operator_spec.rb +1 -1
- data/spec/rubocop/cop/style/nested_ternary_operator_spec.rb +1 -1
- data/spec/rubocop/cop/style/next_spec.rb +16 -0
- data/spec/rubocop/cop/style/numeric_literals_spec.rb +5 -5
- data/spec/rubocop/cop/style/one_line_conditional_spec.rb +1 -1
- data/spec/rubocop/cop/style/parentheses_around_condition_spec.rb +22 -4
- data/spec/rubocop/cop/style/percent_literal_delimiters_spec.rb +31 -31
- data/spec/rubocop/cop/style/percent_q_literals_spec.rb +12 -12
- data/spec/rubocop/cop/style/perl_backrefs_spec.rb +3 -3
- data/spec/rubocop/cop/style/proc_spec.rb +3 -3
- data/spec/rubocop/cop/style/raise_args_spec.rb +9 -9
- data/spec/rubocop/cop/style/redundant_begin_spec.rb +1 -1
- data/spec/rubocop/cop/style/redundant_exception_spec.rb +36 -4
- data/spec/rubocop/cop/style/redundant_self_spec.rb +89 -45
- data/spec/rubocop/cop/style/regexp_literal_spec.rb +9 -9
- data/spec/rubocop/cop/style/rescue_modifier_spec.rb +2 -2
- data/spec/rubocop/cop/style/self_assignment_spec.rb +16 -10
- data/spec/rubocop/cop/style/semicolon_spec.rb +9 -9
- data/spec/rubocop/cop/style/single_line_block_params_spec.rb +2 -2
- data/spec/rubocop/cop/style/single_space_before_first_arg_spec.rb +1 -1
- data/spec/rubocop/cop/style/space_after_colon_spec.rb +5 -5
- data/spec/rubocop/cop/style/space_after_comma_spec.rb +3 -3
- data/spec/rubocop/cop/style/space_after_control_keyword_spec.rb +4 -4
- data/spec/rubocop/cop/style/space_after_not_spec.rb +2 -2
- data/spec/rubocop/cop/style/space_after_semicolon_spec.rb +2 -2
- data/spec/rubocop/cop/style/space_around_block_parameters_spec.rb +150 -0
- data/spec/rubocop/cop/style/space_around_equals_in_parameter_default_spec.rb +18 -9
- data/spec/rubocop/cop/style/space_around_operators_spec.rb +24 -21
- data/spec/rubocop/cop/style/space_before_block_braces_spec.rb +4 -4
- data/spec/rubocop/cop/style/space_before_comma_spec.rb +4 -4
- data/spec/rubocop/cop/style/space_before_comment_spec.rb +3 -3
- data/spec/rubocop/cop/style/space_before_semicolon_spec.rb +2 -2
- data/spec/rubocop/cop/style/space_inside_block_braces_spec.rb +33 -24
- data/spec/rubocop/cop/style/space_inside_brackets_spec.rb +2 -2
- data/spec/rubocop/cop/style/space_inside_hash_literal_braces_spec.rb +9 -9
- data/spec/rubocop/cop/style/space_inside_parens_spec.rb +1 -1
- data/spec/rubocop/cop/style/special_global_vars_spec.rb +6 -6
- data/spec/rubocop/cop/style/string_literals_in_interpolation_spec.rb +3 -3
- data/spec/rubocop/cop/style/string_literals_spec.rb +16 -16
- data/spec/rubocop/cop/style/struct_inheritance_spec.rb +44 -0
- data/spec/rubocop/cop/style/symbol_array_spec.rb +9 -9
- data/spec/rubocop/cop/style/symbol_proc_spec.rb +12 -12
- data/spec/rubocop/cop/style/tab_spec.rb +4 -4
- data/spec/rubocop/cop/style/trailing_blank_lines_spec.rb +2 -2
- data/spec/rubocop/cop/style/trailing_whitespace_spec.rb +2 -2
- data/spec/rubocop/cop/style/trivial_accessors_spec.rb +16 -0
- data/spec/rubocop/cop/style/unneeded_capital_w_spec.rb +11 -22
- data/spec/rubocop/cop/style/variable_interpolation_spec.rb +7 -7
- data/spec/rubocop/cop/style/while_until_do_spec.rb +2 -2
- data/spec/rubocop/cop/{metrics → style}/while_until_modifier_spec.rb +2 -2
- data/spec/rubocop/cop/style/word_array_spec.rb +11 -11
- data/spec/rubocop/cop/util_spec.rb +51 -0
- data/spec/rubocop/cop/variable_force/reference_spec.rb +19 -0
- data/spec/rubocop/cop/variable_force/variable_table_spec.rb +7 -0
- data/spec/rubocop/formatter/disabled_lines_formatter_spec.rb +7 -8
- data/spec/rubocop/formatter/text_util_spec.rb +55 -0
- data/spec/rubocop/options_spec.rb +26 -20
- data/spec/rubocop/rake_task_spec.rb +122 -0
- data/spec/rubocop/runner_spec.rb +37 -2
- data/spec/rubocop/token_spec.rb +5 -1
- data/spec/spec_helper.rb +5 -2
- data/spec/support/cop_helper.rb +3 -0
- data/spec/support/cops/class_must_be_a_module_cop.rb +19 -0
- data/spec/support/cops/module_must_be_a_class_cop.rb +19 -0
- data/spec/support/custom_matchers.rb +1 -1
- metadata +35 -12
@@ -36,6 +36,35 @@ describe RuboCop::Cop::Style::EmptyLinesAroundClassBody, :config do
|
|
36
36
|
expect(cop.messages)
|
37
37
|
.to eq(['Extra empty line detected at class body end.'])
|
38
38
|
end
|
39
|
+
|
40
|
+
it 'registers an offense for singleton class body starting with a blank' do
|
41
|
+
inspect_source(cop,
|
42
|
+
['class << self',
|
43
|
+
'',
|
44
|
+
' do_something',
|
45
|
+
'end'])
|
46
|
+
expect(cop.messages)
|
47
|
+
.to eq(['Extra empty line detected at class body beginning.'])
|
48
|
+
end
|
49
|
+
|
50
|
+
it 'autocorrects singleton class body containing only a blank' do
|
51
|
+
corrected = autocorrect_source(cop,
|
52
|
+
['class << self',
|
53
|
+
'',
|
54
|
+
'end'])
|
55
|
+
expect(corrected).to eq ['class << self',
|
56
|
+
'end'].join("\n")
|
57
|
+
end
|
58
|
+
|
59
|
+
it 'registers an offense for singleton class body ending with a blank' do
|
60
|
+
inspect_source(cop,
|
61
|
+
['class << self',
|
62
|
+
' do_something',
|
63
|
+
'',
|
64
|
+
'end'])
|
65
|
+
expect(cop.messages)
|
66
|
+
.to eq(['Extra empty line detected at class body end.'])
|
67
|
+
end
|
39
68
|
end
|
40
69
|
|
41
70
|
context 'when EnforcedStyle is empty_lines' do
|
@@ -71,5 +100,36 @@ describe RuboCop::Cop::Style::EmptyLinesAroundClassBody, :config do
|
|
71
100
|
'',
|
72
101
|
'end'].join("\n"))
|
73
102
|
end
|
103
|
+
|
104
|
+
it 'registers an offense for singleton class body not starting or ending ' \
|
105
|
+
'with a blank' do
|
106
|
+
inspect_source(cop,
|
107
|
+
['class << self',
|
108
|
+
' do_something',
|
109
|
+
'end'])
|
110
|
+
expect(cop.messages).to eq(['Empty line missing at class body beginning.',
|
111
|
+
'Empty line missing at class body end.'])
|
112
|
+
end
|
113
|
+
|
114
|
+
it 'autocorrects singleton class body containing nothing' do
|
115
|
+
corrected = autocorrect_source(cop,
|
116
|
+
['class << self',
|
117
|
+
'end'])
|
118
|
+
expect(corrected).to eq ['class << self',
|
119
|
+
'',
|
120
|
+
'end'].join("\n")
|
121
|
+
end
|
122
|
+
|
123
|
+
it 'autocorrects beginning and end' do
|
124
|
+
new_source = autocorrect_source(cop,
|
125
|
+
['class << self',
|
126
|
+
' do_something',
|
127
|
+
'end'])
|
128
|
+
expect(new_source).to eq(['class << self',
|
129
|
+
'',
|
130
|
+
' do_something',
|
131
|
+
'',
|
132
|
+
'end'].join("\n"))
|
133
|
+
end
|
74
134
|
end
|
75
135
|
end
|
@@ -19,7 +19,7 @@ describe RuboCop::Cop::Style::EmptyLines do
|
|
19
19
|
|
20
20
|
it 'works when there are no tokens' do
|
21
21
|
inspect_source(cop,
|
22
|
-
|
22
|
+
'#comment')
|
23
23
|
expect(cop.offenses).to be_empty
|
24
24
|
end
|
25
25
|
|
@@ -30,11 +30,11 @@ describe RuboCop::Cop::Style::EmptyLines do
|
|
30
30
|
end
|
31
31
|
|
32
32
|
it 'does not register an offense for empty lines in a string' do
|
33
|
-
inspect_source(cop,
|
33
|
+
inspect_source(cop, 'result = "test
|
34
34
|
|
35
35
|
|
36
36
|
|
37
|
-
string"'
|
37
|
+
string"')
|
38
38
|
expect(cop.offenses).to be_empty
|
39
39
|
end
|
40
40
|
|
@@ -8,7 +8,7 @@ describe RuboCop::Cop::Style::EmptyLiteral do
|
|
8
8
|
describe 'Empty Array' do
|
9
9
|
it 'registers an offense for Array.new()' do
|
10
10
|
inspect_source(cop,
|
11
|
-
|
11
|
+
'test = Array.new()')
|
12
12
|
expect(cop.offenses.size).to eq(1)
|
13
13
|
expect(cop.messages)
|
14
14
|
.to eq(['Use array literal `[]` instead of `Array.new`.'])
|
@@ -16,7 +16,7 @@ describe RuboCop::Cop::Style::EmptyLiteral do
|
|
16
16
|
|
17
17
|
it 'registers an offense for Array.new' do
|
18
18
|
inspect_source(cop,
|
19
|
-
|
19
|
+
'test = Array.new')
|
20
20
|
expect(cop.offenses.size).to eq(1)
|
21
21
|
expect(cop.messages)
|
22
22
|
.to eq(['Use array literal `[]` instead of `Array.new`.'])
|
@@ -24,7 +24,7 @@ describe RuboCop::Cop::Style::EmptyLiteral do
|
|
24
24
|
|
25
25
|
it 'does not register an offense for Array.new(3)' do
|
26
26
|
inspect_source(cop,
|
27
|
-
|
27
|
+
'test = Array.new(3)')
|
28
28
|
expect(cop.offenses).to be_empty
|
29
29
|
end
|
30
30
|
|
@@ -37,7 +37,7 @@ describe RuboCop::Cop::Style::EmptyLiteral do
|
|
37
37
|
describe 'Empty Hash' do
|
38
38
|
it 'registers an offense for Hash.new()' do
|
39
39
|
inspect_source(cop,
|
40
|
-
|
40
|
+
'test = Hash.new()')
|
41
41
|
expect(cop.offenses.size).to eq(1)
|
42
42
|
expect(cop.messages)
|
43
43
|
.to eq(['Use hash literal `{}` instead of `Hash.new`.'])
|
@@ -45,7 +45,7 @@ describe RuboCop::Cop::Style::EmptyLiteral do
|
|
45
45
|
|
46
46
|
it 'registers an offense for Hash.new' do
|
47
47
|
inspect_source(cop,
|
48
|
-
|
48
|
+
'test = Hash.new')
|
49
49
|
expect(cop.offenses.size).to eq(1)
|
50
50
|
expect(cop.messages)
|
51
51
|
.to eq(['Use hash literal `{}` instead of `Hash.new`.'])
|
@@ -53,26 +53,43 @@ describe RuboCop::Cop::Style::EmptyLiteral do
|
|
53
53
|
|
54
54
|
it 'does not register an offense for Hash.new(3)' do
|
55
55
|
inspect_source(cop,
|
56
|
-
|
56
|
+
'test = Hash.new(3)')
|
57
57
|
expect(cop.offenses).to be_empty
|
58
58
|
end
|
59
59
|
|
60
60
|
it 'does not register an offense for Hash.new { block }' do
|
61
61
|
inspect_source(cop,
|
62
|
-
|
62
|
+
'test = Hash.new { block }')
|
63
63
|
expect(cop.offenses).to be_empty
|
64
64
|
end
|
65
65
|
|
66
66
|
it 'auto-corrects Hash.new to {}' do
|
67
|
-
new_source = autocorrect_source(cop, '
|
68
|
-
expect(new_source).to eq('
|
67
|
+
new_source = autocorrect_source(cop, 'Hash.new')
|
68
|
+
expect(new_source).to eq('{}')
|
69
|
+
end
|
70
|
+
|
71
|
+
it 'auto-corrects Hash.new to {} in various contexts' do
|
72
|
+
new_source =
|
73
|
+
autocorrect_source(cop, ['test = Hash.new',
|
74
|
+
'Hash.new.merge("a" => 3)',
|
75
|
+
'yadayada.map { a }.reduce(Hash.new, :merge)'])
|
76
|
+
expect(new_source)
|
77
|
+
.to eq(['test = {}',
|
78
|
+
'{}.merge("a" => 3)',
|
79
|
+
'yadayada.map { a }.reduce({}, :merge)'].join("\n"))
|
80
|
+
end
|
81
|
+
|
82
|
+
it 'does not auto-correct Hash.new to {} if changing code meaning' do
|
83
|
+
source = 'yadayada.map { a }.reduce Hash.new, :merge'
|
84
|
+
new_source = autocorrect_source(cop, source)
|
85
|
+
expect(new_source).to eq(source)
|
69
86
|
end
|
70
87
|
end
|
71
88
|
|
72
89
|
describe 'Empty String' do
|
73
90
|
it 'registers an offense for String.new()' do
|
74
91
|
inspect_source(cop,
|
75
|
-
|
92
|
+
'test = String.new()')
|
76
93
|
expect(cop.offenses.size).to eq(1)
|
77
94
|
expect(cop.messages)
|
78
95
|
.to eq(["Use string literal `''` instead of `String.new`."])
|
@@ -80,7 +97,7 @@ describe RuboCop::Cop::Style::EmptyLiteral do
|
|
80
97
|
|
81
98
|
it 'registers an offense for String.new' do
|
82
99
|
inspect_source(cop,
|
83
|
-
|
100
|
+
'test = String.new')
|
84
101
|
expect(cop.offenses.size).to eq(1)
|
85
102
|
expect(cop.messages)
|
86
103
|
.to eq(["Use string literal `''` instead of `String.new`."])
|
@@ -88,7 +105,7 @@ describe RuboCop::Cop::Style::EmptyLiteral do
|
|
88
105
|
|
89
106
|
it 'does not register an offense for String.new("top")' do
|
90
107
|
inspect_source(cop,
|
91
|
-
|
108
|
+
'test = String.new("top")')
|
92
109
|
expect(cop.offenses).to be_empty
|
93
110
|
end
|
94
111
|
|
@@ -12,14 +12,14 @@ describe RuboCop::Cop::Style::Encoding, :config do
|
|
12
12
|
|
13
13
|
it 'registers no offense when no encoding present but only ASCII ' \
|
14
14
|
'characters' do
|
15
|
-
inspect_source(cop,
|
15
|
+
inspect_source(cop, 'def foo() end')
|
16
16
|
|
17
17
|
expect(cop.offenses).to be_empty
|
18
18
|
end
|
19
19
|
|
20
20
|
it 'registers an offense when there is no encoding present but non ' \
|
21
21
|
'ASCII characters' do
|
22
|
-
inspect_source(cop,
|
22
|
+
inspect_source(cop, 'def foo() \'ä\' end')
|
23
23
|
|
24
24
|
expect(cop.offenses.size).to eq(1)
|
25
25
|
expect(cop.messages).to eq(
|
@@ -86,7 +86,7 @@ describe RuboCop::Cop::Style::Encoding, :config do
|
|
86
86
|
end
|
87
87
|
|
88
88
|
it 'registers an offense when no encoding present' do
|
89
|
-
inspect_source(cop,
|
89
|
+
inspect_source(cop, 'def foo() end')
|
90
90
|
|
91
91
|
expect(cop.offenses.size).to eq(1)
|
92
92
|
expect(cop.messages).to eq(
|
@@ -17,7 +17,7 @@ describe RuboCop::Cop::Style::EndOfLine do
|
|
17
17
|
end
|
18
18
|
|
19
19
|
it 'registers an offense for CR at end of file' do
|
20
|
-
inspect_source_file(cop,
|
20
|
+
inspect_source_file(cop, "x=0\r")
|
21
21
|
expect(cop.messages).to eq(['Carriage return character detected.'])
|
22
22
|
end
|
23
23
|
|
@@ -57,7 +57,7 @@ describe RuboCop::Cop::Style::EndOfLine do
|
|
57
57
|
|
58
58
|
context 'when source is a string' do
|
59
59
|
it 'registers an offense' do
|
60
|
-
inspect_source(cop,
|
60
|
+
inspect_source(cop, "x=0\r")
|
61
61
|
|
62
62
|
expect(cop.messages).to eq(['Carriage return character detected.'])
|
63
63
|
end
|
@@ -6,70 +6,159 @@ describe RuboCop::Cop::Style::EvenOdd do
|
|
6
6
|
subject(:cop) { described_class.new }
|
7
7
|
|
8
8
|
it 'registers an offense for x % 2 == 0' do
|
9
|
-
inspect_source(cop,
|
10
|
-
['x % 2 == 0'])
|
9
|
+
inspect_source(cop, 'x % 2 == 0')
|
11
10
|
expect(cop.offenses.size).to eq(1)
|
12
11
|
expect(cop.messages).to eq(['Replace with `Fixnum#even?`.'])
|
13
12
|
end
|
14
13
|
|
15
14
|
it 'registers an offense for x % 2 != 0' do
|
16
|
-
inspect_source(cop,
|
17
|
-
['x % 2 != 0'])
|
15
|
+
inspect_source(cop, 'x % 2 != 0')
|
18
16
|
expect(cop.offenses.size).to eq(1)
|
19
17
|
expect(cop.messages).to eq(['Replace with `Fixnum#odd?`.'])
|
20
18
|
end
|
21
19
|
|
22
20
|
it 'registers an offense for (x % 2) == 0' do
|
23
|
-
inspect_source(cop,
|
24
|
-
['(x % 2) == 0'])
|
21
|
+
inspect_source(cop, '(x % 2) == 0')
|
25
22
|
expect(cop.offenses.size).to eq(1)
|
26
23
|
expect(cop.messages).to eq(['Replace with `Fixnum#even?`.'])
|
27
24
|
end
|
28
25
|
|
29
26
|
it 'registers an offense for (x % 2) != 0' do
|
30
|
-
inspect_source(cop,
|
31
|
-
['(x % 2) != 0'])
|
27
|
+
inspect_source(cop, '(x % 2) != 0')
|
32
28
|
expect(cop.offenses.size).to eq(1)
|
33
29
|
expect(cop.messages).to eq(['Replace with `Fixnum#odd?`.'])
|
34
30
|
end
|
35
31
|
|
36
32
|
it 'registers an offense for x % 2 == 1' do
|
37
|
-
inspect_source(cop,
|
38
|
-
['x % 2 == 1'])
|
33
|
+
inspect_source(cop, 'x % 2 == 1')
|
39
34
|
expect(cop.offenses.size).to eq(1)
|
40
35
|
expect(cop.messages).to eq(['Replace with `Fixnum#odd?`.'])
|
41
36
|
end
|
42
37
|
|
43
38
|
it 'registers an offense for x % 2 != 1' do
|
44
|
-
inspect_source(cop,
|
45
|
-
['x % 2 != 1'])
|
39
|
+
inspect_source(cop, 'x % 2 != 1')
|
46
40
|
expect(cop.offenses.size).to eq(1)
|
47
41
|
expect(cop.messages).to eq(['Replace with `Fixnum#even?`.'])
|
48
42
|
end
|
49
43
|
|
50
44
|
it 'registers an offense for (x % 2) == 1' do
|
51
|
-
inspect_source(cop,
|
52
|
-
['(x % 2) == 1'])
|
45
|
+
inspect_source(cop, '(x % 2) == 1')
|
53
46
|
expect(cop.offenses.size).to eq(1)
|
54
47
|
expect(cop.messages).to eq(['Replace with `Fixnum#odd?`.'])
|
55
48
|
end
|
56
49
|
|
57
50
|
it 'registers an offense for (x % 2) != 1' do
|
58
|
-
inspect_source(cop,
|
59
|
-
|
51
|
+
inspect_source(cop, '(x % 2) != 1')
|
52
|
+
expect(cop.offenses.size).to eq(1)
|
53
|
+
expect(cop.messages).to eq(['Replace with `Fixnum#even?`.'])
|
54
|
+
end
|
55
|
+
|
56
|
+
it 'registers an offense for (x.y % 2) != 1' do
|
57
|
+
inspect_source(cop, '(x.y % 2) != 1')
|
58
|
+
expect(cop.offenses.size).to eq(1)
|
59
|
+
expect(cop.messages).to eq(['Replace with `Fixnum#even?`.'])
|
60
|
+
end
|
61
|
+
|
62
|
+
it 'registers an offense for (x(y) % 2) != 1' do
|
63
|
+
inspect_source(cop, '(x(y) % 2) != 1')
|
60
64
|
expect(cop.offenses.size).to eq(1)
|
61
65
|
expect(cop.messages).to eq(['Replace with `Fixnum#even?`.'])
|
62
66
|
end
|
63
67
|
|
64
68
|
it 'accepts x % 3 == 0' do
|
65
|
-
inspect_source(cop,
|
66
|
-
['x % 3 == 0'])
|
69
|
+
inspect_source(cop, 'x % 3 == 0')
|
67
70
|
expect(cop.offenses).to be_empty
|
68
71
|
end
|
69
72
|
|
70
73
|
it 'accepts x % 3 != 0' do
|
71
|
-
inspect_source(cop,
|
72
|
-
['x % 3 != 0'])
|
74
|
+
inspect_source(cop, 'x % 3 != 0')
|
73
75
|
expect(cop.offenses).to be_empty
|
74
76
|
end
|
77
|
+
|
78
|
+
it 'converts x % 2 == 0 to #even?' do
|
79
|
+
corrected = autocorrect_source(cop, 'x % 2 == 0')
|
80
|
+
expect(corrected).to eq('x.even?')
|
81
|
+
end
|
82
|
+
|
83
|
+
it 'converts x % 2 != 0 to #odd?' do
|
84
|
+
corrected = autocorrect_source(cop, 'x % 2 != 0')
|
85
|
+
expect(corrected).to eq('x.odd?')
|
86
|
+
end
|
87
|
+
|
88
|
+
it 'converts (x % 2) == 0 to #even?' do
|
89
|
+
corrected = autocorrect_source(cop, '(x % 2) == 0')
|
90
|
+
expect(corrected).to eq('x.even?')
|
91
|
+
end
|
92
|
+
|
93
|
+
it 'converts (x % 2) != 0 to #odd?' do
|
94
|
+
corrected = autocorrect_source(cop, '(x % 2) != 0')
|
95
|
+
expect(corrected).to eq('x.odd?')
|
96
|
+
end
|
97
|
+
|
98
|
+
it 'converts x % 2 == 1 to odd?' do
|
99
|
+
corrected = autocorrect_source(cop, 'x % 2 == 1')
|
100
|
+
expect(corrected).to eq('x.odd?')
|
101
|
+
end
|
102
|
+
|
103
|
+
it 'converts x % 2 != 1 to even?' do
|
104
|
+
corrected = autocorrect_source(cop, 'x % 2 != 1')
|
105
|
+
expect(corrected).to eq('x.even?')
|
106
|
+
end
|
107
|
+
|
108
|
+
it 'converts (x % 2) == 1 to odd?' do
|
109
|
+
corrected = autocorrect_source(cop, '(x % 2) == 1')
|
110
|
+
expect(corrected).to eq('x.odd?')
|
111
|
+
end
|
112
|
+
|
113
|
+
it 'converts (y % 2) != 1 to even?' do
|
114
|
+
corrected = autocorrect_source(cop, '(y % 2) != 1')
|
115
|
+
expect(corrected).to eq('y.even?')
|
116
|
+
end
|
117
|
+
|
118
|
+
it 'converts (x.y % 2) != 1 to even?' do
|
119
|
+
corrected = autocorrect_source(cop, '(x.y % 2) != 1')
|
120
|
+
expect(corrected).to eq('x.y.even?')
|
121
|
+
end
|
122
|
+
|
123
|
+
it 'converts (x(y) % 2) != 1 to even?' do
|
124
|
+
corrected = autocorrect_source(cop, '(x(y) % 2) != 1')
|
125
|
+
expect(corrected).to eq('x(y).even?')
|
126
|
+
end
|
127
|
+
|
128
|
+
it 'converts (x._(y) % 2) != 1 to even?' do
|
129
|
+
corrected = autocorrect_source(cop, '(x._(y) % 2) != 1')
|
130
|
+
expect(corrected).to eq('x._(y).even?')
|
131
|
+
end
|
132
|
+
|
133
|
+
it 'converts (x._(y)) % 2 != 1 to even?' do
|
134
|
+
corrected = autocorrect_source(cop, '(x._(y)) % 2 != 1')
|
135
|
+
expect(corrected).to eq('(x._(y)).even?')
|
136
|
+
end
|
137
|
+
|
138
|
+
it 'converts x._(y) % 2 != 1 to even?' do
|
139
|
+
corrected = autocorrect_source(cop, 'x._(y) % 2 != 1')
|
140
|
+
expect(corrected).to eq('x._(y).even?')
|
141
|
+
end
|
142
|
+
|
143
|
+
it 'converts 1 % 2 != 1 to even?' do
|
144
|
+
corrected = autocorrect_source(cop, '1 % 2 != 1')
|
145
|
+
expect(corrected).to eq('1.even?')
|
146
|
+
end
|
147
|
+
|
148
|
+
it 'converts complex examples' do
|
149
|
+
corrected = autocorrect_source(cop, [
|
150
|
+
'if (y % 2) != 1',
|
151
|
+
' method == :== ? :even : :odd',
|
152
|
+
'elsif x % 2 == 1',
|
153
|
+
' method == :== ? :odd : :even',
|
154
|
+
'end'
|
155
|
+
])
|
156
|
+
expect(corrected).to eq([
|
157
|
+
'if y.even?',
|
158
|
+
' method == :== ? :even : :odd',
|
159
|
+
'elsif x.odd?',
|
160
|
+
' method == :== ? :odd : :even',
|
161
|
+
'end'
|
162
|
+
].join("\n"))
|
163
|
+
end
|
75
164
|
end
|
@@ -6,17 +6,17 @@ describe RuboCop::Cop::Style::ExtraSpacing do
|
|
6
6
|
subject(:cop) { described_class.new }
|
7
7
|
|
8
8
|
it 'registers an offense for double extra spacing on variable assignment' do
|
9
|
-
inspect_source(cop,
|
9
|
+
inspect_source(cop, 'm = "hello"')
|
10
10
|
expect(cop.offenses.size).to eq(1)
|
11
11
|
end
|
12
12
|
|
13
13
|
it 'ignores whitespace at the beginning of the line' do
|
14
|
-
inspect_source(cop,
|
14
|
+
inspect_source(cop, ' m = "hello"')
|
15
15
|
expect(cop.offenses.size).to eq(0)
|
16
16
|
end
|
17
17
|
|
18
18
|
it 'ignores whitespace inside a string' do
|
19
|
-
inspect_source(cop,
|
19
|
+
inspect_source(cop, 'm = "hello this"')
|
20
20
|
expect(cop.offenses.size).to eq(0)
|
21
21
|
end
|
22
22
|
|