ridecharge-rubocop 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +22 -0
- data/.rspec +1 -0
- data/.travis.yml +14 -0
- data/CHANGELOG.md +0 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +35 -0
- data/Rakefile +22 -0
- data/bin/ridecharge-rubocop +49 -0
- data/config/0default.yml +8 -0
- data/config/fleet-magic-todo.yml +380 -0
- data/config/rc-todo.yml +190 -0
- data/config/ridecharge-rubocop_todo.yml +73 -0
- data/config/rubocop/default.yml +308 -0
- data/config/rubocop/disabled.yml +9 -0
- data/config/rubocop/enabled.yml +648 -0
- data/config/standard-todo.yml +24 -0
- data/config/tabs.yml +15 -0
- data/config/vehicle-todo.yml +195 -0
- data/lib/ridecharge/rubocop.rb +2 -0
- data/lib/ridecharge/rubocop/version.rb +5 -0
- data/lib/rubocop/config_loader_monkeypatch.rb +117 -0
- data/lib/rubocop/ridecharge.rb +1 -0
- data/ridecharge-rubocop.gemspec +25 -0
- data/spec/isolated_environment_spec.rb +24 -0
- data/spec/project_spec.rb +118 -0
- data/spec/rubocop/cli_spec.rb +1385 -0
- data/spec/rubocop/config_loader_spec.rb +328 -0
- data/spec/rubocop/config_spec.rb +179 -0
- data/spec/rubocop/config_store_spec.rb +53 -0
- data/spec/rubocop/cop/commissioner_spec.rb +83 -0
- data/spec/rubocop/cop/cop_spec.rb +114 -0
- data/spec/rubocop/cop/corrector_spec.rb +59 -0
- data/spec/rubocop/cop/lint/ambiguous_operator_spec.rb +113 -0
- data/spec/rubocop/cop/lint/ambiguous_regexp_literal_spec.rb +35 -0
- data/spec/rubocop/cop/lint/assignment_in_condition_spec.rb +107 -0
- data/spec/rubocop/cop/lint/block_alignment_spec.rb +411 -0
- data/spec/rubocop/cop/lint/condition_position_spec.rb +49 -0
- data/spec/rubocop/cop/lint/debugger_spec.rb +39 -0
- data/spec/rubocop/cop/lint/else_layout_spec.rb +65 -0
- data/spec/rubocop/cop/lint/empty_ensure_spec.rb +27 -0
- data/spec/rubocop/cop/lint/end_alignment_spec.rb +136 -0
- data/spec/rubocop/cop/lint/end_in_method_spec.rb +29 -0
- data/spec/rubocop/cop/lint/ensure_return_spec.rb +39 -0
- data/spec/rubocop/cop/lint/eval_spec.rb +35 -0
- data/spec/rubocop/cop/lint/handle_exceptions_spec.rb +30 -0
- data/spec/rubocop/cop/lint/invalid_character_literal_spec.rb +33 -0
- data/spec/rubocop/cop/lint/literal_in_condition_spec.rb +63 -0
- data/spec/rubocop/cop/lint/loop_spec.rb +27 -0
- data/spec/rubocop/cop/lint/parentheses_as_grouped_expression_spec.rb +57 -0
- data/spec/rubocop/cop/lint/require_parentheses_spec.rb +82 -0
- data/spec/rubocop/cop/lint/rescue_exception_spec.rb +131 -0
- data/spec/rubocop/cop/lint/shadowing_outer_local_variable_spec.rb +237 -0
- data/spec/rubocop/cop/lint/syntax_spec.rb +34 -0
- data/spec/rubocop/cop/lint/unreachable_code_spec.rb +63 -0
- data/spec/rubocop/cop/lint/useless_assignment_spec.rb +1570 -0
- data/spec/rubocop/cop/lint/useless_comparison_spec.rb +30 -0
- data/spec/rubocop/cop/lint/useless_else_without_rescue_spec.rb +48 -0
- data/spec/rubocop/cop/lint/useless_setter_call_spec.rb +149 -0
- data/spec/rubocop/cop/lint/void_spec.rb +57 -0
- data/spec/rubocop/cop/offence_spec.rb +133 -0
- data/spec/rubocop/cop/rails/default_scope_spec.rb +37 -0
- data/spec/rubocop/cop/rails/has_and_belongs_to_many_spec.rb +13 -0
- data/spec/rubocop/cop/rails/output_spec.rb +41 -0
- data/spec/rubocop/cop/rails/read_attribute_spec.rb +13 -0
- data/spec/rubocop/cop/rails/validation_spec.rb +21 -0
- data/spec/rubocop/cop/style/access_modifier_indentation_spec.rb +361 -0
- data/spec/rubocop/cop/style/accessor_method_name_spec.rb +81 -0
- data/spec/rubocop/cop/style/alias_spec.rb +59 -0
- data/spec/rubocop/cop/style/align_array_spec.rb +75 -0
- data/spec/rubocop/cop/style/align_hash_spec.rb +310 -0
- data/spec/rubocop/cop/style/align_parameters_spec.rb +222 -0
- data/spec/rubocop/cop/style/and_or_spec.rb +57 -0
- data/spec/rubocop/cop/style/ascii_comments_spec.rb +22 -0
- data/spec/rubocop/cop/style/ascii_identifiers_spec.rb +36 -0
- data/spec/rubocop/cop/style/attr_spec.rb +19 -0
- data/spec/rubocop/cop/style/begin_block_spec.rb +13 -0
- data/spec/rubocop/cop/style/block_comments_spec.rb +21 -0
- data/spec/rubocop/cop/style/block_nesting_spec.rb +156 -0
- data/spec/rubocop/cop/style/blocks_spec.rb +99 -0
- data/spec/rubocop/cop/style/braces_around_hash_parameters_spec.rb +284 -0
- data/spec/rubocop/cop/style/case_equality_spec.rb +12 -0
- data/spec/rubocop/cop/style/case_indentation_spec.rb +289 -0
- data/spec/rubocop/cop/style/character_literal_spec.rb +37 -0
- data/spec/rubocop/cop/style/class_and_module_camel_case_spec.rb +40 -0
- data/spec/rubocop/cop/style/class_length_spec.rb +131 -0
- data/spec/rubocop/cop/style/class_methods_spec.rb +45 -0
- data/spec/rubocop/cop/style/class_vars_spec.rb +19 -0
- data/spec/rubocop/cop/style/collection_methods_spec.rb +48 -0
- data/spec/rubocop/cop/style/colon_method_call_spec.rb +60 -0
- data/spec/rubocop/cop/style/comment_annotation_spec.rb +86 -0
- data/spec/rubocop/cop/style/constant_name_spec.rb +65 -0
- data/spec/rubocop/cop/style/cyclomatic_complexity_spec.rb +204 -0
- data/spec/rubocop/cop/style/def_with_parentheses_spec.rb +39 -0
- data/spec/rubocop/cop/style/documentation_spec.rb +123 -0
- data/spec/rubocop/cop/style/dot_position_spec.rb +94 -0
- data/spec/rubocop/cop/style/empty_line_between_defs_spec.rb +127 -0
- data/spec/rubocop/cop/style/empty_lines_around_access_modifier_spec.rb +56 -0
- data/spec/rubocop/cop/style/empty_lines_around_body_spec.rb +131 -0
- data/spec/rubocop/cop/style/empty_lines_spec.rb +40 -0
- data/spec/rubocop/cop/style/empty_literal_spec.rb +100 -0
- data/spec/rubocop/cop/style/encoding_spec.rb +56 -0
- data/spec/rubocop/cop/style/end_block_spec.rb +13 -0
- data/spec/rubocop/cop/style/end_of_line_spec.rb +47 -0
- data/spec/rubocop/cop/style/even_odd_spec.rb +75 -0
- data/spec/rubocop/cop/style/favor_join_spec.rb +31 -0
- data/spec/rubocop/cop/style/favor_sprintf_spec.rb +47 -0
- data/spec/rubocop/cop/style/favor_unless_over_negated_if_spec.rb +76 -0
- data/spec/rubocop/cop/style/favor_until_over_negated_while_spec.rb +41 -0
- data/spec/rubocop/cop/style/final_newline_spec.rb +30 -0
- data/spec/rubocop/cop/style/flip_flop_spec.rb +23 -0
- data/spec/rubocop/cop/style/for_spec.rb +105 -0
- data/spec/rubocop/cop/style/global_vars_spec.rb +34 -0
- data/spec/rubocop/cop/style/hash_methods_spec.rb +45 -0
- data/spec/rubocop/cop/style/hash_syntax_spec.rb +131 -0
- data/spec/rubocop/cop/style/if_unless_modifier_spec.rb +128 -0
- data/spec/rubocop/cop/style/if_with_semicolon_spec.rb +19 -0
- data/spec/rubocop/cop/style/indentation_consistency_spec.rb +490 -0
- data/spec/rubocop/cop/style/indentation_width_spec.rb +470 -0
- data/spec/rubocop/cop/style/lambda_call_spec.rb +65 -0
- data/spec/rubocop/cop/style/lambda_spec.rb +41 -0
- data/spec/rubocop/cop/style/leading_comment_space_spec.rb +64 -0
- data/spec/rubocop/cop/style/line_end_concatenation_spec.rb +34 -0
- data/spec/rubocop/cop/style/line_length_spec.rb +20 -0
- data/spec/rubocop/cop/style/method_call_parentheses_spec.rb +59 -0
- data/spec/rubocop/cop/style/method_called_on_do_end_block_spec.rb +60 -0
- data/spec/rubocop/cop/style/method_def_parentheses_spec.rb +106 -0
- data/spec/rubocop/cop/style/method_length_spec.rb +147 -0
- data/spec/rubocop/cop/style/method_name_spec.rb +125 -0
- data/spec/rubocop/cop/style/module_function_spec.rb +24 -0
- data/spec/rubocop/cop/style/multiline_block_chain_spec.rb +78 -0
- data/spec/rubocop/cop/style/multiline_if_then_spec.rb +107 -0
- data/spec/rubocop/cop/style/multiline_ternary_operator_spec.rb +18 -0
- data/spec/rubocop/cop/style/nested_ternary_operator_spec.rb +21 -0
- data/spec/rubocop/cop/style/nil_comparison_spec.rb +30 -0
- data/spec/rubocop/cop/style/not_spec.rb +22 -0
- data/spec/rubocop/cop/style/numeric_literals_spec.rb +64 -0
- data/spec/rubocop/cop/style/one_line_conditional_spec.rb +13 -0
- data/spec/rubocop/cop/style/op_method_spec.rb +74 -0
- data/spec/rubocop/cop/style/parameter_lists_spec.rb +44 -0
- data/spec/rubocop/cop/style/parentheses_around_condition_spec.rb +122 -0
- data/spec/rubocop/cop/style/perl_backrefs_spec.rb +17 -0
- data/spec/rubocop/cop/style/predicate_name_spec.rb +25 -0
- data/spec/rubocop/cop/style/proc_spec.rb +27 -0
- data/spec/rubocop/cop/style/raise_args_spec.rb +82 -0
- data/spec/rubocop/cop/style/redundant_begin_spec.rb +57 -0
- data/spec/rubocop/cop/style/redundant_exception_spec.rb +27 -0
- data/spec/rubocop/cop/style/redundant_return_spec.rb +171 -0
- data/spec/rubocop/cop/style/redundant_self_spec.rb +142 -0
- data/spec/rubocop/cop/style/regexp_literal_spec.rb +83 -0
- data/spec/rubocop/cop/style/rescue_modifier_spec.rb +116 -0
- data/spec/rubocop/cop/style/semicolon_spec.rb +100 -0
- data/spec/rubocop/cop/style/signal_exception_spec.rb +266 -0
- data/spec/rubocop/cop/style/single_line_block_params_spec.rb +68 -0
- data/spec/rubocop/cop/style/single_line_methods_spec.rb +52 -0
- data/spec/rubocop/cop/style/space_after_colon_spec.rb +38 -0
- data/spec/rubocop/cop/style/space_after_comma_spec.rb +30 -0
- data/spec/rubocop/cop/style/space_after_control_keyword_spec.rb +84 -0
- data/spec/rubocop/cop/style/space_after_method_name_spec.rb +70 -0
- data/spec/rubocop/cop/style/space_after_not_spec.rb +22 -0
- data/spec/rubocop/cop/style/space_after_semicolon_spec.rb +23 -0
- data/spec/rubocop/cop/style/space_around_block_braces_spec.rb +283 -0
- data/spec/rubocop/cop/style/space_around_equals_in_parameter_default_spec.rb +33 -0
- data/spec/rubocop/cop/style/space_around_operators_spec.rb +325 -0
- data/spec/rubocop/cop/style/space_before_modifier_keyword_spec.rb +70 -0
- data/spec/rubocop/cop/style/space_inside_brackets_spec.rb +52 -0
- data/spec/rubocop/cop/style/space_inside_hash_literal_braces_spec.rb +138 -0
- data/spec/rubocop/cop/style/space_inside_parens_spec.rb +34 -0
- data/spec/rubocop/cop/style/special_global_vars_spec.rb +56 -0
- data/spec/rubocop/cop/style/string_literals_spec.rb +212 -0
- data/spec/rubocop/cop/style/symbol_array_spec.rb +37 -0
- data/spec/rubocop/cop/style/tab_spec.rb +17 -0
- data/spec/rubocop/cop/style/trailing_blank_lines_spec.rb +43 -0
- data/spec/rubocop/cop/style/trailing_comma_spec.rb +230 -0
- data/spec/rubocop/cop/style/trailing_whitespace_spec.rb +30 -0
- data/spec/rubocop/cop/style/trivial_accessors_spec.rb +415 -0
- data/spec/rubocop/cop/style/unless_else_spec.rb +25 -0
- data/spec/rubocop/cop/style/variable_interpolation_spec.rb +47 -0
- data/spec/rubocop/cop/style/variable_name_spec.rb +107 -0
- data/spec/rubocop/cop/style/when_then_spec.rb +41 -0
- data/spec/rubocop/cop/style/while_until_do_spec.rb +53 -0
- data/spec/rubocop/cop/style/while_until_modifier_spec.rb +75 -0
- data/spec/rubocop/cop/style/word_array_spec.rb +97 -0
- data/spec/rubocop/cop/team_spec.rb +156 -0
- data/spec/rubocop/cop/util_spec.rb +49 -0
- data/spec/rubocop/cop/variable_inspector/assignment_spec.rb +213 -0
- data/spec/rubocop/cop/variable_inspector/locatable_spec.rb +734 -0
- data/spec/rubocop/cop/variable_inspector/scope_spec.rb +184 -0
- data/spec/rubocop/cop/variable_inspector/variable_spec.rb +73 -0
- data/spec/rubocop/cop/variable_inspector/variable_table_spec.rb +269 -0
- data/spec/rubocop/cop/variable_inspector_spec.rb +29 -0
- data/spec/rubocop/file_inspector_spec.rb +78 -0
- data/spec/rubocop/formatter/base_formatter_spec.rb +191 -0
- data/spec/rubocop/formatter/clang_style_formatter_spec.rb +114 -0
- data/spec/rubocop/formatter/colorizable_spec.rb +107 -0
- data/spec/rubocop/formatter/disabled_config_formatter_spec.rb +50 -0
- data/spec/rubocop/formatter/emacs_style_formatter_spec.rb +62 -0
- data/spec/rubocop/formatter/file_list_formatter_spec.rb +33 -0
- data/spec/rubocop/formatter/formatter_set_spec.rb +132 -0
- data/spec/rubocop/formatter/json_formatter_spec.rb +148 -0
- data/spec/rubocop/formatter/offence_count_formatter_spec.rb +52 -0
- data/spec/rubocop/formatter/progress_formatter_spec.rb +182 -0
- data/spec/rubocop/formatter/simple_text_formatter_spec.rb +123 -0
- data/spec/rubocop/options_spec.rb +145 -0
- data/spec/rubocop/path_util_spec.rb +42 -0
- data/spec/rubocop/processed_source_spec.rb +114 -0
- data/spec/rubocop/source_parser_spec.rb +139 -0
- data/spec/rubocop/target_finder_spec.rb +180 -0
- data/spec/rubocop/token_spec.rb +25 -0
- data/spec/spec_helper.rb +136 -0
- data/spec/support/ast_helper.rb +15 -0
- data/spec/support/file_helper.rb +21 -0
- data/spec/support/isolated_environment.rb +34 -0
- data/spec/support/mri_syntax_checker.rb +73 -0
- data/spec/support/shared_context.rb +22 -0
- data/spec/support/shared_examples.rb +33 -0
- data/spec/support/statement_modifier_helper.rb +41 -0
- metadata +511 -0
@@ -0,0 +1,99 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
require 'spec_helper'
|
4
|
+
|
5
|
+
describe Rubocop::Cop::Style::Blocks do
|
6
|
+
subject(:cop) { described_class.new }
|
7
|
+
|
8
|
+
it 'accepts a multiline block with do-end' do
|
9
|
+
inspect_source(cop, ['each do |x|',
|
10
|
+
'end'])
|
11
|
+
expect(cop.offences).to be_empty
|
12
|
+
end
|
13
|
+
|
14
|
+
it 'registers an offence for a single line block with do-end' do
|
15
|
+
inspect_source(cop, ['each do |x| end'])
|
16
|
+
expect(cop.messages)
|
17
|
+
.to eq(['Prefer {...} over do...end for single-line blocks.'])
|
18
|
+
end
|
19
|
+
|
20
|
+
it 'accepts a single line block with braces' do
|
21
|
+
inspect_source(cop, ['each { |x| }'])
|
22
|
+
expect(cop.offences).to be_empty
|
23
|
+
end
|
24
|
+
|
25
|
+
it 'auto-corrects do and end for single line blocks to { and }' do
|
26
|
+
new_source = autocorrect_source(cop, 'block do |x| end')
|
27
|
+
expect(new_source).to eq('block { |x| }')
|
28
|
+
end
|
29
|
+
|
30
|
+
context 'when there are braces around a multi-line block' do
|
31
|
+
it 'registers an offence in the simple case' do
|
32
|
+
inspect_source(cop, ['each { |x|',
|
33
|
+
'}'])
|
34
|
+
expect(cop.messages)
|
35
|
+
.to eq(['Avoid using {...} for multi-line blocks.'])
|
36
|
+
end
|
37
|
+
|
38
|
+
it 'accepts braces if do-end would change the meaning' do
|
39
|
+
src = ['scope :foo, lambda { |f|',
|
40
|
+
' where(condition: "value")',
|
41
|
+
'}',
|
42
|
+
'',
|
43
|
+
'expect { something }.to raise_error(ErrorClass) { |error|',
|
44
|
+
' # ...',
|
45
|
+
'}',
|
46
|
+
'',
|
47
|
+
'expect { x }.to change {',
|
48
|
+
' Counter.count',
|
49
|
+
'}.from(0).to(1)']
|
50
|
+
inspect_source(cop, src)
|
51
|
+
expect(cop.offences).to be_empty
|
52
|
+
end
|
53
|
+
|
54
|
+
it 'registers an offence for braces if do-end would not change ' \
|
55
|
+
'the meaning' do
|
56
|
+
src = ['scope :foo, (lambda { |f|',
|
57
|
+
' where(condition: "value")',
|
58
|
+
'})',
|
59
|
+
'',
|
60
|
+
'expect { something }.to(raise_error(ErrorClass) { |error|',
|
61
|
+
' # ...',
|
62
|
+
'})']
|
63
|
+
inspect_source(cop, src)
|
64
|
+
expect(cop.offences.size).to eq(2)
|
65
|
+
end
|
66
|
+
|
67
|
+
it 'can handle special method names such as []= and done?' do
|
68
|
+
src = ['h2[k2] = Hash.new { |h3,k3|',
|
69
|
+
' h3[k3] = 0',
|
70
|
+
'}',
|
71
|
+
'',
|
72
|
+
'x = done? list.reject { |e|',
|
73
|
+
' e.nil?',
|
74
|
+
'}']
|
75
|
+
inspect_source(cop, src)
|
76
|
+
expect(cop.messages)
|
77
|
+
.to eq(['Avoid using {...} for multi-line blocks.'])
|
78
|
+
end
|
79
|
+
|
80
|
+
it 'auto-corrects { and } to do and end' do
|
81
|
+
source = <<-END.strip_indent
|
82
|
+
each{ |x|
|
83
|
+
some_method
|
84
|
+
other_method
|
85
|
+
}
|
86
|
+
END
|
87
|
+
|
88
|
+
expected_source = <<-END.strip_indent
|
89
|
+
each do |x|
|
90
|
+
some_method
|
91
|
+
other_method
|
92
|
+
end
|
93
|
+
END
|
94
|
+
|
95
|
+
new_source = autocorrect_source(cop, source)
|
96
|
+
expect(new_source).to eq(expected_source)
|
97
|
+
end
|
98
|
+
end
|
99
|
+
end
|
@@ -0,0 +1,284 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
require 'spec_helper'
|
4
|
+
|
5
|
+
describe Rubocop::Cop::Style::BracesAroundHashParameters, :config do
|
6
|
+
subject(:cop) { described_class.new(config) }
|
7
|
+
|
8
|
+
context 'no_braces' do
|
9
|
+
let(:cop_config) do
|
10
|
+
{ 'EnforcedStyle' => 'no_braces' }
|
11
|
+
end
|
12
|
+
|
13
|
+
describe 'accepts' do
|
14
|
+
it 'one non-hash parameter' do
|
15
|
+
inspect_source(cop, ['where(2)'])
|
16
|
+
expect(cop.messages).to be_empty
|
17
|
+
expect(cop.highlights).to be_empty
|
18
|
+
end
|
19
|
+
|
20
|
+
it 'one empty hash parameter' do
|
21
|
+
inspect_source(cop, ['where({})'])
|
22
|
+
expect(cop.messages).to be_empty
|
23
|
+
expect(cop.highlights).to be_empty
|
24
|
+
end
|
25
|
+
|
26
|
+
it 'one hash parameter with separators' do
|
27
|
+
inspect_source(cop, ["where( { \n }\t ) "])
|
28
|
+
expect(cop.messages).to be_empty
|
29
|
+
expect(cop.highlights).to be_empty
|
30
|
+
end
|
31
|
+
|
32
|
+
it 'multiple non-hash parameters' do
|
33
|
+
inspect_source(cop, ['where(1, "2")'])
|
34
|
+
expect(cop.messages).to be_empty
|
35
|
+
expect(cop.highlights).to be_empty
|
36
|
+
end
|
37
|
+
|
38
|
+
it 'one hash parameter without braces' do
|
39
|
+
inspect_source(cop, ['where(x: "y")'])
|
40
|
+
expect(cop.messages).to be_empty
|
41
|
+
expect(cop.highlights).to be_empty
|
42
|
+
end
|
43
|
+
|
44
|
+
it 'one hash parameter without braces and multiple keys' do
|
45
|
+
inspect_source(cop, ['where(x: "y", foo: "bar")'])
|
46
|
+
expect(cop.messages).to be_empty
|
47
|
+
expect(cop.highlights).to be_empty
|
48
|
+
end
|
49
|
+
|
50
|
+
it 'one hash parameter without braces and one hash value' do
|
51
|
+
inspect_source(cop, ['where(x: { "y" => "z" })'])
|
52
|
+
expect(cop.messages).to be_empty
|
53
|
+
expect(cop.highlights).to be_empty
|
54
|
+
end
|
55
|
+
|
56
|
+
it 'multiple hash parameters with braces' do
|
57
|
+
inspect_source(cop, ['where({ x: 1 }, { y: 2 })'])
|
58
|
+
expect(cop.messages).to be_empty
|
59
|
+
expect(cop.highlights).to be_empty
|
60
|
+
end
|
61
|
+
|
62
|
+
it 'property assignment with braces' do
|
63
|
+
inspect_source(cop, ['x.z = { y: "z" }'])
|
64
|
+
expect(cop.messages).to be_empty
|
65
|
+
expect(cop.highlights).to be_empty
|
66
|
+
end
|
67
|
+
|
68
|
+
it 'operator with a hash parameter with braces' do
|
69
|
+
inspect_source(cop, ['x.z - { y: "z" }'])
|
70
|
+
expect(cop.messages).to be_empty
|
71
|
+
expect(cop.highlights).to be_empty
|
72
|
+
end
|
73
|
+
|
74
|
+
end
|
75
|
+
|
76
|
+
describe 'registers an offence for' do
|
77
|
+
it 'one non-hash parameter followed by a hash parameter with braces' do
|
78
|
+
inspect_source(cop, ['where(1, { y: 2 })'])
|
79
|
+
expect(cop.messages).to eq([
|
80
|
+
'Redundant curly braces around a hash parameter.'
|
81
|
+
])
|
82
|
+
expect(cop.highlights).to eq(['{ y: 2 }'])
|
83
|
+
expect(cop.config_to_allow_offences).to eq('EnforcedStyle' => 'braces')
|
84
|
+
end
|
85
|
+
|
86
|
+
it 'correct + opposite style' do
|
87
|
+
inspect_source(cop, ['where(1, y: 2)',
|
88
|
+
'where(1, { y: 2 })'])
|
89
|
+
expect(cop.messages).to eq([
|
90
|
+
'Redundant curly braces around a hash parameter.'
|
91
|
+
])
|
92
|
+
expect(cop.config_to_allow_offences).to eq('Enabled' => false)
|
93
|
+
end
|
94
|
+
|
95
|
+
it 'opposite + correct style' do
|
96
|
+
inspect_source(cop, ['where(1, { y: 2 })',
|
97
|
+
'where(1, y: 2)'])
|
98
|
+
expect(cop.messages).to eq([
|
99
|
+
'Redundant curly braces around a hash parameter.'
|
100
|
+
])
|
101
|
+
expect(cop.config_to_allow_offences).to eq('Enabled' => false)
|
102
|
+
end
|
103
|
+
|
104
|
+
it 'one object method hash parameter with braces' do
|
105
|
+
inspect_source(cop, ['x.func({ y: "z" })'])
|
106
|
+
expect(cop.messages).to eq([
|
107
|
+
'Redundant curly braces around a hash parameter.'
|
108
|
+
])
|
109
|
+
expect(cop.highlights).to eq(['{ y: "z" }'])
|
110
|
+
end
|
111
|
+
|
112
|
+
it 'one hash parameter with braces' do
|
113
|
+
inspect_source(cop, ['where({ x: 1 })'])
|
114
|
+
expect(cop.messages).to eq([
|
115
|
+
'Redundant curly braces around a hash parameter.'
|
116
|
+
])
|
117
|
+
expect(cop.highlights).to eq(['{ x: 1 }'])
|
118
|
+
end
|
119
|
+
|
120
|
+
it 'one hash parameter with braces and separators' do
|
121
|
+
inspect_source(cop, ["where( \n { x: 1 } )"])
|
122
|
+
expect(cop.messages).to eq([
|
123
|
+
'Redundant curly braces around a hash parameter.'
|
124
|
+
])
|
125
|
+
expect(cop.highlights).to eq(['{ x: 1 }'])
|
126
|
+
end
|
127
|
+
|
128
|
+
it 'one hash parameter with braces and multiple keys' do
|
129
|
+
inspect_source(cop, ['where({ x: 1, foo: "bar" })'])
|
130
|
+
expect(cop.messages).to eq([
|
131
|
+
'Redundant curly braces around a hash parameter.'
|
132
|
+
])
|
133
|
+
expect(cop.highlights).to eq(['{ x: 1, foo: "bar" }'])
|
134
|
+
end
|
135
|
+
end
|
136
|
+
|
137
|
+
describe 'auto-corrects' do
|
138
|
+
it 'one non-hash parameter followed by a hash parameter with braces' do
|
139
|
+
corrected = autocorrect_source(cop, ['where(1, { y: 2 })'])
|
140
|
+
expect(corrected).to eq 'where(1, y: 2 )'
|
141
|
+
end
|
142
|
+
|
143
|
+
it 'one object method hash parameter with braces' do
|
144
|
+
corrected = autocorrect_source(cop, ['x.func({ y: "z" })'])
|
145
|
+
expect(corrected).to eq 'x.func( y: "z" )'
|
146
|
+
end
|
147
|
+
|
148
|
+
it 'one hash parameter with braces' do
|
149
|
+
corrected = autocorrect_source(cop, ['where({ x: 1 })'])
|
150
|
+
expect(corrected).to eq 'where( x: 1 )'
|
151
|
+
end
|
152
|
+
|
153
|
+
it 'one hash parameter with braces and separators' do
|
154
|
+
corrected = autocorrect_source(cop, ["where( \n { x: 1 } )"])
|
155
|
+
expect(corrected).to eq "where( \n x: 1 )"
|
156
|
+
end
|
157
|
+
|
158
|
+
it 'one hash parameter with braces and multiple keys' do
|
159
|
+
corrected = autocorrect_source(cop, ['where({ x: 1, foo: "bar" })'])
|
160
|
+
expect(corrected).to eq 'where( x: 1, foo: "bar" )'
|
161
|
+
end
|
162
|
+
|
163
|
+
it 'one hash parameter with braces and a trailing comma' do
|
164
|
+
corrected = autocorrect_source(cop, ['where({ x: 1, y: 2, })'])
|
165
|
+
expect(corrected).to eq 'where( x: 1, y: 2 )'
|
166
|
+
end
|
167
|
+
end
|
168
|
+
end
|
169
|
+
|
170
|
+
context 'braces' do
|
171
|
+
let(:cop_config) do
|
172
|
+
{ 'EnforcedStyle' => 'braces' }
|
173
|
+
end
|
174
|
+
|
175
|
+
describe 'accepts' do
|
176
|
+
it 'an empty hash parameter' do
|
177
|
+
inspect_source(cop, ['where({})'])
|
178
|
+
expect(cop.messages).to be_empty
|
179
|
+
expect(cop.highlights).to be_empty
|
180
|
+
end
|
181
|
+
|
182
|
+
it 'one non-hash parameter' do
|
183
|
+
inspect_source(cop, ['where(2)'])
|
184
|
+
expect(cop.messages).to be_empty
|
185
|
+
expect(cop.highlights).to be_empty
|
186
|
+
end
|
187
|
+
|
188
|
+
it 'multiple non-hash parameters' do
|
189
|
+
inspect_source(cop, ['where(1, "2")'])
|
190
|
+
expect(cop.messages).to be_empty
|
191
|
+
expect(cop.highlights).to be_empty
|
192
|
+
end
|
193
|
+
|
194
|
+
it 'one hash parameter with braces' do
|
195
|
+
inspect_source(cop, ['where({ x: 1 })'])
|
196
|
+
expect(cop.messages).to be_empty
|
197
|
+
expect(cop.highlights).to be_empty
|
198
|
+
end
|
199
|
+
|
200
|
+
it 'multiple hash parameters with braces' do
|
201
|
+
inspect_source(cop, ['where({ x: 1 }, { y: 2 })'])
|
202
|
+
expect(cop.messages).to be_empty
|
203
|
+
expect(cop.highlights).to be_empty
|
204
|
+
end
|
205
|
+
|
206
|
+
it 'one hash parameter with braces and spaces around it' do
|
207
|
+
inspect_source(cop, [
|
208
|
+
'where( { x: 1 } )'
|
209
|
+
])
|
210
|
+
expect(cop.messages).to be_empty
|
211
|
+
expect(cop.highlights).to be_empty
|
212
|
+
end
|
213
|
+
|
214
|
+
it 'one hash parameter with braces and separators around it' do
|
215
|
+
inspect_source(cop, ["where( \t { x: 1 \n } )"])
|
216
|
+
expect(cop.messages).to be_empty
|
217
|
+
expect(cop.highlights).to be_empty
|
218
|
+
end
|
219
|
+
end
|
220
|
+
|
221
|
+
describe 'registers an offence for' do
|
222
|
+
it 'one hash parameter without braces' do
|
223
|
+
inspect_source(cop, ['where(x: "y")'])
|
224
|
+
expect(cop.messages).to eq([
|
225
|
+
'Missing curly braces around a hash parameter.'
|
226
|
+
])
|
227
|
+
expect(cop.highlights).to eq(['x: "y"'])
|
228
|
+
expect(cop.config_to_allow_offences).to eq('EnforcedStyle' =>
|
229
|
+
'no_braces')
|
230
|
+
end
|
231
|
+
|
232
|
+
it 'opposite + correct style' do
|
233
|
+
inspect_source(cop, ['where(y: 2)',
|
234
|
+
'where({ y: 2 })'])
|
235
|
+
expect(cop.messages).to eq([
|
236
|
+
'Missing curly braces around a hash parameter.'
|
237
|
+
])
|
238
|
+
expect(cop.config_to_allow_offences).to eq('Enabled' => false)
|
239
|
+
end
|
240
|
+
|
241
|
+
it 'correct + opposite style' do
|
242
|
+
inspect_source(cop, ['where({ y: 2 })',
|
243
|
+
'where(y: 2)'])
|
244
|
+
expect(cop.messages).to eq([
|
245
|
+
'Missing curly braces around a hash parameter.'
|
246
|
+
])
|
247
|
+
expect(cop.config_to_allow_offences).to eq('Enabled' => false)
|
248
|
+
end
|
249
|
+
|
250
|
+
it 'one hash parameter with multiple keys and without braces' do
|
251
|
+
inspect_source(cop, ['where(x: "y", foo: "bar")'])
|
252
|
+
expect(cop.messages).to eq([
|
253
|
+
'Missing curly braces around a hash parameter.'
|
254
|
+
])
|
255
|
+
expect(cop.highlights).to eq(['x: "y", foo: "bar"'])
|
256
|
+
end
|
257
|
+
|
258
|
+
it 'one hash parameter without braces with one hash value' do
|
259
|
+
inspect_source(cop, ['where(x: { "y" => "z" })'])
|
260
|
+
expect(cop.messages).to eq([
|
261
|
+
'Missing curly braces around a hash parameter.'
|
262
|
+
])
|
263
|
+
expect(cop.highlights).to eq(['x: { "y" => "z" }'])
|
264
|
+
end
|
265
|
+
end
|
266
|
+
|
267
|
+
describe 'auto-corrects' do
|
268
|
+
it 'one hash parameter without braces' do
|
269
|
+
corrected = autocorrect_source(cop, ['where(x: "y")'])
|
270
|
+
expect(corrected).to eq 'where({x: "y"})'
|
271
|
+
end
|
272
|
+
|
273
|
+
it 'one hash parameter with multiple keys and without braces' do
|
274
|
+
corrected = autocorrect_source(cop, ['where(x: "y", foo: "bar")'])
|
275
|
+
expect(corrected).to eq 'where({x: "y", foo: "bar"})'
|
276
|
+
end
|
277
|
+
|
278
|
+
it 'one hash parameter without braces with one hash value' do
|
279
|
+
corrected = autocorrect_source(cop, ['where(x: { "y" => "z" })'])
|
280
|
+
expect(corrected).to eq 'where({x: { "y" => "z" }})'
|
281
|
+
end
|
282
|
+
end
|
283
|
+
end
|
284
|
+
end
|
@@ -0,0 +1,12 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
require 'spec_helper'
|
4
|
+
|
5
|
+
describe Rubocop::Cop::Style::CaseEquality do
|
6
|
+
subject(:cop) { described_class.new }
|
7
|
+
|
8
|
+
it 'registers an offence for ===' do
|
9
|
+
inspect_source(cop, ['Array === var'])
|
10
|
+
expect(cop.offences.size).to eq(1)
|
11
|
+
end
|
12
|
+
end
|
@@ -0,0 +1,289 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
require 'spec_helper'
|
4
|
+
|
5
|
+
describe Rubocop::Cop::Style::CaseIndentation do
|
6
|
+
subject(:cop) { described_class.new(config) }
|
7
|
+
let(:config) do
|
8
|
+
merged = Rubocop::ConfigLoader
|
9
|
+
.default_configuration['CaseIndentation'].merge(cop_config)
|
10
|
+
Rubocop::Config.new('CaseIndentation' => merged)
|
11
|
+
end
|
12
|
+
|
13
|
+
context 'with IndentWhenRelativeTo: case' do
|
14
|
+
context 'with IndentOneStep: false' do
|
15
|
+
let(:cop_config) do
|
16
|
+
{ 'IndentWhenRelativeTo' => 'case', 'IndentOneStep' => false }
|
17
|
+
end
|
18
|
+
|
19
|
+
context 'regarding assignment where the right hand side is a case' do
|
20
|
+
it 'accepts a correcty indented assignment' do
|
21
|
+
source = ['output = case variable',
|
22
|
+
" when 'value1'",
|
23
|
+
" 'output1'",
|
24
|
+
' else',
|
25
|
+
" 'output2'",
|
26
|
+
' end']
|
27
|
+
inspect_source(cop, source)
|
28
|
+
expect(cop.offences).to be_empty
|
29
|
+
end
|
30
|
+
|
31
|
+
it 'registers on offence for an assignment indented as end' do
|
32
|
+
source = ['output = case variable',
|
33
|
+
"when 'value1'",
|
34
|
+
" 'output1'",
|
35
|
+
'else',
|
36
|
+
" 'output2'",
|
37
|
+
'end']
|
38
|
+
inspect_source(cop, source)
|
39
|
+
expect(cop.messages).to eq(['Indent when as deep as case.'])
|
40
|
+
expect(cop.config_to_allow_offences).to eq('IndentWhenRelativeTo' =>
|
41
|
+
'end')
|
42
|
+
end
|
43
|
+
|
44
|
+
it 'registers on offence for an assignment indented some other way' do
|
45
|
+
source = ['output = case variable',
|
46
|
+
" when 'value1'",
|
47
|
+
" 'output1'",
|
48
|
+
' else',
|
49
|
+
" 'output2'",
|
50
|
+
'end']
|
51
|
+
inspect_source(cop, source)
|
52
|
+
expect(cop.messages).to eq(['Indent when as deep as case.'])
|
53
|
+
expect(cop.config_to_allow_offences).to eq('Enabled' => false)
|
54
|
+
end
|
55
|
+
|
56
|
+
it 'registers on offence for correct + opposite' do
|
57
|
+
source = ['output = case variable',
|
58
|
+
" when 'value1'",
|
59
|
+
" 'output1'",
|
60
|
+
' else',
|
61
|
+
" 'output2'",
|
62
|
+
' end',
|
63
|
+
'output = case variable',
|
64
|
+
"when 'value1'",
|
65
|
+
" 'output1'",
|
66
|
+
'else',
|
67
|
+
" 'output2'",
|
68
|
+
'end']
|
69
|
+
inspect_source(cop, source)
|
70
|
+
expect(cop.messages).to eq(['Indent when as deep as case.'])
|
71
|
+
expect(cop.config_to_allow_offences).to eq('Enabled' => false)
|
72
|
+
end
|
73
|
+
end
|
74
|
+
|
75
|
+
it "registers an offence for a when clause that's deeper than case" do
|
76
|
+
source = ['case a',
|
77
|
+
' when 0 then return',
|
78
|
+
' else',
|
79
|
+
' case b',
|
80
|
+
' when 1 then return',
|
81
|
+
' end',
|
82
|
+
'end']
|
83
|
+
inspect_source(cop, source)
|
84
|
+
expect(cop.messages).to eq(['Indent when as deep as case.'] * 2)
|
85
|
+
end
|
86
|
+
|
87
|
+
it "accepts a when clause that's equally indented with case" do
|
88
|
+
source = ['y = case a',
|
89
|
+
' when 0 then break',
|
90
|
+
' when 0 then return',
|
91
|
+
' else',
|
92
|
+
' z = case b',
|
93
|
+
' when 1 then return',
|
94
|
+
' when 1 then break',
|
95
|
+
' end',
|
96
|
+
' end',
|
97
|
+
'case c',
|
98
|
+
'when 2 then encoding',
|
99
|
+
'end',
|
100
|
+
'']
|
101
|
+
inspect_source(cop, source)
|
102
|
+
expect(cop.offences).to be_empty
|
103
|
+
end
|
104
|
+
|
105
|
+
it "doesn't get confused by strings with case in them" do
|
106
|
+
source = ['a = "case"',
|
107
|
+
'case x',
|
108
|
+
'when 0',
|
109
|
+
'end',
|
110
|
+
'']
|
111
|
+
inspect_source(cop, source)
|
112
|
+
expect(cop.messages).to be_empty
|
113
|
+
end
|
114
|
+
|
115
|
+
it "doesn't get confused by symbols named case or when" do
|
116
|
+
source = ['KEYWORDS = { :case => true, :when => true }',
|
117
|
+
'case type',
|
118
|
+
'when 0',
|
119
|
+
' ParameterNode',
|
120
|
+
'when 1',
|
121
|
+
' MethodCallNode',
|
122
|
+
'end',
|
123
|
+
'']
|
124
|
+
inspect_source(cop, source)
|
125
|
+
expect(cop.messages).to be_empty
|
126
|
+
end
|
127
|
+
|
128
|
+
it 'accepts correctly indented whens in complex combinations' do
|
129
|
+
source = ['each {',
|
130
|
+
' case state',
|
131
|
+
' when 0',
|
132
|
+
' case name',
|
133
|
+
' when :a',
|
134
|
+
' end',
|
135
|
+
' when 1',
|
136
|
+
' loop {',
|
137
|
+
' case name',
|
138
|
+
' when :b',
|
139
|
+
' end',
|
140
|
+
' }',
|
141
|
+
' end',
|
142
|
+
'}',
|
143
|
+
'case s',
|
144
|
+
'when Array',
|
145
|
+
'end',
|
146
|
+
'']
|
147
|
+
inspect_source(cop, source)
|
148
|
+
expect(cop.messages).to be_empty
|
149
|
+
end
|
150
|
+
end
|
151
|
+
|
152
|
+
context 'with IndentOneStep: true' do
|
153
|
+
let(:cop_config) do
|
154
|
+
{ 'IndentWhenRelativeTo' => 'case', 'IndentOneStep' => true }
|
155
|
+
end
|
156
|
+
|
157
|
+
context 'regarding assignment where the right hand side is a case' do
|
158
|
+
it 'accepts a correcty indented assignment' do
|
159
|
+
source = ['output = case variable',
|
160
|
+
" when 'value1'",
|
161
|
+
" 'output1'",
|
162
|
+
' else',
|
163
|
+
" 'output2'",
|
164
|
+
' end']
|
165
|
+
inspect_source(cop, source)
|
166
|
+
expect(cop.offences).to be_empty
|
167
|
+
end
|
168
|
+
|
169
|
+
it 'registers on offence for an assignment indented some other way' do
|
170
|
+
source = ['output = case variable',
|
171
|
+
" when 'value1'",
|
172
|
+
" 'output1'",
|
173
|
+
' else',
|
174
|
+
" 'output2'",
|
175
|
+
' end']
|
176
|
+
inspect_source(cop, source)
|
177
|
+
expect(cop.messages).to eq(['Indent when one step more than case.'])
|
178
|
+
end
|
179
|
+
end
|
180
|
+
|
181
|
+
it "accepts a when clause that's 2 spaces deeper than case" do
|
182
|
+
source = ['case a',
|
183
|
+
' when 0 then return',
|
184
|
+
' else',
|
185
|
+
' case b',
|
186
|
+
' when 1 then return',
|
187
|
+
' end',
|
188
|
+
'end']
|
189
|
+
inspect_source(cop, source)
|
190
|
+
expect(cop.offences).to be_empty
|
191
|
+
end
|
192
|
+
|
193
|
+
it "registers an offence for a when clause that's equally indented " \
|
194
|
+
'with case' do
|
195
|
+
source = ['y = case a',
|
196
|
+
' when 0 then break',
|
197
|
+
' when 0 then return',
|
198
|
+
' z = case b',
|
199
|
+
' when 1 then return',
|
200
|
+
' when 1 then break',
|
201
|
+
' end',
|
202
|
+
' end',
|
203
|
+
'case c',
|
204
|
+
'when 2 then encoding',
|
205
|
+
'end',
|
206
|
+
'']
|
207
|
+
inspect_source(cop, source)
|
208
|
+
expect(cop.messages)
|
209
|
+
.to eq(['Indent when one step more than case.'] * 5)
|
210
|
+
end
|
211
|
+
end
|
212
|
+
end
|
213
|
+
|
214
|
+
context 'with IndentWhenRelativeTo: end' do
|
215
|
+
context 'with IndentOneStep: false' do
|
216
|
+
let(:cop_config) do
|
217
|
+
{ 'IndentWhenRelativeTo' => 'end', 'IndentOneStep' => false }
|
218
|
+
end
|
219
|
+
|
220
|
+
context 'regarding assignment where the right hand side is a case' do
|
221
|
+
it 'accepts a correcty indented assignment' do
|
222
|
+
source = ['output = case variable',
|
223
|
+
"when 'value1'",
|
224
|
+
" 'output1'",
|
225
|
+
'else',
|
226
|
+
" 'output2'",
|
227
|
+
'end']
|
228
|
+
inspect_source(cop, source)
|
229
|
+
expect(cop.offences).to be_empty
|
230
|
+
end
|
231
|
+
|
232
|
+
it 'registers on offence for an assignment indented some other way' do
|
233
|
+
source = ['output = case variable',
|
234
|
+
" when 'value1'",
|
235
|
+
" 'output1'",
|
236
|
+
' else',
|
237
|
+
" 'output2'",
|
238
|
+
'end']
|
239
|
+
inspect_source(cop, source)
|
240
|
+
expect(cop.messages).to eq(['Indent when as deep as end.'])
|
241
|
+
end
|
242
|
+
end
|
243
|
+
end
|
244
|
+
|
245
|
+
context 'with IndentOneStep: true' do
|
246
|
+
let(:cop_config) do
|
247
|
+
{ 'IndentWhenRelativeTo' => 'end', 'IndentOneStep' => true }
|
248
|
+
end
|
249
|
+
|
250
|
+
context 'regarding assignment where the right hand side is a case' do
|
251
|
+
it 'accepts a correcty indented assignment' do
|
252
|
+
source = ['output = case variable',
|
253
|
+
" when 'value1'",
|
254
|
+
" 'output1'",
|
255
|
+
' else',
|
256
|
+
" 'output2'",
|
257
|
+
'end']
|
258
|
+
inspect_source(cop, source)
|
259
|
+
expect(cop.offences).to be_empty
|
260
|
+
end
|
261
|
+
|
262
|
+
it 'registers on offence for an assignment indented as case' do
|
263
|
+
source = ['output = case variable',
|
264
|
+
" when 'value1'",
|
265
|
+
" 'output1'",
|
266
|
+
' else',
|
267
|
+
" 'output2'",
|
268
|
+
' end']
|
269
|
+
inspect_source(cop, source)
|
270
|
+
expect(cop.messages).to eq(['Indent when one step more than end.'])
|
271
|
+
expect(cop.config_to_allow_offences).to eq('IndentWhenRelativeTo' =>
|
272
|
+
'case')
|
273
|
+
end
|
274
|
+
|
275
|
+
it 'registers on offence for an assignment indented some other way' do
|
276
|
+
source = ['output = case variable',
|
277
|
+
" when 'value1'",
|
278
|
+
" 'output1'",
|
279
|
+
' else',
|
280
|
+
" 'output2'",
|
281
|
+
' end']
|
282
|
+
inspect_source(cop, source)
|
283
|
+
expect(cop.messages).to eq(['Indent when one step more than end.'])
|
284
|
+
expect(cop.config_to_allow_offences).to eq('Enabled' => false)
|
285
|
+
end
|
286
|
+
end
|
287
|
+
end
|
288
|
+
end
|
289
|
+
end
|