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,156 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
require 'spec_helper'
|
4
|
+
|
5
|
+
describe Rubocop::Cop::Team do
|
6
|
+
subject(:team) { described_class.new(cop_classes, config, options) }
|
7
|
+
let(:cop_classes) { Rubocop::Cop::Cop.non_rails }
|
8
|
+
let(:config) { Rubocop::ConfigLoader.default_configuration }
|
9
|
+
let(:options) { nil }
|
10
|
+
|
11
|
+
describe '#autocorrect?' do
|
12
|
+
subject { team.autocorrect? }
|
13
|
+
|
14
|
+
context 'when the option argument of .new is omitted' do
|
15
|
+
subject { described_class.new(cop_classes, config).autocorrect? }
|
16
|
+
it { should be_false }
|
17
|
+
end
|
18
|
+
|
19
|
+
context 'when { auto_correct: true } is passed to .new' do
|
20
|
+
let(:options) { { auto_correct: true } }
|
21
|
+
it { should be_true }
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
describe '#debug?' do
|
26
|
+
subject { team.debug? }
|
27
|
+
|
28
|
+
context 'when the option argument of .new is omitted' do
|
29
|
+
subject { described_class.new(cop_classes, config).debug? }
|
30
|
+
it { should be_false }
|
31
|
+
end
|
32
|
+
|
33
|
+
context 'when { debug: true } is passed to .new' do
|
34
|
+
let(:options) { { debug: true } }
|
35
|
+
it { should be_true }
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
39
|
+
describe '#inspect_file', :isolated_environment do
|
40
|
+
include FileHelper
|
41
|
+
|
42
|
+
let(:file_path) { 'example.rb' }
|
43
|
+
let(:offences) { team.inspect_file(file_path) }
|
44
|
+
|
45
|
+
before do
|
46
|
+
create_file(file_path, [
|
47
|
+
'#' * 90,
|
48
|
+
'puts test;'
|
49
|
+
])
|
50
|
+
end
|
51
|
+
|
52
|
+
it 'returns offences' do
|
53
|
+
expect(offences).not_to be_empty
|
54
|
+
expect(offences.all? { |o| o.is_a?(Rubocop::Cop::Offence) }).to be_true
|
55
|
+
end
|
56
|
+
|
57
|
+
context 'when Parser cannot parse the file' do
|
58
|
+
before do
|
59
|
+
create_file(file_path, [
|
60
|
+
'#' * 90,
|
61
|
+
'class Test'
|
62
|
+
])
|
63
|
+
end
|
64
|
+
|
65
|
+
it 'returns only error offences' do
|
66
|
+
expect(offences.size).to eq(1)
|
67
|
+
offence = offences.first
|
68
|
+
expect(offence.cop_name).to eq('Syntax')
|
69
|
+
expect(offence.severity).to eq(:error)
|
70
|
+
end
|
71
|
+
end
|
72
|
+
|
73
|
+
context 'when Parser reports non-fatal warning for the file' do
|
74
|
+
before do
|
75
|
+
create_file(file_path, [
|
76
|
+
'# encoding: utf-8',
|
77
|
+
'#' * 90,
|
78
|
+
'puts *test'
|
79
|
+
])
|
80
|
+
end
|
81
|
+
|
82
|
+
let(:cop_names) { offences.map(&:cop_name) }
|
83
|
+
|
84
|
+
it 'returns Parser warning offences' do
|
85
|
+
expect(cop_names).to include('AmbiguousOperator')
|
86
|
+
end
|
87
|
+
|
88
|
+
it 'returns offences from cops' do
|
89
|
+
expect(cop_names).to include('LineLength')
|
90
|
+
end
|
91
|
+
end
|
92
|
+
|
93
|
+
context 'when autocorrection is enabled' do
|
94
|
+
let(:options) { { auto_correct: true } }
|
95
|
+
|
96
|
+
before do
|
97
|
+
create_file(file_path, [
|
98
|
+
'# encoding: utf-8',
|
99
|
+
'puts "string"'
|
100
|
+
])
|
101
|
+
end
|
102
|
+
|
103
|
+
it 'does autocorrection' do
|
104
|
+
team.inspect_file(file_path)
|
105
|
+
corrected_source = File.read(file_path)
|
106
|
+
expect(corrected_source).to eq([
|
107
|
+
'# encoding: utf-8',
|
108
|
+
"puts 'string'",
|
109
|
+
''
|
110
|
+
].join("\n"))
|
111
|
+
end
|
112
|
+
|
113
|
+
it 'still returns offences' do
|
114
|
+
expect(offences.first.cop_name).to eq('StringLiterals')
|
115
|
+
end
|
116
|
+
end
|
117
|
+
end
|
118
|
+
|
119
|
+
describe '#cops' do
|
120
|
+
subject(:cops) { team.cops }
|
121
|
+
|
122
|
+
it 'returns cop instances' do
|
123
|
+
expect(cops).not_to be_empty
|
124
|
+
expect(cops.all? { |c| c.is_a?(Rubocop::Cop::Cop) }).to be_true
|
125
|
+
end
|
126
|
+
|
127
|
+
context 'when only some cop classes are passed to .new' do
|
128
|
+
let(:cop_classes) do
|
129
|
+
[Rubocop::Cop::Lint::Void, Rubocop::Cop::Style::LineLength]
|
130
|
+
end
|
131
|
+
|
132
|
+
it 'returns only intances of the classes' do
|
133
|
+
expect(cops.size).to eq(2)
|
134
|
+
cops.sort! { |a, b| a.name <=> b.name }
|
135
|
+
expect(cops[0].name).to eq('LineLength')
|
136
|
+
expect(cops[1].name).to eq('Void')
|
137
|
+
end
|
138
|
+
end
|
139
|
+
|
140
|
+
context 'when some classes are disabled with config' do
|
141
|
+
before do
|
142
|
+
%w(Void LineLength).each do |cop_name|
|
143
|
+
config.for_cop(cop_name)['Enabled'] = false
|
144
|
+
end
|
145
|
+
end
|
146
|
+
|
147
|
+
let(:cop_names) { cops.map(&:name) }
|
148
|
+
|
149
|
+
it 'does not return intances of the classes' do
|
150
|
+
expect(cops).not_to be_empty
|
151
|
+
expect(cop_names).not_to include('Void')
|
152
|
+
expect(cop_names).not_to include('LineLength')
|
153
|
+
end
|
154
|
+
end
|
155
|
+
end
|
156
|
+
end
|
@@ -0,0 +1,49 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
require 'spec_helper'
|
4
|
+
|
5
|
+
describe Rubocop::Cop::Util do
|
6
|
+
describe '#line_range' do
|
7
|
+
include ASTHelper
|
8
|
+
|
9
|
+
let(:source) do
|
10
|
+
<<-END
|
11
|
+
foo = 1
|
12
|
+
bar = 2
|
13
|
+
class Test
|
14
|
+
def some_method
|
15
|
+
do_something
|
16
|
+
end
|
17
|
+
end
|
18
|
+
baz = 8
|
19
|
+
END
|
20
|
+
end
|
21
|
+
|
22
|
+
let(:ast) do
|
23
|
+
processed_source = parse_source(source)
|
24
|
+
processed_source.ast
|
25
|
+
end
|
26
|
+
|
27
|
+
let(:node) do
|
28
|
+
target_node = scan_node(ast) do |node|
|
29
|
+
break node if node.type == :class
|
30
|
+
end
|
31
|
+
fail 'No target node found!' unless target_node
|
32
|
+
target_node
|
33
|
+
end
|
34
|
+
|
35
|
+
context 'when Source::Range object is passed' do
|
36
|
+
it 'returns line range of that' do
|
37
|
+
line_range = Rubocop::Cop::Util.line_range(node.loc.expression)
|
38
|
+
expect(line_range).to eq(3..7)
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
42
|
+
context 'when AST::Node object is passed' do
|
43
|
+
it 'returns line range of the expression' do
|
44
|
+
line_range = Rubocop::Cop::Util.line_range(node)
|
45
|
+
expect(line_range).to eq(3..7)
|
46
|
+
end
|
47
|
+
end
|
48
|
+
end
|
49
|
+
end
|
@@ -0,0 +1,213 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
require 'spec_helper'
|
4
|
+
|
5
|
+
describe Rubocop::Cop::VariableInspector::Assignment do
|
6
|
+
include ASTHelper
|
7
|
+
include AST::Sexp
|
8
|
+
|
9
|
+
let(:ast) do
|
10
|
+
processed_source = Rubocop::SourceParser.parse(source)
|
11
|
+
processed_source.ast
|
12
|
+
end
|
13
|
+
|
14
|
+
let(:source) do
|
15
|
+
<<-END
|
16
|
+
class SomeClass
|
17
|
+
def some_method(flag)
|
18
|
+
puts 'Hello World!'
|
19
|
+
|
20
|
+
if flag > 0
|
21
|
+
foo = 1
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
25
|
+
END
|
26
|
+
end
|
27
|
+
|
28
|
+
let(:def_node) do
|
29
|
+
found_node = scan_node(ast, include_origin_node: true) do |node|
|
30
|
+
break node if node.type == :def
|
31
|
+
end
|
32
|
+
fail 'No def node found!' unless found_node
|
33
|
+
found_node
|
34
|
+
end
|
35
|
+
|
36
|
+
let(:lvasgn_node) do
|
37
|
+
found_node = scan_node(ast) do |node|
|
38
|
+
break node if node.type == :lvasgn
|
39
|
+
end
|
40
|
+
fail 'No lvasgn node found!' unless found_node
|
41
|
+
found_node
|
42
|
+
end
|
43
|
+
|
44
|
+
let(:name) { lvasgn_node.children.first }
|
45
|
+
let(:scope) { Rubocop::Cop::VariableInspector::Scope.new(def_node) }
|
46
|
+
let(:variable) do
|
47
|
+
Rubocop::Cop::VariableInspector::Variable.new(name, lvasgn_node, scope)
|
48
|
+
end
|
49
|
+
let(:assignment) { described_class.new(lvasgn_node, variable) }
|
50
|
+
|
51
|
+
describe '.new' do
|
52
|
+
let(:variable) { double('variable') }
|
53
|
+
|
54
|
+
context 'when an assignment node is passed' do
|
55
|
+
it 'does not raise error' do
|
56
|
+
node = s(:lvasgn, :foo)
|
57
|
+
expect { described_class.new(node, variable) }.not_to raise_error
|
58
|
+
end
|
59
|
+
end
|
60
|
+
|
61
|
+
context 'when an argument declaration node is passed' do
|
62
|
+
it 'raises error' do
|
63
|
+
node = s(:arg, :foo)
|
64
|
+
expect { described_class.new(node, variable) }
|
65
|
+
.to raise_error(ArgumentError)
|
66
|
+
end
|
67
|
+
end
|
68
|
+
|
69
|
+
context 'when any other type node is passed' do
|
70
|
+
it 'raises error' do
|
71
|
+
node = s(:def)
|
72
|
+
expect { described_class.new(node, variable) }
|
73
|
+
.to raise_error(ArgumentError)
|
74
|
+
end
|
75
|
+
end
|
76
|
+
end
|
77
|
+
|
78
|
+
describe '#name' do
|
79
|
+
it 'returns the variable name' do
|
80
|
+
expect(assignment.name).to eq(:foo)
|
81
|
+
end
|
82
|
+
end
|
83
|
+
|
84
|
+
describe '#meta_assignment_node' do
|
85
|
+
context 'when it is += operator assignment' do
|
86
|
+
let(:source) do
|
87
|
+
<<-END
|
88
|
+
def some_method
|
89
|
+
foo += 1
|
90
|
+
end
|
91
|
+
END
|
92
|
+
end
|
93
|
+
|
94
|
+
it 'returns op_asgn node' do
|
95
|
+
expect(assignment.meta_assignment_node.type).to eq(:op_asgn)
|
96
|
+
end
|
97
|
+
end
|
98
|
+
|
99
|
+
context 'when it is ||= operator assignment' do
|
100
|
+
let(:source) do
|
101
|
+
<<-END
|
102
|
+
def some_method
|
103
|
+
foo ||= 1
|
104
|
+
end
|
105
|
+
END
|
106
|
+
end
|
107
|
+
|
108
|
+
it 'returns or_asgn node' do
|
109
|
+
expect(assignment.meta_assignment_node.type).to eq(:or_asgn)
|
110
|
+
end
|
111
|
+
end
|
112
|
+
|
113
|
+
context 'when it is &&= operator assignment' do
|
114
|
+
let(:source) do
|
115
|
+
<<-END
|
116
|
+
def some_method
|
117
|
+
foo &&= 1
|
118
|
+
end
|
119
|
+
END
|
120
|
+
end
|
121
|
+
|
122
|
+
it 'returns and_asgn node' do
|
123
|
+
expect(assignment.meta_assignment_node.type).to eq(:and_asgn)
|
124
|
+
end
|
125
|
+
end
|
126
|
+
|
127
|
+
context 'when it is multiple assignment' do
|
128
|
+
let(:source) do
|
129
|
+
<<-END
|
130
|
+
def some_method
|
131
|
+
foo, bar = [1, 2]
|
132
|
+
end
|
133
|
+
END
|
134
|
+
end
|
135
|
+
|
136
|
+
it 'returns masgn node' do
|
137
|
+
expect(assignment.meta_assignment_node.type).to eq(:masgn)
|
138
|
+
end
|
139
|
+
end
|
140
|
+
end
|
141
|
+
|
142
|
+
describe '#operator' do
|
143
|
+
context 'when it is normal assignment' do
|
144
|
+
let(:source) do
|
145
|
+
<<-END
|
146
|
+
def some_method
|
147
|
+
foo = 1
|
148
|
+
end
|
149
|
+
END
|
150
|
+
end
|
151
|
+
|
152
|
+
it 'returns =' do
|
153
|
+
expect(assignment.operator).to eq('=')
|
154
|
+
end
|
155
|
+
end
|
156
|
+
|
157
|
+
context 'when it is += operator assignment' do
|
158
|
+
let(:source) do
|
159
|
+
<<-END
|
160
|
+
def some_method
|
161
|
+
foo += 1
|
162
|
+
end
|
163
|
+
END
|
164
|
+
end
|
165
|
+
|
166
|
+
it 'returns +=' do
|
167
|
+
expect(assignment.operator).to eq('+=')
|
168
|
+
end
|
169
|
+
end
|
170
|
+
|
171
|
+
context 'when it is ||= operator assignment' do
|
172
|
+
let(:source) do
|
173
|
+
<<-END
|
174
|
+
def some_method
|
175
|
+
foo ||= 1
|
176
|
+
end
|
177
|
+
END
|
178
|
+
end
|
179
|
+
|
180
|
+
it 'returns ||=' do
|
181
|
+
expect(assignment.operator).to eq('||=')
|
182
|
+
end
|
183
|
+
end
|
184
|
+
|
185
|
+
context 'when it is &&= operator assignment' do
|
186
|
+
let(:source) do
|
187
|
+
<<-END
|
188
|
+
def some_method
|
189
|
+
foo &&= 1
|
190
|
+
end
|
191
|
+
END
|
192
|
+
end
|
193
|
+
|
194
|
+
it 'returns &&=' do
|
195
|
+
expect(assignment.operator).to eq('&&=')
|
196
|
+
end
|
197
|
+
end
|
198
|
+
|
199
|
+
context 'when it is multiple assignment' do
|
200
|
+
let(:source) do
|
201
|
+
<<-END
|
202
|
+
def some_method
|
203
|
+
foo, bar = [1, 2]
|
204
|
+
end
|
205
|
+
END
|
206
|
+
end
|
207
|
+
|
208
|
+
it 'returns =' do
|
209
|
+
expect(assignment.operator).to eq('=')
|
210
|
+
end
|
211
|
+
end
|
212
|
+
end
|
213
|
+
end
|
@@ -0,0 +1,734 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
require 'spec_helper'
|
4
|
+
|
5
|
+
describe Rubocop::Cop::VariableInspector::Locatable do
|
6
|
+
include ASTHelper
|
7
|
+
include AST::Sexp
|
8
|
+
|
9
|
+
class LocatableObject
|
10
|
+
include Rubocop::Cop::VariableInspector::Locatable
|
11
|
+
|
12
|
+
attr_reader :node, :scope
|
13
|
+
|
14
|
+
def initialize(node, scope)
|
15
|
+
@node = node
|
16
|
+
@scope = scope
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
let(:ast) do
|
21
|
+
processed_source = Rubocop::SourceParser.parse(source)
|
22
|
+
processed_source.ast
|
23
|
+
end
|
24
|
+
|
25
|
+
let(:def_node) do
|
26
|
+
found_node = scan_node(ast, include_origin_node: true) do |node|
|
27
|
+
break node if node.type == :def
|
28
|
+
end
|
29
|
+
fail 'No def node found!' unless found_node
|
30
|
+
found_node
|
31
|
+
end
|
32
|
+
|
33
|
+
let(:lvasgn_node) do
|
34
|
+
found_node = scan_node(ast) do |node|
|
35
|
+
break node if node.type == :lvasgn
|
36
|
+
end
|
37
|
+
fail 'No lvasgn node found!' unless found_node
|
38
|
+
found_node
|
39
|
+
end
|
40
|
+
|
41
|
+
let(:scope) { Rubocop::Cop::VariableInspector::Scope.new(def_node) }
|
42
|
+
let(:assignment) { LocatableObject.new(lvasgn_node, scope) }
|
43
|
+
|
44
|
+
describe '#ancestor_nodes_in_scope' do
|
45
|
+
let(:source) do
|
46
|
+
<<-END
|
47
|
+
class SomeClass
|
48
|
+
def some_method(flag)
|
49
|
+
puts 'Hello World!'
|
50
|
+
|
51
|
+
if flag > 0
|
52
|
+
foo = 1
|
53
|
+
end
|
54
|
+
end
|
55
|
+
end
|
56
|
+
END
|
57
|
+
end
|
58
|
+
|
59
|
+
it 'returns its ancestor nodes in the scope excluding scope node' do
|
60
|
+
ancestor_types = assignment.ancestor_nodes_in_scope.map(&:type)
|
61
|
+
expect(ancestor_types).to eq([:begin, :if])
|
62
|
+
end
|
63
|
+
end
|
64
|
+
|
65
|
+
describe '#branch_point_node' do
|
66
|
+
context 'when it is not in branch' do
|
67
|
+
let(:source) do
|
68
|
+
<<-END
|
69
|
+
def some_method(flag)
|
70
|
+
foo = 1
|
71
|
+
end
|
72
|
+
END
|
73
|
+
end
|
74
|
+
|
75
|
+
it 'returns nil' do
|
76
|
+
expect(assignment.branch_point_node).to be_nil
|
77
|
+
end
|
78
|
+
end
|
79
|
+
|
80
|
+
context 'when it is inside of if' do
|
81
|
+
let(:source) do
|
82
|
+
<<-END
|
83
|
+
def some_method(flag)
|
84
|
+
if flag
|
85
|
+
foo = 1
|
86
|
+
end
|
87
|
+
end
|
88
|
+
END
|
89
|
+
end
|
90
|
+
|
91
|
+
it 'returns the if node' do
|
92
|
+
expect(assignment.branch_point_node.type).to eq(:if)
|
93
|
+
end
|
94
|
+
end
|
95
|
+
|
96
|
+
context 'when it is inside of else of if' do
|
97
|
+
let(:source) do
|
98
|
+
<<-END
|
99
|
+
def some_method(flag)
|
100
|
+
if flag
|
101
|
+
else
|
102
|
+
foo = 1
|
103
|
+
end
|
104
|
+
end
|
105
|
+
END
|
106
|
+
end
|
107
|
+
|
108
|
+
it 'returns the if node' do
|
109
|
+
expect(assignment.branch_point_node.type).to eq(:if)
|
110
|
+
end
|
111
|
+
end
|
112
|
+
|
113
|
+
context 'when it is inside of if condition' do
|
114
|
+
let(:source) do
|
115
|
+
<<-END
|
116
|
+
def some_method(flag)
|
117
|
+
if foo = 1
|
118
|
+
do_something
|
119
|
+
end
|
120
|
+
end
|
121
|
+
END
|
122
|
+
end
|
123
|
+
|
124
|
+
it 'returns nil' do
|
125
|
+
expect(assignment.branch_point_node).to be_nil
|
126
|
+
end
|
127
|
+
end
|
128
|
+
|
129
|
+
context 'when multiple if are nested' do
|
130
|
+
context 'and it is inside of inner if' do
|
131
|
+
let(:source) do
|
132
|
+
<<-END
|
133
|
+
def some_method(a, b)
|
134
|
+
if a
|
135
|
+
if b
|
136
|
+
foo = 1
|
137
|
+
end
|
138
|
+
end
|
139
|
+
end
|
140
|
+
END
|
141
|
+
end
|
142
|
+
|
143
|
+
it 'returns inner if node' do
|
144
|
+
if_node = assignment.branch_point_node
|
145
|
+
expect(if_node.type).to eq(:if)
|
146
|
+
condition_node = if_node.children.first
|
147
|
+
expect(condition_node).to eq(s(:lvar, :b))
|
148
|
+
end
|
149
|
+
end
|
150
|
+
|
151
|
+
context 'and it is inside of inner if condition' do
|
152
|
+
let(:source) do
|
153
|
+
<<-END
|
154
|
+
def some_method(a, b)
|
155
|
+
if a
|
156
|
+
if foo = 1
|
157
|
+
do_something
|
158
|
+
end
|
159
|
+
end
|
160
|
+
end
|
161
|
+
END
|
162
|
+
end
|
163
|
+
|
164
|
+
it 'returns the next outer if node' do
|
165
|
+
if_node = assignment.branch_point_node
|
166
|
+
expect(if_node.type).to eq(:if)
|
167
|
+
condition_node = if_node.children.first
|
168
|
+
expect(condition_node).to eq(s(:lvar, :a))
|
169
|
+
end
|
170
|
+
end
|
171
|
+
end
|
172
|
+
|
173
|
+
context 'when it is inside of when of case' do
|
174
|
+
let(:source) do
|
175
|
+
<<-END
|
176
|
+
def some_method(flag)
|
177
|
+
case flag
|
178
|
+
when 1
|
179
|
+
foo = 1
|
180
|
+
end
|
181
|
+
end
|
182
|
+
END
|
183
|
+
end
|
184
|
+
|
185
|
+
it 'returns the case node' do
|
186
|
+
expect(assignment.branch_point_node.type).to eq(:case)
|
187
|
+
end
|
188
|
+
end
|
189
|
+
|
190
|
+
context 'when it is on the left side of &&' do
|
191
|
+
let(:source) do
|
192
|
+
<<-END
|
193
|
+
def some_method
|
194
|
+
(foo = 1) && do_something
|
195
|
+
end
|
196
|
+
END
|
197
|
+
end
|
198
|
+
|
199
|
+
it 'returns nil' do
|
200
|
+
expect(assignment.branch_point_node).to be_nil
|
201
|
+
end
|
202
|
+
end
|
203
|
+
|
204
|
+
context 'when it is on the right side of &&' do
|
205
|
+
let(:source) do
|
206
|
+
<<-END
|
207
|
+
def some_method
|
208
|
+
do_something && (foo = 1)
|
209
|
+
end
|
210
|
+
END
|
211
|
+
end
|
212
|
+
|
213
|
+
it 'returns the and node' do
|
214
|
+
expect(assignment.branch_point_node.type).to eq(:and)
|
215
|
+
end
|
216
|
+
end
|
217
|
+
|
218
|
+
context 'when it is on the left side of ||' do
|
219
|
+
let(:source) do
|
220
|
+
<<-END
|
221
|
+
def some_method
|
222
|
+
(foo = 1) || do_something
|
223
|
+
end
|
224
|
+
END
|
225
|
+
end
|
226
|
+
|
227
|
+
it 'returns nil' do
|
228
|
+
expect(assignment.branch_point_node).to be_nil
|
229
|
+
end
|
230
|
+
end
|
231
|
+
|
232
|
+
context 'when it is on the right side of ||' do
|
233
|
+
let(:source) do
|
234
|
+
<<-END
|
235
|
+
def some_method
|
236
|
+
do_something || (foo = 1)
|
237
|
+
end
|
238
|
+
END
|
239
|
+
end
|
240
|
+
|
241
|
+
it 'returns the or node' do
|
242
|
+
expect(assignment.branch_point_node.type).to eq(:or)
|
243
|
+
end
|
244
|
+
end
|
245
|
+
|
246
|
+
context 'when multiple && are chained' do
|
247
|
+
context 'and it is on the right side of the right &&' do
|
248
|
+
let(:source) do
|
249
|
+
<<-END
|
250
|
+
def some_method
|
251
|
+
do_something && do_anything && (foo = 1)
|
252
|
+
end
|
253
|
+
END
|
254
|
+
end
|
255
|
+
|
256
|
+
it 'returns the right and node' do
|
257
|
+
and_node = assignment.branch_point_node
|
258
|
+
expect(and_node.type).to eq(:and)
|
259
|
+
right_side_node = and_node.children[1]
|
260
|
+
expect(right_side_node.type).to eq(:begin)
|
261
|
+
end
|
262
|
+
end
|
263
|
+
|
264
|
+
context 'and it is on the right side of the left &&' do
|
265
|
+
let(:source) do
|
266
|
+
<<-END
|
267
|
+
def some_method
|
268
|
+
do_something && (foo = 1) && do_anything
|
269
|
+
end
|
270
|
+
END
|
271
|
+
end
|
272
|
+
|
273
|
+
it 'returns the left and node' do
|
274
|
+
and_node = assignment.branch_point_node
|
275
|
+
expect(and_node.type).to eq(:and)
|
276
|
+
right_side_node = and_node.children[1]
|
277
|
+
expect(right_side_node.type).to eq(:begin)
|
278
|
+
end
|
279
|
+
end
|
280
|
+
end
|
281
|
+
|
282
|
+
context 'when it is inside of begin with rescue' do
|
283
|
+
let(:source) do
|
284
|
+
<<-END
|
285
|
+
def some_method(flag)
|
286
|
+
begin
|
287
|
+
foo = 1
|
288
|
+
rescue
|
289
|
+
do_something
|
290
|
+
end
|
291
|
+
end
|
292
|
+
END
|
293
|
+
end
|
294
|
+
|
295
|
+
it 'returns the rescue node' do
|
296
|
+
expect(assignment.branch_point_node.type).to eq(:rescue)
|
297
|
+
end
|
298
|
+
end
|
299
|
+
|
300
|
+
context 'when it is inside of rescue' do
|
301
|
+
let(:source) do
|
302
|
+
<<-END
|
303
|
+
def some_method(flag)
|
304
|
+
begin
|
305
|
+
do_something
|
306
|
+
rescue
|
307
|
+
foo = 1
|
308
|
+
end
|
309
|
+
end
|
310
|
+
END
|
311
|
+
end
|
312
|
+
|
313
|
+
it 'returns the rescue node' do
|
314
|
+
expect(assignment.branch_point_node.type).to eq(:rescue)
|
315
|
+
end
|
316
|
+
end
|
317
|
+
|
318
|
+
context 'when it is inside of begin with ensure' do
|
319
|
+
let(:source) do
|
320
|
+
<<-END
|
321
|
+
def some_method(flag)
|
322
|
+
begin
|
323
|
+
foo = 1
|
324
|
+
ensure
|
325
|
+
do_something
|
326
|
+
end
|
327
|
+
end
|
328
|
+
END
|
329
|
+
end
|
330
|
+
|
331
|
+
it 'returns the ensure node' do
|
332
|
+
expect(assignment.branch_point_node.type).to eq(:ensure)
|
333
|
+
end
|
334
|
+
end
|
335
|
+
|
336
|
+
context 'when it is inside of ensure' do
|
337
|
+
let(:source) do
|
338
|
+
<<-END
|
339
|
+
def some_method(flag)
|
340
|
+
begin
|
341
|
+
do_something
|
342
|
+
ensure
|
343
|
+
foo = 1
|
344
|
+
end
|
345
|
+
end
|
346
|
+
END
|
347
|
+
end
|
348
|
+
|
349
|
+
it 'returns nil' do
|
350
|
+
expect(assignment.branch_point_node).to be_nil
|
351
|
+
end
|
352
|
+
end
|
353
|
+
|
354
|
+
context 'when it is inside of begin without rescue' do
|
355
|
+
let(:source) do
|
356
|
+
<<-END
|
357
|
+
def some_method(flag)
|
358
|
+
begin
|
359
|
+
foo = 1
|
360
|
+
end
|
361
|
+
end
|
362
|
+
END
|
363
|
+
end
|
364
|
+
|
365
|
+
it 'returns nil' do
|
366
|
+
expect(assignment.branch_point_node).to be_nil
|
367
|
+
end
|
368
|
+
end
|
369
|
+
end
|
370
|
+
|
371
|
+
describe '#branch_body_node' do
|
372
|
+
context 'when it is not in branch' do
|
373
|
+
let(:source) do
|
374
|
+
<<-END
|
375
|
+
def some_method(flag)
|
376
|
+
foo = 1
|
377
|
+
end
|
378
|
+
END
|
379
|
+
end
|
380
|
+
|
381
|
+
it 'returns nil' do
|
382
|
+
expect(assignment.branch_body_node).to be_nil
|
383
|
+
end
|
384
|
+
end
|
385
|
+
|
386
|
+
context 'when it is inside body of if' do
|
387
|
+
let(:source) do
|
388
|
+
<<-END
|
389
|
+
def some_method(flag)
|
390
|
+
if flag
|
391
|
+
foo = 1
|
392
|
+
puts foo
|
393
|
+
end
|
394
|
+
end
|
395
|
+
END
|
396
|
+
end
|
397
|
+
|
398
|
+
it 'returns the body node' do
|
399
|
+
expect(assignment.branch_body_node.type).to eq(:begin)
|
400
|
+
end
|
401
|
+
end
|
402
|
+
|
403
|
+
context 'when it is inside body of else of if' do
|
404
|
+
let(:source) do
|
405
|
+
<<-END
|
406
|
+
def some_method(flag)
|
407
|
+
if flag
|
408
|
+
do_something
|
409
|
+
else
|
410
|
+
foo = 1
|
411
|
+
puts foo
|
412
|
+
end
|
413
|
+
end
|
414
|
+
END
|
415
|
+
end
|
416
|
+
|
417
|
+
it 'returns the body node' do
|
418
|
+
expect(assignment.branch_body_node.type).to eq(:begin)
|
419
|
+
end
|
420
|
+
end
|
421
|
+
|
422
|
+
context 'when it is on the right side of &&' do
|
423
|
+
let(:source) do
|
424
|
+
<<-END
|
425
|
+
def some_method
|
426
|
+
do_something && (foo = 1)
|
427
|
+
end
|
428
|
+
END
|
429
|
+
end
|
430
|
+
|
431
|
+
it 'returns the right side node' do
|
432
|
+
expect(assignment.branch_body_node.type).to eq(:begin)
|
433
|
+
end
|
434
|
+
end
|
435
|
+
|
436
|
+
context 'when it is on the right side of ||' do
|
437
|
+
let(:source) do
|
438
|
+
<<-END
|
439
|
+
def some_method
|
440
|
+
do_something || (foo = 1)
|
441
|
+
end
|
442
|
+
END
|
443
|
+
end
|
444
|
+
|
445
|
+
it 'returns the right side node' do
|
446
|
+
expect(assignment.branch_body_node.type).to eq(:begin)
|
447
|
+
end
|
448
|
+
end
|
449
|
+
|
450
|
+
context 'when it is inside of begin with rescue' do
|
451
|
+
let(:source) do
|
452
|
+
<<-END
|
453
|
+
def some_method(flag)
|
454
|
+
begin
|
455
|
+
foo = 1
|
456
|
+
rescue
|
457
|
+
do_something
|
458
|
+
end
|
459
|
+
end
|
460
|
+
END
|
461
|
+
end
|
462
|
+
|
463
|
+
it 'returns the body node' do
|
464
|
+
expect(assignment.branch_body_node.type).to eq(:lvasgn)
|
465
|
+
end
|
466
|
+
end
|
467
|
+
|
468
|
+
context 'when it is inside of rescue' do
|
469
|
+
let(:source) do
|
470
|
+
<<-END
|
471
|
+
def some_method(flag)
|
472
|
+
begin
|
473
|
+
do_something
|
474
|
+
rescue
|
475
|
+
foo = 1
|
476
|
+
end
|
477
|
+
end
|
478
|
+
END
|
479
|
+
end
|
480
|
+
|
481
|
+
it 'returns the resbody node' do
|
482
|
+
expect(assignment.branch_body_node.type).to eq(:resbody)
|
483
|
+
end
|
484
|
+
end
|
485
|
+
|
486
|
+
context 'when it is inside of begin with ensure' do
|
487
|
+
let(:source) do
|
488
|
+
<<-END
|
489
|
+
def some_method(flag)
|
490
|
+
begin
|
491
|
+
foo = 1
|
492
|
+
ensure
|
493
|
+
do_something
|
494
|
+
end
|
495
|
+
end
|
496
|
+
END
|
497
|
+
end
|
498
|
+
|
499
|
+
it 'returns the body node' do
|
500
|
+
expect(assignment.branch_body_node.type).to eq(:lvasgn)
|
501
|
+
end
|
502
|
+
end
|
503
|
+
end
|
504
|
+
|
505
|
+
describe '#branch_id' do
|
506
|
+
context 'when it is not in branch' do
|
507
|
+
let(:source) do
|
508
|
+
<<-END
|
509
|
+
def some_method(flag)
|
510
|
+
foo = 1
|
511
|
+
end
|
512
|
+
END
|
513
|
+
end
|
514
|
+
|
515
|
+
it 'returns nil' do
|
516
|
+
expect(assignment.branch_id).to be_nil
|
517
|
+
end
|
518
|
+
end
|
519
|
+
|
520
|
+
context 'when it is inside body of if' do
|
521
|
+
let(:source) do
|
522
|
+
<<-END
|
523
|
+
def some_method(flag)
|
524
|
+
if flag
|
525
|
+
foo = 1
|
526
|
+
puts foo
|
527
|
+
end
|
528
|
+
end
|
529
|
+
END
|
530
|
+
end
|
531
|
+
|
532
|
+
it 'returns BRANCHNODEID_if_true' do
|
533
|
+
expect(assignment.branch_id).to match(/^\d+_if_true/)
|
534
|
+
end
|
535
|
+
end
|
536
|
+
|
537
|
+
context 'when it is inside body of else of if' do
|
538
|
+
let(:source) do
|
539
|
+
<<-END
|
540
|
+
def some_method(flag)
|
541
|
+
if flag
|
542
|
+
do_something
|
543
|
+
else
|
544
|
+
foo = 1
|
545
|
+
puts foo
|
546
|
+
end
|
547
|
+
end
|
548
|
+
END
|
549
|
+
end
|
550
|
+
|
551
|
+
it 'returns BRANCHNODEID_if_false' do
|
552
|
+
expect(assignment.branch_id).to match(/^\d+_if_false/)
|
553
|
+
end
|
554
|
+
end
|
555
|
+
|
556
|
+
context 'when it is inside body of when of case' do
|
557
|
+
let(:source) do
|
558
|
+
<<-END
|
559
|
+
def some_method(flag)
|
560
|
+
case flag
|
561
|
+
when first
|
562
|
+
do_something
|
563
|
+
when second
|
564
|
+
foo = 1
|
565
|
+
puts foo
|
566
|
+
else
|
567
|
+
do_something
|
568
|
+
end
|
569
|
+
end
|
570
|
+
END
|
571
|
+
end
|
572
|
+
|
573
|
+
it 'returns BRANCHNODEID_case_whenINDEX' do
|
574
|
+
expect(assignment.branch_id).to match(/^\d+_case_when1/)
|
575
|
+
end
|
576
|
+
end
|
577
|
+
|
578
|
+
context 'when it is inside body of when of case' do
|
579
|
+
let(:source) do
|
580
|
+
<<-END
|
581
|
+
def some_method(flag)
|
582
|
+
case flag
|
583
|
+
when first
|
584
|
+
do_something
|
585
|
+
when second
|
586
|
+
do_something
|
587
|
+
else
|
588
|
+
foo = 1
|
589
|
+
puts foo
|
590
|
+
end
|
591
|
+
end
|
592
|
+
END
|
593
|
+
end
|
594
|
+
|
595
|
+
it 'returns BRANCHNODEID_case_else' do
|
596
|
+
expect(assignment.branch_id).to match(/^\d+_case_else/)
|
597
|
+
end
|
598
|
+
end
|
599
|
+
|
600
|
+
context 'when it is on the left side of &&' do
|
601
|
+
let(:source) do
|
602
|
+
<<-END
|
603
|
+
def some_method
|
604
|
+
(foo = 1) && do_something
|
605
|
+
end
|
606
|
+
END
|
607
|
+
end
|
608
|
+
|
609
|
+
it 'returns nil' do
|
610
|
+
expect(assignment.branch_id).to be_nil
|
611
|
+
end
|
612
|
+
end
|
613
|
+
|
614
|
+
context 'when it is on the right side of &&' do
|
615
|
+
let(:source) do
|
616
|
+
<<-END
|
617
|
+
def some_method
|
618
|
+
do_something && (foo = 1)
|
619
|
+
end
|
620
|
+
END
|
621
|
+
end
|
622
|
+
|
623
|
+
it 'returns BRANCHNODEID_and_right' do
|
624
|
+
expect(assignment.branch_id).to match(/^\d+_and_right/)
|
625
|
+
end
|
626
|
+
end
|
627
|
+
|
628
|
+
context 'when it is on the left side of ||' do
|
629
|
+
let(:source) do
|
630
|
+
<<-END
|
631
|
+
def some_method
|
632
|
+
(foo = 1) || do_something
|
633
|
+
end
|
634
|
+
END
|
635
|
+
end
|
636
|
+
|
637
|
+
it 'returns nil' do
|
638
|
+
expect(assignment.branch_id).to be_nil
|
639
|
+
end
|
640
|
+
end
|
641
|
+
|
642
|
+
context 'when it is on the right side of ||' do
|
643
|
+
let(:source) do
|
644
|
+
<<-END
|
645
|
+
def some_method
|
646
|
+
do_something || (foo = 1)
|
647
|
+
end
|
648
|
+
END
|
649
|
+
end
|
650
|
+
|
651
|
+
it 'returns BRANCHNODEID_or_right' do
|
652
|
+
expect(assignment.branch_id).to match(/^\d+_or_right/)
|
653
|
+
end
|
654
|
+
end
|
655
|
+
|
656
|
+
context 'when it is inside of begin with rescue' do
|
657
|
+
let(:source) do
|
658
|
+
<<-END
|
659
|
+
def some_method(flag)
|
660
|
+
begin
|
661
|
+
foo = 1
|
662
|
+
rescue
|
663
|
+
do_something
|
664
|
+
end
|
665
|
+
end
|
666
|
+
END
|
667
|
+
end
|
668
|
+
|
669
|
+
it 'returns BRANCHNODEID_rescue_main' do
|
670
|
+
expect(assignment.branch_id).to match(/^\d+_rescue_main/)
|
671
|
+
end
|
672
|
+
end
|
673
|
+
|
674
|
+
context 'when it is inside of rescue' do
|
675
|
+
let(:source) do
|
676
|
+
<<-END
|
677
|
+
def some_method(flag)
|
678
|
+
begin
|
679
|
+
do_something
|
680
|
+
rescue FirstError
|
681
|
+
do_something
|
682
|
+
rescue SecondError
|
683
|
+
foo = 1
|
684
|
+
end
|
685
|
+
end
|
686
|
+
END
|
687
|
+
end
|
688
|
+
|
689
|
+
it 'returns BRANCHNODEID_rescue_rescueINDEX' do
|
690
|
+
expect(assignment.branch_id).to match(/^\d+_rescue_rescue1/)
|
691
|
+
end
|
692
|
+
end
|
693
|
+
|
694
|
+
context 'when it is inside of else of rescue' do
|
695
|
+
let(:source) do
|
696
|
+
<<-END
|
697
|
+
def some_method(flag)
|
698
|
+
begin
|
699
|
+
do_something
|
700
|
+
rescue FirstError
|
701
|
+
do_something
|
702
|
+
rescue SecondError
|
703
|
+
do_something
|
704
|
+
else
|
705
|
+
foo = 1
|
706
|
+
end
|
707
|
+
end
|
708
|
+
END
|
709
|
+
end
|
710
|
+
|
711
|
+
it 'returns BRANCHNODEID_rescue_else' do
|
712
|
+
expect(assignment.branch_id).to match(/^\d+_rescue_else/)
|
713
|
+
end
|
714
|
+
end
|
715
|
+
|
716
|
+
context 'when it is inside of begin with ensure' do
|
717
|
+
let(:source) do
|
718
|
+
<<-END
|
719
|
+
def some_method(flag)
|
720
|
+
begin
|
721
|
+
foo = 1
|
722
|
+
ensure
|
723
|
+
do_something
|
724
|
+
end
|
725
|
+
end
|
726
|
+
END
|
727
|
+
end
|
728
|
+
|
729
|
+
it 'returns BRANCHNODEID_ensure_main' do
|
730
|
+
expect(assignment.branch_id).to match(/^\d+_ensure_main/)
|
731
|
+
end
|
732
|
+
end
|
733
|
+
end
|
734
|
+
end
|