repo
stringlengths
5
92
file_url
stringlengths
80
287
file_path
stringlengths
5
197
content
stringlengths
0
32.8k
language
stringclasses
1 value
license
stringclasses
7 values
commit_sha
stringlengths
40
40
retrieved_at
stringdate
2026-01-04 15:37:27
2026-01-04 17:58:21
truncated
bool
2 classes
rubocop/rubocop
https://github.com/rubocop/rubocop/blob/99fa0fdd0481910d7d052f4c2ed01ad36178f404/spec/rubocop/cop/internal_affairs/method_name_equal_spec.rb
spec/rubocop/cop/internal_affairs/method_name_equal_spec.rb
# frozen_string_literal: true RSpec.describe RuboCop::Cop::InternalAffairs::MethodNameEqual, :config do it 'registers an offense when using `#method == :do_something`' do expect_offense(<<~RUBY) node.method_name == :do_something ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Use `node.method?(:do_something)` instead. RUBY expect_correction(<<~RUBY) node.method?(:do_something) RUBY end it 'registers an offense when using `#method != :do_something`' do expect_offense(<<~RUBY) node.method_name != :do_something ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Use `!node.method?(:do_something)` instead. RUBY expect_correction(<<~RUBY) !node.method?(:do_something) RUBY end it 'registers an offense when using `#method == other_node.do_something`' do expect_offense(<<~RUBY) node.method_name == other_node.do_something ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Use `node.method?(other_node.do_something)` instead. RUBY expect_correction(<<~RUBY) node.method?(other_node.do_something) RUBY end it 'does not register an offense when using `#method?`' do expect_no_offenses(<<~RUBY) node.method?(:do_something) RUBY end end
ruby
MIT
99fa0fdd0481910d7d052f4c2ed01ad36178f404
2026-01-04T15:37:41.211519Z
false
rubocop/rubocop
https://github.com/rubocop/rubocop/blob/99fa0fdd0481910d7d052f4c2ed01ad36178f404/spec/rubocop/cop/internal_affairs/example_heredoc_delimiter_spec.rb
spec/rubocop/cop/internal_affairs/example_heredoc_delimiter_spec.rb
# frozen_string_literal: true RSpec.describe RuboCop::Cop::InternalAffairs::ExampleHeredocDelimiter, :config do context 'when expected heredoc delimiter is used at RuboCop specific expectation' do it 'does not register an offense' do expect_no_offenses(<<~RUBY_) it 'does not register an offense' do expect_no_offenses(<<~RUBY) example_ruby_code RUBY end RUBY_ end end context 'when unexpected heredoc delimiter is used at non RuboCop specific expectation' do it 'does not register an offense' do expect_no_offenses(<<~RUBY) expect_foo(<<~CODE) example_text CODE RUBY end end context 'when unexpected heredoc delimiter is used but heredoc body contains an expected delimiter line' do it 'does not register an offense' do expect_no_offenses(<<~RUBY_) it 'does not register an offense' do expect_no_offenses(<<~CODE) RUBY CODE end RUBY_ end end context 'when unexpected heredoc delimiter is used in single-line heredoc' do it 'registers an offense' do expect_offense(<<~RUBY) it 'does not register an offense' do expect_no_offenses(<<~CODE) ^^^^^^^ Use `RUBY` for heredoc delimiter of example Ruby code. example_ruby_code CODE end RUBY expect_correction(<<~RUBY_) it 'does not register an offense' do expect_no_offenses(<<~RUBY) example_ruby_code RUBY end RUBY_ end end context 'when unexpected heredoc delimiter is used in multi-line heredoc' do it 'registers an offense' do expect_offense(<<~RUBY) it 'does not register an offense' do expect_no_offenses(<<~CODE) ^^^^^^^ Use `RUBY` for heredoc delimiter of example Ruby code. example_ruby_code1 example_ruby_code2 CODE end RUBY expect_correction(<<~RUBY_) it 'does not register an offense' do expect_no_offenses(<<~RUBY) example_ruby_code1 example_ruby_code2 RUBY end RUBY_ end end end
ruby
MIT
99fa0fdd0481910d7d052f4c2ed01ad36178f404
2026-01-04T15:37:41.211519Z
false
rubocop/rubocop
https://github.com/rubocop/rubocop/blob/99fa0fdd0481910d7d052f4c2ed01ad36178f404/spec/rubocop/cop/internal_affairs/redundant_context_config_parameter_spec.rb
spec/rubocop/cop/internal_affairs/redundant_context_config_parameter_spec.rb
# frozen_string_literal: true RSpec.describe RuboCop::Cop::InternalAffairs::RedundantContextConfigParameter, :config do it 'registers an offense when using `:config` parameter' do expect_offense(<<~RUBY) context 'foo', :config do ^^^^^^^ Remove the redundant `:config` parameter. end RUBY expect_correction(<<~RUBY) context 'foo' do end RUBY end it 'registers an offense when using `:config` parameter with other parameters' do expect_offense(<<~RUBY) context 'foo', :ruby30, :rails70, :config do ^^^^^^^ Remove the redundant `:config` parameter. end RUBY expect_correction(<<~RUBY) context 'foo', :ruby30, :rails70 do end RUBY end it 'does not register an offense when not using `:config`' do expect_no_offenses(<<~RUBY) context 'foo' do end RUBY end it 'does not register an offense when using `:ruby30` only' do expect_no_offenses(<<~RUBY) context 'foo', :ruby30 do end RUBY end it 'does not register an offense when using `:config` in other than `context`' do expect_no_offenses(<<~RUBY) shared_context 'foo', :config do end RUBY end end
ruby
MIT
99fa0fdd0481910d7d052f4c2ed01ad36178f404
2026-01-04T15:37:41.211519Z
false
rubocop/rubocop
https://github.com/rubocop/rubocop/blob/99fa0fdd0481910d7d052f4c2ed01ad36178f404/spec/rubocop/cop/internal_affairs/single_line_comparison_spec.rb
spec/rubocop/cop/internal_affairs/single_line_comparison_spec.rb
# frozen_string_literal: true RSpec.describe RuboCop::Cop::InternalAffairs::SingleLineComparison, :config do it 'registers and corrects an offense when comparing `loc.first_line` with `loc.last_line`' do expect_offense(<<~RUBY) node.loc.first_line == node.loc.last_line ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Use `node.single_line?`. RUBY expect_correction(<<~RUBY) node.single_line? RUBY end it 'registers and corrects an offense when comparing `loc.last_line` with `loc.first_line`' do expect_offense(<<~RUBY) node.loc.last_line == node.loc.first_line ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Use `node.single_line?`. RUBY expect_correction(<<~RUBY) node.single_line? RUBY end it 'registers and corrects an offense when comparing `loc.line` with `loc.last_line`' do expect_offense(<<~RUBY) node.loc.line == node.loc.last_line ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Use `node.single_line?`. RUBY expect_correction(<<~RUBY) node.single_line? RUBY end it 'registers and corrects an offense when comparing `loc.last_line` with `loc.line`' do expect_offense(<<~RUBY) node.loc.last_line == node.loc.line ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Use `node.single_line?`. RUBY expect_correction(<<~RUBY) node.single_line? RUBY end it 'registers and corrects an offense when comparing `source_range.first_line` with `source_range.last_line`' do expect_offense(<<~RUBY) node.source_range.first_line == node.source_range.last_line ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Use `node.single_line?`. RUBY expect_correction(<<~RUBY) node.single_line? RUBY end it 'registers and corrects an offense when comparing `source_range.last_line` with `source_range.first_line`' do expect_offense(<<~RUBY) node.source_range.last_line == node.source_range.first_line ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Use `node.single_line?`. RUBY expect_correction(<<~RUBY) node.single_line? RUBY end it 'registers and corrects an offense when comparing `first_line` with `last_line`' do expect_offense(<<~RUBY) node.first_line == node.last_line ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Use `node.single_line?`. RUBY expect_correction(<<~RUBY) node.single_line? RUBY end it 'registers and corrects an offense when comparing `last_line` with `first_line`' do expect_offense(<<~RUBY) node.last_line == node.first_line ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Use `node.single_line?`. RUBY expect_correction(<<~RUBY) node.single_line? RUBY end it 'registers and corrects an offense when negative comparing `first_line` with `last_line`' do expect_offense(<<~RUBY) node.first_line != node.last_line ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Use `!node.single_line?`. RUBY expect_correction(<<~RUBY) !node.single_line? RUBY end it 'registers and corrects an offense when negative comparing `last_line` with `first_line`' do expect_offense(<<~RUBY) node.last_line != node.first_line ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Use `!node.single_line?`. RUBY expect_correction(<<~RUBY) !node.single_line? RUBY end it 'does not register an offense when comparing the same line' do expect_no_offenses(<<~RUBY) node.loc.first_line == node.loc.line RUBY end it 'does not register an offense when the receivers are not a match' do expect_no_offenses(<<~RUBY) nodes.first.first_line == nodes.last.last_line RUBY end context 'with safe navigation' do it 'registers an offense and corrects' do expect_offense(<<~RUBY) node&.first_line == node&.last_line ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Use `node&.single_line?`. node&.first_line != node&.last_line ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Use `!node&.single_line?`. RUBY expect_correction(<<~RUBY) node&.single_line? !node&.single_line? RUBY end end end
ruby
MIT
99fa0fdd0481910d7d052f4c2ed01ad36178f404
2026-01-04T15:37:41.211519Z
false
rubocop/rubocop
https://github.com/rubocop/rubocop/blob/99fa0fdd0481910d7d052f4c2ed01ad36178f404/spec/rubocop/cop/internal_affairs/node_pattern_groups_spec.rb
spec/rubocop/cop/internal_affairs/node_pattern_groups_spec.rb
# frozen_string_literal: true RSpec.describe RuboCop::Cop::InternalAffairs::NodePatternGroups, :config do shared_examples 'node group' do |node_group, members| describe "`#{node_group}` node group" do let(:source) { members.join(' ') } let(:names) { members.join('`, `') } it 'registers an offense and corrects' do expect_offense(<<~RUBY, source: source) def_node_matcher :my_matcher, '{%{source}}' ^^{source}^ Replace `#{names}` in node pattern union with `#{node_group}`. RUBY expect_correction(<<~RUBY) def_node_matcher :my_matcher, '#{node_group}' RUBY end it 'registers an offense and corrects with a heredoc' do expect_offense(<<~RUBY, source: source) def_node_matcher :my_matcher, <<~PATTERN {%{source}} ^^{source}^ Replace `#{names}` in node pattern union with `#{node_group}`. PATTERN RUBY expect_correction(<<~RUBY) def_node_matcher :my_matcher, <<~PATTERN #{node_group} PATTERN RUBY end end end it_behaves_like 'node group', 'any_block', %i[itblock numblock block] it_behaves_like 'node group', 'any_def', %i[def defs] it_behaves_like 'node group', 'any_match_pattern', %i[match_pattern match_pattern_p] it_behaves_like 'node group', 'argument', %i[arg blockarg forward_arg kwarg kwoptarg kwrestarg optarg restarg shadowarg] it_behaves_like 'node group', 'boolean', %i[false true] it_behaves_like 'node group', 'call', %i[csend send] it_behaves_like 'node group', 'numeric', %i[complex float int rational] it_behaves_like 'node group', 'range', %i[erange irange] it 'can handle an invalid pattern' do expect_no_offenses(<<~RUBY) def_node_matcher :my_matcher, <<~PATTERN ({send csend PATTERN RUBY end # The following tests mostly use the `call` node group to avoid duplication, # but would apply to the others as well. it 'does not register an offense for `call`' do expect_no_offenses(<<~RUBY) def_node_matcher :my_matcher, 'call' RUBY end it 'does not register an offense for `(call)`' do expect_no_offenses(<<~RUBY) def_node_matcher :my_matcher, '(call)' RUBY end it 'does not register an offense when not called in `def_node_matcher` or `def_node_search`' do expect_no_offenses(<<~RUBY) '{send csend}' RUBY end it 'does not register an offense for `{send def}`' do expect_no_offenses(<<~RUBY) def_node_matcher :my_matcher, '{send def}' RUBY end it 'does not register an offense for `{csend def}`' do expect_no_offenses(<<~RUBY) def_node_matcher :my_matcher, '{csend def}' RUBY end it 'does not register an offense for `{call def}`' do expect_no_offenses(<<~RUBY) def_node_matcher :my_matcher, '{call def}' RUBY end it 'does not register an offense for `{send (csend)}' do expect_no_offenses(<<~RUBY) def_node_matcher :my_matcher, '{send (csend)}' RUBY end it 'does not register an offense for a dynamic pattern' do expect_no_offenses(<<~'RUBY') def_node_matcher :my_matcher, '{#{TYPES.join(' ')}}' RUBY end it 'does not register an offense for node types within an any-order node' do expect_no_offenses(<<~RUBY) def_node_matcher :my_matcher, '<true false>' RUBY end it 'does not register an offense for node types within an any-order node within a union' do expect_no_offenses(<<~RUBY) def_node_matcher :my_matcher, <<~PATTERN { <true ...> <false ...> } PATTERN RUBY end it 'registers an offense and corrects `{send csend}`' do expect_offense(<<~RUBY) def_node_matcher :my_matcher, '{send csend}' ^^^^^^^^^^^^ Replace `send`, `csend` in node pattern union with `call`. RUBY expect_correction(<<~RUBY) def_node_matcher :my_matcher, 'call' RUBY end it 'registers an offense and corrects `{ send csend }`' do expect_offense(<<~RUBY) def_node_matcher :my_matcher, '{ send csend }' ^^^^^^^^^^^^^^ Replace `send`, `csend` in node pattern union with `call`. RUBY expect_correction(<<~RUBY) def_node_matcher :my_matcher, 'call' RUBY end it 'registers an offense and corrects `{csend send}`' do expect_offense(<<~RUBY) def_node_matcher :my_matcher, '{csend send}' ^^^^^^^^^^^^ Replace `csend`, `send` in node pattern union with `call`. RUBY expect_correction(<<~RUBY) def_node_matcher :my_matcher, 'call' RUBY end it 'registers an offense and corrects `({send csend})`' do expect_offense(<<~RUBY) def_node_matcher :my_matcher, '({send csend})' ^^^^^^^^^^^^ Replace `send`, `csend` in node pattern union with `call`. RUBY expect_correction(<<~RUBY) def_node_matcher :my_matcher, '(call)' RUBY end it 'registers an offense and corrects `{send csend def}`' do expect_offense(<<~RUBY) def_node_matcher :my_matcher, '{send csend def}' ^^^^^^^^^^^^^^^^ Replace `send`, `csend` in node pattern union with `call`. RUBY expect_correction(<<~RUBY) def_node_matcher :my_matcher, '{call def}' RUBY end it 'registers an offense and corrects `{ send csend def }`' do expect_offense(<<~RUBY) def_node_matcher :my_matcher, '{ send csend def }' ^^^^^^^^^^^^^^^^^^ Replace `send`, `csend` in node pattern union with `call`. RUBY expect_correction(<<~RUBY) def_node_matcher :my_matcher, '{ call def }' RUBY end it 'registers an offense and corrects `{send def csend}`' do expect_offense(<<~RUBY) def_node_matcher :my_matcher, '{send def csend}' ^^^^^^^^^^^^^^^^ Replace `send`, `csend` in node pattern union with `call`. RUBY expect_correction(<<~RUBY) def_node_matcher :my_matcher, '{call def}' RUBY end it 'registers an offense and corrects `{def send csend}`' do expect_offense(<<~RUBY) def_node_matcher :my_matcher, '{def send csend}' ^^^^^^^^^^^^^^^^ Replace `send`, `csend` in node pattern union with `call`. RUBY expect_correction(<<~RUBY) def_node_matcher :my_matcher, '{def call}' RUBY end it 'registers an offense and corrects multiple groups in a single union' do # Two offenses will actually be registered but in separate correction iterations because # RuboCop does not allow for multiple offenses of the same type on the same range. expect_offense(<<~RUBY) def_node_matcher :my_matcher, '{send csend true false}' ^^^^^^^^^^^^^^^^^^^^^^^ Replace `true`, `false` in node pattern union with `boolean`. RUBY expect_correction(<<~RUBY) def_node_matcher :my_matcher, '{call boolean}' RUBY end it 'registers an offense and corrects `{send csend (def _ :foo)}`' do expect_offense(<<~RUBY) def_node_matcher :my_matcher, '{send csend (def _ :foo)}' ^^^^^^^^^^^^^^^^^^^^^^^^^ Replace `send`, `csend` in node pattern union with `call`. RUBY expect_correction(<<~RUBY) def_node_matcher :my_matcher, '{call (def _ :foo)}' RUBY end it 'registers an offense and corrects multiple unions inside a node' do expect_offense(<<~RUBY) def_node_matcher :my_matcher, <<~PATTERN ({send csend} {send csend} ...) ^^^^^^^^^^^^ Replace `send`, `csend` in node pattern union with `call`. ^^^^^^^^^^^^ Replace `send`, `csend` in node pattern union with `call`. PATTERN RUBY expect_correction(<<~RUBY) def_node_matcher :my_matcher, <<~PATTERN (call call ...) PATTERN RUBY end it 'registers an offense and corrects a complex pattern' do expect_offense(<<~RUBY) def_node_matcher :my_matcher, '({send csend} (const {nil? cbase} :FileUtils) :cd ...)' ^^^^^^^^^^^^ Replace `send`, `csend` in node pattern union with `call`. RUBY expect_correction(<<~RUBY) def_node_matcher :my_matcher, '(call (const {nil? cbase} :FileUtils) :cd ...)' RUBY end it 'registers offenses when there are multiple matchers' do expect_offense(<<~RUBY) def_node_matcher :matcher1, <<~PATTERN {send csend} ^^^^^^^^^^^^ Replace `send`, `csend` in node pattern union with `call`. PATTERN def_node_matcher :matcher2, <<~PATTERN (send nil !nil?) PATTERN def_node_matcher :matcher3, <<~PATTERN (send {send csend} _ :foo) ^^^^^^^^^^^^ Replace `send`, `csend` in node pattern union with `call`. PATTERN RUBY expect_correction(<<~RUBY) def_node_matcher :matcher1, <<~PATTERN call PATTERN def_node_matcher :matcher2, <<~PATTERN (send nil !nil?) PATTERN def_node_matcher :matcher3, <<~PATTERN (send call _ :foo) PATTERN RUBY end context 'in heredoc' do it 'does not register an offense for `call`' do expect_no_offenses(<<~RUBY) def_node_matcher :my_matcher, <<~PATTERN call PATTERN RUBY end it 'does not register an offense for a dynamic pattern' do expect_no_offenses(<<~'RUBY') def_node_matcher :my_matcher, <<~PATTERN { #{TYPES.join(' ')} } PATTERN RUBY end it 'registers an offense and corrects `{send csend}`' do expect_offense(<<~RUBY) def_node_matcher :my_matcher, <<~PATTERN {csend send} ^^^^^^^^^^^^ Replace `csend`, `send` in node pattern union with `call`. PATTERN RUBY expect_correction(<<~RUBY) def_node_matcher :my_matcher, <<~PATTERN call PATTERN RUBY end it 'registers an offense and corrects `{send csend}` on multiple lines' do expect_offense(<<~RUBY) def_node_matcher :my_matcher, <<~PATTERN {csend ^^^^^^ Replace `csend`, `send` in node pattern union with `call`. send} PATTERN RUBY expect_correction(<<~RUBY) def_node_matcher :my_matcher, <<~PATTERN call PATTERN RUBY end it 'registers an offense and corrects `{send csend (def _ :foo)}` in a multiline heredoc' do expect_offense(<<~RUBY) def_node_matcher :my_matcher, <<~PATTERN { ^ Replace `send`, `csend` in node pattern union with `call`. send csend (def _ :foo) } PATTERN RUBY expect_correction(<<~RUBY) def_node_matcher :my_matcher, <<~PATTERN { call (def _ :foo) } PATTERN RUBY end it 'registers an offense and corrects `{(def _ :foo) send csend}` in a multiline heredoc' do expect_offense(<<~RUBY) def_node_matcher :my_matcher, <<~PATTERN { ^ Replace `send`, `csend` in node pattern union with `call`. (def _ :foo) send csend } PATTERN RUBY expect_correction(<<~RUBY) def_node_matcher :my_matcher, <<~PATTERN { (def _ :foo) call } PATTERN RUBY end end context 'in a % string' do it 'registers an offense and corrects with `%`' do expect_offense(<<~RUBY) def_node_matcher :my_matcher, %[{send csend}] ^^^^^^^^^^^^ Replace `send`, `csend` in node pattern union with `call`. RUBY expect_correction(<<~RUBY) def_node_matcher :my_matcher, %[call] RUBY end it 'registers an offense and corrects with `%q`' do expect_offense(<<~RUBY) def_node_matcher :my_matcher, %q[{send csend}] ^^^^^^^^^^^^ Replace `send`, `csend` in node pattern union with `call`. RUBY expect_correction(<<~RUBY) def_node_matcher :my_matcher, %q[call] RUBY end it 'registers an offense and corrects with `%Q`' do expect_offense(<<~RUBY) def_node_matcher :my_matcher, %Q[{send csend}] ^^^^^^^^^^^^ Replace `send`, `csend` in node pattern union with `call`. RUBY expect_correction(<<~RUBY) def_node_matcher :my_matcher, %Q[call] RUBY end end context 'with arguments' do it 'registers an offense and corrects when the arguments match' do expect_offense(<<~RUBY) def_node_matcher :my_matcher, <<~PATTERN {(send _ :foo) (csend _ :foo)} ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Replace `(send _ :foo)`, `(csend _ :foo)` in node pattern union with `(call _ :foo)`. PATTERN RUBY expect_correction(<<~RUBY) def_node_matcher :my_matcher, <<~PATTERN (call _ :foo) PATTERN RUBY end it 'does not register an offense if one node has arguments and the other does not' do expect_no_offenses(<<~RUBY) def_node_matcher :my_matcher, <<~PATTERN {send (csend _ :foo)} PATTERN RUBY end it 'does not register an offense if when the nodes have different arguments' do expect_no_offenses(<<~RUBY) def_node_matcher :my_matcher, <<~PATTERN {(send _ :foo) (csend _ :bar)} PATTERN RUBY end end context 'with nested arguments' do it 'registers an offense and corrects when the arguments match' do expect_offense(<<~RUBY) def_node_matcher :my_matcher, <<~PATTERN {(send (send ...) :foo) (csend (send ...) :foo)} ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Replace `(send (send ...) :foo)`, `(csend (send ...) :foo)` in node pattern union with `(call (send ...) :foo)`. PATTERN RUBY expect_correction(<<~RUBY) def_node_matcher :my_matcher, <<~PATTERN (call (send ...) :foo) PATTERN RUBY end end context 'union with pipes' do it 'registers an offense and corrects `{send | csend}`' do expect_offense(<<~RUBY) def_node_matcher :my_matcher, '{send | csend}' ^^^^^^^^^^^^^^ Replace `send`, `csend` in node pattern union with `call`. RUBY expect_correction(<<~RUBY) def_node_matcher :my_matcher, 'call' RUBY end it 'registers an offense and corrects `{ send | csend }`' do expect_offense(<<~RUBY) def_node_matcher :my_matcher, '{ send | csend }' ^^^^^^^^^^^^^^^^ Replace `send`, `csend` in node pattern union with `call`. RUBY expect_correction(<<~RUBY) def_node_matcher :my_matcher, 'call' RUBY end it 'registers an offense and corrects `{send | csend | def}`' do expect_offense(<<~RUBY) def_node_matcher :my_matcher, '{send | csend | def}' ^^^^^^^^^^^^^^^^^^^^ Replace `send`, `csend` in node pattern union with `call`. RUBY expect_correction(<<~RUBY) def_node_matcher :my_matcher, '{call | def}' RUBY end it 'registers an offense and corrects `{ send | csend | def }`' do expect_offense(<<~RUBY) def_node_matcher :my_matcher, '{ send | csend | def }' ^^^^^^^^^^^^^^^^^^^^^^ Replace `send`, `csend` in node pattern union with `call`. RUBY expect_correction(<<~RUBY) def_node_matcher :my_matcher, '{ call | def }' RUBY end it 'registers an offense and corrects `{send | csend | def} in a multiline heredoc`' do expect_offense(<<~RUBY) def_node_matcher :my_matcher, <<~PATTERN { ^ Replace `send`, `csend` in node pattern union with `call`. send | csend | def } PATTERN RUBY expect_correction(<<~RUBY) def_node_matcher :my_matcher, <<~PATTERN { call | def } PATTERN RUBY end it 'registers an offense and corrects `{send | def | csend}`' do expect_offense(<<~RUBY) def_node_matcher :my_matcher, '{send | def | csend}' ^^^^^^^^^^^^^^^^^^^^ Replace `send`, `csend` in node pattern union with `call`. RUBY expect_correction(<<~RUBY) def_node_matcher :my_matcher, '{call | def}' RUBY end it 'registers an offense and corrects `{def | send | csend}`' do expect_offense(<<~RUBY) def_node_matcher :my_matcher, '{def | send | csend}' ^^^^^^^^^^^^^^^^^^^^ Replace `send`, `csend` in node pattern union with `call`. RUBY expect_correction(<<~RUBY) def_node_matcher :my_matcher, '{def | call}' RUBY end it 'does not register an offense for `{(send ... csend) | def}`' do expect_no_offenses(<<~RUBY) def_node_matcher :my_matcher, '{(send ... csend) | def}' RUBY end it 'does not register an offense for `{(send ... lvar) | csend}`' do expect_no_offenses(<<~RUBY) def_node_matcher :my_matcher, '{(send ... lvar) | csend}' RUBY end it 'registers an offense for pipes with arguments and other elements' do expect_offense(<<~RUBY) def_node_matcher :my_matcher, '{(send ...) | (csend ...) | def}' ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Replace `(send ...)`, `(csend ...)` in node pattern union with `(call ...)`. RUBY expect_correction(<<~RUBY) def_node_matcher :my_matcher, '{(call ...) | def}' RUBY end it 'registers an offense for pipes with arguments and no other elements' do expect_offense(<<~RUBY) def_node_matcher :my_matcher, '{(send ...) | (csend ...)}' ^^^^^^^^^^^^^^^^^^^^^^^^^^ Replace `(send ...)`, `(csend ...)` in node pattern union with `(call ...)`. RUBY expect_correction(<<~RUBY) def_node_matcher :my_matcher, '(call ...)' RUBY end end context 'with nested unions' do it 'registers an offense and corrects for a union inside a union' do expect_offense(<<~RUBY) def_node_matcher :my_matcher, <<~PATTERN {lvar {send csend} def} ^^^^^^^^^^^^ Replace `send`, `csend` in node pattern union with `call`. PATTERN RUBY expect_correction(<<~RUBY) def_node_matcher :my_matcher, <<~PATTERN {lvar call def} PATTERN RUBY end it 'registers an offense and corrects for a union inside a node type' do expect_offense(<<~RUBY) def_node_matcher :my_matcher, <<~PATTERN (send {send csend} ...) ^^^^^^^^^^^^ Replace `send`, `csend` in node pattern union with `call`. PATTERN RUBY expect_correction(<<~RUBY) def_node_matcher :my_matcher, <<~PATTERN (send call ...) PATTERN RUBY end it 'registers an offense and corrects for a union inside a node type inside a union' do expect_offense(<<~RUBY) def_node_matcher :my_matcher, <<~PATTERN { (send {send csend} ...) ^^^^^^^^^^^^ Replace `send`, `csend` in node pattern union with `call`. def } PATTERN RUBY expect_correction(<<~RUBY) def_node_matcher :my_matcher, <<~PATTERN { (send call ...) def } PATTERN RUBY end end context 'with sequences' do it 'registers an offense and corrects for a union inside a sequence' do expect_offense(<<~RUBY) def_node_matcher :my_matcher, <<~PATTERN (send ({send csend} ...) ...) ^^^^^^^^^^^^ Replace `send`, `csend` in node pattern union with `call`. PATTERN RUBY expect_correction(<<~RUBY) def_node_matcher :my_matcher, <<~PATTERN (send (call ...) ...) PATTERN RUBY end it 'registers an offense and corrects for a union inside a sequence inside a union' do expect_offense(<<~RUBY) def_node_matcher :my_matcher, <<~PATTERN { (send ({send csend} ...) ...) ^^^^^^^^^^^^ Replace `send`, `csend` in node pattern union with `call`. def } PATTERN RUBY expect_correction(<<~RUBY) def_node_matcher :my_matcher, <<~PATTERN { (send (call ...) ...) def } PATTERN RUBY end it 'registers an offense and corrects for a union within a nested sequence' do expect_offense(<<~RUBY) def_node_matcher :my_matcher, '(if _ {(send {true false} ...) | (csend {true false} ...)})' ^^^^^^^^^^^^ Replace `true`, `false` in node pattern union with `boolean`. ^^^^^^^^^^^^ Replace `true`, `false` in node pattern union with `boolean`. ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Replace `(send {true false} ...)`, `(csend {true false} ...)` in node pattern union with `(call {true false} ...)`. RUBY expect_correction(<<~RUBY) def_node_matcher :my_matcher, '(if _ (call boolean ...))' RUBY end end context 'with subsequences' do it 'does not register an offense for node types in separate sequences' do expect_no_offenses(<<~RUBY) def_node_matcher :my_matcher, '(if _ {(true) (false) | (false) (true)})' RUBY end it 'registers an offense and corrects for a union within a subsequence' do expect_offense(<<~RUBY) def_node_matcher :my_matcher, '(if _ ({send {true false} ... | csend {true false} ...}) ...)' ^^^^^^^^^^^^ Replace `true`, `false` in node pattern union with `boolean`. ^^^^^^^^^^^^ Replace `true`, `false` in node pattern union with `boolean`. ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Replace `send {true false} ...`, `csend {true false} ...` in node pattern union with `call {true false} ...`. RUBY expect_correction(<<~RUBY) def_node_matcher :my_matcher, '(if _ (call boolean ...) ...)' RUBY end end it 'registers an offense and corrects `{(true) (false)}`' do expect_offense(<<~RUBY) def_node_matcher :my_matcher, '{(true) (false)}' ^^^^^^^^^^^^^^^^ Replace `(true)`, `(false)` in node pattern union with `(boolean)`. RUBY expect_correction(<<~RUBY) def_node_matcher :my_matcher, '(boolean)' RUBY end it 'does not register an offense for types that make up a group but in different sequences' do expect_no_offenses(<<~RUBY) def_node_matcher :optional_option?, <<~PATTERN { (hash (pair (sym :optional) true)) (hash (pair (sym :required) false)) } PATTERN RUBY end it 'registers an offense and correct when called with `def_node_search`' do expect_offense(<<~RUBY) def_node_search :my_matcher, '{send csend}' ^^^^^^^^^^^^ Replace `send`, `csend` in node pattern union with `call`. RUBY expect_correction(<<~RUBY) def_node_search :my_matcher, 'call' RUBY end end
ruby
MIT
99fa0fdd0481910d7d052f4c2ed01ad36178f404
2026-01-04T15:37:41.211519Z
false
rubocop/rubocop
https://github.com/rubocop/rubocop/blob/99fa0fdd0481910d7d052f4c2ed01ad36178f404/spec/rubocop/cop/internal_affairs/example_description_spec.rb
spec/rubocop/cop/internal_affairs/example_description_spec.rb
# frozen_string_literal: true RSpec.describe RuboCop::Cop::InternalAffairs::ExampleDescription, :config do context 'with `expect_offense`' do it 'registers an offense when given an improper description in `it`' do expect_offense(<<~RUBY) it 'does not register an offense' do ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Description does not match use of `expect_offense`. expect_offense('code') end RUBY expect_correction(<<~RUBY) it 'registers an offense' do expect_offense('code') end RUBY end it 'registers an offense when given an improper description in `xit`' do expect_offense(<<~RUBY) xit 'does not register an offense' do ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Description does not match use of `expect_offense`. expect_offense('code') end RUBY expect_correction(<<~RUBY) xit 'registers an offense' do expect_offense('code') end RUBY end it 'registers an offense when given an improper description in `specify`' do expect_offense(<<~RUBY) specify 'does not register an offense' do ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Description does not match use of `expect_offense`. expect_offense('code') end RUBY expect_correction(<<~RUBY) specify 'registers an offense' do expect_offense('code') end RUBY end it 'registers an offense when given an improper description in `fit`' do expect_offense(<<~RUBY) fit 'does not register an offense' do ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Description does not match use of `expect_offense`. expect_offense('code') end RUBY expect_correction(<<~RUBY) fit 'registers an offense' do expect_offense('code') end RUBY end it 'registers an offense when given an improper description for `registers no offense`' do expect_offense(<<~RUBY) it 'registers no offense' do ^^^^^^^^^^^^^^^^^^^^^^ Description does not match use of `expect_offense`. expect_offense('code') end RUBY expect_correction(<<~RUBY) it 'registers an offense' do expect_offense('code') end RUBY end it 'registers an offense when given an improper description contains string interpolation' do expect_offense(<<~'RUBY') it "does not register an offense #{string_interpolation}" do ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Description does not match use of `expect_offense`. expect_offense('code') end RUBY expect_correction(<<~'RUBY') it "registers an offense #{string_interpolation}" do expect_offense('code') end RUBY end it 'registers an offense when given an improper description with single option' do expect_offense(<<~RUBY) it 'does not register an offense', :ruby30 do ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Description does not match use of `expect_offense`. expect_offense('code') end RUBY expect_correction(<<~RUBY) it 'registers an offense', :ruby30 do expect_offense('code') end RUBY end it 'registers an offense when given an improper description with multiple options' do expect_offense(<<~RUBY) it 'does not register an offense', :ruby30, :rails70 do ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Description does not match use of `expect_offense`. expect_offense('code') end RUBY expect_correction(<<~RUBY) it 'registers an offense', :ruby30, :rails70 do expect_offense('code') end RUBY end it 'registers an offense when given an improper description for `accepts`' do expect_offense(<<~RUBY) it 'accepts the case' do ^^^^^^^^^^^^^^^^^^ Description does not match use of `expect_offense`. expect_offense('code') end RUBY expect_correction(<<~RUBY) it 'registers the case' do expect_offense('code') end RUBY end it 'registers an offense when given an improper description for `allows`' do expect_offense(<<~RUBY) it 'allows the case' do ^^^^^^^^^^^^^^^^^ Description does not match use of `expect_offense`. expect_offense('code') end RUBY expect_correction(<<~RUBY) it 'registers the case' do expect_offense('code') end RUBY end it 'registers an offense when given an improper description for `register`' do expect_offense(<<~RUBY) it 'register the case' do ^^^^^^^^^^^^^^^^^^^ Description does not match use of `expect_offense`. expect_offense('code') end RUBY expect_correction(<<~RUBY) it 'registers the case' do expect_offense('code') end RUBY end it 'does not register an offense when given a proper description' do expect_no_offenses(<<~RUBY) it 'finds an offense' do expect_offense('code') end RUBY end it 'does not register an offense when given an unexpected description' do expect_no_offenses(<<~RUBY) it 'foo bar baz' do expect_offense('code') end RUBY end end context 'with `expect_no_offenses`' do it 'registers an offense when given an improper description for `registers`' do expect_offense(<<~RUBY) it 'registers an offense' do ^^^^^^^^^^^^^^^^^^^^^^ Description does not match use of `expect_no_offenses`. expect_no_offenses('code') end RUBY expect_correction(<<~RUBY) it 'does not register an offense' do expect_no_offenses('code') end RUBY end it 'registers an offense when given an improper description for `handles`' do expect_offense(<<~RUBY) it 'handles the case' do ^^^^^^^^^^^^^^^^^^ Description does not match use of `expect_no_offenses`. expect_no_offenses('code') end RUBY expect_correction(<<~RUBY) it 'does not register the case' do expect_no_offenses('code') end RUBY end it 'registers an offense when given an improper description contains string interpolation for `registers`' do expect_offense(<<~'RUBY') it "registers an offense #{string_interpolation}" do ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Description does not match use of `expect_no_offenses`. expect_no_offenses('code') end RUBY expect_correction(<<~'RUBY') it "does not register an offense #{string_interpolation}" do expect_no_offenses('code') end RUBY end it 'does not register an offense when given a proper description' do expect_no_offenses(<<~RUBY) it 'does not flag' do expect_no_offense('code') end RUBY end it 'does not crash when given a proper description that is split with +' do expect_no_offenses(<<~RUBY) it "does " + 'not register an offense' do expect_no_offense('code') end RUBY end it 'does not register an offense when given an unexpected description' do expect_no_offenses(<<~RUBY) it 'foo bar baz' do expect_offense('code') end RUBY end end context 'with `expect_correction`' do it 'registers an offense when given an improper description' do expect_offense(<<~RUBY) it 'does not autocorrect' do ^^^^^^^^^^^^^^^^^^^^^^ Description does not match use of `expect_correction`. expect_correction('code', source: 'new code') end RUBY expect_correction(<<~RUBY) it 'autocorrects' do expect_correction('code', source: 'new code') end RUBY end context 'in conjunction with expect_offense' do it 'registers an offense when given an improper description' do expect_offense(<<~RUBY) it 'registers an offense but does not autocorrect' do ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Description does not match use of `expect_correction`. expect_offense('code') expect_correction('code') end RUBY expect_correction(<<~RUBY) it 'registers an offense and autocorrects' do expect_offense('code') expect_correction('code') end RUBY end context 'when the description is invalid for both methods' do it 'registers an offense for the first method encountered' do expect_offense(<<~RUBY) it 'does not register an offense and does not autocorrect' do ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Description does not match use of `expect_offense`. expect_offense('code') expect_correction('code') end RUBY expect_correction(<<~RUBY) it 'registers an offense and autocorrects' do expect_offense('code') expect_correction('code') end RUBY end end end end context 'with `expect_no_corrections`' do it 'registers an offense when given an improper description' do expect_offense(<<~RUBY) it 'autocorrects' do ^^^^^^^^^^^^^^ Description does not match use of `expect_no_corrections`. expect_no_corrections end RUBY expect_correction(<<~RUBY) it 'does not correct' do expect_no_corrections end RUBY end context 'in conjunction with expect_offense' do it 'registers an offense when given an improper description' do expect_offense(<<~RUBY) it 'autocorrects' do ^^^^^^^^^^^^^^ Description does not match use of `expect_no_corrections`. expect_offense('code') expect_no_corrections end RUBY expect_correction(<<~RUBY) it 'does not correct' do expect_offense('code') expect_no_corrections end RUBY end end it 'registers an offense with a "registers an offense and corrects" description' do expect_offense(<<~RUBY) it 'registers an offense and corrects' do ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Description does not match use of `expect_no_corrections`. expect_no_corrections end RUBY expect_correction(<<~RUBY) it 'registers an offense but does not correct' do expect_no_corrections end RUBY end it 'registers an offense with a "registers an offense and autocorrects" description' do expect_offense(<<~RUBY) it 'registers an offense and autocorrects' do ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Description does not match use of `expect_no_corrections`. expect_no_corrections end RUBY expect_correction(<<~RUBY) it 'registers an offense but does not correct' do expect_no_corrections end RUBY end end context 'when not making an expectation on offenses' do it 'does not register an offense' do expect_no_offenses(<<~RUBY) it 'registers an offense' do end RUBY end end context 'when given a proper description in `aggregate_failures` block' do it 'does not register an offense' do expect_no_offenses(<<~RUBY) it 'does not register an offense' do aggregate_failures do expect_no_offenses(code) end end RUBY end end end
ruby
MIT
99fa0fdd0481910d7d052f4c2ed01ad36178f404
2026-01-04T15:37:41.211519Z
false
rubocop/rubocop
https://github.com/rubocop/rubocop/blob/99fa0fdd0481910d7d052f4c2ed01ad36178f404/spec/rubocop/cop/internal_affairs/redundant_message_argument_spec.rb
spec/rubocop/cop/internal_affairs/redundant_message_argument_spec.rb
# frozen_string_literal: true RSpec.describe RuboCop::Cop::InternalAffairs::RedundantMessageArgument, :config do context 'when `MSG` is passed' do it 'registers an offense' do expect_offense(<<~RUBY, 'example_cop.rb') add_offense(node, message: MSG) ^^^^^^^^^^^^ Redundant message argument to `#add_offense`. RUBY expect_correction(<<~RUBY) add_offense(node) RUBY end end it 'does not register an offense when formatted `MSG` is passed' do expect_no_offenses(<<~RUBY, 'example_cop.rb') add_offense(node, location: :expression, message: MSG % foo) RUBY end context 'when `#message` is passed' do it 'does not register an offense' do expect_no_offenses(<<~RUBY) add_offense( node, location: :expression, message: message, severity: :error ) RUBY end end context 'when `#message` with offending node is passed' do context 'when message is the only keyword argument' do it 'does not register an offense' do expect_no_offenses(<<~RUBY, 'example_cop.rb') add_offense(node, message: message(node)) RUBY end end context 'when there are others keyword arguments' do it 'does not register an offense' do expect_no_offenses(<<~RUBY, 'example_cop.rb') add_no_offenses(node, location: :selector, message: message(node), severity: :fatal) RUBY end end end it 'does not register an offense when `#message` with another node is passed' do expect_no_offenses(<<~RUBY, 'example_cop.rb') add_offense(node, message: message(other_node)) RUBY end end
ruby
MIT
99fa0fdd0481910d7d052f4c2ed01ad36178f404
2026-01-04T15:37:41.211519Z
false
rubocop/rubocop
https://github.com/rubocop/rubocop/blob/99fa0fdd0481910d7d052f4c2ed01ad36178f404/spec/rubocop/cop/internal_affairs/location_expression_spec.rb
spec/rubocop/cop/internal_affairs/location_expression_spec.rb
# frozen_string_literal: true RSpec.describe RuboCop::Cop::InternalAffairs::LocationExpression, :config do it 'registers and corrects an offense when using `location.expression`' do expect_offense(<<~RUBY) node.location.expression ^^^^^^^^^^^^^^^^^^^ Use `source_range` instead. RUBY expect_correction(<<~RUBY) node.source_range RUBY end it 'registers and corrects an offense when using `node&.location&.expression`' do expect_offense(<<~RUBY) node&.location&.expression ^^^^^^^^^^^^^^^^^^^^ Use `source_range` instead. RUBY expect_correction(<<~RUBY) node&.source_range RUBY end it 'registers and corrects an offense when using `loc.expression`' do expect_offense(<<~RUBY) node.loc.expression ^^^^^^^^^^^^^^ Use `source_range` instead. RUBY expect_correction(<<~RUBY) node.source_range RUBY end it 'registers and corrects an offense when using `node&.loc&.expression`' do expect_offense(<<~RUBY) node&.loc&.expression ^^^^^^^^^^^^^^^ Use `source_range` instead. RUBY expect_correction(<<~RUBY) node&.source_range RUBY end it 'registers and corrects an offense when using `loc.expression.end_pos`' do expect_offense(<<~RUBY) node.loc.expression.end_pos ^^^^^^^^^^^^^^ Use `source_range` instead. RUBY expect_correction(<<~RUBY) node.source_range.end_pos RUBY end it 'registers and corrects an offense when using `node&.loc&.expression&.end_pos`' do expect_offense(<<~RUBY) node&.loc&.expression&.end_pos ^^^^^^^^^^^^^^^ Use `source_range` instead. RUBY expect_correction(<<~RUBY) node&.source_range&.end_pos RUBY end it 'does not register an offense when using `location.expression` without a receiver' do expect_no_offenses(<<~RUBY) location.expression RUBY end it 'does not register an offense when using `location&.expression` without a receiver' do expect_no_offenses(<<~RUBY) location&.expression RUBY end it 'does not register an offense when using `loc.expression` without a receiver' do expect_no_offenses(<<~RUBY) loc.expression RUBY end it 'does not register an offense when using `loc&.expression` without a receiver' do expect_no_offenses(<<~RUBY) loc&.expression RUBY end it 'does not register an offense when assigning `node.location`' do expect_no_offenses(<<~RUBY) loc = node.location RUBY end it 'does not register an offense when assigning `node&.location`' do expect_no_offenses(<<~RUBY) loc = node&.location RUBY end end
ruby
MIT
99fa0fdd0481910d7d052f4c2ed01ad36178f404
2026-01-04T15:37:41.211519Z
false
rubocop/rubocop
https://github.com/rubocop/rubocop/blob/99fa0fdd0481910d7d052f4c2ed01ad36178f404/spec/rubocop/cop/internal_affairs/redundant_method_dispatch_node_spec.rb
spec/rubocop/cop/internal_affairs/redundant_method_dispatch_node_spec.rb
# frozen_string_literal: true RSpec.describe RuboCop::Cop::InternalAffairs::RedundantMethodDispatchNode, :config do it 'registers an offense when using `node.send_node.method_name`' do expect_offense(<<~RUBY) node.send_node.method_name ^^^^^^^^^^ Remove the redundant `send_node`. RUBY expect_correction(<<~RUBY) node.method_name RUBY end it 'registers an offense when using `node.send_node.method?(:method_name)`' do expect_offense(<<~RUBY) node.send_node.method?(:method_name) ^^^^^^^^^^ Remove the redundant `send_node`. RUBY expect_correction(<<~RUBY) node.method?(:method_name) RUBY end it 'registers an offense when using `node.send_node.method?(node.method_name)`' do expect_offense(<<~RUBY) node.send_node.method?(node.method_name) ^^^^^^^^^^ Remove the redundant `send_node`. RUBY expect_correction(<<~RUBY) node.method?(node.method_name) RUBY end it 'does not register an offense when using `node.method_name`' do expect_no_offenses(<<~RUBY) node.method_name RUBY end it 'registers an offense when using `node.send_node.receiver`' do expect_offense(<<~RUBY) node.send_node.receiver ^^^^^^^^^^ Remove the redundant `send_node`. RUBY expect_correction(<<~RUBY) node.receiver RUBY end it 'does not register an offense when using `node.receiver`' do expect_no_offenses(<<~RUBY) node.receiver RUBY end it 'does not register an offense when using `node.send_node.arguments?`' do expect_no_offenses(<<~RUBY) node.send_node.arguments? RUBY end it 'does not register an offense when using `send_node.method_name`' do expect_no_offenses(<<~RUBY) send_node.method_name RUBY end end
ruby
MIT
99fa0fdd0481910d7d052f4c2ed01ad36178f404
2026-01-04T15:37:41.211519Z
false
rubocop/rubocop
https://github.com/rubocop/rubocop/blob/99fa0fdd0481910d7d052f4c2ed01ad36178f404/spec/rubocop/cop/internal_affairs/redundant_source_range_spec.rb
spec/rubocop/cop/internal_affairs/redundant_source_range_spec.rb
# frozen_string_literal: true RSpec.describe RuboCop::Cop::InternalAffairs::RedundantSourceRange, :config do it 'does not register an offense when using `source_range.source`' do expect_no_offenses(<<~RUBY) source_range.source RUBY end it 'registers an offense when using `node.source_range.source`' do expect_offense(<<~RUBY) node.source_range.source ^^^^^^^^^^^^ Remove the redundant `source_range`. RUBY expect_correction(<<~RUBY) node.source RUBY end it 'registers an offense when using `node.source_range&.source`' do expect_offense(<<~RUBY) node&.source_range&.source ^^^^^^^^^^^^ Remove the redundant `source_range`. RUBY expect_correction(<<~RUBY) node&.source RUBY end it 'does not register an offense when using `node.source`' do expect_no_offenses(<<~RUBY) node.source RUBY end it 'does not register an offense when using `node&.source`' do expect_no_offenses(<<~RUBY) node&.source RUBY end it 'registers an offense when using `add_offense(node.source_range)`' do expect_offense(<<~RUBY) add_offense(node.source_range) ^^^^^^^^^^^^ Remove the redundant `source_range`. RUBY expect_correction(<<~RUBY) add_offense(node) RUBY end it 'registers an offense when using `add_offense(node.source_range, message: message)`' do expect_offense(<<~RUBY) add_offense(node.source_range, message: message) ^^^^^^^^^^^^ Remove the redundant `source_range`. RUBY expect_correction(<<~RUBY) add_offense(node, message: message) RUBY end it 'does not register an offense when using `add_offense(node)`' do expect_no_offenses(<<~RUBY) add_offense(node) RUBY end it 'registers an offense when using `corrector.replace(node.source_range, content)`' do expect_offense(<<~RUBY) add_offense do |corrector| corrector.replace(node.source_range, content) ^^^^^^^^^^^^ Remove the redundant `source_range`. end RUBY expect_correction(<<~RUBY) add_offense do |corrector| corrector.replace(node, content) end RUBY end it 'registers an offense when using `corrector.remove(node.source_range)`' do expect_offense(<<~RUBY) add_offense do |corrector| corrector.remove(node.source_range) ^^^^^^^^^^^^ Remove the redundant `source_range`. end RUBY expect_correction(<<~RUBY) add_offense do |corrector| corrector.remove(node) end RUBY end it 'registers an offense when using `corrector.insert_before(node.source_range, content)`' do expect_offense(<<~RUBY) add_offense do |corrector| corrector.insert_before(node.source_range, content) ^^^^^^^^^^^^ Remove the redundant `source_range`. end RUBY expect_correction(<<~RUBY) add_offense do |corrector| corrector.insert_before(node, content) end RUBY end it 'registers an offense when using `corrector.insert_before_multi(node.source_range, content)`' do expect_offense(<<~RUBY) add_offense do |corrector| corrector.insert_before_multi(node.source_range, content) ^^^^^^^^^^^^ Remove the redundant `source_range`. end RUBY expect_correction(<<~RUBY) add_offense do |corrector| corrector.insert_before_multi(node, content) end RUBY end it 'registers an offense when using `corrector.insert_after(node.source_range, content)`' do expect_offense(<<~RUBY) add_offense do |corrector| corrector.insert_after(node.source_range, content) ^^^^^^^^^^^^ Remove the redundant `source_range`. end RUBY expect_correction(<<~RUBY) add_offense do |corrector| corrector.insert_after(node, content) end RUBY end it 'registers an offense when using `corrector.insert_after_multi(node.source_range, content)`' do expect_offense(<<~RUBY) add_offense do |corrector| corrector.insert_after_multi(node.source_range, content) ^^^^^^^^^^^^ Remove the redundant `source_range`. end RUBY expect_correction(<<~RUBY) add_offense do |corrector| corrector.insert_after_multi(node, content) end RUBY end it 'registers an offense when using `corrector.swap(node.source_range, before, after)`' do expect_offense(<<~RUBY) add_offense do |corrector| corrector.swap(node.source_range, before, after) ^^^^^^^^^^^^ Remove the redundant `source_range`. end RUBY expect_correction(<<~RUBY) add_offense do |corrector| corrector.swap(node, before, after) end RUBY end it 'registers an offense when using `lvar.source_range`' do expect_offense(<<~RUBY) def foo(corrector, lvar) corrector.insert_after(lvar.source_range, content) ^^^^^^^^^^^^ Remove the redundant `source_range`. end RUBY expect_correction(<<~RUBY) def foo(corrector, lvar) corrector.insert_after(lvar, content) end RUBY end it 'does not register an offense when using `corrector.replace(node, content)`' do expect_no_offenses(<<~RUBY) add_offense do |corrector| corrector.replace(node, content) end RUBY end it 'does not register an offense when using `processed_source.buffer.source_range`' do expect_no_offenses(<<~RUBY) add_offense do |corrector| corrector.insert_before(processed_source.buffer.source_range, preceding_comment) end RUBY end end
ruby
MIT
99fa0fdd0481910d7d052f4c2ed01ad36178f404
2026-01-04T15:37:41.211519Z
false
rubocop/rubocop
https://github.com/rubocop/rubocop/blob/99fa0fdd0481910d7d052f4c2ed01ad36178f404/spec/rubocop/cop/internal_affairs/node_type_group_spec.rb
spec/rubocop/cop/internal_affairs/node_type_group_spec.rb
# frozen_string_literal: true RSpec.describe RuboCop::Cop::InternalAffairs::NodeTypeGroup, :config do it 'registers an offense when using `type?` with entire `numeric` group' do expect_offense(<<~RUBY) node.type?(:int, :float, :rational, :complex) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Use `:numeric` instead of individually listing group types. RUBY expect_correction(<<~RUBY) node.numeric_type? RUBY end it 'registers an offense when using `type?` with entire `numeric` group in any order' do expect_offense(<<~RUBY) node.type?(:rational, :complex, :int, :float) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Use `:numeric` instead of individually listing group types. RUBY expect_correction(<<~RUBY) node.numeric_type? RUBY end it 'registers an offense when using `type?` with entire `numeric` when other types are present' do expect_offense(<<~RUBY) node.type?(:rational, :complex, :send, :int, :float, :def) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Use `:numeric` instead of individually listing group types. RUBY expect_correction(<<~RUBY) node.type?(:numeric, :send, :def) RUBY end it 'registers an offense when using `type?` with multiple groups' do expect_offense(<<~RUBY) node.type?(:rational, :complex, :irange, :int, :float, :erange) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Use `:numeric` instead of individually listing group types. RUBY expect_correction(<<~RUBY) node.type?(:numeric, :range) RUBY end it 'registers an offense when using `type?` and other node types as argument' do expect_offense(<<~RUBY) node.type?(bar, :irange, foo, :erange) ^^^^^^^^^^^^^^^^^^^^^^^^^^ Use `:range` instead of individually listing group types. RUBY expect_correction(<<~RUBY) node.type?(bar, :range, foo) RUBY end it 'registers an offense for save navigation' do expect_offense(<<~RUBY) node&.type?(:irange, :erange) ^^^^^^^^^^^^^^^^ Use `:range` instead of individually listing group types. RUBY expect_correction(<<~RUBY) node&.range_type? RUBY end it 'registers an offense when chained to a method call' do expect_offense(<<~RUBY) node.each_child_node(:irange, :erange).any? ^^^^^^^^^^^^^^^^ Use `:range` instead of individually listing group types. RUBY expect_correction(<<~RUBY) node.each_child_node(:range).any? RUBY end it 'registers an offense when chained to a method call with block' do expect_offense(<<~RUBY) node.each_child_node(:irange, :erange).any? do |node| ^^^^^^^^^^^^^^^^ Use `:range` instead of individually listing group types. foo?(node) end RUBY expect_correction(<<~RUBY) node.each_child_node(:range).any? do |node| foo?(node) end RUBY end it 'registers no offense when the group is incomplete' do expect_no_offenses(<<~RUBY) node.type?(:int, :float, :complex) RUBY end it 'registers no offense with no arguments' do expect_no_offenses(<<~RUBY) node.type? RUBY end it 'registers no offense when there is no receiver' do expect_no_offenses(<<~RUBY) type?(:irange, :erange) RUBY end %i[each_ancestor each_child_node each_descendant each_node].each do |method| it "registers an offense for #{method}" do expect_offense(<<~RUBY, method: method) node.#{method}(:irange, :erange) _{method} ^^^^^^^^^^^^^^^^ Use `:range` instead of individually listing group types. RUBY expect_correction(<<~RUBY) node.#{method}(:range) RUBY end end end
ruby
MIT
99fa0fdd0481910d7d052f4c2ed01ad36178f404
2026-01-04T15:37:41.211519Z
false
rubocop/rubocop
https://github.com/rubocop/rubocop/blob/99fa0fdd0481910d7d052f4c2ed01ad36178f404/spec/rubocop/cop/internal_affairs/redundant_expect_offense_arguments_spec.rb
spec/rubocop/cop/internal_affairs/redundant_expect_offense_arguments_spec.rb
# frozen_string_literal: true RSpec.describe RuboCop::Cop::InternalAffairs::RedundantExpectOffenseArguments, :config do it 'registers an offense when using `expect_no_offenses` with string and single keyword arguments' do expect_offense(<<~RUBY) expect_no_offenses('code', keyword: keyword) ^^^^^^^^^^^^^^^^^^ Remove the redundant arguments. RUBY expect_correction(<<~RUBY) expect_no_offenses('code') RUBY end it 'registers an offense when using `expect_no_offenses` with heredoc and single keyword arguments' do expect_offense(<<~RUBY) expect_no_offenses(<<~CODE, keyword: keyword) ^^^^^^^^^^^^^^^^^^ Remove the redundant arguments. CODE RUBY expect_correction(<<~RUBY) expect_no_offenses(<<~CODE) CODE RUBY end it 'registers an offense when using `expect_no_offenses` with heredoc multiple keyword arguments' do expect_offense(<<~RUBY) expect_no_offenses(<<~CODE, foo: foo, bar: bar) ^^^^^^^^^^^^^^^^^^^^ Remove the redundant arguments. CODE RUBY expect_correction(<<~RUBY) expect_no_offenses(<<~CODE) CODE RUBY end it 'does not register an offense when using `expect_no_offenses` with string argument only' do expect_no_offenses(<<~RUBY) expect_no_offenses('code') RUBY end it 'does not register an offense when using `expect_no_offenses` with heredoc argument only' do expect_no_offenses(<<~RUBY) expect_no_offenses(<<~CODE) CODE RUBY end it 'does not register an offense when using `expect_no_offenses` with string and positional arguments' do expect_no_offenses(<<~RUBY) expect_no_offenses('code', file) RUBY end it 'does not register an offense when using `expect_no_offenses` with heredoc and positional arguments' do expect_no_offenses(<<~RUBY) expect_no_offenses(<<~CODE, file) CODE RUBY end it 'does not crash when using `expect_no_offenses` with no arguments' do expect_no_offenses(<<~RUBY) expect_no_offenses CODE RUBY end end
ruby
MIT
99fa0fdd0481910d7d052f4c2ed01ad36178f404
2026-01-04T15:37:41.211519Z
false
rubocop/rubocop
https://github.com/rubocop/rubocop/blob/99fa0fdd0481910d7d052f4c2ed01ad36178f404/spec/rubocop/cop/internal_affairs/node_first_or_last_argument_spec.rb
spec/rubocop/cop/internal_affairs/node_first_or_last_argument_spec.rb
# frozen_string_literal: true RSpec.describe RuboCop::Cop::InternalAffairs::NodeFirstOrLastArgument, :config do shared_examples 'registers an offense' do |receiver:, position:, accessor:, dot: '.'| offending_source = "#{receiver}.arguments#{accessor}" correction_source = "#{receiver}#{dot}#{position}_argument" it "registers an offense when using `#{offending_source}`" do expect_offense(<<~RUBY, receiver: receiver, accessor: accessor, dot: dot) %{receiver}%{dot}arguments%{accessor} _{receiver}_{dot}^^^^^^^^^^{accessor} Use `##{position}_argument` instead of `#arguments#{accessor}`. RUBY expect_correction(<<~RUBY) #{correction_source} RUBY end end shared_examples 'does not register an offense' do |code| it "does not register an offense when using `#{code}`" do expect_no_offenses(code) end end it_behaves_like 'registers an offense', receiver: 'node', position: 'first', accessor: '.first' it_behaves_like 'registers an offense', receiver: 'node', position: 'first', accessor: '[0]' it_behaves_like 'registers an offense', receiver: 'node', position: 'last', accessor: '.last' it_behaves_like 'registers an offense', receiver: 'node', position: 'last', accessor: '[-1]' it_behaves_like 'registers an offense', receiver: 'node', position: 'first', accessor: '&.first' it_behaves_like 'registers an offense', receiver: 'node', position: 'first', accessor: '&.[](0)' it_behaves_like 'registers an offense', receiver: 'node', position: 'last', accessor: '&.last' it_behaves_like 'registers an offense', receiver: 'node', position: 'last', accessor: '&.[](-1)' it_behaves_like 'registers an offense', receiver: 'node', position: 'first', accessor: '&.first', dot: '&.' it_behaves_like 'registers an offense', receiver: 'node', position: 'first', accessor: '&.[](0)', dot: '&.' it_behaves_like 'registers an offense', receiver: 'node', position: 'last', accessor: '&.last', dot: '&.' it_behaves_like 'registers an offense', receiver: 'node', position: 'last', accessor: '&.[](-1)', dot: '&.' it_behaves_like 'registers an offense', receiver: 'some_node', position: 'first', accessor: '.first' it_behaves_like 'registers an offense', receiver: 'some_node', position: 'first', accessor: '[0]' it_behaves_like 'registers an offense', receiver: 'some_node', position: 'last', accessor: '.last' it_behaves_like 'registers an offense', receiver: 'some_node', position: 'last', accessor: '[-1]' it_behaves_like 'does not register an offense', 'node.first_argument' it_behaves_like 'does not register an offense', 'node.last_argument' it_behaves_like 'does not register an offense', 'arguments.first' it_behaves_like 'does not register an offense', 'arguments.last' it_behaves_like 'does not register an offense', 'arguments[0]' it_behaves_like 'does not register an offense', 'arguments[-1]' end
ruby
MIT
99fa0fdd0481910d7d052f4c2ed01ad36178f404
2026-01-04T15:37:41.211519Z
false
rubocop/rubocop
https://github.com/rubocop/rubocop/blob/99fa0fdd0481910d7d052f4c2ed01ad36178f404/spec/rubocop/cop/utils/format_string_spec.rb
spec/rubocop/cop/utils/format_string_spec.rb
# frozen_string_literal: true RSpec.describe RuboCop::Cop::Utils::FormatString do def format_sequences(string) RuboCop::Cop::Utils::FormatString.new(string).format_sequences end it 'finds the correct number of fields' do # rubocop:disable RSpec/MultipleExpectations expect(format_sequences('').size).to eq(0) expect(format_sequences('%s').size).to eq(1) expect(format_sequences('%s %s').size).to eq(2) expect(format_sequences('%s %s %%').size).to eq(3) expect(format_sequences('%s %s %%').size).to eq(3) expect(format_sequences('% d').size).to eq(1) expect(format_sequences('%+d').size).to eq(1) expect(format_sequences('%d').size).to eq(1) expect(format_sequences('%+o').size).to eq(1) expect(format_sequences('%#o').size).to eq(1) expect(format_sequences('%.0e').size).to eq(1) expect(format_sequences('%#.0e').size).to eq(1) expect(format_sequences('% 020d').size).to eq(1) expect(format_sequences('%20d').size).to eq(1) expect(format_sequences('%+20d').size).to eq(1) expect(format_sequences('%020d').size).to eq(1) expect(format_sequences('%+020d').size).to eq(1) expect(format_sequences('% 020d').size).to eq(1) expect(format_sequences('%-20d').size).to eq(1) expect(format_sequences('%-+20d').size).to eq(1) expect(format_sequences('%- 20d').size).to eq(1) expect(format_sequences('%020x').size).to eq(1) expect(format_sequences('%#20.8x').size).to eq(1) expect(format_sequences('%+g:% g:%-g').size).to eq(3) expect(format_sequences('%+-d').size) # multiple flags .to eq(1) expect(format_sequences('%*s').size).to eq(1) expect(format_sequences('%-*s').size).to eq(1) end describe '#named_interpolation?' do shared_examples 'named format sequence' do |format_string| it 'detects named format sequence' do expect(described_class.new(format_string)).to be_named_interpolation end it 'does not detect escaped named format sequence' do escaped = format_string.gsub('%', '%%') expect(described_class.new(escaped)).not_to be_named_interpolation expect(described_class.new("prefix:#{escaped}")).not_to be_named_interpolation end end it_behaves_like 'named format sequence', '%<greeting>2s' it_behaves_like 'named format sequence', '%2<greeting>s' it_behaves_like 'named format sequence', '%+0<num>8.2f' it_behaves_like 'named format sequence', '%+08<num>.2f' end describe '#valid?' do it 'returns true when there are only unnumbered formats' do fs = described_class.new('%s %d') expect(fs).to be_valid end it 'returns true when there are only numbered formats' do fs = described_class.new('%1$s %2$d') expect(fs).to be_valid end it 'returns true when there are only named formats' do fs = described_class.new('%{foo}s') expect(fs).to be_valid end it 'returns true when there are only named with escaped `%` formats' do fs = described_class.new('%%%{foo}d') expect(fs).to be_valid end it 'returns false when there are unnumbered and numbered formats' do fs = described_class.new('%s %1$d') expect(fs).not_to be_valid end it 'returns false when there are unnumbered and named formats' do fs = described_class.new('%s %{foo}d') expect(fs).not_to be_valid end it 'returns false when there are numbered and named formats' do fs = described_class.new('%1$s %{foo}d') expect(fs).not_to be_valid end end describe described_class::FormatSequence do subject(:sequence) { format_sequences(format_string).first } describe '#variable_width?' do context 'when no width is given' do let(:format_string) { '%s' } it { is_expected.not_to be_variable_width } end context 'when a fixed width is given' do let(:format_string) { '%2s' } it { is_expected.not_to be_variable_width } end context 'when a negative fixed width is given' do let(:format_string) { '%-2s' } it { is_expected.not_to be_variable_width } end context 'when a variable width is given' do let(:format_string) { '%*s' } it { is_expected.to be_variable_width } end context 'when a negative variable width is given' do let(:format_string) { '%-*s' } it { is_expected.to be_variable_width } end context 'when a variable width with an explicit argument number is given' do let(:format_string) { '%*2$s' } it { is_expected.to be_variable_width } end end describe '#variable_width_argument_number' do subject { sequence.variable_width_argument_number } context 'when no width is given' do let(:format_string) { '%s' } it { is_expected.to be_nil } end context 'when a fixed width is given' do let(:format_string) { '%2s' } it { is_expected.to be_nil } end context 'when a negative fixed width is given' do let(:format_string) { '%-2s' } it { is_expected.to be_nil } end context 'when a variable width is given' do let(:format_string) { '%*s' } it { is_expected.to eq(1) } end context 'when a negative variable width is given' do let(:format_string) { '%-*s' } it { is_expected.to eq(1) } end context 'when a variable width with an explicit argument number is given' do let(:format_string) { '%*2$s' } it { is_expected.to eq(2) } end end end end
ruby
MIT
99fa0fdd0481910d7d052f4c2ed01ad36178f404
2026-01-04T15:37:41.211519Z
false
rubocop/rubocop
https://github.com/rubocop/rubocop/blob/99fa0fdd0481910d7d052f4c2ed01ad36178f404/spec/rubocop/cop/layout/first_hash_element_indentation_spec.rb
spec/rubocop/cop/layout/first_hash_element_indentation_spec.rb
# frozen_string_literal: true RSpec.describe RuboCop::Cop::Layout::FirstHashElementIndentation, :config do let(:config) do supported_styles = { 'SupportedStyles' => %w[special_inside_parentheses consistent align_braces] } RuboCop::Config.new('Layout/HashAlignment' => align_hash_config, 'Layout/FirstHashElementIndentation' => cop_config.merge(supported_styles).merge( 'IndentationWidth' => cop_indent ), 'Layout/IndentationWidth' => { 'Width' => 2 }) end let(:align_hash_config) do { 'Enabled' => true, 'EnforcedColonStyle' => 'key', 'EnforcedHashRocketStyle' => 'key' } end let(:cop_config) { { 'EnforcedStyle' => 'special_inside_parentheses' } } let(:cop_indent) { nil } # use indentation width from Layout/IndentationWidth shared_examples 'right brace' do it 'registers an offense and corrects incorrectly indented }' do expect_offense(<<~RUBY) a << { } ^ Indent the right brace the same as the start of the line where the left brace is. RUBY expect_correction(<<~RUBY) a << { } RUBY end end context 'when the HashAlignment style is separator for :' do let(:align_hash_config) do { 'Enabled' => true, 'EnforcedColonStyle' => 'separator', 'EnforcedHashRocketStyle' => 'key' } end it 'accepts correctly indented first pair' do expect_no_offenses(<<~RUBY) a << { a: 1, aaa: 222 } RUBY end it 'registers an offense and corrects incorrectly indented first pair with :' do expect_offense(<<~RUBY) a << { a: 1, ^^^^ Use 2 spaces for indentation in a hash, relative to the start of the line where the left curly brace is. aaa: 222 } RUBY expect_correction(<<~RUBY) a << { a: 1, aaa: 222 } RUBY end it_behaves_like 'right brace' end context 'when the HashAlignment style is separator for =>' do let(:align_hash_config) do { 'Enabled' => true, 'EnforcedColonStyle' => 'key', 'EnforcedHashRocketStyle' => 'separator' } end it 'accepts correctly indented first pair' do expect_no_offenses(<<~RUBY) a << { 'a' => 1, 'aaa' => 222 } RUBY end it 'registers an offense and corrects incorrectly indented first pair with =>' do expect_offense(<<~RUBY) a << { 'a' => 1, ^^^^^^^^ Use 2 spaces for indentation in a hash, relative to the start of the line where the left curly brace is. 'aaa' => 222 } RUBY expect_correction(<<~RUBY) a << { 'a' => 1, 'aaa' => 222 } RUBY end it_behaves_like 'right brace' end context 'when hash is operand' do it 'accepts correctly indented first pair' do expect_no_offenses(<<~RUBY) a << { a: 1 } RUBY end it 'registers an offense and corrects incorrectly indented first pair' do expect_offense(<<~RUBY) a << { a: 1 ^^^^ Use 2 spaces for indentation in a hash, relative to the start of the line where the left curly brace is. } RUBY expect_correction(<<~RUBY) a << { a: 1 } RUBY end it_behaves_like 'right brace' end context 'when hash is argument to setter' do it 'accepts correctly indented first pair' do expect_no_offenses(<<~RUBY) config.rack_cache = { :metastore => "rails:/", :entitystore => "rails:/", :verbose => false } RUBY end it 'registers an offense and corrects incorrectly indented first pair' do expect_offense(<<~RUBY) config.rack_cache = { :metastore => "rails:/", ^^^^^^^^^^^^^^^^^^^^^^^ Use 2 spaces for indentation in a hash, relative to the start of the line where the left curly brace is. :entitystore => "rails:/", :verbose => false } RUBY expect_correction(<<~RUBY) config.rack_cache = { :metastore => "rails:/", :entitystore => "rails:/", :verbose => false } RUBY end end context 'when hash is right hand side in assignment' do it 'registers an offense and corrects incorrectly indented first pair' do expect_offense(<<~RUBY) a = { a: 1, ^^^^ Use 2 spaces for indentation in a hash, relative to the start of the line where the left curly brace is. b: 2, c: 3 } RUBY expect_correction(<<~RUBY) a = { a: 1, b: 2, c: 3 } RUBY end it 'accepts correctly indented first pair' do expect_no_offenses(<<~RUBY) a = { a: 1 } RUBY end it 'accepts several pairs per line' do expect_no_offenses(<<~RUBY) a = { a: 1, b: 2 } RUBY end it 'accepts a first pair on the same line as the left brace' do expect_no_offenses(<<~RUBY) a = { "a" => 1, "b" => 2 } RUBY end it 'accepts single line hash' do expect_no_offenses('a = { a: 1, b: 2 }') end it 'accepts an empty hash' do expect_no_offenses('a = {}') end context 'when indentation width is overridden for this cop' do let(:cop_indent) { 3 } it 'registers an offense and corrects incorrectly indented first pair' do expect_offense(<<~RUBY) a = { a: 1, ^^^^ Use 3 spaces for indentation in a hash, relative to the start of the line where the left curly brace is. b: 2, c: 3 } RUBY expect_correction(<<~RUBY) a = { a: 1, b: 2, c: 3 } RUBY end it 'accepts correctly indented first pair' do expect_no_offenses(<<~RUBY) a = { a: 1 } RUBY end end end context 'when hash is method argument' do context 'and arguments are surrounded by parentheses' do context 'and EnforcedStyle is special_inside_parentheses' do it 'accepts special indentation for first argument' do expect_no_offenses(<<~RUBY) h = { a: 1 } func({ a: 1 }) func(x, { a: 1 }) h = { a: 1 } func({ a: 1 }) func(x, { a: 1 }) RUBY end it "registers an offense and corrects 'consistent' indentation" do expect_offense(<<~RUBY) func({ a: 1 ^^^^ Use 2 spaces for indentation in a hash, relative to the first position after the preceding left parenthesis. }) ^ Indent the right brace the same as the first position after the preceding left parenthesis. RUBY expect_correction(<<~RUBY) func({ a: 1 }) RUBY end context 'when using safe navigation operator' do it "registers an offense and corrects 'consistent' indentation" do expect_offense(<<~RUBY) receiver&.func({ a: 1 ^^^^ Use 2 spaces for indentation in a hash, relative to the first position after the preceding left parenthesis. }) ^ Indent the right brace the same as the first position after the preceding left parenthesis. RUBY expect_correction(<<~RUBY) receiver&.func({ a: 1 }) RUBY end end it "registers an offense and corrects 'align_braces' indentation" do expect_offense(<<~RUBY) var = { a: 1 ^^^^ Use 2 spaces for indentation in a hash, relative to the start of the line where the left curly brace is. } ^ Indent the right brace the same as the start of the line where the left brace is. RUBY expect_correction(<<~RUBY) var = { a: 1 } RUBY end it 'accepts special indentation for second argument' do expect_no_offenses(<<~'RUBY') body.should have_tag("input", :attributes => { :name => /q\[(id_eq)\]/ }) RUBY end it 'accepts normal indentation for hash within hash' do expect_no_offenses(<<~RUBY) scope = scope.where( klass.table_name => { reflection.type => model.base_class.sti_name } ) RUBY end it 'registers an offense for incorrectly indented hash that is the value of a single pair hash' do expect_offense(<<~RUBY) func(x: { a: 1, b: 2 }) ^^^^ Use 2 spaces for indentation in a hash, relative to the first position after the preceding left parenthesis. RUBY expect_correction(<<~RUBY) func(x: { a: 1, b: 2 }) RUBY end it 'registers an offense for a hash that is a value of a multi pairs hash' \ 'when the indent of its elements is not based on the hash key' do expect_offense(<<~RUBY) func(x: { a: 1, ^^^^ Use 2 spaces for indentation in a hash, relative to the parent hash key. b: 2 }, ^ Indent the right brace the same as the parent hash key. y: { c: 1, d: 2 }) RUBY expect_correction(<<~RUBY) func(x: { a: 1, b: 2 }, y: { c: 1, d: 2 }) RUBY end it 'accepts indent based on the preceding left parenthesis' \ 'when the right brace and its following pair is on the same line' do expect_no_offenses(<<~RUBY) func(:x, y: { a: 1, b: 2 }, z: { c: 1, d: 2 }) RUBY end it 'accepts indent based on the left brace when the outer hash key and ' \ 'the left brace is not on the same line' do expect_no_offenses(<<~RUBY) func(x: { a: 1, b: 2 }, y: { c: 1, d: 2 }) RUBY end end context 'and EnforcedStyle is consistent' do let(:cop_config) { { 'EnforcedStyle' => 'consistent' } } it 'accepts normal indentation for first argument' do expect_no_offenses(<<~RUBY) h = { a: 1 } func({ a: 1 }) func(x, { a: 1 }) h = { a: 1 } func({ a: 1 }) func(x, { a: 1 }) RUBY end it 'registers an offense and corrects incorrect indentation' do expect_offense(<<~RUBY) func({ a: 1 ^^^^ Use 2 spaces for indentation in a hash, relative to the start of the line where the left curly brace is. }) ^ Indent the right brace the same as the start of the line where the left brace is. RUBY expect_correction(<<~RUBY) func({ a: 1 }) RUBY end it 'accepts normal indentation for second argument' do expect_no_offenses(<<~'RUBY') body.should have_tag("input", :attributes => { :name => /q\[(id_eq)\]/ }) RUBY end it 'registers an offense for incorrectly indented hash that is the value of a single pair hash' do expect_offense(<<~RUBY) func(x: { a: 1, b: 2 }) ^^^^ Use 2 spaces for indentation in a hash, relative to the start of the line where the left curly brace is. RUBY expect_correction(<<~RUBY) func(x: { a: 1, b: 2 }) RUBY end it 'registers an offense for a hash that is a value of a multi pairs hash' \ 'when the indent of its elements is not based on the hash key' do expect_offense(<<~RUBY) func(x: { a: 1, ^^^^ Use 2 spaces for indentation in a hash, relative to the parent hash key. b: 2 }, ^ Indent the right brace the same as the parent hash key. y: { c: 1, d: 2 }) RUBY expect_correction(<<~RUBY) func(x: { a: 1, b: 2 }, y: { c: 1, d: 2 }) RUBY end it 'accepts indent based on the start of the line where the left brace is' \ 'when the right brace and its following pair is on the same line' do expect_no_offenses(<<~RUBY) func(:x, y: { a: 1, b: 2 }, z: { c: 1, d: 2 }) RUBY end it 'accepts indent based on the left brace when the outer hash key and ' \ 'the left brace is not on the same line' do expect_no_offenses(<<~RUBY) func(x: { a: 1, b: 2 }, y: { c: 1, d: 2 }) RUBY end end end context 'and argument are not surrounded by parentheses' do it 'accepts braceless hash' do expect_no_offenses('func a: 1, b: 2') end it 'accepts single line hash with braces' do expect_no_offenses('func x, { a: 1, b: 2 }') end it 'accepts a correctly indented multi-line hash with braces' do expect_no_offenses(<<~RUBY) func x, { a: 1, b: 2 } RUBY end it 'registers an offense for incorrectly indented multi-line hash with braces' do expect_offense(<<~RUBY) func x, { a: 1, b: 2 } ^^^^ Use 2 spaces for indentation in a hash, relative to the start of the line where the left curly brace is. RUBY expect_correction(<<~RUBY) func x, { a: 1, b: 2 } RUBY end it 'registers an offense for the first inner hash member not based on the start of line ' \ 'when the outer hash pair has no following siblings' do expect_offense(<<~RUBY) func x: :foo, y: { a: 1, b: 2 } ^^^^ Use 2 spaces for indentation in a hash, relative to the start of the line where the left curly brace is. RUBY expect_correction(<<~RUBY) func x: :foo, y: { a: 1, b: 2 } RUBY end it 'registers an offense for a hash that is a value of a multi pairs hash' \ 'when the indent of its elements is not based on the hash key' do expect_offense(<<~RUBY) func x: { a: 1, ^^^^ Use 2 spaces for indentation in a hash, relative to the parent hash key. b: 2 }, ^ Indent the right brace the same as the parent hash key. y: { c: 1, d: 2 } RUBY expect_correction(<<~RUBY) func x: { a: 1, b: 2 }, y: { c: 1, d: 2 } RUBY end it 'accepts indent based on the start of the line where the left brace is' \ 'when the right brace and its following pair is on the same line' do expect_no_offenses(<<~RUBY) func :x, y: { a: 1, b: 2 }, z: { c: 1, d: 2 } RUBY end it 'accepts indent based on the left brace when the outer hash key and ' \ 'the left brace is not on the same line' do expect_no_offenses(<<~RUBY) { x: { a: 1, b: 2 }, y: { c: 1, d: 2 } } RUBY end end end context 'when EnforcedStyle is align_braces' do let(:cop_config) { { 'EnforcedStyle' => 'align_braces' } } it 'accepts correctly indented first pair' do expect_no_offenses(<<~RUBY) a = { a: 1 } RUBY end it 'accepts several pairs per line' do expect_no_offenses(<<~RUBY) a = { a: 1, b: 2 } RUBY end it 'accepts a first pair on the same line as the left brace' do expect_no_offenses(<<~RUBY) a = { "a" => 1, "b" => 2 } RUBY end it 'accepts single line hash' do expect_no_offenses('a = { a: 1, b: 2 }') end it 'accepts an empty hash' do expect_no_offenses('a = {}') end context "when 'consistent' style is used" do it 'registers an offense and corrects incorrect indentation' do expect_offense(<<~RUBY) func({ a: 1 ^^^^ Use 2 spaces for indentation in a hash, relative to the position of the opening brace. }) ^ Indent the right brace the same as the left brace. RUBY expect_correction(<<~RUBY) func({ a: 1 }) RUBY end end context "when 'special_inside_parentheses' style is used" do it 'registers an offense and corrects incorrect indentation' do expect_offense(<<~RUBY) var = { a: 1 ^^^^ Use 2 spaces for indentation in a hash, relative to the position of the opening brace. } ^ Indent the right brace the same as the left brace. func({ a: 1 }) RUBY expect_correction(<<~RUBY) var = { a: 1 } func({ a: 1 }) RUBY end end it 'registers an offense and corrects incorrectly indented }' do expect_offense(<<~RUBY) a << { } ^ Indent the right brace the same as the left brace. RUBY expect_correction(<<~RUBY) a << { } RUBY end it 'registers an offense for incorrectly indented hash that is the value of a single pair hash' do expect_offense(<<~RUBY) func x: { a: 1, b: 2 } ^^^^ Use 2 spaces for indentation in a hash, relative to the position of the opening brace. RUBY expect_correction(<<~RUBY) func x: { a: 1, b: 2 } RUBY end it 'registers an offense for a hash that is a value of a multi pairs hash' \ 'when the indent of its elements is not based on the hash key' do expect_offense(<<~RUBY) func x: { a: 1, ^^^^ Use 2 spaces for indentation in a hash, relative to the position of the opening brace. b: 2 }, ^ Indent the right brace the same as the left brace. y: { c: 1, d: 2 } RUBY expect_correction(<<~RUBY) func x: { a: 1, b: 2 }, y: { c: 1, d: 2 } RUBY end end end
ruby
MIT
99fa0fdd0481910d7d052f4c2ed01ad36178f404
2026-01-04T15:37:41.211519Z
false
rubocop/rubocop
https://github.com/rubocop/rubocop/blob/99fa0fdd0481910d7d052f4c2ed01ad36178f404/spec/rubocop/cop/layout/single_line_block_chain_spec.rb
spec/rubocop/cop/layout/single_line_block_chain_spec.rb
# frozen_string_literal: true RSpec.describe RuboCop::Cop::Layout::SingleLineBlockChain, :config do it 'registers an offense for method call chained on the same line as a block' do expect_offense(<<~RUBY) example.select { |item| item.cond? }.join('-') ^^^^^ Put method call on a separate line if chained to a single line block. RUBY expect_correction(<<~RUBY) example.select { |item| item.cond? } .join('-') RUBY end it 'registers an offense for method call chained on the same line as a numblock' do expect_offense(<<~RUBY) example.select { _1.cond? }.join('-') ^^^^^ Put method call on a separate line if chained to a single line block. RUBY expect_correction(<<~RUBY) example.select { _1.cond? } .join('-') RUBY end it 'registers an offense for method call chained on the same line as a itblock', :ruby34 do expect_offense(<<~RUBY) example.select { it.cond? }.join('-') ^^^^^ Put method call on a separate line if chained to a single line block. RUBY expect_correction(<<~RUBY) example.select { it.cond? } .join('-') RUBY end it 'registers an offense for safe navigation method call chained on the same line as a block' do expect_offense(<<~RUBY) example.select { |item| item.cond? }&.join('-') ^^^^^^ Put method call on a separate line if chained to a single line block. RUBY expect_correction(<<~RUBY) example.select { |item| item.cond? } &.join('-') RUBY end it 'registers an offense for no selector method call chained on the same line as a block' do expect_offense(<<~RUBY) example.select { |item| item.cond? }.(42) ^^ Put method call on a separate line if chained to a single line block. RUBY expect_correction(<<~RUBY) example.select { |item| item.cond? } .(42) RUBY end it 'does not register an offense for method call chained on a new line after a single line block' do expect_no_offenses(<<~RUBY) example.select { |item| item.cond? } .join('-') RUBY end it 'does not register an offense for method call chained on a new line after a single line block with trailing dot' do expect_no_offenses(<<~RUBY) example.select { |item| item.cond? }. join('-') RUBY end it 'does not register an offense for method call chained without a dot' do expect_no_offenses(<<~RUBY) example.select { |item| item.cond? } + 2 RUBY end it 'does not register an offense for method call chained on the same line as a multiline block' do expect_no_offenses(<<~RUBY) example.select do |item| item.cond? end.join('-') RUBY end end
ruby
MIT
99fa0fdd0481910d7d052f4c2ed01ad36178f404
2026-01-04T15:37:41.211519Z
false
rubocop/rubocop
https://github.com/rubocop/rubocop/blob/99fa0fdd0481910d7d052f4c2ed01ad36178f404/spec/rubocop/cop/layout/multiline_array_line_breaks_spec.rb
spec/rubocop/cop/layout/multiline_array_line_breaks_spec.rb
# frozen_string_literal: true RSpec.describe RuboCop::Cop::Layout::MultilineArrayLineBreaks, :config do context 'when on same line' do it 'does not add any offenses' do expect_no_offenses(<<~RUBY) [1,2,3] RUBY end end context 'when on same line, separate line from brackets' do it 'does not add any offenses' do expect_no_offenses(<<~RUBY) [ 1,2,3, ] RUBY end end context 'when two elements on same line' do it 'registers an offense and corrects' do expect_offense(<<~RUBY) [1, 2, 4] ^ Each item in a multi-line array must start on a separate line. RUBY expect_correction(<<~RUBY) [1, 2,\s 4] RUBY end end context 'when nested arrays' do it 'registers an offense and corrects' do expect_offense(<<~RUBY) [1, [2, 3], 4] ^ Each item in a multi-line array must start on a separate line. RUBY expect_correction(<<~RUBY) [1, [2, 3],\s 4] RUBY end end context 'ignore last element' do let(:cop_config) { { 'AllowMultilineFinalElement' => true } } it 'ignores last value that is a multiline hash' do expect_no_offenses(<<~RUBY) [1, 2, 3, { a: 1 }] RUBY end it 'registers and corrects values that are multiline hashes and not the last value' do expect_offense(<<~RUBY) [1, 2, 3, { ^ Each item in a multi-line array must start on a separate line. ^ Each item in a multi-line array must start on a separate line. ^ Each item in a multi-line array must start on a separate line. a: 1 }, 4] RUBY expect_correction(<<~RUBY) [1,#{trailing_whitespace} 2,#{trailing_whitespace} 3,#{trailing_whitespace} { a: 1 },#{trailing_whitespace} 4] RUBY end end end
ruby
MIT
99fa0fdd0481910d7d052f4c2ed01ad36178f404
2026-01-04T15:37:41.211519Z
false
rubocop/rubocop
https://github.com/rubocop/rubocop/blob/99fa0fdd0481910d7d052f4c2ed01ad36178f404/spec/rubocop/cop/layout/def_end_alignment_spec.rb
spec/rubocop/cop/layout/def_end_alignment_spec.rb
# frozen_string_literal: true RSpec.describe RuboCop::Cop::Layout::DefEndAlignment, :config do context 'when EnforcedStyleAlignWith is start_of_line' do let(:cop_config) { { 'EnforcedStyleAlignWith' => 'start_of_line', 'AutoCorrect' => true } } it_behaves_like 'misaligned', <<~RUBY, false def test end ^^^ `end` at 2, 2 is not aligned with `def` at 1, 0. def Test.test end ^^^ `end` at 2, 2 is not aligned with `def` at 1, 0. RUBY it_behaves_like 'aligned', "\xef\xbb\xbfdef", 'test', 'end' it_behaves_like 'aligned', 'def', 'test', 'end' it_behaves_like 'aligned', 'def', 'Test.test', 'end', 'defs' it_behaves_like 'aligned', 'foo def', 'test', 'end' it_behaves_like 'aligned', 'foo bar def', 'test', 'end' it_behaves_like 'misaligned', <<~RUBY, :def foo def test end ^^^ `end` at 2, 4 is not aligned with `foo def` at 1, 0. RUBY context 'correct + opposite' do it 'registers an offense' do expect_offense(<<~RUBY) foo def a a1 end foo def b b1 end ^^^ `end` at 7, 4 is not aligned with `foo def` at 5, 0. RUBY expect(cop.config_to_allow_offenses).to eq('Enabled' => false) expect_correction(<<~RUBY) foo def a a1 end foo def b b1 end RUBY end end context 'when using refinements and `private def`' do it 'does not register an offense' do expect_no_offenses(<<~RUBY) using Module.new { refine Hash do class << Hash private def _ruby2_keywords_hash(*args) end end end } RUBY end end context 'when including an anonymous module containing `private def`' do it 'does not register an offense' do expect_no_offenses(<<~RUBY) include Module.new { private def foo end } RUBY end end end context 'when EnforcedStyleAlignWith is def' do let(:cop_config) { { 'EnforcedStyleAlignWith' => 'def', 'AutoCorrect' => true } } it_behaves_like 'misaligned', <<~RUBY, false def test end ^^^ `end` at 2, 2 is not aligned with `def` at 1, 0. def Test.test end ^^^ `end` at 2, 2 is not aligned with `def` at 1, 0. RUBY it_behaves_like 'aligned', 'def', 'test', 'end' it_behaves_like 'aligned', 'def', 'Test.test', 'end', 'defs' it_behaves_like('aligned', 'foo def', 'test', ' end') it_behaves_like 'misaligned', <<~RUBY, :start_of_line foo def test end ^^^ `end` at 2, 0 is not aligned with `def` at 1, 4. RUBY context 'correct + opposite' do it 'registers an offense' do expect_offense(<<~RUBY) foo def a a1 end ^^^ `end` at 3, 0 is not aligned with `def` at 1, 4. foo def b b1 end RUBY expect(cop.config_to_allow_offenses).to eq('Enabled' => false) expect_correction(<<~RUBY) foo def a a1 end foo def b b1 end RUBY end end end end
ruby
MIT
99fa0fdd0481910d7d052f4c2ed01ad36178f404
2026-01-04T15:37:41.211519Z
false
rubocop/rubocop
https://github.com/rubocop/rubocop/blob/99fa0fdd0481910d7d052f4c2ed01ad36178f404/spec/rubocop/cop/layout/class_structure_spec.rb
spec/rubocop/cop/layout/class_structure_spec.rb
# frozen_string_literal: true RSpec.describe RuboCop::Cop::Layout::ClassStructure, :config do let(:config) do RuboCop::Config.new( 'Layout/ClassStructure' => { 'ExpectedOrder' => %w[ module_inclusion constants attribute_macros delegate macros public_class_methods initializer public_methods protected_attribute_macros protected_methods private_attribute_macros private_delegate private_methods ], 'Categories' => { 'attribute_macros' => %w[ attr_accessor attr_reader attr_writer ], 'macros' => %w[ validates validate ], 'module_inclusion' => %w[ prepend extend include ] } } ) end context 'when the first line ends with a comment' do it 'reports an offense and swaps the lines' do expect_offense <<~RUBY class GridTask DESC = 'Grid Task' # grid task name OID, subclasses should set this extend Helpers::MakeFromFile ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ `module_inclusion` is supposed to appear before `constants`. end RUBY expect_correction <<~RUBY class GridTask extend Helpers::MakeFromFile DESC = 'Grid Task' # grid task name OID, subclasses should set this end RUBY end end context 'with a complete ordered example' do it 'does not create offense' do expect_no_offenses <<~RUBY class Person # extend and include go first extend SomeModule include AnotherModule # inner classes CustomError = Class.new(StandardError) # constants are next SOME_CONSTANT = 20 # afterwards we have attribute macros attr_reader :name # then we have public delegate macros delegate :to_s, to: :name # followed by other macros (if any) validates :name # public class methods are next in line def self.some_method end # initialization goes between class methods and other instance methods def initialize end # followed by other public instance methods def some_method end # protected attribute macros and methods go next protected attr_reader :protected_name def some_protected_method end # private attribute macros, delegate macros and methods are grouped near the end private attr_reader :private_name delegate :some_private_delegate, to: :name def some_private_method end end RUBY end end context 'simple example' do specify do expect_offense <<~RUBY class Person CONST = 'wrong place' include AnotherModule ^^^^^^^^^^^^^^^^^^^^^ `module_inclusion` is supposed to appear before `constants`. extend SomeModule end RUBY expect_correction(<<~RUBY) class Person include AnotherModule extend SomeModule CONST = 'wrong place' end RUBY end end it 'registers an offense and corrects when public instance method is before class method' do expect_offense(<<~RUBY) class Foo def instance_method; end def self.class_method; end ^^^^^^^^^^^^^^^^^^^^^^^^^^ `public_class_methods` is supposed to appear before `public_methods`. end RUBY expect_correction(<<~RUBY) class Foo def self.class_method; end def instance_method; end end RUBY end context 'with protected methods declared before private' do let(:code) { <<~RUBY } class MyClass def public_method end private def first_private_method end def second_private_method end protected def first_protected_method ^^^^^^^^^^^^^^^^^^^^^^^^^^ `protected_methods` is supposed to appear before `private_methods`. end def second_protected_method end end RUBY it { expect_offense(code) } end context 'with attribute macros before after validations' do let(:code) { <<~RUBY } class Person include AnotherModule extend SomeModule CustomError = Class.new(StandardError) validates :name attr_reader :name ^^^^^^^^^^^^^^^^^ `attribute_macros` is supposed to appear before `macros`. def self.some_public_class_method end def initialize end def some_public_method end def yet_other_public_method end protected def some_protected_method end def other_public_method end private :other_public_method private def some_private_method end end RUBY it { expect_offense(code) } end context 'constant is not a literal' do it 'registers an offense but does not autocorrect' do expect_offense <<~RUBY class Person def name; end foo = 5 LIMIT = foo + 1 ^^^^^^^^^^^^^^^ `constants` is supposed to appear before `public_methods`. end RUBY expect_no_corrections end end it 'registers an offense and corrects when there is a comment in the macro method' do expect_offense(<<~RUBY) class Foo # This is a comment for macro method. validates :attr attr_reader :foo ^^^^^^^^^^^^^^^^ `attribute_macros` is supposed to appear before `macros`. end RUBY expect_correction(<<~RUBY) class Foo attr_reader :foo # This is a comment for macro method. validates :attr end RUBY end it 'registers an offense and corrects when literal constant is after method definitions' do expect_offense(<<~RUBY) class Foo def name; end LIMIT = 10 ^^^^^^^^^^ `constants` is supposed to appear before `public_methods`. CONST = 'wrong place'.freeze RECURSIVE_BASIC_LITERALS_CONST = [1, 2].freeze DYNAMIC_CONST = foo.freeze end RUBY expect_correction(<<~RUBY) class Foo LIMIT = 10 CONST = 'wrong place'.freeze RECURSIVE_BASIC_LITERALS_CONST = [1, 2].freeze def name; end DYNAMIC_CONST = foo.freeze end RUBY end it 'ignores misplaced private constants' do expect_offense(<<~RUBY) class Foo def name; end PRIVATE_CONST1 = 1 PRIVATE_CONST2 = 2 private_constant :PRIVATE_CONST1, :PRIVATE_CONST2 PUBLIC_CONST = 'public' ^^^^^^^^^^^^^^^^^^^^^^^ `constants` is supposed to appear before `public_methods`. end RUBY expect_correction(<<~RUBY) class Foo PUBLIC_CONST = 'public' def name; end PRIVATE_CONST1 = 1 PRIVATE_CONST2 = 2 private_constant :PRIVATE_CONST1, :PRIVATE_CONST2 end RUBY end it 'registers an offense and corrects when str heredoc constant is defined after public method' do expect_offense(<<~RUBY) class Foo def do_something end CONSTANT = <<~EOS ^^^^^^^^^^^^^^^^^ `constants` is supposed to appear before `public_methods`. str EOS end RUBY expect_correction(<<~RUBY) class Foo CONSTANT = <<~EOS str EOS def do_something end end RUBY end it 'registers an offense and corrects when dstr heredoc constant is defined after public method' do expect_offense(<<~'RUBY') class Foo def do_something end CONSTANT = <<~EOS ^^^^^^^^^^^^^^^^^ `constants` is supposed to appear before `public_methods`. #{str} EOS end RUBY expect_correction(<<~'RUBY') class Foo CONSTANT = <<~EOS #{str} EOS def do_something end end RUBY end it 'registers an offense and corrects when xstr heredoc constant is defined after public method' do expect_offense(<<~RUBY) class Foo def do_something end CONSTANT = <<~`EOS` ^^^^^^^^^^^^^^^^^^^ `constants` is supposed to appear before `public_methods`. str EOS end RUBY expect_correction(<<~RUBY) class Foo CONSTANT = <<~`EOS` str EOS def do_something end end RUBY end it 'registers an offense and corrects when public class method with heredoc after instance method' do expect_offense(<<~RUBY) class Foo def instance_method 'instance method' end def self.class_method ^^^^^^^^^^^^^^^^^^^^^ `public_class_methods` is supposed to appear before `public_methods`. <<~EOS class method EOS end end RUBY expect_correction(<<~RUBY) class Foo def self.class_method <<~EOS class method EOS end def instance_method 'instance method' end end RUBY end context 'when def modifier is used' do it 'registers an offense and corrects public method with modifier declared after private method with modifier' do expect_offense(<<~RUBY) class A private def foo end public def bar ^^^^^^^^^^^^^^ `public_methods` is supposed to appear before `private_methods`. end end RUBY expect_correction(<<~RUBY) class A public def bar end private def foo end end RUBY end it 'registers an offense and corrects public method without modifier declared after private method with modifier' do expect_offense(<<~RUBY) class A private def foo end def bar ^^^^^^^ `public_methods` is supposed to appear before `private_methods`. end end RUBY expect_correction(<<~RUBY) class A def bar end private def foo end end RUBY end it 'registers an offense and corrects when definitions that need to be sorted are defined alternately' do expect_offense(<<~RUBY) class A private def foo; end def bar; end ^^^^^^^^^^^^ `public_methods` is supposed to appear before `private_methods`. private def baz; end def qux; end ^^^^^^^^^^^^ `public_methods` is supposed to appear before `private_methods`. end RUBY expect_correction(<<~RUBY) class A def bar; end def qux; end private def foo; end private def baz; end end RUBY end it 'registers an offense and corrects public method after private method marked by its name' do expect_offense(<<~RUBY) class A def foo end private :foo def bar ^^^^^^^ `public_methods` is supposed to appear before `private_methods`. end end RUBY expect_correction(<<~RUBY) class A def bar end def foo end private :foo end RUBY end end context 'initializer is private and comes after attribute macro' do it 'registers an offense and autocorrects' do expect_offense(<<~RUBY) class A private attr_accessor :foo def initialize ^^^^^^^^^^^^^^ `initializer` is supposed to appear before `private_attribute_macros`. end end RUBY expect_correction(<<~RUBY) class A private def initialize end attr_accessor :foo end RUBY end end context 'when singleton class' do context 'simple example' do specify do expect_offense <<~RUBY class << self CONST = 'wrong place' include AnotherModule ^^^^^^^^^^^^^^^^^^^^^ `module_inclusion` is supposed to appear before `constants`. extend SomeModule end RUBY expect_correction(<<~RUBY) class << self include AnotherModule extend SomeModule CONST = 'wrong place' end RUBY end end end end
ruby
MIT
99fa0fdd0481910d7d052f4c2ed01ad36178f404
2026-01-04T15:37:41.211519Z
false
rubocop/rubocop
https://github.com/rubocop/rubocop/blob/99fa0fdd0481910d7d052f4c2ed01ad36178f404/spec/rubocop/cop/layout/space_after_not_spec.rb
spec/rubocop/cop/layout/space_after_not_spec.rb
# frozen_string_literal: true RSpec.describe RuboCop::Cop::Layout::SpaceAfterNot, :config do it 'registers an offense and corrects a single space after !' do expect_offense(<<~RUBY) ! something ^^^^^^^^^^^ Do not leave space between `!` and its argument. RUBY expect_correction(<<~RUBY) !something RUBY end it 'registers an offense and corrects multiple spaces after !' do expect_offense(<<~RUBY) ! something ^^^^^^^^^^^^^ Do not leave space between `!` and its argument. RUBY expect_correction(<<~RUBY) !something RUBY end it 'registers an offense and corrects newline after !' do expect_offense(<<~RUBY) ! ^ Do not leave space between `!` and its argument. something RUBY expect_correction(<<~RUBY) !something RUBY end it 'accepts no space after !' do expect_no_offenses('!something') end it 'accepts space after not keyword' do expect_no_offenses('not something') end it 'registers an offense and corrects space after ! with ' \ 'the negated receiver wrapped in parentheses' do expect_offense(<<~RUBY) ! (model) ^^^^^^^^^ Do not leave space between `!` and its argument. RUBY expect_correction(<<~RUBY) !(model) RUBY end end
ruby
MIT
99fa0fdd0481910d7d052f4c2ed01ad36178f404
2026-01-04T15:37:41.211519Z
false
rubocop/rubocop
https://github.com/rubocop/rubocop/blob/99fa0fdd0481910d7d052f4c2ed01ad36178f404/spec/rubocop/cop/layout/trailing_empty_lines_spec.rb
spec/rubocop/cop/layout/trailing_empty_lines_spec.rb
# frozen_string_literal: true RSpec.describe RuboCop::Cop::Layout::TrailingEmptyLines, :config do context 'when EnforcedStyle is final_newline' do let(:cop_config) { { 'EnforcedStyle' => 'final_newline' } } it 'accepts final newline' do expect_no_offenses("x = 0\n") end it 'accepts an empty file' do expect_no_offenses('') end it 'accepts final blank lines if they come after __END__' do expect_no_offenses(<<~RUBY) x = 0 __END__ RUBY end it 'accepts final blank lines if they come after __END__ in empty file' do expect_no_offenses(<<~RUBY) __END__ RUBY end it 'registers an offense for multiple trailing blank lines' do expect_offense(<<~RUBY) x = 0 ^{} 3 trailing blank lines detected. RUBY expect_correction("x = 0\n") end it 'registers an offense for multiple blank lines in an empty file' do expect_offense(<<~RUBY) ^{} 3 trailing blank lines detected. RUBY expect_correction("\n") end it 'registers an offense for no final newline after assignment' do expect_offense(<<~RUBY, chomp: true) x = 0 ^{} Final newline missing. RUBY end it 'registers an offense for no final newline after block comment' do expect_offense(<<~RUBY, chomp: true) puts 'testing rubocop when final new line is missing after block comments' =begin first line second line third line =end ^{} Final newline missing. RUBY end it 'autocorrects even if some lines have space' do expect_offense(<<~RUBY) x = 0 ^{} 4 trailing blank lines detected. #{trailing_whitespace} RUBY expect_correction("x = 0\n") end it 'accepts the `%` form string `"%\n\n"` at the end of file' do expect_no_offenses("%\n\n") end it 'accepts when assigning the `%` form string `"%\n\n"` to a variable at the end of file' do expect_no_offenses("x = %\n\n") end end context 'when EnforcedStyle is final_blank_line' do let(:cop_config) { { 'EnforcedStyle' => 'final_blank_line' } } it 'registers an offense for final newline' do expect_offense(<<~RUBY, chomp: true) x = 0\n ^{} Trailing blank line missing. RUBY end it 'registers an offense for multiple trailing blank lines' do expect_offense(<<~RUBY) x = 0 ^{} 3 trailing blank lines instead of 1 detected. RUBY expect_correction(<<~RUBY) x = 0 RUBY end it 'registers an offense for multiple blank lines in an empty file' do expect_offense(<<~RUBY) ^{} 3 trailing blank lines instead of 1 detected. RUBY expect_correction(<<~RUBY) RUBY end it 'registers an offense for no final newline' do expect_offense(<<~RUBY, chomp: true) x = 0 ^{} Final newline missing. RUBY end it 'accepts final blank line' do expect_no_offenses("x = 0\n\n") end it 'autocorrects missing blank line' do expect_correction(<<~RUBY, source: "x = 0\n") x = 0 RUBY end it 'autocorrects missing newline' do expect_correction(<<~RUBY, source: 'x = 0') x = 0 RUBY end it 'accepts the `%` form string `"%\n\n"` at the end of file' do expect_no_offenses("%\n\n") end it 'accepts when assigning the `%` form string `"%\n\n"` to a variable at the end of file' do expect_no_offenses("x = %\n\n") end end end
ruby
MIT
99fa0fdd0481910d7d052f4c2ed01ad36178f404
2026-01-04T15:37:41.211519Z
false
rubocop/rubocop
https://github.com/rubocop/rubocop/blob/99fa0fdd0481910d7d052f4c2ed01ad36178f404/spec/rubocop/cop/layout/line_length_spec.rb
spec/rubocop/cop/layout/line_length_spec.rb
# frozen_string_literal: true RSpec.describe RuboCop::Cop::Layout::LineLength, :config do let(:cop_config) { { 'Max' => 80, 'AllowedPatterns' => nil } } let(:config) do RuboCop::Config.new( 'Layout/LineLength' => { 'URISchemes' => %w[http https] }.merge(cop_config), 'Layout/IndentationStyle' => { 'IndentationWidth' => 2 } ) end it "registers an offense for a line that's 81 characters wide" do maximum_string = '#' * 80 expect_offense(<<~RUBY, maximum_string: maximum_string) #{maximum_string}# _{maximum_string}^ Line is too long. [81/80] RUBY expect(cop.config_to_allow_offenses).to eq(exclude_limit: { 'Max' => 81 }) end it 'highlights excessive characters' do maximum_string = '#' * 80 expect_offense(<<~RUBY, maximum_string: maximum_string) #{maximum_string}abc _{maximum_string}^^^ Line is too long. [83/80] RUBY end it "accepts a line that's 80 characters wide" do expect_no_offenses('#' * 80) end it 'accepts the first line if it is a shebang line' do expect_no_offenses(<<~RUBY) #!/System/Library/Frameworks/Ruby.framework/Versions/2.3/usr/bin/ruby --disable-gems do_something RUBY end it 'registers an offense for long line before __END__ but not after' do maximum_string = '#' * 80 expect_offense(<<~RUBY, maximum_string: maximum_string) #{maximum_string}#{'#' * 70} _{maximum_string}#{'^' * 70} Line is too long. [150/80] __END__ #{'#' * 200} RUBY end context 'when line is indented with tabs' do let(:cop_config) { { 'Max' => 10, 'AllowedPatterns' => nil } } it 'accepts a short line' do expect_no_offenses("\t\t\t123") end it 'registers an offense for a long line' do expect_offense(<<~RUBY) \t\t\t\t\t\t\t\t\t\t\t\t1 ^^^^^^^^^^^^^ Line is too long. [25/10] RUBY end end context 'when AllowURI option is enabled' do let(:cop_config) { { 'Max' => 80, 'AllowURI' => true, 'AllowQualifiedName' => true } } context 'and the URL fits within the max allowed characters' do it 'registers an offense for the line' do expect_offense(<<-RUBY) # Some documentation comment... # See: https://github.com/rubocop/rubocop and then words that are not part of a URL ^^^^^^^^^^^^^ Line is too long. [93/80] RUBY end end context 'and all the excessive characters are part of a URL' do it 'accepts the line' do expect_no_offenses(<<-RUBY) # Some documentation comment... # See: https://github.com/rubocop/rubocop/commit/3b48d8bdf5b1c2e05e35061837309890f04ab08c RUBY end context 'and the URL is wrapped in single quotes' do it 'accepts the line' do expect_no_offenses(<<-RUBY) # See: 'https://github.com/rubocop/rubocop/commit/3b48d8bdf5b1c2e05e35061837309890f04ab08c' RUBY end end context 'and the URL is wrapped in double quotes' do it 'accepts the line' do expect_no_offenses(<<-RUBY) # See: "https://github.com/rubocop/rubocop/commit/3b48d8bdf5b1c2e05e35061837309890f04ab08c" RUBY end end context 'and the URL is wrapped in braces' do it 'accepts the line' do expect_no_offenses(<<-RUBY) # See: {https://github.com/rubocop/rubocop/commit/3b48d8bdf5b1c2e05e35061837309890f04ab08c} RUBY end end context 'and the URL is wrapped in braces with title' do it 'accepts the line' do expect_no_offenses(<<-RUBY) # See: {https://github.com/rubocop/rubocop/commit/3b48d8bdf5b1c2e05e35061837309890f04ab08c Optional Title} RUBY end end end context 'and the excessive characters include a complete URL' do it 'registers an offense for the line' do expect_offense(<<-RUBY) # See: http://google.com/, http://gmail.com/, https://maps.google.com/, http://plus.google.com/ ^^^^^^^^^^^^^^^^^^^^^^^^^ Line is too long. [105/80] RUBY end end context 'and the excessive characters include part of a URL and another word' do it 'registers an offense for the line' do expect_offense(<<-RUBY) # See: https://github.com/rubocop/rubocop/commit/3b48d8bdf5b1c2e05e35061837309890f04ab08c and ^^^^ Line is too long. [103/80] # http://google.com/ RUBY end end context 'and the excessive characters include part of a URL in double quotes' do it 'does not include the quote as part of the offense' do expect_offense(<<-RUBY) # See: "https://github.com/rubocop/rubocop/commit/3b48d8bdf5b1c2e05e35061837309890f04ab08c" and ^^^^ Line is too long. [105/80] # "http://google.com/" RUBY end end context 'and the excessive characters include part of a URL in braces and another word' do it 'registers an offense for the line' do expect_offense(<<-RUBY) # See: {https://github.com/rubocop/rubocop/commit/3b48d8bdf5b1c2e05e35061837309890f04ab08c} and ^^^^ Line is too long. [105/80] # http://google.com/ RUBY end end context 'and the excessive characters include part of a URL and trailing whitespace' do it 'registers an offense for the line' do expect_offense(<<-RUBY) # See: https://github.com/rubocop/rubocop/commit/3b48d8bdf5b1c2e05e35061837309890f04ab08c#{trailing_whitespace} ^ Line is too long. [100/80] # http://google.com/ RUBY end end context 'with URI starting before or after limit depending on tabs count' do let(:cop_config) { { 'Max' => 30, 'AllowURI' => true } } it 'registers an offense for the line' do expect_offense(<<~RUBY) \t\t\t\t# There is some content http://test.com ^^^^^^^^^^^^^^^^^ Line is too long. [47/30] RUBY end end context 'and an error other than URI::InvalidURIError is raised ' \ 'while validating a URI-ish string' do let(:cop_config) { { 'Max' => 80, 'AllowURI' => true, 'URISchemes' => %w[LDAP] } } it 'does not crash' do expect do expect_offense(<<~RUBY) xxxxxxxxxxxxxxxxxxxxxxxxxxxxzxxxxxxxxxxx = LDAP::DEFAULT_GROUP_UNIQUE_MEMBER_LIST_KEY ^^^^^ Line is too long. [85/80] RUBY end.not_to raise_error end end context 'and the URL does not have a http(s) scheme' do it 'rejects the line' do expect_offense(<<~RUBY) #{'x' * 40} = 'otherprotocol://a.very.long.line.which.violates.LineLength/sadf' #{' ' * 40} ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Line is too long. [108/80] RUBY end context 'and the scheme has been configured' do let(:cop_config) do { 'Max' => 80, 'AllowURI' => true, 'URISchemes' => %w[otherprotocol] } end it 'does not register an offense' do expect_no_offenses(<<~RUBY) #{'x' * 40} = 'otherprotocol://a.very.long.line.which.violates.LineLength/sadf' RUBY end end end context 'and the URI is assigned' do it 'does not register an offense' do expect_no_offenses(<<~RUBY) #{'x' * 40} = 'https://a.very.long.line.which.violates.LineLength/sadf' #{'x' * 40} = "https://a.very.long.line.which.violates.LineLength/sadf" RUBY end end context 'and the URI is an argument' do it 'does not register an offense' do expect_no_offenses(<<~RUBY) #{'x' * 40}("https://a.very.long.line.which.violates.LineLength/sadf") #{'x' * 40} "https://a.very.long.line.which.violates.LineLength/sadf" #{'x' * 40}('https://a.very.long.line.which.violates.LineLength/sadf') #{'x' * 40} 'https://a.very.long.line.which.violates.LineLength/sadf' RUBY end end end context 'when AllowQualifiedName option is enabled' do let(:cop_config) { { 'Max' => 80, 'AllowQualifiedName' => true } } context 'and the namespace fits within the max allowed characters' do it 'registers an offense for the line' do expect_offense(<<-RUBY) # invoke the normal migration method # Should call ActiveRecord::Oracle::SchemaStatements::create_table in the end ^^^^^^^ Line is too long. [87/80] RUBY end end context 'and all the excessive characters are part of a qualifed name' do it 'accepts the line' do expect_no_offenses(<<-RUBY) # invoke the normal migration method # should end up calling ActiveRecord::ConnectionAdapters::OracleEnhanced::SchemaStatements::create_table RUBY end context 'and the qualifed name is wrapped in single quotes' do it 'accepts the line' do expect_no_offenses(<<-RUBY) # should end up calling 'ActiveRecord::ConnectionAdapters::OracleEnhanced::SchemaStatements::create_table' RUBY end end context 'and the qualifed name is wrapped in double quotes' do it 'accepts the line' do expect_no_offenses(<<-RUBY) # should end up calling "ActiveRecord::ConnectionAdapters::OracleEnhanced::SchemaStatements::create_table" RUBY end end context 'and the qualifed name is wrapped in braces' do it 'accepts the line' do expect_no_offenses(<<-RUBY) # should end up calling {ActiveRecord::ConnectionAdapters::OracleEnhanced::SchemaStatements::create_table} RUBY end end end context 'and the excessive characters include a complete qualifed name' do it 'registers an offense for the line' do expect_offense(<<-RUBY) # Invoke the normal migration method, in oracle envs should end up calling ActiveRecord::Oracle::create_table ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Line is too long. [119/80] RUBY end end context 'and the excessive characters include a complete qualifed name when multiple entries are present' do it 'registers an offense for the line' do expect_offense(<<-RUBY) # Refer ActiveRecord::Migrations, in oracle envs should end up calling ActiveRecord::Oracle::create_table ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Line is too long. [115/80] RUBY end end context 'and the excessive characters include part of a qualifed name and another word' do it 'registers an offense for the line' do expect_offense(<<-RUBY) # Should call ActiveRecord::ConnectionAdapters::OracleEnhanced::SchemaStatements::create_table and ^^^^ Line is too long. [108/80] # ActiveRecord::Example RUBY end end context 'and the excessive characters include part of a qualifed name in double quotes' do it 'does not include the quote as part of the offense' do expect_offense(<<-RUBY) # Should call "ActiveRecord::ConnectionAdapters::OracleEnhanced::SchemaStatements::create_table" and ^^^^ Line is too long. [110/80] # "ActiveRecord::Example" RUBY end end context 'and the excessive characters include part of a qualifed name in braces and another word' do it 'registers an offense for the line' do expect_offense(<<-RUBY) # Should call {ActiveRecord::ConnectionAdapters::OracleEnhanced::SchemaStatements::create_table} and ^^^^ Line is too long. [110/80] # {ActiveRecord::Example} RUBY end end context 'and the excessive characters include part of a qualifed name and trailing whitespace' do it 'registers an offense for the line' do expect_offense(<<-RUBY) # Should call ActiveRecord::ConnectionAdapters::OracleEnhanced::SchemaStatements::create_table#{trailing_whitespace} ^ Line is too long. [105/80] # http://google.com/ RUBY end end context 'and the qualifed name is an argument' do it 'does not register an offense' do expect_no_offenses(<<~RUBY) #{'x' * 40}("ActiveRecord::Oracle::SchemaStatements::create_table") #{'x' * 40} "ActiveRecord::Oracle::SchemaStatements::create_table" #{'x' * 40}('ActiveRecord::Oracle::SchemaStatements::create_table') #{'x' * 40} 'ActiveRecord::Oracle::SchemaStatements::create_table' RUBY end end end context 'when AllowQualifiedName option is not enabled' do let(:cop_config) { { 'Max' => 80 } } context 'and all the excessive characters are part of a qualifed name' do it 'registers an offense' do expect_offense(<<-RUBY) # invoke the normal migration method # should end up calling ActiveRecord::ConnectionAdapters::OracleEnhanced::SchemaStatements::create_table ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Line is too long. [114/80] RUBY end end end context 'when AllowedPatterns option is set' do let(:cop_config) { { 'Max' => 18, 'AllowedPatterns' => ['^\s*test\s', /^\s*def\s+test_/] } } it 'only registers an offense for lines not matching the pattern' do expect_offense(<<~RUBY) class ExampleTest < TestCase ^^^^^^^^^^ Line is too long. [28/18] test 'some really long test description which exceeds length' do end def test_some_other_long_test_description_which_exceeds_length end end RUBY end end context 'when AllowHeredoc option is enabled' do let(:cop_config) { { 'Max' => 80, 'AllowHeredoc' => true } } it 'accepts long lines in heredocs' do expect_no_offenses(<<~RUBY) <<-SQL SELECT posts.id, posts.title, users.name FROM posts LEFT JOIN users ON posts.user_id = users.id; SQL RUBY end context 'and SplitStrings option is enabled' do let(:cop_config) do super().merge('SplitStrings' => true) end it 'does not register an offense' do expect_no_offenses(<<~'RUBY') <<~MESSAGE #{'hello' * 1} #{'world' * 2} #{'hello' * 1} #{'world' * 2} #{'hello' * 1} #{'world' * 2} MESSAGE RUBY end end context 'when the source has no AST' do it 'does not crash' do expect { expect_no_offenses('# this results in AST being nil') }.not_to raise_error end end context 'and only certain heredoc delimiters are permitted' do let(:cop_config) { { 'Max' => 80, 'AllowHeredoc' => %w[SQL OK], 'AllowedPatterns' => [] } } it 'rejects long lines in heredocs with not permitted delimiters' do expect_offense(<<-RUBY) foo(<<-DOC, <<-SQL, <<-FOO) 1st offense: Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. #{' ' * 68}^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Line is too long. [149/80] \#{<<-OK} no offense (permitted): Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. OK 2nd offense: Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. #{' ' * 68}^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Line is too long. [149/80] DOC no offense (permitted): Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. \#{<<-XXX} no offense (nested inside permitted): Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. XXX no offense (permitted): Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. SQL 3rd offense: Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. #{' ' * 68}^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Line is too long. [149/80] \#{<<-SQL} no offense (permitted): Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. SQL 4th offense: Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. #{' ' * 68}^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Line is too long. [149/80] FOO RUBY end end end context 'when AllowURI option is disabled' do let(:cop_config) { { 'Max' => 80, 'AllowURI' => false } } context 'and all the excessive characters are part of a URL' do it 'registers an offense for the line' do expect_offense(<<-RUBY) # Lorem ipsum dolar sit amet. # See: https://github.com/rubocop/rubocop/commit/3b48d8bdf5b1c2e05e35061837309890f04ab08c ^^^^^^^^^^^^^^^^^^^ Line is too long. [99/80] RUBY end end end context 'when AllowRBSInlineAnnotation is disabled' do let(:cop_config) { { 'Max' => 10, 'AllowRBSInlineAnnotation' => false } } it 'registers an offense for a long line with an RBS::Inline annotation' do expect_offense(<<~RUBY) #: () -> String ^^^^^ Line is too long. [15/10] def hash end RUBY end it 'registers an offense for a long line with an RBS::Inline annotation on the same line as the code' do expect_offense(<<~RUBY) def each_address(&block) #: void ^^^^^^^^^^^^^^^^^^^^^^ Line is too long. [32/10] end RUBY end end context 'when AllowRBSInlineAnnotation is enabled' do let(:cop_config) { { 'Max' => 10, 'AllowRBSInlineAnnotation' => true } } it 'does not register an offense for a long line with an RBS::Inline annotation' do expect_no_offenses(<<~RUBY) #: () -> String def hash end RUBY end it 'does not register an offense for a long line with an RBS::Inline annotation on the same line as the code' do expect_no_offenses(<<~RUBY) def each_address(&block) #: void end RUBY end end context 'when AllowCopDirectives is disabled' do let(:cop_config) { { 'Max' => 80, 'AllowCopDirectives' => false } } context 'and the source is acceptable length' do context 'with a trailing RuboCop directive' do it 'registers an offense for the line' do expect_offense(<<~RUBY) #{'a' * 80} # rubocop:disable Layout/SomeCop #{' ' * 80}^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Line is too long. [113/80] RUBY end end context 'with an inline comment' do it 'highlights the excess comment' do expect_offense(<<~RUBY) #{'a' * 80} ### #{' ' * 80}^^^^ Line is too long. [84/80] RUBY end end end context 'and the source is too long and has a trailing cop directive' do it 'highlights the excess source and cop directive' do expect_offense(<<~RUBY) #{'a' * 80} b # rubocop:disable Metrics/AbcSize #{' ' * 80}^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Line is too long. [116/80] RUBY end end end context 'when AllowCopDirectives is enabled' do let(:cop_config) { { 'Max' => 80, 'AllowCopDirectives' => true } } context 'and the RuboCop directive is excessively long' do it 'accepts the line' do expect_no_offenses(<<~RUBY) # rubocop:disable Metrics/SomeReallyLongMetricNameThatShouldBeMuchShorterAndNeedsANameChange RUBY end end context 'and the RuboCop directive causes an excessive line length' do it 'accepts the line' do expect_no_offenses(<<~RUBY) def method_definition_that_is_just_under_the_line_length_limit(foo, bar) # rubocop:disable Metrics/AbcSize # complex method end RUBY end context 'and has explanatory text' do it 'does not register an offense' do expect_no_offenses(<<~RUBY) def method_definition_that_is_just_under_the_line_length_limit(foo) # rubocop:disable Metrics/AbcSize inherently complex! # complex end RUBY end end end context 'and the source is too long' do it 'highlights only the non-directive part' do expect_offense(<<~RUBY) #{'a' * 80}bcd # rubocop:enable Style/ClassVars #{' ' * 80}^^^ Line is too long. [83/80] RUBY end context 'and the source contains non-directive # as comment' do it 'highlights only the non-directive part' do expect_offense(<<~RUBY) #{'a' * 70} # bbbbbbbbbbbbbb # rubocop:enable Style/ClassVars' #{' ' * 70} ^^^^^^^ Line is too long. [87/80] RUBY end end context 'and the source contains non-directive #s as non-comment' do it 'registers an offense for the line' do expect_offense(<<-RUBY) LARGE_DATA_STRING_PATTERN = %r{\\A([A-Za-z0-9+/#]*={0,2})#([A-Za-z0-9+/#]*={0,2})#([A-Za-z0-9+/#]*={0,2})\\z} # rubocop:disable Style/ClassVars #{' ' * 68}^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Line is too long. [119/80] RUBY end end end end context 'affecting by IndentationWidth from Layout\Tab' do shared_examples 'with tabs indentation' do it "registers an offense for a line that's including 2 tab with size 2 " \ 'and 28 other characters' do expect_offense(<<~RUBY) \t\t#{'#' * 28}a #{' ' * 24}^^^ Line is too long. [33/30] RUBY expect(cop.config_to_allow_offenses).to eq(exclude_limit: { 'Max' => 33 }) end it "accepts a line that's including 1 tab with size 2 and 28 other characters" do expect_no_offenses("\t#{'#' * 28}") end end context 'without AllowURI option' do let(:config) do RuboCop::Config.new( 'Layout/IndentationWidth' => { 'Width' => 1 }, 'Layout/IndentationStyle' => { 'Enabled' => false, 'IndentationWidth' => 2 }, 'Layout/LineLength' => { 'Max' => 30 } ) end it_behaves_like 'with tabs indentation' end context 'with AllowURI option' do let(:config) do RuboCop::Config.new( 'Layout/IndentationWidth' => { 'Width' => 1 }, 'Layout/IndentationStyle' => { 'Enabled' => false, 'IndentationWidth' => 2 }, 'Layout/LineLength' => { 'Max' => 30, 'AllowURI' => true } ) end it_behaves_like 'with tabs indentation' it "accepts a line that's including URI" do expect_no_offenses("\t\t# https://github.com/rubocop/rubocop") end it "accepts a line that's including URI and exceeds by 1 char" do expect_no_offenses("\t\t# https://github.com/ruboco") end it "accepts a line that's including URI with text" do expect_no_offenses("\t\t# See https://github.com/rubocop/rubocop") end it "accepts a line that's including URI in quotes with text" do expect_no_offenses("\t\t# See 'https://github.com/rubocop/rubocop'") end it 'registers the line which looks like YARD comment' do expect_offense(<<-RUBY) \texpect(some_exception_variable) {|e| e.url.should == 'http://host/path'} ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Line is too long. [83/30] RUBY end end end context 'autocorrection' do let(:split_strings) { true } let(:cop_config) do { 'Max' => 40, 'AllowedPatterns' => nil, 'AutoCorrect' => true, 'SplitStrings' => split_strings } end context 'string' do context 'when under limit' do it 'does not add any offenses' do expect_no_offenses(<<~RUBY) 'aaaaaaaaaaaaaaaaaaa' RUBY end it 'does not add any offenses with interpolation' do expect_no_offenses(<<~'RUBY') 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa#{bbbbb}' RUBY end end context 'when over limit' do context 'when SplitStrings: true' do let(:split_strings) { true } it 'breaks the string at the limit' do expect_offense(<<~RUBY) 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaabbbbb' ^^^ Line is too long. [43/40] RUBY expect_correction(<<~'RUBY') 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa' \ 'bbbbb' RUBY end context 'when AllowHeredoc: false' do let(:cop_config) { super().merge('AllowHeredoc' => false) } context 'with multiple string interpolations' do it 'registers an offense and autocorrects' do expect_offense(<<~'RUBY') <<~MESSAGE #{'hello' * 1} #{'world' * 2} #{'hello' * 1} ^^^^^^ Line is too long. [46/40] MESSAGE RUBY expect_correction(<<~'RUBY') <<~MESSAGE #{'hello' * 1} #{'world' * 2} #{'he' \ 'llo' * 1} MESSAGE RUBY end end end context 'when the string straddles after the limit' do it 'registers an offense but does not correct' do expect_offense(<<~RUBY) foo 'aaaa' ^^^ Line is too long. [43/40] RUBY expect_no_corrections end end context 'when the string starts after the limit' do it 'registers an offense but does not correct' do expect_offense(<<~RUBY) foo 'aaaa' ^^^^^^ Line is too long. [46/40] RUBY expect_no_corrections end end context 'when there is already a continuation' do it 'breaks the string at the limit' do expect_offense(<<~'RUBY') 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa' \ 'bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbcccccccccccc' ^^^^^^^^^^ Line is too long. [50/40] RUBY expect_correction(<<~'RUBY') 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa' \ 'bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb' \ 'cccccccccccc' RUBY end end context 'when the string is not at the start of the source' do it 'breaks the string at the limit' do expect_offense(<<~RUBY) x = 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaabbbbb' ^^^ Line is too long. [43/40] RUBY expect_correction(<<~'RUBY', loop: false) x = 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa' \ 'bbbbb' RUBY end context 'with spaces' do it 'breaks the string at the last space' do expect_offense(<<~RUBY) x = 'aaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbbbb' ^^^^ Line is too long. [44/40] RUBY expect_correction(<<~'RUBY', loop: false) x = 'aaaaaaaaaaaaaaaaaaaaaa ' \ 'bbbbbbbbbbbbbbb' RUBY end end context 'with escape characters' do it 'breaks the string at the last space' do expect_offense(<<~'RUBY') x = 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\nbbbbbbbbbb' ^^^^^^^^^ Line is too long. [49/40] RUBY expect_correction(<<~'RUBY', loop: false) x = 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa' \ '\nbbbbbbbbbb' RUBY end end end context 'inside a hash' do it 'breaks the hash not the string' do expect_offense(<<~RUBY) { x: 'aaaa', y: 'bbbbbbbbbbbbbbbbbbbbbbbbbbb', z: 'cccccccccccccccccccccccccccccccccccccccccc' } ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Line is too long. [96/40] RUBY expect_correction(<<~RUBY) { x: 'aaaa',#{' '} y: 'bbbbbbbbbbbbbbbbbbbbbbbbbbb', z: 'cccccccccccccccccccccccccccccccccccccccccc' } RUBY end end context 'due to a comment' do it 'registers an offense but does not correct' do expect_offense(<<~RUBY) 'aaaaaaaaaaaaaaaaaaaaa' # this comment makes the line too long ^^^^^^^^^^^^^^^^^^^^^^ Line is too long. [62/40] RUBY expect_no_corrections end end context 'when there is a space in the string' do it 'breaks the string at the space' do expect_offense(<<~RUBY) 'aaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbbbbbbb' ^^^^^^ Line is too long. [46/40] RUBY expect_correction(<<~'RUBY') 'aaaaaaaaaaaaaaaaaaaaaaaaa ' \ 'bbbbbbbbbbbbbbbbbb' RUBY end end context 'when there are multiple spaces in the string' do it 'breaks the string at the last space before the limit' do expect_offense(<<~RUBY) 'aaaaaaaaaaaaaaaaaaaaaaaaa bbbbb ccccccccc dddddddddddddddddddddd eeeeeeeeeeee' ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Line is too long. [79/40] RUBY
ruby
MIT
99fa0fdd0481910d7d052f4c2ed01ad36178f404
2026-01-04T15:37:41.211519Z
true
rubocop/rubocop
https://github.com/rubocop/rubocop/blob/99fa0fdd0481910d7d052f4c2ed01ad36178f404/spec/rubocop/cop/layout/argument_alignment_spec.rb
spec/rubocop/cop/layout/argument_alignment_spec.rb
# frozen_string_literal: true RSpec.describe RuboCop::Cop::Layout::ArgumentAlignment, :config do let(:config) do RuboCop::Config.new('Layout/ArgumentAlignment' => cop_config, 'Layout/IndentationWidth' => { 'Width' => indentation_width }) end let(:indentation_width) { 2 } context 'aligned with first argument' do let(:cop_config) { { 'EnforcedStyle' => 'with_first_argument' } } it 'registers an offense and corrects arguments with single indent' do expect_offense(<<~RUBY) function(a, if b then c else d end) ^^^^^^^^^^^^^^^^^^^^^^ Align the arguments of a method call if they span more than one line. RUBY expect_correction(<<~RUBY) function(a, if b then c else d end) RUBY end it 'registers an offense and corrects multiline missed indentation' do expect_offense(<<~RUBY) func(a, b, ^ Align the arguments of a method call if they span more than one line. c) ^ Align the arguments of a method call if they span more than one line. RUBY expect_correction(<<~RUBY) func(a, b, c) RUBY end it 'registers an offense and corrects arguments with double indent' do expect_offense(<<~RUBY) function(a, if b then c else d end) ^^^^^^^^^^^^^^^^^^^^^^ Align the arguments of a method call if they span more than one line. RUBY expect_correction(<<~RUBY) function(a, if b then c else d end) RUBY end it 'accepts multiline []= method call' do expect_no_offenses(<<~RUBY) Test.config["something"] = true RUBY end it 'accepts correctly aligned arguments' do expect_no_offenses(<<~RUBY) function(a, 0, 1, (x + y), if b then c else d end) RUBY end it 'accepts correctly aligned arguments with fullwidth characters' do expect_no_offenses(<<~RUBY) f 'Ruby', g(a, b) RUBY end it 'accepts calls that only span one line' do expect_no_offenses('find(path, s, @special[sexp[0]])') end it "doesn't get confused by a symbol argument" do expect_no_offenses(<<~RUBY) add_offense(index, MSG % kind) RUBY end it 'registers an offense and corrects when missed indentation kwargs' do expect_offense(<<~RUBY) func1(foo: 'foo', bar: 'bar', ^^^^^^^^^^ Align the arguments of a method call if they span more than one line. baz: 'baz') ^^^^^^^^^^ Align the arguments of a method call if they span more than one line. func2(do_something, foo: 'foo', ^^^^^^^^^^^ Align the arguments of a method call if they span more than one line. bar: 'bar', baz: 'baz') RUBY expect_correction(<<~RUBY) func1(foo: 'foo', bar: 'bar', baz: 'baz') func2(do_something, foo: 'foo', bar: 'bar', baz: 'baz') RUBY end it 'registers an offense and corrects splat operator' do expect_offense(<<~RUBY) func1(*a, *b, c) func2(a, *b, ^^ Align the arguments of a method call if they span more than one line. c) func3(*a) RUBY expect_correction(<<~RUBY) func1(*a, *b, c) func2(a, *b, c) func3(*a) RUBY end it "doesn't get confused by extra comma at the end" do expect_offense(<<~RUBY) func1(a, b,) ^ Align the arguments of a method call if they span more than one line. RUBY expect_correction(<<~RUBY) func1(a, b,) RUBY end it 'can handle a correctly aligned string literal as first argument' do expect_no_offenses(<<~RUBY) add_offense(x, a) RUBY end it 'can handle a string literal as other argument' do expect_no_offenses(<<~RUBY) add_offense( "", a) RUBY end it "doesn't get confused by a line break inside a parameter" do expect_no_offenses(<<~RUBY) read(path, { headers: true, converters: :numeric }) RUBY end it "doesn't get confused by symbols with embedded expressions" do expect_no_offenses('send(:"#{name}_comments_path")') end it "doesn't get confused by regexen with embedded expressions" do expect_no_offenses('a(/#{name}/)') end it 'accepts braceless hashes' do expect_no_offenses(<<~RUBY) run(collection, :entry_name => label, :paginator => paginator) RUBY end it 'accepts the first parameter being on a new row' do expect_no_offenses(<<~RUBY) match( a, b ) RUBY end it 'can handle heredoc strings' do expect_no_offenses(<<~'RUBY') class_eval(<<-EOS, __FILE__, __LINE__ + 1) def run_#{name}_callbacks(*args) a = 1 return value end EOS RUBY end it 'can handle a method call within a method call' do expect_no_offenses(<<~RUBY) a(a1, b(b1, b2), a2) RUBY end it 'can handle a call embedded in a string' do expect_no_offenses('model("#{index(name)}", child)') end it 'can handle do-end' do expect_no_offenses(<<~RUBY) run(lambda do |e| w = e['warden'] end) RUBY end it 'can handle a call with a block inside another call' do expect_no_offenses(<<~'RUBY') new(table_name, exec_query("info('#{row['name']}')").map { |col| col['name'] }) RUBY end it 'can handle a ternary condition with a block reference' do expect_no_offenses('cond ? a : func(&b)') end it 'can handle parentheses used with no arguments' do expect_no_offenses('func()') end it 'can handle a multiline hash as second parameter' do expect_no_offenses(<<~RUBY) tag(:input, { :value => value }) RUBY end it 'can handle method calls without parentheses' do expect_no_offenses('a(b c, d)') end it 'can handle other method calls without parentheses' do expect_no_offenses(<<~RUBY) chars(Unicode.apply_mapping @wrapped_string, :uppercase) RUBY end it "doesn't crash and burn when there are nested issues" do # regression test; see GH issue 2441 expect do expect_offense(<<~RUBY) build(:house, :rooms => [ ^^^^^^^^^^^ Align the arguments of a method call if they span more than one line. build(:bedroom, :bed => build(:bed, ^^^^^^^^^^^^^^^^^^^ Align the arguments of a method call if they span more than one line. :occupants => [], ^^^^^^^^^^^^^^^^^ Align the arguments of a method call if they span more than one line. :size => "king" ) ) ] ) RUBY end.not_to raise_error end context 'assigned methods' do it 'accepts the first parameter being on a new row' do expect_no_offenses(<<~RUBY) assigned_value = match( a, b, c ) RUBY end it 'accepts the first parameter being on method row' do expect_no_offenses(<<~RUBY) assigned_value = match(a, b, c ) RUBY end end it 'registers an offense and corrects multi-line outdented parameters' do expect_offense(<<~RUBY) create :transaction, :closed, account: account, ^^^^^^^^^^^^^^^^^^^^^^^^^^ Align the arguments of a method call if they span more than one line. open_price: 1.29, close_price: 1.30 RUBY expect_correction(<<~RUBY) create :transaction, :closed, account: account, open_price: 1.29, close_price: 1.30 RUBY end it 'registers an offense and correct multi-line parameters indented too far' do expect_offense(<<~RUBY) create :transaction, :closed, account: account, ^^^^^^^^^^^^^^^^^^^^^^^^^^ Align the arguments of a method call if they span more than one line. open_price: 1.29, close_price: 1.30 RUBY expect_correction(<<~RUBY) create :transaction, :closed, account: account, open_price: 1.29, close_price: 1.30 RUBY end it 'does not crash in autocorrect on dynamic string in parameter value' do expect_offense(<<~'RUBY') class MyModel < ActiveRecord::Base has_many :other_models, class_name: "legacy_name", ^^^^^^^^^^^^^^^^^^^^^^^^^^ Align the arguments of a method call if they span more than one line. order: "#{legacy_name.table_name}.published DESC" end RUBY expect_correction(<<~'RUBY') class MyModel < ActiveRecord::Base has_many :other_models, class_name: "legacy_name", order: "#{legacy_name.table_name}.published DESC" end RUBY end it 'can handle a method call using square brackets' do expect_offense(<<~RUBY) callable[ foo, bar, ^^^ Align the arguments of a method call if they span more than one line. baz ^^^ Align the arguments of a method call if they span more than one line. ] RUBY expect_correction(<<~RUBY) callable[ foo, bar, baz ] RUBY end context 'when using safe navigation operator' do it 'registers an offense and corrects arguments with single indent' do expect_offense(<<~RUBY) receiver&.function(a, if b then c else d end) ^^^^^^^^^^^^^^^^^^^^^^ Align the arguments of a method call if they span more than one line. RUBY expect_correction(<<~RUBY) receiver&.function(a, if b then c else d end) RUBY end end end context 'aligned with fixed indentation' do let(:cop_config) { { 'EnforcedStyle' => 'with_fixed_indentation' } } it 'autocorrects by outdenting when indented too far' do expect_offense(<<~RUBY) create :transaction, :closed, account: account, ^^^^^^^^^^^^^^^^^^^^ Use one level of indentation for arguments following the first line of a multi-line method call. open_price: 1.29, ^^^^^^^^^^^^^^^^^ Use one level of indentation for arguments following the first line of a multi-line method call. close_price: 1.30 ^^^^^^^^^^^^^^^^^ Use one level of indentation for arguments following the first line of a multi-line method call. RUBY expect_correction(<<~RUBY) create :transaction, :closed, account: account, open_price: 1.29, close_price: 1.30 RUBY end it 'autocorrects by indenting when not indented' do expect_offense(<<~RUBY) create :transaction, :closed, account: account, ^^^^^^^^^^^^^^^^^^^^ Use one level of indentation for arguments following the first line of a multi-line method call. open_price: 1.29, ^^^^^^^^^^^^^^^^^ Use one level of indentation for arguments following the first line of a multi-line method call. close_price: 1.30 ^^^^^^^^^^^^^^^^^ Use one level of indentation for arguments following the first line of a multi-line method call. RUBY expect_correction(<<~RUBY) create :transaction, :closed, account: account, open_price: 1.29, close_price: 1.30 RUBY end it 'registers an offense and corrects when missed indentation kwargs' do expect_offense(<<~RUBY) func1(foo: 'foo', bar: 'bar', ^^^^^^^^^^ Use one level of indentation for arguments following the first line of a multi-line method call. baz: 'baz') ^^^^^^^^^^ Use one level of indentation for arguments following the first line of a multi-line method call. func2(do_something, foo: 'foo', ^^^^^^^^^^ Use one level of indentation for arguments following the first line of a multi-line method call. bar: 'bar', ^^^^^^^^^^ Use one level of indentation for arguments following the first line of a multi-line method call. baz: 'baz') ^^^^^^^^^^ Use one level of indentation for arguments following the first line of a multi-line method call. RUBY expect_correction(<<~RUBY) func1(foo: 'foo', bar: 'bar', baz: 'baz') func2(do_something, foo: 'foo', bar: 'bar', baz: 'baz') RUBY end it 'corrects indentation for kwargs starting on same line as other args' do expect_offense(<<~RUBY) func(do_something, foo: 'foo', bar: 'bar', ^^^^^^^^^^ Use one level of indentation for arguments following the first line of a multi-line method call. baz: 'baz') ^^^^^^^^^^ Use one level of indentation for arguments following the first line of a multi-line method call. RUBY expect_correction(<<~RUBY) func(do_something, foo: 'foo', bar: 'bar', baz: 'baz') RUBY end it 'autocorrects when first line is indented' do expect_offense(<<-RUBY.strip_margin('|')) | create :transaction, :closed, | account: account, | ^^^^^^^^^^^^^^^^^^^^ Use one level of indentation for arguments following the first line of a multi-line method call. | open_price: 1.29, | ^^^^^^^^^^^^^^^^^ Use one level of indentation for arguments following the first line of a multi-line method call. | close_price: 1.30 | ^^^^^^^^^^^^^^^^^ Use one level of indentation for arguments following the first line of a multi-line method call. RUBY expect_correction(<<-RUBY.strip_margin('|')) | create :transaction, :closed, | account: account, | open_price: 1.29, | close_price: 1.30 RUBY end context 'multi-line method calls' do it 'can handle existing indentation from multi-line method calls' do expect_no_offenses(<<~RUBY) something .method_name( a, b, c ) RUBY end it 'registers offenses and corrects double indentation from relevant method' do expect_offense(<<~RUBY) something .method_name( a, ^ Use one level of indentation for arguments following the first line of a multi-line method call. b, ^ Use one level of indentation for arguments following the first line of a multi-line method call. c ^ Use one level of indentation for arguments following the first line of a multi-line method call. ) RUBY expect_correction(<<~RUBY) something .method_name( a, b, c ) RUBY end it 'does not err on method call without a method name' do expect_no_offenses(<<~RUBY) something .( a, b, c ) RUBY end it 'autocorrects relative to position of relevant method call' do expect_offense(<<-RUBY.strip_margin('|')) | something | .method_name( | a, | ^ Use one level of indentation for arguments following the first line of a multi-line method call. | b, | ^ Use one level of indentation for arguments following the first line of a multi-line method call. | c | ^ Use one level of indentation for arguments following the first line of a multi-line method call. | ) RUBY expect_correction(<<-RUBY.strip_margin('|')) | something | .method_name( | a, | b, | c | ) RUBY end it 'can handle a method call using square brackets' do expect_offense(<<~RUBY) callable[ foo, ^^^ Use one level of indentation for arguments following the first line of a multi-line method call. bar, ^^^ Use one level of indentation for arguments following the first line of a multi-line method call. baz ^^^ Use one level of indentation for arguments following the first line of a multi-line method call. ] RUBY expect_correction(<<~RUBY) callable[ foo, bar, baz ] RUBY end end context 'assigned methods' do context 'with IndentationWidth:Width set to 4' do let(:indentation_width) { 4 } it 'accepts the first parameter being on a new row' do expect_no_offenses(<<~RUBY) assigned_value = match( a, b, c ) RUBY end it 'accepts the first parameter being on method row' do expect_no_offenses(<<~RUBY) assigned_value = match(a, b, c ) RUBY end it 'autocorrects even when first argument is in wrong position' do expect_offense(<<-RUBY.strip_margin('|')) | assigned_value = match( | a, | ^ Use one level of indentation for arguments following the first line of a multi-line method call. | b, | ^ Use one level of indentation for arguments following the first line of a multi-line method call. | c | ^ Use one level of indentation for arguments following the first line of a multi-line method call. | ) RUBY expect_correction(<<-RUBY.strip_margin('|')) | assigned_value = match( | a, | b, | c | ) RUBY end end context 'with ArgumentAlignment:IndentationWidth set to 4' do let(:config) do RuboCop::Config.new('Layout/ArgumentAlignment' => cop_config.merge('IndentationWidth' => 4)) end it 'accepts the first parameter being on a new row' do expect_no_offenses(<<~RUBY) assigned_value = match( a, b, c ) RUBY end it 'accepts the first parameter being on method row' do expect_no_offenses(<<~RUBY) assigned_value = match(a, b, c ) RUBY end end end it 'does not register an offense when using aligned braced hash as an argument' do expect_no_offenses(<<~RUBY) do_something( { foo: 'bar', baz: 'qux' } ) RUBY end end end
ruby
MIT
99fa0fdd0481910d7d052f4c2ed01ad36178f404
2026-01-04T15:37:41.211519Z
false
rubocop/rubocop
https://github.com/rubocop/rubocop/blob/99fa0fdd0481910d7d052f4c2ed01ad36178f404/spec/rubocop/cop/layout/empty_line_after_multiline_condition_spec.rb
spec/rubocop/cop/layout/empty_line_after_multiline_condition_spec.rb
# frozen_string_literal: true RSpec.describe RuboCop::Cop::Layout::EmptyLineAfterMultilineCondition, :config do it 'registers an offense when no new line after `if` with multiline condition' do expect_offense(<<~RUBY) if multiline && ^^^^^^^^^^^^ Use empty line after multiline condition. condition do_something end RUBY end it 'does not register an offense when new line after `if` with multiline condition' do expect_no_offenses(<<~RUBY) if multiline && condition do_something end RUBY end it 'does not register an offense for `if` with single line condition' do expect_no_offenses(<<~RUBY) if singleline do_something end RUBY end it 'registers an offense when no new line after modifier `if` with multiline condition' do expect_offense(<<~RUBY) do_something if multiline && ^^^^^^^^^^^^ Use empty line after multiline condition. condition do_something_else RUBY end it 'does not register an offense when new line after modifier `if` with multiline condition' do expect_no_offenses(<<~RUBY) do_something if multiline && condition do_something_else RUBY end it 'does not register an offense when modifier `if` with multiline condition' \ 'is the last child of its parent' do expect_no_offenses(<<~RUBY) def m do_something if multiline && condition end RUBY end it 'does not register an offense when `if` at the top level' do expect_no_offenses(<<~RUBY) do_something if condition RUBY end it 'registers an offense when no new line after `elsif` with multiline condition' do expect_offense(<<~RUBY) if condition do_something elsif multiline && ^^^^^^^^^^^^ Use empty line after multiline condition. condition do_something_else end RUBY end it 'does not register an offense when new line after `elsif` with multiline condition' do expect_no_offenses(<<~RUBY) if condition do_something elsif multiline && condition do_something_else end RUBY end it 'registers an offense when no new line after `while` with multiline condition' do expect_offense(<<~RUBY) while multiline && ^^^^^^^^^^^^ Use empty line after multiline condition. condition do_something end RUBY end it 'registers an offense when no new line after `until` with multiline condition' do expect_offense(<<~RUBY) until multiline && ^^^^^^^^^^^^ Use empty line after multiline condition. condition do_something end RUBY end it 'does not register an offense when new line after `while` with multiline condition' do expect_no_offenses(<<~RUBY) while multiline && condition do_something end RUBY end it 'does not register an offense for `while` with single line condition' do expect_no_offenses(<<~RUBY) while singleline do_something end RUBY end it 'registers an offense when no new line after modifier `while` with multiline condition' do expect_offense(<<~RUBY) begin do_something end while multiline && ^^^^^^^^^^^^ Use empty line after multiline condition. condition do_something_else RUBY end it 'does not register an offense when new line after modifier `while` with multiline condition' do expect_no_offenses(<<~RUBY) begin do_something end while multiline && condition do_something_else RUBY end it 'does not register an offense when modifier `while` with multiline condition' \ 'is the last child of its parent' do expect_no_offenses(<<~RUBY) def m begin do_something end while multiline && condition end RUBY end it 'registers an offense when no new line after `when` with multiline condition' do expect_offense(<<~RUBY) case x when foo, ^^^^^^^^^ Use empty line after multiline condition. bar do_something end RUBY end it 'does not register an offense when new line after `when` with multiline condition' do expect_no_offenses(<<~RUBY) case x when foo, bar do_something end RUBY end it 'does not register an offense for `when` with singleline condition' do expect_no_offenses(<<~RUBY) case x when foo, bar do_something end RUBY end it 'registers an offense when no new line after `rescue` with multiline exceptions' do expect_offense(<<~RUBY) begin do_something rescue FooError, ^^^^^^^^^^^^^^^^ Use empty line after multiline condition. BarError handle_error end RUBY end it 'does not register an offense when new line after `rescue` with multiline exceptions' do expect_no_offenses(<<~RUBY) begin do_something rescue FooError, BarError handle_error end RUBY end it 'does not register an offense for `rescue` with singleline exceptions' do expect_no_offenses(<<~RUBY) begin do_something rescue FooError handle_error end RUBY end end
ruby
MIT
99fa0fdd0481910d7d052f4c2ed01ad36178f404
2026-01-04T15:37:41.211519Z
false
rubocop/rubocop
https://github.com/rubocop/rubocop/blob/99fa0fdd0481910d7d052f4c2ed01ad36178f404/spec/rubocop/cop/layout/empty_lines_after_module_inclusion_spec.rb
spec/rubocop/cop/layout/empty_lines_after_module_inclusion_spec.rb
# frozen_string_literal: true RSpec.describe RuboCop::Cop::Layout::EmptyLinesAfterModuleInclusion, :config do described_class::MODULE_INCLUSION_METHODS.each do |method| it "registers an offense and corrects for code that immediately follows #{method}" do expect_offense(<<~RUBY, method: method) #{method} Foo ^{method}^^^^ Add an empty line after module inclusion. def do_something end RUBY expect_correction(<<~RUBY) #{method} Foo def do_something end RUBY end it "registers an offense and corrects for code that immediately follows #{method} with comment" do expect_offense(<<~RUBY, method: method) #{method} Foo # my comment ^{method}^^^^ Add an empty line after module inclusion. def do_something end RUBY expect_correction(<<~RUBY) #{method} Foo # my comment def do_something end RUBY end it "registers an offense and corrects for code that immediately follows #{method} and comment line" do expect_offense(<<~RUBY, method: method) #{method} Foo ^{method}^^^^ Add an empty line after module inclusion. # my comment def do_something end RUBY expect_correction(<<~RUBY) #{method} Foo # my comment def do_something end RUBY end it "registers an offense and corrects for #{method} and `rubocop:enable` comment line" do expect_offense(<<~RUBY, method: method) # rubocop:disable Department/Cop #{method} Foo ^{method}^^^^ Add an empty line after module inclusion. # rubocop:enable Department/Cop def do_something end RUBY expect_correction(<<~RUBY) # rubocop:disable Department/Cop #{method} Foo # rubocop:enable Department/Cop def do_something end RUBY end it "registers an offense and corrects for #{method} and `rubocop:enable` comment line and other comment" do expect_offense(<<~RUBY, method: method) # rubocop:disable Department/Cop #{method} Foo ^{method}^^^^ Add an empty line after module inclusion. # rubocop:enable Department/Cop # comment def do_something end RUBY expect_correction(<<~RUBY) # rubocop:disable Department/Cop #{method} Foo # rubocop:enable Department/Cop # comment def do_something end RUBY end it "registers an offense and corrects when #{method} has multiple arguments" do expect_offense(<<~RUBY, method: method) class Foo #{method} Bar, Baz ^{method}^^^^^^^^^ Add an empty line after module inclusion. def do_something end end RUBY expect_correction(<<~RUBY) class Foo #{method} Bar, Baz def do_something end end RUBY end it "registers an offense and corrects for code that immediately follows #{method} inside a class" do expect_offense(<<~RUBY, method: method) class Bar #{method} Foo ^{method}^^^^ Add an empty line after module inclusion. def do_something end end RUBY expect_correction(<<~RUBY) class Bar #{method} Foo def do_something end end RUBY end it "registers an offense and corrects for code that immediately follows #{method} inside Class.new" do expect_offense(<<~RUBY, method: method) Class.new do #{method} Foo ^{method}^^^^ Add an empty line after module inclusion. def do_something end end RUBY expect_correction(<<~RUBY) Class.new do #{method} Foo def do_something end end RUBY end it 'does not register an offense for #{method} separated from the code and `rubocop:enable` ' \ 'comment line with a newline' do expect_no_offenses(<<~RUBY) # rubocop:disable Department/Cop #{method} Foo # rubocop:enable Department/Cop def do_something end RUBY end it "does not register an offense for #{method} separated from the code with a newline" do expect_no_offenses(<<~RUBY) #{method} Foo def do_something end RUBY end it "does not register an offense for multiple #{method} methods separated from the code with a newline" do expect_no_offenses(<<~RUBY) #{method} Foo #{method} Foo def do_something end RUBY end it "does not register an offense when #{method} is used in class definition" do expect_no_offenses(<<~RUBY) module Baz class Bar #{method} Foo end end RUBY end it "does not register an offense when #{method} is used in Class.new" do expect_no_offenses(<<~RUBY) Class.new do #{method} Foo end RUBY end it "does not register an offense when #{method} is used with block method" do expect_no_offenses(<<~RUBY) #{method} Module.new do |arg| do_something(arg) end RUBY end it "does not register an offense when #{method} is used with numbered block method", :ruby27 do expect_no_offenses(<<~RUBY) #{method} Module.new do do_something(_1) end RUBY end it "does not register an offense when #{method} is used with `it` block method", :ruby34 do expect_no_offenses(<<~RUBY) #{method} Module.new do do_something(it) end RUBY end it "does not register an offense when using #{method} in `if` ... `else` branches" do expect_no_offenses(<<~RUBY) if condition #{method} Foo else do_something end RUBY end it "does not register an offense where #{method} is the last line" do expect_no_offenses("#{method} Foo") end end it 'does not register an offense when there are multiple grouped module inclusion methods' do expect_no_offenses(<<~RUBY) module Foo module Bar extend Baz prepend Baz include Baz end end RUBY end it 'does not register an offense for RSpec matcher `include`' do expect_no_offenses(<<~RUBY) it 'something' do expect(foo).to include(bar), "baz" end RUBY end it 'does not register an offense when `include` has zero arguments' do expect_no_offenses(<<~RUBY) class Foo includes = [include, sdk_include].compact.map do |inc| inc + "blah" end.join(' ') end RUBY end it 'does not register an offense when module inclusion is called with modifier' do expect_no_offenses(<<~RUBY) class Foo include Bar include Baz if condition include Qux end RUBY end end
ruby
MIT
99fa0fdd0481910d7d052f4c2ed01ad36178f404
2026-01-04T15:37:41.211519Z
false
rubocop/rubocop
https://github.com/rubocop/rubocop/blob/99fa0fdd0481910d7d052f4c2ed01ad36178f404/spec/rubocop/cop/layout/empty_lines_around_exception_handling_keywords_spec.rb
spec/rubocop/cop/layout/empty_lines_around_exception_handling_keywords_spec.rb
# frozen_string_literal: true RSpec.describe RuboCop::Cop::Layout::EmptyLinesAroundExceptionHandlingKeywords, :config do let(:message) { '^{} Extra empty line detected' } shared_examples 'accepts' do |name, code| it "accepts #{name}" do expect_no_offenses(code) end end it 'registers an offense for above rescue keyword with a blank' do expect_offense(<<~RUBY) begin f1 #{message} before the `rescue`. rescue f2 end RUBY expect_correction(<<~RUBY) begin f1 rescue f2 end RUBY end it 'registers an offense for rescue section starting with a blank' do expect_offense(<<~RUBY) begin f1 rescue #{message} after the `rescue`. f2 end RUBY expect_correction(<<~RUBY) begin f1 rescue f2 end RUBY end it 'registers an offense for rescue section ending with a blank' do expect_offense(<<~RUBY) begin f1 rescue f2 #{message} before the `else`. else f3 end RUBY expect_correction(<<~RUBY) begin f1 rescue f2 else f3 end RUBY end it 'registers an offense for rescue section ending for method definition a blank' do expect_offense(<<~RUBY) def foo f1 rescue f2 #{message} before the `else`. else f3 end RUBY expect_correction(<<~RUBY) def foo f1 rescue f2 else f3 end RUBY end it 'registers an offense when there is a blank line above `rescue` keyword in a block', :ruby25 do expect_offense(<<~RUBY) foo do f1 #{message} before the `rescue`. rescue f2 else f3 end RUBY expect_correction(<<~RUBY) foo do f1 rescue f2 else f3 end RUBY end it 'registers an offense when `rescue` section starts with a blank line in a block', :ruby25 do expect_offense(<<~RUBY) foo do f1 rescue #{message} after the `rescue`. f2 else f3 end RUBY expect_correction(<<~RUBY) foo do f1 rescue f2 else f3 end RUBY end it 'registers an offense when `rescue` section ends with a blank line in a block', :ruby25 do expect_offense(<<~RUBY) foo do f1 rescue f2 #{message} before the `else`. else f3 end RUBY expect_correction(<<~RUBY) foo do f1 rescue f2 else f3 end RUBY end it 'registers an offense when there is a blank line above `rescue` keyword in a numbered block', :ruby27 do expect_offense(<<~RUBY) foo do f1(_1) #{message} before the `rescue`. rescue f2 else f3 end RUBY expect_correction(<<~RUBY) foo do f1(_1) rescue f2 else f3 end RUBY end it 'registers an offense when `rescue` section starts with a blank line in a numbered block', :ruby27 do expect_offense(<<~RUBY) foo do f1(_1) rescue #{message} after the `rescue`. f2 else f3 end RUBY expect_correction(<<~RUBY) foo do f1(_1) rescue f2 else f3 end RUBY end it 'registers an offense when `rescue` section ends with a blank line in a numbered block', :ruby27 do expect_offense(<<~RUBY) foo do f1(_1) rescue f2 #{message} before the `else`. else f3 end RUBY expect_correction(<<~RUBY) foo do f1(_1) rescue f2 else f3 end RUBY end it_behaves_like 'accepts', 'no empty line', <<~RUBY begin f1 rescue f2 else f3 ensure f4 end RUBY it_behaves_like 'accepts', 'empty lines around begin body', <<~RUBY begin f1 end RUBY it_behaves_like 'accepts', 'empty begin', <<~RUBY begin end RUBY it_behaves_like 'accepts', 'empty method definition', <<~RUBY def foo end RUBY it_behaves_like 'accepts', '`begin` and `rescue` are on the same line', <<~RUBY begin; foo; rescue => e; end RUBY it_behaves_like 'accepts', '`rescue` and `end` are on the same line', <<~RUBY begin foo rescue => e; end RUBY it_behaves_like 'accepts', 'last `rescue` and `end` are on the same line', <<~RUBY begin foo rescue => x rescue => y; end RUBY it_behaves_like 'accepts', '`def` and `rescue` are on the same line', <<~RUBY def do_something; foo; rescue => e; end RUBY it_behaves_like 'accepts', '`ensure` and `end` are on the same line', <<~RUBY def do_something ensure end RUBY it_behaves_like 'accepts', '`else` and `end` are on the same line', <<~RUBY def do_something rescue else end RUBY it_behaves_like 'accepts', '`ensure` body expression and `end` are on the same line', <<~RUBY def do_something foo ensure bar end RUBY it_behaves_like 'accepts', '`else` body expression and `end` are on the same line', <<~RUBY def do_something rescue else foo end RUBY it 'with complex begin-end - registers many offenses' do expect_offense(<<~RUBY) begin do_something1 #{message} before the `rescue`. rescue RuntimeError #{message} after the `rescue`. do_something2 #{message} before the `rescue`. rescue ArgumentError => ex #{message} after the `rescue`. do_something3 #{message} before the `rescue`. rescue #{message} after the `rescue`. do_something3 #{message} before the `else`. else #{message} after the `else`. do_something4 #{message} before the `ensure`. ensure #{message} after the `ensure`. do_something4 end RUBY expect_correction(<<~RUBY) begin do_something1 rescue RuntimeError do_something2 rescue ArgumentError => ex do_something3 rescue do_something3 else do_something4 ensure do_something4 end RUBY end it 'with complex method definition - registers many offenses' do expect_offense(<<~RUBY) def foo do_something1 #{message} before the `rescue`. rescue RuntimeError #{message} after the `rescue`. do_something2 #{message} before the `rescue`. rescue ArgumentError => ex #{message} after the `rescue`. do_something3 #{message} before the `rescue`. rescue #{message} after the `rescue`. do_something3 #{message} before the `else`. else #{message} after the `else`. do_something4 #{message} before the `ensure`. ensure #{message} after the `ensure`. do_something4 end RUBY expect_correction(<<~RUBY) def foo do_something1 rescue RuntimeError do_something2 rescue ArgumentError => ex do_something3 rescue do_something3 else do_something4 ensure do_something4 end RUBY end end
ruby
MIT
99fa0fdd0481910d7d052f4c2ed01ad36178f404
2026-01-04T15:37:41.211519Z
false
rubocop/rubocop
https://github.com/rubocop/rubocop/blob/99fa0fdd0481910d7d052f4c2ed01ad36178f404/spec/rubocop/cop/layout/space_after_method_name_spec.rb
spec/rubocop/cop/layout/space_after_method_name_spec.rb
# frozen_string_literal: true RSpec.describe RuboCop::Cop::Layout::SpaceAfterMethodName, :config do it 'registers an offense and corrects def with space before the parenthesis' do expect_offense(<<~RUBY) def func (x) ^ Do not put a space between a method name and the opening parenthesis. a end RUBY expect_correction(<<~RUBY) def func(x) a end RUBY end it 'registers an offense and corrects class def with space before parenthesis' do expect_offense(<<~RUBY) def self.func (x) ^ Do not put a space between a method name and the opening parenthesis. a end RUBY expect_correction(<<~RUBY) def self.func(x) a end RUBY end it 'registers an offense and corrects assignment def with space before parenthesis' do expect_offense(<<~RUBY) def func= (x) ^ Do not put a space between a method name and the opening parenthesis. a end RUBY expect_correction(<<~RUBY) def func=(x) a end RUBY end it 'accepts a def without arguments' do expect_no_offenses(<<~RUBY) def func a end RUBY end it 'accepts a defs without arguments' do expect_no_offenses(<<~RUBY) def self.func a end RUBY end it 'accepts a def with arguments but no parentheses' do expect_no_offenses(<<~RUBY) def func x a end RUBY end it 'accepts class method def with arguments but no parentheses' do expect_no_offenses(<<~RUBY) def self.func x a end RUBY end it 'accepts an assignment def with arguments but no parentheses' do expect_no_offenses(<<~RUBY) def func= x a end RUBY end end
ruby
MIT
99fa0fdd0481910d7d052f4c2ed01ad36178f404
2026-01-04T15:37:41.211519Z
false
rubocop/rubocop
https://github.com/rubocop/rubocop/blob/99fa0fdd0481910d7d052f4c2ed01ad36178f404/spec/rubocop/cop/layout/space_after_colon_spec.rb
spec/rubocop/cop/layout/space_after_colon_spec.rb
# frozen_string_literal: true RSpec.describe RuboCop::Cop::Layout::SpaceAfterColon, :config do it 'registers an offense and corrects colon without space after it' do expect_offense(<<~RUBY) {a:3} ^ Space missing after colon. RUBY expect_correction(<<~RUBY) {a: 3} RUBY end it 'accepts colons in symbols' do expect_no_offenses('x = :a') end it 'accepts colon in ternary followed by space' do expect_no_offenses('x = w ? a : b') end it 'accepts hashes with a space after colons' do expect_no_offenses('{a: 3}') end it 'accepts hash rockets' do expect_no_offenses('x = {"a"=>1}') end it 'accepts if' do expect_no_offenses(<<~RUBY) x = if w a end RUBY end it 'accepts colons in strings' do expect_no_offenses("str << ':'") end it 'accepts required keyword arguments' do expect_no_offenses(<<~RUBY) def f(x:, y:) end RUBY end it 'accepts colons denoting required keyword argument' do expect_no_offenses(<<~RUBY) def initialize(table:, nodes:) end RUBY end it 'registers an offense and corrects a keyword optional argument without a space' do expect_offense(<<~RUBY) def m(var:1, other_var: 2) ^ Space missing after colon. end RUBY expect_correction(<<~RUBY) def m(var: 1, other_var: 2) end RUBY end context 'Ruby >= 3.1', :ruby31 do it 'does not register an offense colon without space after it when using hash value omission' do expect_no_offenses('{x:, y:}') end it 'accepts colons denoting hash value omission argument' do expect_no_offenses(<<~RUBY) foo(table:, nodes:) RUBY end end end
ruby
MIT
99fa0fdd0481910d7d052f4c2ed01ad36178f404
2026-01-04T15:37:41.211519Z
false
rubocop/rubocop
https://github.com/rubocop/rubocop/blob/99fa0fdd0481910d7d052f4c2ed01ad36178f404/spec/rubocop/cop/layout/empty_lines_spec.rb
spec/rubocop/cop/layout/empty_lines_spec.rb
# frozen_string_literal: true RSpec.describe RuboCop::Cop::Layout::EmptyLines, :config do it 'registers an offense for consecutive empty lines' do expect_offense(<<~RUBY) test = 5 ^{} Extra blank line detected. top RUBY expect_correction(<<~RUBY) test = 5 top RUBY end it 'does not register an offense when there are no tokens' do expect_no_offenses('#comment') end it 'does not register an offense for comments' do expect_no_offenses(<<~RUBY) test #comment top RUBY end it 'does not register an offense for empty lines in a string' do expect_no_offenses(<<~RUBY) result = "test string" RUBY end it 'does not register an offense for heredocs with empty lines inside' do expect_no_offenses(<<~RUBY) str = <<-TEXT line 1 line 2 TEXT puts str RUBY end end
ruby
MIT
99fa0fdd0481910d7d052f4c2ed01ad36178f404
2026-01-04T15:37:41.211519Z
false
rubocop/rubocop
https://github.com/rubocop/rubocop/blob/99fa0fdd0481910d7d052f4c2ed01ad36178f404/spec/rubocop/cop/layout/rescue_ensure_alignment_spec.rb
spec/rubocop/cop/layout/rescue_ensure_alignment_spec.rb
# frozen_string_literal: true RSpec.describe RuboCop::Cop::Layout::RescueEnsureAlignment, :config do it 'accepts the modifier form' do expect_no_offenses('test rescue nil') end context 'rescue with begin' do it 'registers an offense' do expect_offense(<<~RUBY) begin something rescue ^^^^^^ `rescue` at 3, 4 is not aligned with `begin` at 1, 0. error end RUBY expect_correction(<<~RUBY) begin something rescue error end RUBY end context 'as RHS of assignment' do let(:cop_config) { { 'EnforcedStyle' => 'require_parentheses' } } context '`Layout/BeginEndAlignment` cop is not enabled' do let(:other_cops) do { 'Layout/BeginEndAlignment' => { 'Enabled' => false, 'EnforcedStyleAlignWith' => 'start_of_line' } } end it 'accepts multi-line, aligned' do expect_no_offenses(<<~RUBY) x ||= begin 1 rescue 2 end RUBY end it 'accepts multi-line, indented' do expect_no_offenses(<<~RUBY) x ||= begin 1 rescue 2 end RUBY end it 'registers an offense and corrects for incorrect alignment' do expect_offense(<<~RUBY) x ||= begin 1 rescue ^^^^^^ `rescue` at 3, 0 is not aligned with `begin` at 1, 6. 2 end RUBY # Except for `rescue`, it will be aligned by `Layout/BeginEndAlignment` autocorrection. expect_correction(<<~RUBY) x ||= begin 1 rescue 2 end RUBY end end context 'when `EnforcedStyleAlignWith: start_of_line` of `Layout/BeginEndAlignment` cop' do let(:other_cops) do { 'Layout/BeginEndAlignment' => { 'EnforcedStyleAlignWith' => 'start_of_line' } } end it 'accepts multi-line, aligned' do expect_no_offenses(<<~RUBY) x ||= begin 1 rescue 2 end RUBY end it 'accepts multi-line, indented' do expect_no_offenses(<<~RUBY) x ||= begin 1 rescue 2 end RUBY end it 'registers an offense and corrects for incorrect alignment' do expect_offense(<<~RUBY) x ||= begin 1 rescue ^^^^^^ `rescue` at 3, 6 is not aligned with `x ||= begin` at 1, 0. 2 end RUBY # Except for `rescue`, it will be aligned by `Layout/BeginEndAlignment` autocorrection. expect_correction(<<~RUBY) x ||= begin 1 rescue 2 end RUBY end end context 'when `EnforcedStyleAlignWith: begin` of `Layout/BeginEndAlignment` cop' do let(:other_cops) do { 'Layout/BeginEndAlignment' => { 'EnforcedStyleAlignWith' => 'begin' } } end it 'accepts multi-line, aligned' do expect_no_offenses(<<~RUBY) x ||= begin 1 rescue 2 end RUBY end it 'accepts multi-line, indented' do expect_no_offenses(<<~RUBY) x ||= begin 1 rescue 2 end RUBY end it 'registers an offense and corrects for incorrect alignment' do expect_offense(<<~RUBY) x ||= begin 1 rescue ^^^^^^ `rescue` at 3, 0 is not aligned with `begin` at 1, 6. 2 end RUBY # Except for `rescue`, it will be aligned by `Layout/BeginEndAlignment` autocorrection. expect_correction(<<~RUBY) x ||= begin 1 rescue 2 end RUBY end end end end context 'rescue with def' do it 'registers an offense' do expect_offense(<<~RUBY) def test something rescue ^^^^^^ `rescue` at 3, 4 is not aligned with `def test` at 1, 0. error end RUBY expect_correction(<<~RUBY) def test something rescue error end RUBY end end context 'rescue with defs' do it 'registers an offense' do expect_offense(<<~RUBY) def Test.test something rescue ^^^^^^ `rescue` at 3, 4 is not aligned with `def Test.test` at 1, 0. error end RUBY expect_correction(<<~RUBY) def Test.test something rescue error end RUBY end end context 'rescue with class' do it 'registers an offense when rescue used with class' do expect_offense(<<~RUBY) class C something rescue ^^^^^^ `rescue` at 3, 4 is not aligned with `class C` at 1, 0. error end RUBY expect_correction(<<~RUBY) class C something rescue error end RUBY end end context 'rescue with module' do it 'registers an offense when rescue used with module' do expect_offense(<<~RUBY) module M something rescue ^^^^^^ `rescue` at 3, 4 is not aligned with `module M` at 1, 0. error end RUBY expect_correction(<<~RUBY) module M something rescue error end RUBY end end context 'rescue with self class' do it 'registers an offense when rescue used with class' do expect_offense(<<~RUBY) class << self something rescue ^^^^^^ `rescue` at 3, 4 is not aligned with `class << self` at 1, 0. error end RUBY expect_correction(<<~RUBY) class << self something rescue error end RUBY end end context 'ensure with begin' do it 'registers an offense when ensure used with begin' do expect_offense(<<~RUBY) begin something ensure ^^^^^^ `ensure` at 3, 4 is not aligned with `begin` at 1, 0. error end RUBY expect_correction(<<~RUBY) begin something ensure error end RUBY end end context 'ensure with def' do it 'registers an offense' do expect_offense(<<~RUBY) def test something ensure ^^^^^^ `ensure` at 3, 4 is not aligned with `def test` at 1, 0. error end RUBY expect_correction(<<~RUBY) def test something ensure error end RUBY end end context 'ensure with defs' do it 'registers an offense' do expect_offense(<<~RUBY) def Test.test something ensure ^^^^^^ `ensure` at 3, 4 is not aligned with `def Test.test` at 1, 0. error end RUBY expect_correction(<<~RUBY) def Test.test something ensure error end RUBY end end context 'ensure with class' do it 'registers an offense' do expect_offense(<<~RUBY) class C something ensure ^^^^^^ `ensure` at 3, 4 is not aligned with `class C` at 1, 0. error end RUBY expect_correction(<<~RUBY) class C something ensure error end RUBY end end context 'ensure with module' do it 'registers an offense when ensure used with module' do expect_offense(<<~RUBY) module M something ensure ^^^^^^ `ensure` at 3, 4 is not aligned with `module M` at 1, 0. error end RUBY expect_correction(<<~RUBY) module M something ensure error end RUBY end end context 'ensure with self class' do it 'registers an offense when ensure used with self class' do expect_offense(<<~RUBY) class << self something ensure ^^^^^^ `ensure` at 3, 4 is not aligned with `class << self` at 1, 0. error end RUBY expect_correction(<<~RUBY) class << self something ensure error end RUBY end end it 'accepts end being misaligned' do expect_no_offenses(<<~RUBY) def method1 'foo' end def method2 'bar' rescue 'baz' end RUBY end it 'accepts rescue and ensure on the same line' do expect_no_offenses('begin; puts 1; rescue; ensure; puts 2; end') end it 'accepts correctly aligned rescue' do expect_no_offenses(<<~RUBY) begin something rescue error end RUBY end it 'accepts correctly aligned ensure' do expect_no_offenses(<<~RUBY) begin something ensure error end RUBY end it 'accepts correctly aligned rescue/ensure with def' do expect_no_offenses(<<~RUBY) def foo something rescue StandardError handle_error ensure error end RUBY end it 'accepts correctly aligned rescue/ensure with def with no body' do expect_no_offenses(<<~RUBY) def foo rescue StandardError handle_error ensure error end RUBY end it 'accepts correctly aligned rescue in assigned begin-end block' do expect_no_offenses(<<~RUBY) foo = begin bar rescue BazError qux end RUBY end it 'accepts aligned rescue in do-end block' do expect_no_offenses(<<~RUBY) [1, 2, 3].each do |el| el.to_s rescue StandardError => _exception next end RUBY end it 'accepts aligned rescue in do-end block with `.()` call' do expect_no_offenses(<<~RUBY) foo.() do |el| el.to_s rescue StandardError => _exception next end RUBY end it 'accepts aligned `rescue` with do-end block that line break with leading dot for method calls' do expect_no_offenses(<<~RUBY) [1, 2, 3] .each do |el| el.to_s rescue StandardError => _exception next end RUBY end it 'registers an offense and corrects indented `rescue` with do-end block that line break with leading dot for method calls' do expect_offense(<<~RUBY) [1, 2, 3] .each do |el| el.to_s rescue StandardError => _exception ^^^^^^ `rescue` at 4, 6 is not aligned with `.each do` at 2, 2. next end RUBY expect_correction(<<~RUBY) [1, 2, 3] .each do |el| el.to_s rescue StandardError => _exception next end RUBY end it 'registers an offense and corrects unindented `rescue` with do-end block that line break with leading dot for method calls' do expect_offense(<<~RUBY) [1, 2, 3] .each do |el| el.to_s rescue StandardError => _exception ^^^^^^ `rescue` at 4, 0 is not aligned with `.each do` at 2, 2. next end RUBY expect_correction(<<~RUBY) [1, 2, 3] .each do |el| el.to_s rescue StandardError => _exception next end RUBY end it 'accepts aligned `rescue` with do-end block that line break with trailing dot for method calls' do expect_no_offenses(<<~RUBY) [1, 2, 3]. each do |el| el.to_s rescue StandardError => _exception next end RUBY end it 'registers an offense and corrects indented `rescue` with do-end block that line break with trailing dot for method calls' do expect_offense(<<~RUBY) [1, 2, 3]. each do |el| el.to_s rescue StandardError => _exception ^^^^^^ `rescue` at 4, 6 is not aligned with `each do` at 2, 2. next end RUBY expect_correction(<<~RUBY) [1, 2, 3]. each do |el| el.to_s rescue StandardError => _exception next end RUBY end it 'registers an offense and corrects unindented `rescue` with do-end block that line break with trailing dot for method calls' do expect_offense(<<~RUBY) [1, 2, 3]. each do |el| el.to_s rescue StandardError => _exception ^^^^^^ `rescue` at 4, 0 is not aligned with `each do` at 2, 2. next end RUBY expect_correction(<<~RUBY) [1, 2, 3]. each do |el| el.to_s rescue StandardError => _exception next end RUBY end it 'accepts aligned `ensure` with do-end block that line break with leading dot for method calls' do expect_no_offenses(<<~RUBY) [1, 2, 3] .each do |el| el.to_s ensure next end RUBY end it 'registers an offense and corrects indented `ensure` with do-end block that line break with leading dot for method calls' do expect_offense(<<~RUBY) [1, 2, 3] .each do |el| el.to_s ensure ^^^^^^ `ensure` at 4, 6 is not aligned with `.each do` at 2, 2. next end RUBY expect_correction(<<~RUBY) [1, 2, 3] .each do |el| el.to_s ensure next end RUBY end it 'registers an offense and corrects unindented `ensure` with do-end block that line break with leading dot for method calls' do expect_offense(<<~RUBY) [1, 2, 3] .each do |el| el.to_s ensure ^^^^^^ `ensure` at 4, 0 is not aligned with `.each do` at 2, 2. next end RUBY expect_correction(<<~RUBY) [1, 2, 3] .each do |el| el.to_s ensure next end RUBY end it 'accepts aligned `ensure` with do-end block that line break with trailing dot for method calls' do expect_no_offenses(<<~RUBY) [1, 2, 3]. each do |el| el.to_s ensure next end RUBY end it 'registers an offense and corrects indented `ensure` with do-end block that line break with trailing dot for method calls' do expect_offense(<<~RUBY) [1, 2, 3]. each do |el| el.to_s ensure ^^^^^^ `ensure` at 4, 6 is not aligned with `each do` at 2, 2. next end RUBY expect_correction(<<~RUBY) [1, 2, 3]. each do |el| el.to_s ensure next end RUBY end it 'registers an offense and corrects unindented `ensure` with do-end block that line break with trailing dot for method calls' do expect_offense(<<~RUBY) [1, 2, 3]. each do |el| el.to_s ensure ^^^^^^ `ensure` at 4, 0 is not aligned with `each do` at 2, 2. next end RUBY expect_correction(<<~RUBY) [1, 2, 3]. each do |el| el.to_s ensure next end RUBY end it 'accepts aligned rescue do-end block assigned to local variable' do expect_no_offenses(<<~RUBY) result = [1, 2, 3].map do |el| el.to_s rescue StandardError => _exception next end RUBY end it 'accepts aligned rescue in do-end block assigned to instance variable' do expect_no_offenses(<<~RUBY) @instance = [].map do |_| rescue StandardError => _ end RUBY end it 'accepts aligned rescue in do-end block assigned to class variable' do expect_no_offenses(<<~RUBY) @@class = [].map do |_| rescue StandardError => _ end RUBY end it 'accepts aligned rescue in do-end block assigned to global variable' do expect_no_offenses(<<~RUBY) $global = [].map do |_| rescue StandardError => _ end RUBY end it 'accepts aligned rescue in do-end block assigned to class' do expect_no_offenses(<<~RUBY) CLASS = [].map do |_| rescue StandardError => _ end RUBY end it 'accepts aligned rescue in do-end block on multi-assignment' do expect_no_offenses(<<~RUBY) a, b = [].map do |_| rescue StandardError => _ end RUBY end it 'accepts aligned rescue in do-end block on operation assignment' do expect_no_offenses(<<~RUBY) a += [].map do |_| rescue StandardError => _ end RUBY end it 'accepts aligned rescue in do-end block on and-assignment' do expect_no_offenses(<<~RUBY) a &&= [].map do |_| rescue StandardError => _ end RUBY end it 'accepts aligned rescue in do-end block on or-assignment' do expect_no_offenses(<<~RUBY) a ||= [].map do |_| rescue StandardError => _ end RUBY end it 'accepts aligned rescue in assigned do-end block starting on newline' do expect_no_offenses(<<~RUBY) valid = proc do |bar| baz rescue qux end RUBY end it 'accepts aligned rescue in do-end block in a method' do expect_no_offenses(<<~RUBY) def foo [1, 2, 3].each do |el| el.to_s rescue StandardError => _exception next end end RUBY end context 'rescue with do-end block' do it 'registers an offense' do expect_offense(<<~RUBY) def foo [1, 2, 3].each do |el| el.to_s rescue StandardError => _exception ^^^^^^ `rescue` at 4, 0 is not aligned with `[1, 2, 3].each do` at 2, 2. next end end RUBY expect_correction(<<~RUBY) def foo [1, 2, 3].each do |el| el.to_s rescue StandardError => _exception next end end RUBY end end context 'Ruby 2.7', :ruby27 do it 'accepts aligned rescue in do-end numbered block in a method' do expect_no_offenses(<<~RUBY) def foo [1, 2, 3].each do _1.to_s rescue StandardError => _exception next end end RUBY end context 'rescue with do-end numbered block' do it 'registers an offense' do expect_offense(<<~RUBY) def foo [1, 2, 3].each do _1.to_s rescue StandardError => _exception ^^^^^^ `rescue` at 4, 0 is not aligned with `[1, 2, 3].each do` at 2, 2. next end end RUBY expect_correction(<<~RUBY) def foo [1, 2, 3].each do _1.to_s rescue StandardError => _exception next end end RUBY end end end context 'Ruby 3.4', :ruby34 do it 'accepts aligned rescue in do-end `it` block in a method' do expect_no_offenses(<<~RUBY) def foo [1, 2, 3].each do it.to_s rescue StandardError => _exception next end end RUBY end context 'rescue with do-end `it` block' do it 'registers an offense' do expect_offense(<<~RUBY) def foo [1, 2, 3].each do it.to_s rescue StandardError => _exception ^^^^^^ `rescue` at 4, 0 is not aligned with `[1, 2, 3].each do` at 2, 2. next end end RUBY expect_correction(<<~RUBY) def foo [1, 2, 3].each do it.to_s rescue StandardError => _exception next end end RUBY end end end context 'rescue in do-end block assigned to local variable' do it 'registers an offense' do expect_offense(<<~RUBY) result = [1, 2, 3].map do |el| rescue StandardError => _exception ^^^^^^ `rescue` at 2, 2 is not aligned with `result` at 1, 0. end RUBY expect_correction(<<~RUBY) result = [1, 2, 3].map do |el| rescue StandardError => _exception end RUBY end end context 'rescue in do-end block assigned to instance variable' do it 'registers an offense' do expect_offense(<<~RUBY) @instance = [1, 2, 3].map do |el| rescue StandardError => _exception ^^^^^^ `rescue` at 2, 2 is not aligned with `@instance` at 1, 0. end RUBY expect_correction(<<~RUBY) @instance = [1, 2, 3].map do |el| rescue StandardError => _exception end RUBY end end context 'rescue in do-end block assigned to class variable' do it 'registers an offense' do expect_offense(<<~RUBY) @@class = [].map do |_| rescue StandardError => _ ^^^^^^ `rescue` at 2, 2 is not aligned with `@@class` at 1, 0. end RUBY expect_correction(<<~RUBY) @@class = [].map do |_| rescue StandardError => _ end RUBY end end context 'rescue in do-end block assigned to global variable' do it 'registers an offense' do expect_offense(<<~RUBY) $global = [].map do |_| rescue StandardError => _ ^^^^^^ `rescue` at 2, 2 is not aligned with `$global` at 1, 0. end RUBY expect_correction(<<~RUBY) $global = [].map do |_| rescue StandardError => _ end RUBY end end context 'rescue in do-end block assigned to class' do it 'registers an offense' do expect_offense(<<~RUBY) CLASS = [].map do |_| rescue StandardError => _ ^^^^^^ `rescue` at 2, 2 is not aligned with `CLASS` at 1, 0. end RUBY expect_correction(<<~RUBY) CLASS = [].map do |_| rescue StandardError => _ end RUBY end end context 'rescue in do-end block assigned to object attribute' do it 'registers an offense' do expect_offense(<<~RUBY) obj.attr = do_something do rescue StandardError ^^^^^^ `rescue` at 2, 2 is not aligned with `obj` at 1, 0. end RUBY expect_correction(<<~RUBY) obj.attr = do_something do rescue StandardError end RUBY end end context 'rescue in do-end block on multi-assignment' do it 'registers an offense' do expect_offense(<<~RUBY) a, b = [].map do |_| rescue StandardError => _ ^^^^^^ `rescue` at 2, 2 is not aligned with `a, b` at 1, 0. end RUBY expect_correction(<<~RUBY) a, b = [].map do |_| rescue StandardError => _ end RUBY end end context 'rescue in do-end block on operation assignment' do it 'registers an offense' do expect_offense(<<~RUBY) a += [].map do |_| rescue StandardError => _ ^^^^^^ `rescue` at 2, 2 is not aligned with `a` at 1, 0. end RUBY expect_correction(<<~RUBY) a += [].map do |_| rescue StandardError => _ end RUBY end end context 'rescue in do-end block on and-assignment' do it 'registers an offense' do expect_offense(<<~RUBY) a &&= [].map do |_| rescue StandardError => _ ^^^^^^ `rescue` at 2, 2 is not aligned with `a` at 1, 0. end RUBY expect_correction(<<~RUBY) a &&= [].map do |_| rescue StandardError => _ end RUBY end end context 'rescue in do-end block on or-assignment' do it 'registers an offense' do expect_offense(<<~RUBY) a ||= [].map do |_| rescue StandardError => _ ^^^^^^ `rescue` at 2, 2 is not aligned with `a` at 1, 0. end RUBY expect_correction(<<~RUBY) a ||= [].map do |_| rescue StandardError => _ end RUBY end end context 'rescue in assigned do-end block starting on newline' do it 'registers an offense' do expect_offense(<<~RUBY) valid = proc do |bar| baz rescue ^^^^^^ `rescue` at 4, 4 is not aligned with `proc do` at 2, 2. qux end RUBY expect_correction(<<~RUBY) valid = proc do |bar| baz rescue qux end RUBY end end context 'when using zsuper with block' do it 'registers and corrects an offense and corrects when incorrect alignment' do expect_offense(<<~RUBY) super do nil ensure ^^^^^^ `ensure` at 3, 4 is not aligned with `super do` at 1, 0. nil end RUBY expect_correction(<<~RUBY) super do nil ensure nil end RUBY end it 'does not register an offense when correct alignment' do expect_no_offenses(<<~RUBY) super do nil ensure nil end RUBY end end describe 'excluded file', :config do let(:config) do RuboCop::Config.new('Layout/RescueEnsureAlignment' => { 'Enabled' => true, 'Exclude' => ['**/**'] }) end it 'processes excluded files with issue' do expect_no_offenses(<<~RUBY, 'foo.rb') begin foo rescue bar end RUBY end end context 'allows inline access modifier' do let(:cop_config) do { 'Style/AccessModifierDeclarations' => { 'EnforcedStyle' => 'inline' } } end shared_examples 'access modifier' do |modifier| context 'rescue with def' do it 'registers an offense' do expect_offense(<<~RUBY) #{modifier} def test 'foo' rescue ^^^^^^ `rescue` at 3, 2 is not aligned with `#{modifier} def test` at 1, 0. 'baz' end RUBY expect_correction(<<~RUBY) #{modifier} def test 'foo' rescue 'baz' end RUBY end it 'corrects alignment' do expect_no_offenses(<<~RUBY) #{modifier} def test 'foo' rescue 'baz' end RUBY end end context 'rescue with defs' do it 'registers an offense' do expect_offense(<<~RUBY) #{modifier} def Test.test 'foo' rescue ^^^^^^ `rescue` at 3, 2 is not aligned with `#{modifier} def Test.test` at 1, 0. 'baz' end RUBY expect_correction(<<~RUBY) #{modifier} def Test.test 'foo' rescue 'baz' end RUBY end it 'corrects alignment' do expect_no_offenses(<<~RUBY) #{modifier} def Test.test 'foo' rescue 'baz' end RUBY end end context 'ensure with def' do it 'registers an offense' do expect_offense(<<~RUBY) #{modifier} def test 'foo' ensure ^^^^^^ `ensure` at 3, 2 is not aligned with `#{modifier} def test` at 1, 0. 'baz' end RUBY expect_correction(<<~RUBY) #{modifier} def test 'foo' ensure 'baz' end RUBY end it 'corrects alignment' do expect_no_offenses(<<~RUBY) #{modifier} def test 'foo' ensure 'baz' end RUBY end end context 'ensure with defs' do it 'registers an offense' do expect_offense(<<~RUBY) #{modifier} def Test.test 'foo' ensure ^^^^^^ `ensure` at 3, 2 is not aligned with `#{modifier} def Test.test` at 1, 0. 'baz' end RUBY expect_correction(<<~RUBY) #{modifier} def Test.test 'foo' ensure 'baz' end RUBY end it 'corrects alignment' do expect_no_offenses(<<~RUBY) #{modifier} def Test.test 'foo' ensure 'baz' end RUBY end end end context 'with private modifier' do it_behaves_like 'access modifier', 'private' end context 'with private_class_method modifier' do it_behaves_like 'access modifier', 'private_class_method' end context 'with public_class_method modifier' do it_behaves_like 'access modifier', 'public_class_method' end end context 'allows inline expression before' do context 'rescue' do it 'registers an offense' do expect_offense(<<~RUBY) def test 'foo'; rescue; 'baz' ^^^^^^ `rescue` at 2, 9 is not aligned with `def test` at 1, 0. end def test begin 'foo'; rescue; 'baz' ^^^^^^ `rescue` at 7, 11 is not aligned with `begin` at 6, 2. end end RUBY expect_no_corrections end end context 'ensure' do it 'registers an offense' do expect_offense(<<~RUBY) def test 'foo'; ensure; 'baz' ^^^^^^ `ensure` at 2, 9 is not aligned with `def test` at 1, 0. end def test begin 'foo'; ensure; 'baz' ^^^^^^ `ensure` at 7, 11 is not aligned with `begin` at 6, 2. end end RUBY expect_no_corrections end end end end
ruby
MIT
99fa0fdd0481910d7d052f4c2ed01ad36178f404
2026-01-04T15:37:41.211519Z
false
rubocop/rubocop
https://github.com/rubocop/rubocop/blob/99fa0fdd0481910d7d052f4c2ed01ad36178f404/spec/rubocop/cop/layout/space_after_comma_spec.rb
spec/rubocop/cop/layout/space_after_comma_spec.rb
# frozen_string_literal: true RSpec.describe RuboCop::Cop::Layout::SpaceAfterComma, :config do let(:config) { RuboCop::Config.new('Layout/SpaceInsideHashLiteralBraces' => brace_config) } let(:brace_config) { {} } shared_examples 'ends with an item' do |items, annotation_start, correct_items| it 'registers an offense and does autocorrection' do expect_offense(<<~RUBY) #{source.call(items)} #{' ' * annotation_start}^ Space missing after comma. RUBY expect_correction(<<~RUBY) #{source.call(correct_items)} RUBY end end shared_examples 'trailing comma' do |items| it 'accepts the last comma' do expect_no_offenses(source.call(items)) end end context 'block argument commas without space' do let(:source) { ->(items) { "each { |#{items}| }" } } it_behaves_like 'ends with an item', 's,t', 9, 's, t' it_behaves_like 'trailing comma', 's, t,' end context 'array index commas without space' do let(:source) { ->(items) { "formats[#{items}]" } } it_behaves_like 'ends with an item', '0,1', 9, '0, 1' it_behaves_like 'trailing comma', '0,' end context 'method call arg commas without space' do let(:source) { ->(args) { "a(#{args})" } } it_behaves_like 'ends with an item', '1,2', 3, '1, 2' end context 'inside hash braces' do shared_examples 'common behavior' do it 'accepts a space between a comma and a closing brace' do expect_no_offenses('{ foo:bar, }') end end context 'when EnforcedStyle for SpaceInsideBlockBraces is space' do let(:brace_config) { { 'Enabled' => true, 'EnforcedStyle' => 'space' } } it_behaves_like 'common behavior' it 'registers an offense for no space between a comma and a closing brace' do expect_offense(<<~RUBY) { foo:bar,} ^ Space missing after comma. RUBY end end context 'when EnforcedStyle for SpaceInsideBlockBraces is no_space' do let(:brace_config) { { 'Enabled' => true, 'EnforcedStyle' => 'no_space' } } it_behaves_like 'common behavior' it 'accepts no space between a comma and a closing brace' do expect_no_offenses('{foo:bar,}') end end end end
ruby
MIT
99fa0fdd0481910d7d052f4c2ed01ad36178f404
2026-01-04T15:37:41.211519Z
false
rubocop/rubocop
https://github.com/rubocop/rubocop/blob/99fa0fdd0481910d7d052f4c2ed01ad36178f404/spec/rubocop/cop/layout/first_parameter_indentation_spec.rb
spec/rubocop/cop/layout/first_parameter_indentation_spec.rb
# frozen_string_literal: true RSpec.describe RuboCop::Cop::Layout::FirstParameterIndentation, :config do let(:config) do supported_styles = { 'SupportedStyles' => %w[consistent align_parentheses] } RuboCop::Config.new('Layout/FirstParameterIndentation' => cop_config.merge(supported_styles).merge( 'IndentationWidth' => cop_indent ), 'Layout/IndentationWidth' => { 'Width' => 2 }) end let(:cop_indent) { nil } # use indent from Layout/IndentationWidth context 'consistent style' do let(:cop_config) { { 'EnforcedStyle' => 'consistent' } } context 'no paren method defs' do it 'ignores' do expect_no_offenses(<<~RUBY) def abc foo, bar, baz foo end RUBY end it 'ignores with hash args' do expect_no_offenses(<<~RUBY) def abc foo: 1, bar: 3, baz: 3 foo end RUBY end end context 'single line method defs' do it 'ignores' do expect_no_offenses(<<~RUBY) def abc(foo, bar, baz) foo end RUBY end it 'ignores with hash args' do expect_no_offenses(<<~RUBY) def abc(foo: 1, bar: 3, baz: 3) foo end RUBY end end context 'valid indentation on multi-line defs' do it 'accepts correctly indented first element' do expect_no_offenses(<<~RUBY) def abc( foo, bar, baz ) foo end RUBY end it 'accepts correctly indented first element hash' do expect_no_offenses(<<~RUBY) def abc( foo: 1, bar: 3, baz: 3 ) foo end RUBY end end context 'valid indentation on static multi-line defs' do it 'accepts correctly indented first element' do expect_no_offenses(<<~RUBY) def self.abc( foo, bar, baz ) foo end RUBY end it 'accepts correctly indented first element hash' do expect_no_offenses(<<~RUBY) def self.abc( foo: 1, bar: 3, baz: 3 ) foo end RUBY end end context 'invalid indentation on multi-line defs' do context 'normal arguments' do it 'registers an offense and corrects incorrectly indented first element' do expect_offense(<<~RUBY) def abc( foo, ^^^ Use 2 spaces for indentation in method args, relative to the start of the line where the left parenthesis is. bar, baz ) foo end RUBY expect_correction(<<~RUBY) def abc( foo, bar, baz ) foo end RUBY end end context 'hash arguments' do it 'registers an offense and corrects incorrectly indented first element' do expect_offense(<<~RUBY) def abc( foo: 1, ^^^^^^ Use 2 spaces for indentation in method args, relative to the start of the line where the left parenthesis is. bar: 3, baz: 3 ) foo end RUBY expect_correction(<<~RUBY) def abc( foo: 1, bar: 3, baz: 3 ) foo end RUBY end end context 'hash arguments static method def' do it 'registers an offense and corrects incorrectly indented first element' do expect_offense(<<~RUBY) def self.abc( foo: 1, ^^^^^^ Use 2 spaces for indentation in method args, relative to the start of the line where the left parenthesis is. bar: 3, baz: 3 ) foo end RUBY expect_correction(<<~RUBY) def self.abc( foo: 1, bar: 3, baz: 3 ) foo end RUBY end end end end context 'align_parentheses style' do let(:cop_config) { { 'EnforcedStyle' => 'align_parentheses' } } context 'no paren method defs' do it 'ignores' do expect_no_offenses(<<~RUBY) def abc foo, bar, baz foo end RUBY end it 'ignores with hash args' do expect_no_offenses(<<~RUBY) def abc foo: 1, bar: 3, baz: 3 foo end RUBY end end context 'single line method defs' do it 'ignores' do expect_no_offenses(<<~RUBY) def abc(foo, bar, baz) foo end RUBY end it 'ignores with hash args' do expect_no_offenses(<<~RUBY) def abc(foo: 1, bar: 3, baz: 3) foo end RUBY end end context 'valid indentation on multi-line defs' do it 'accepts correctly indented first element' do expect_no_offenses(<<~RUBY) def abc( foo, bar, baz ) foo end RUBY end it 'accepts correctly indented first element hash' do expect_no_offenses(<<~RUBY) def abc( foo: 1, bar: 3, baz: 3 ) foo end RUBY end end context 'invalid indentation on multi-line defs' do context 'normal arguments' do it 'registers an offense and corrects incorrectly indented first element' do expect_offense(<<~RUBY) def abc( foo, ^^^ Use 2 spaces for indentation in method args, relative to the position of the opening parenthesis. bar, baz ) foo end RUBY expect_correction(<<~RUBY) def abc( foo, bar, baz ) foo end RUBY end end context 'hash arguments' do it 'registers an offense and corrects incorrectly indented first element' do expect_offense(<<~RUBY) def abc( foo: 1, ^^^^^^ Use 2 spaces for indentation in method args, relative to the position of the opening parenthesis. bar: 3, baz: 3 ) foo end RUBY expect_correction(<<~RUBY) def abc( foo: 1, bar: 3, baz: 3 ) foo end RUBY end end context 'hash arguments static def' do it 'registers an offense and corrects incorrectly indented first element' do expect_offense(<<~RUBY) def self.abc( foo: 1, ^^^^^^ Use 2 spaces for indentation in method args, relative to the position of the opening parenthesis. bar: 3, baz: 3 ) foo end RUBY expect_correction(<<~RUBY) def self.abc( foo: 1, bar: 3, baz: 3 ) foo end RUBY end end end end end
ruby
MIT
99fa0fdd0481910d7d052f4c2ed01ad36178f404
2026-01-04T15:37:41.211519Z
false
rubocop/rubocop
https://github.com/rubocop/rubocop/blob/99fa0fdd0481910d7d052f4c2ed01ad36178f404/spec/rubocop/cop/layout/space_inside_range_literal_spec.rb
spec/rubocop/cop/layout/space_inside_range_literal_spec.rb
# frozen_string_literal: true RSpec.describe RuboCop::Cop::Layout::SpaceInsideRangeLiteral, :config do it 'registers an offense for space inside .. literal' do expect_offense(<<~RUBY) 1 .. 2 ^^^^^^ Space inside range literal. 1.. 2 ^^^^^ Space inside range literal. 1 ..2 ^^^^^ Space inside range literal. RUBY expect_correction(<<~RUBY) 1..2 1..2 1..2 RUBY end it 'accepts no space inside .. literal' do expect_no_offenses('1..2') end it 'registers an offense for space inside ... literal' do expect_offense(<<~RUBY) 1 ... 2 ^^^^^^^ Space inside range literal. 1... 2 ^^^^^^ Space inside range literal. 1 ...2 ^^^^^^ Space inside range literal. RUBY expect_correction(<<~RUBY) 1...2 1...2 1...2 RUBY end it 'accepts no space inside ... literal' do expect_no_offenses('1...2') end it 'accepts complex range literal with space in it' do expect_no_offenses('0...(line - 1)') end it 'accepts multiline range literal with no space in it' do expect_no_offenses(<<~RUBY) x = 0.. 10 RUBY end it 'registers an offense in multiline range literal with space in it' do expect_offense(<<~RUBY) x = 0 .. ^^^^ Space inside range literal. 10 RUBY expect_correction(<<~RUBY) x = 0..10 RUBY end end
ruby
MIT
99fa0fdd0481910d7d052f4c2ed01ad36178f404
2026-01-04T15:37:41.211519Z
false
rubocop/rubocop
https://github.com/rubocop/rubocop/blob/99fa0fdd0481910d7d052f4c2ed01ad36178f404/spec/rubocop/cop/layout/indentation_consistency_spec.rb
spec/rubocop/cop/layout/indentation_consistency_spec.rb
# frozen_string_literal: true RSpec.describe RuboCop::Cop::Layout::IndentationConsistency, :config do let(:cop_config) { { 'EnforcedStyle' => 'normal' } } context 'with top-level code' do it 'accepts an empty expression string interpolation' do expect_no_offenses(<<~'RUBY') "#{}" RUBY end it 'accepts when using access modifier at the top level' do expect_no_offenses(<<~RUBY) public def foo end RUBY end it 'registers and corrects an offense when using access modifier and indented method definition ' \ 'at the top level' do expect_offense(<<~RUBY) public def foo ^^^^^^^ Inconsistent indentation detected. end RUBY expect_correction(<<~RUBY) public def foo end RUBY end end context 'with if statement' do it 'registers an offense and corrects bad indentation in an if body' do expect_offense(<<~RUBY) if cond func func ^^^^ Inconsistent indentation detected. end RUBY expect_correction(<<~RUBY) if cond func func end RUBY end it 'registers an offense and corrects bad indentation in an else body' do expect_offense(<<~RUBY) if cond func1 else func2 func2 ^^^^^ Inconsistent indentation detected. end RUBY expect_correction(<<~RUBY) if cond func1 else func2 func2 end RUBY end it 'registers an offense and corrects bad indentation in an elsif body' do expect_offense(<<~RUBY) if a1 b1 elsif a2 b2 b3 ^^ Inconsistent indentation detected. else c end RUBY expect_correction(<<~RUBY) if a1 b1 elsif a2 b2 b3 else c end RUBY end it 'accepts a one line if statement' do expect_no_offenses('if cond then func1 else func2 end') end it 'accepts a correctly aligned if/elsif/else/end' do expect_no_offenses(<<~RUBY) if a1 b1 elsif a2 b2 else c end RUBY end it 'accepts if/elsif/else/end laid out as a table' do expect_no_offenses(<<~RUBY) if @io == $stdout then str << "$stdout" elsif @io == $stdin then str << "$stdin" elsif @io == $stderr then str << "$stderr" else str << @io.class.to_s end RUBY end it 'accepts if/then/else/end laid out as another table' do expect_no_offenses(<<~RUBY) if File.exist?('config.save') then ConfigTable.load else ConfigTable.new end RUBY end it 'accepts if/elsif/else/end with fullwidth characters' do expect_no_offenses(<<~RUBY) p 'Ruby', if a then b c end RUBY end it 'accepts an empty if' do expect_no_offenses(<<~RUBY) if a else end RUBY end it 'accepts an if in assignment with end aligned with variable' do expect_no_offenses(<<~RUBY) var = if a 0 end @var = if a 0 end $var = if a 0 end var ||= if a 0 end var &&= if a 0 end var -= if a 0 end VAR = if a 0 end RUBY end it 'accepts an if/else in assignment with end aligned with variable' do expect_no_offenses(<<~RUBY) var = if a 0 else 1 end RUBY end it 'accepts an if/else in assignment with end aligned with variable ' \ 'and chaining after the end' do expect_no_offenses(<<~RUBY) var = if a 0 else 1 end.abc.join("") RUBY end it 'accepts an if/else in assignment with end aligned with variable ' \ 'and chaining with a block after the end' do expect_no_offenses(<<~RUBY) var = if a 0 else 1 end.abc.tap {} RUBY end it 'accepts an if in assignment with end aligned with if' do expect_no_offenses(<<~RUBY) var = if a 0 end RUBY end it 'accepts an if/else in assignment with end aligned with if' do expect_no_offenses(<<~RUBY) var = if a 0 else 1 end RUBY end it 'accepts an if/else in assignment on next line with end aligned with if' do expect_no_offenses(<<~RUBY) var = if a 0 else 1 end RUBY end it 'accepts an if/else branches with rescue clauses' do # Because of how the rescue clauses come out of Parser, these are # special and need to be tested. expect_no_offenses(<<~RUBY) if a a rescue nil else a rescue nil end RUBY end end context 'with unless' do it 'registers an offense and corrects bad indentation in an unless body' do expect_offense(<<~RUBY) unless cond func func ^^^^ Inconsistent indentation detected. end RUBY expect_correction(<<~RUBY) unless cond func func end RUBY end it 'accepts an empty unless' do expect_no_offenses(<<~RUBY) unless a else end RUBY end end context 'with case' do it 'registers an offense and corrects bad indentation in a case/when body' do expect_offense(<<~RUBY) case a when b c d ^ Inconsistent indentation detected. end RUBY expect_correction(<<~RUBY) case a when b c d end RUBY end it 'registers an offense and corrects bad indentation in a case/else body' do expect_offense(<<~RUBY) case a when b c when d e else f g ^ Inconsistent indentation detected. end RUBY expect_correction(<<~RUBY) case a when b c when d e else f g end RUBY end it 'accepts correctly indented case/when/else' do expect_no_offenses(<<~RUBY) case a when b c c when d else f end RUBY end it 'accepts case/when/else laid out as a table' do expect_no_offenses(<<~RUBY) case sexp.loc.keyword.source when 'if' then cond, body, _else = *sexp when 'unless' then cond, _else, body = *sexp else cond, body = *sexp end RUBY end it 'accepts case/when/else with then beginning a line' do expect_no_offenses(<<~RUBY) case sexp.loc.keyword.source when 'if' then cond, body, _else = *sexp end RUBY end it 'accepts indented when/else plus indented body' do # "Indent when as deep as case" is the job of another cop. expect_no_offenses(<<~RUBY) case code_type when 'ruby', 'sql', 'plain' code_type when 'erb' 'ruby; html-script: true' when "html" 'xml' else 'plain' end RUBY end end context 'with while/until' do it 'registers an offense and corrects bad indentation in a while body' do expect_offense(<<~RUBY) while cond func func ^^^^ Inconsistent indentation detected. end RUBY expect_correction(<<~RUBY) while cond func func end RUBY end it 'registers an offense and corrects bad indentation in begin/end/while' do expect_offense(<<~RUBY) something = begin func1 func2 ^^^^^ Inconsistent indentation detected. end while cond RUBY expect_correction(<<~RUBY) something = begin func1 func2 end while cond RUBY end it 'registers an offense and corrects bad indentation in an until body' do expect_offense(<<~RUBY) until cond func func ^^^^ Inconsistent indentation detected. end RUBY expect_correction(<<~RUBY) until cond func func end RUBY end it 'accepts an empty while' do expect_no_offenses(<<~RUBY) while a end RUBY end end context 'with for' do it 'registers an offense and corrects bad indentation in a for body' do expect_offense(<<~RUBY) for var in 1..10 func func ^^^^ Inconsistent indentation detected. end RUBY expect_correction(<<~RUBY) for var in 1..10 func func end RUBY end it 'accepts an empty for' do expect_no_offenses(<<~RUBY) for var in 1..10 end RUBY end end context 'with def/defs' do it 'registers an offense and corrects bad indentation in a def body' do expect_offense(<<~RUBY) def test func1 func2 ^^^^^ Inconsistent indentation detected. end RUBY expect_correction(<<~RUBY) def test func1 func2 end RUBY end it 'registers an offense and corrects bad indentation in a defs body' do expect_offense(<<~RUBY) def self.test func func ^^^^ Inconsistent indentation detected. end RUBY expect_correction(<<~RUBY) def self.test func func end RUBY end it 'accepts an empty def body' do expect_no_offenses(<<~RUBY) def test end RUBY end it 'accepts an empty defs body' do expect_no_offenses(<<~RUBY) def self.test end RUBY end end context 'with class' do context 'with indented_internal_methods style configured' do let(:cop_config) { { 'EnforcedStyle' => 'indented_internal_methods' } } it 'accepts different indentation in different visibility sections' do expect_no_offenses(<<~RUBY) class Cat def meow puts('Meow!') end protected def can_we_be_friends?(another_cat) # some logic end def related_to?(another_cat) # some logic end private # Here we go back an indentation level again. This is a # violation of the indented_internal_methods style, # but it's not for this cop to report. # Layout/IndentationWidth will handle it. def meow_at_3am? rand < 0.8 end # As long as the indentation of this method is # consistent with that of the last one, we're fine. def meow_at_4am? rand < 0.8 end end RUBY end it 'accepts different indentation in different visibility sections when using `Struct.new`' do expect_no_offenses(<<~RUBY) Foo = Struct.new(:foo) do def meow puts('Meow!') end protected def can_we_be_friends?(another_cat) # some logic end def related_to?(another_cat) # some logic end private # Here we go back an indentation level again. This is a # violation of the indented_internal_methods style, # but it's not for this cop to report. # Layout/IndentationWidth will handle it. def meow_at_3am? rand < 0.8 end # As long as the indentation of this method is # consistent with that of the last one, we're fine. def meow_at_4am? rand < 0.8 end end RUBY end end context 'with normal style configured' do it 'registers an offense and corrects bad indentation in a class body' do expect_offense(<<~RUBY) class Test def func1 end def func2 ^^^^^^^^^ Inconsistent indentation detected. end end RUBY expect_correction(<<~RUBY) class Test def func1 end def func2 end end RUBY end it 'accepts an empty class body' do expect_no_offenses(<<~RUBY) class Test end RUBY end it 'accepts indented public, protected, and private' do expect_no_offenses(<<~RUBY) class Test public def e end protected def f end private def g end end RUBY end it 'registers an offense and corrects bad indentation ' \ 'in def but not for outdented public, protected, and private' do expect_offense(<<~RUBY) class Test public def e end protected def f end private def g ^^^^^ Inconsistent indentation detected. end end RUBY expect_correction(<<~RUBY) class Test public def e end protected def f end private def g end end RUBY end end end context 'with module' do it 'registers an offense and corrects bad indentation in a module body' do expect_offense(<<~RUBY) module Test def func1 end def func2 ^^^^^^^^^ Inconsistent indentation detected. end end RUBY expect_correction(<<~RUBY) module Test def func1 end def func2 end end RUBY end it 'accepts an empty module body' do expect_no_offenses(<<~RUBY) module Test end RUBY end it 'registers an offense and corrects bad indentation of private methods' do expect_offense(<<~RUBY) module Test def pub end private def priv ^^^^^^^^ Inconsistent indentation detected. end end RUBY expect_correction(<<~RUBY) module Test def pub end private def priv end end RUBY end context 'even when there are no public methods' do it 'registers an offense and corrects bad indentation of private methods' do expect_offense(<<~RUBY) module Test private def priv ^^^^^^^^ Inconsistent indentation detected. end end RUBY expect_correction(<<~RUBY) module Test private def priv end end RUBY end end end context 'with block' do it 'registers an offense and correct bad indentation in a do/end body' do expect_offense(<<~RUBY) a = func do b c ^ Inconsistent indentation detected. end RUBY expect_correction(<<~RUBY) a = func do b c end RUBY end it 'registers an offense and corrects bad indentation in a {} body' do expect_offense(<<~RUBY) func { b c ^ Inconsistent indentation detected. } RUBY expect_correction(<<~RUBY) func { b c } RUBY end it 'accepts a correctly indented block body' do expect_no_offenses(<<~RUBY) a = func do b c end RUBY end it 'accepts an empty block body' do expect_no_offenses(<<~RUBY) a = func do end RUBY end it 'does not autocorrect an offense within another offense' do # rubocop:disable InternalAffairs/ExampleDescription expect_offense(<<~RUBY) require 'spec_helper' describe ArticlesController do render_views describe "GET 'index'" do ^^^^^^^^^^^^^^^^^^^^^^^^^ Inconsistent indentation detected. it "returns success" do end describe "admin user" do ^^^^^^^^^^^^^^^^^^^^^^^^ Inconsistent indentation detected. before(:each) do end end end end RUBY # The offense on line 4 is corrected, affecting lines 4 to 11. expect_correction(<<~RUBY, loop: false) require 'spec_helper' describe ArticlesController do render_views describe "GET 'index'" do it "returns success" do end describe "admin user" do before(:each) do end end end end RUBY end end end
ruby
MIT
99fa0fdd0481910d7d052f4c2ed01ad36178f404
2026-01-04T15:37:41.211519Z
false
rubocop/rubocop
https://github.com/rubocop/rubocop/blob/99fa0fdd0481910d7d052f4c2ed01ad36178f404/spec/rubocop/cop/layout/space_inside_array_percent_literal_spec.rb
spec/rubocop/cop/layout/space_inside_array_percent_literal_spec.rb
# frozen_string_literal: true RSpec.describe RuboCop::Cop::Layout::SpaceInsideArrayPercentLiteral, :config do %w[i I w W].each do |type| [%w[{ }], %w[( )], %w([ ]), %w[! !]].each do |(ldelim, rdelim)| context "for #{type} type and #{[ldelim, rdelim]} delimiters" do define_method(:code_example) { |content| ['%', type, ldelim, content, rdelim].join } it 'registers an offense for unnecessary spaces' do expect_offense(<<~RUBY) #{code_example('1 2')} ^^^ Use only a single space inside array percent literal. RUBY expect_correction("#{code_example('1 2')}\n") end it 'registers an offense for multiple spaces between items' do expect_offense(<<~RUBY) #{code_example('1 2 3')} ^^^ Use only a single space inside array percent literal. ^^^ Use only a single space inside array percent literal. RUBY expect_correction("#{code_example('1 2 3')}\n") end it 'registers an offense when literals with escaped and additional spaces' do expect_offense(<<~RUBY) #{code_example('a\ b \ c')} ^^ Use only a single space inside array percent literal. RUBY expect_correction("#{code_example('a\ b \ c')}\n") end it 'accepts literals without additional spaces' do expect_no_offenses(code_example('a b c')) end it 'accepts literals with escaped spaces' do expect_no_offenses(code_example('a\ b\ \ c')) end it 'accepts multi-line literals' do expect_no_offenses(<<~RUBY) %#{type}( a b c ) RUBY end it 'accepts multi-line literals within a method' do expect_no_offenses(<<~RUBY) def foo %#{type}( a b c ) end RUBY end it 'accepts newlines and additional following alignment spaces' do expect_no_offenses(<<~RUBY) %#{type}(a b c) RUBY end end end end it 'accepts non array percent literals' do expect_no_offenses('%q( a b c )') end end
ruby
MIT
99fa0fdd0481910d7d052f4c2ed01ad36178f404
2026-01-04T15:37:41.211519Z
false
rubocop/rubocop
https://github.com/rubocop/rubocop/blob/99fa0fdd0481910d7d052f4c2ed01ad36178f404/spec/rubocop/cop/layout/end_of_line_spec.rb
spec/rubocop/cop/layout/end_of_line_spec.rb
# frozen_string_literal: true RSpec.describe RuboCop::Cop::Layout::EndOfLine, :config do include EncodingHelper shared_examples 'all configurations' do it 'accepts an empty file' do expect_no_offenses('') end end shared_examples 'iso-8859-15' do |eol| it 'can inspect non-UTF-8 encoded source with proper encoding comment' do # Weird place to have a test on working with non-utf-8 encodings. # Encodings are not specific to the EndOfLine cop, so the test is better # be moved somewhere more general? # Also working with encodings is actually the responsibility of # 'whitequark/parser' gem, not RuboCop itself so these test really belongs there(?) encoding = 'iso-8859-15' input = (+<<~RUBY).force_encoding(encoding) # coding: ISO-8859-15#{eol} # Euro symbol: \xa4#{eol} RUBY expect do Tempfile.open('tmp', encoding: encoding) { |f| expect_no_offenses(input, f) } end.to raise_error( RSpec::Expectations::ExpectationNotMetError, /Carriage return character (detected|missing)./ ) end end context 'when EnforcedStyle is native' do let(:cop_config) { { 'EnforcedStyle' => 'native' } } it 'registers an offense for an incorrect EOL' do if RuboCop::Platform.windows? expect_offense(<<~RUBY) x=0 ^^^ Carriage return character missing. y=1\r RUBY else expect_offense(<<~RUBY) x=0 y=1\r ^^^ Carriage return character detected. RUBY end end end context 'when EnforcedStyle is crlf' do let(:cop_config) { { 'EnforcedStyle' => 'crlf' } } it_behaves_like 'all configurations' it 'registers an offense for CR+LF' do expect_offense(<<~RUBY) x=0 ^^^ Carriage return character missing. y=1\r RUBY end it 'does not register offense for no CR at end of file' do expect_no_offenses('x=0') end it 'does not register offenses after __END__' do expect_no_offenses(<<~RUBY) x=0\r __END__ x=0 RUBY end context 'and there are many lines ending with LF' do it 'registers only one offense' do expect_offense(<<~RUBY) x=0 ^^^ Carriage return character missing. y=1 RUBY end it_behaves_like 'iso-8859-15', '' end context 'and the default external encoding is US_ASCII' do around do |example| with_default_external_encoding(Encoding::US_ASCII) { example.run } end it 'does not crash on UTF-8 encoded non-ascii characters' do expect_no_offenses(<<~RUBY) class Epd::ReportsController < EpdAreaController\r 'terecht bij uw ROM-coördinator.'\r end\r RUBY end it_behaves_like 'iso-8859-15', '' end end context 'when EnforcedStyle is lf' do let(:cop_config) { { 'EnforcedStyle' => 'lf' } } it_behaves_like 'all configurations' it 'registers an offense for CR+LF' do expect_offense(<<~RUBY) x=0 y=1\r ^^^ Carriage return character detected. RUBY end it 'registers an offense for CR at end of file' do expect_offense(<<~RUBY) x=0\r ^^^ Carriage return character detected. RUBY end it 'does not register offenses after __END__' do expect_no_offenses(<<~RUBY) x=0 __END__ x=0\r RUBY end context 'and there are many lines ending with CR+LF' do it 'registers only one offense' do expect_offense(<<~RUBY) x=0\r ^^^ Carriage return character detected. \r y=1 RUBY end it_behaves_like 'iso-8859-15', "\r" end context 'and the default external encoding is US_ASCII' do around do |example| with_default_external_encoding(Encoding::US_ASCII) { example.run } end it 'does not crash on UTF-8 encoded non-ascii characters' do expect_no_offenses(<<~RUBY) class Epd::ReportsController < EpdAreaController 'terecht bij uw ROM-coördinator.' end RUBY end it_behaves_like 'iso-8859-15', "\r" end end end
ruby
MIT
99fa0fdd0481910d7d052f4c2ed01ad36178f404
2026-01-04T15:37:41.211519Z
false
rubocop/rubocop
https://github.com/rubocop/rubocop/blob/99fa0fdd0481910d7d052f4c2ed01ad36178f404/spec/rubocop/cop/layout/space_before_brackets_spec.rb
spec/rubocop/cop/layout/space_before_brackets_spec.rb
# frozen_string_literal: true RSpec.describe RuboCop::Cop::Layout::SpaceBeforeBrackets, :config do context 'when referencing' do it 'registers an offense and corrects when using space between lvar receiver and left brackets' do expect_offense(<<~RUBY) collection = do_something collection [index_or_key] ^ Remove the space before the opening brackets. RUBY expect_correction(<<~RUBY) collection = do_something collection[index_or_key] RUBY end it 'registers an offense and corrects when using space between ivar receiver and left brackets' do expect_offense(<<~RUBY) @collection [index_or_key] ^ Remove the space before the opening brackets. RUBY expect_correction(<<~RUBY) @collection[index_or_key] RUBY end it 'registers an offense and corrects when using space between cvar receiver and left brackets' do expect_offense(<<~RUBY) @@collection [index_or_key] ^ Remove the space before the opening brackets. RUBY expect_correction(<<~RUBY) @@collection[index_or_key] RUBY end it 'registers an offense and corrects when using space between gvar receiver and left brackets' do expect_offense(<<~RUBY) $collection [index_or_key] ^ Remove the space before the opening brackets. RUBY expect_correction(<<~RUBY) $collection[index_or_key] RUBY end it 'registers an offense and corrects when using space between method argument parentheses and left bracket' do expect_offense(<<~RUBY) collection.call(arg) [index_or_key] ^ Remove the space before the opening brackets. RUBY expect_correction(<<~RUBY) collection.call(arg)[index_or_key] RUBY end it 'does not register an offense when using space between method call and left brackets' do expect_no_offenses(<<~RUBY) do_something [item_of_array_literal] RUBY end it 'does not register an offense when not using space between variable receiver and left brackets' do expect_no_offenses(<<~RUBY) collection = do_something collection[index_or_key] RUBY end it 'does not register an offense when not using space between method call and left brackets' do expect_no_offenses(<<~RUBY) do_something[item_of_array_literal] RUBY end it 'does not register an offense when array literal argument is enclosed in parentheses' do expect_no_offenses(<<~RUBY) do_something([item_of_array_literal]) RUBY end it 'does not register an offense when it is used as a method argument' do expect_no_offenses(<<~RUBY) expect(offenses).to eq [] RUBY end it 'does not register an offense when using multiple arguments' do expect_no_offenses(<<~RUBY) do_something [foo], bar RUBY end it 'does not register an offense when without receiver' do expect_no_offenses(<<~RUBY) [index_or_key] RUBY end it 'does not register an offense when call desugared `Hash#[]` to lvar receiver' do expect_no_offenses(<<~RUBY) collection.[](index_or_key) RUBY end it 'does not register an offense when call desugared `Hash#[]` to lvar receiver with a space after the dot' do expect_no_offenses(<<~RUBY) collection. [](index_or_key) RUBY end it 'does not register an offense when call desugared `Hash#[]` to lvar receiver with a space before the dot' do expect_no_offenses(<<~RUBY) collection .[](index_or_key) RUBY end it 'does not register an offense when call desugared `Hash#[]` to lvar receiver with a space after safe navigation operator' do expect_no_offenses(<<~RUBY) collection&. [](index_or_key) RUBY end it 'does not register an offense when call desugared `Hash#[]` to lvar receiver with a space before safe navigation operator' do expect_no_offenses(<<~RUBY) collection &.[](index_or_key) RUBY end it 'does not register an offense when call desugared `Hash#[]` to ivar receiver' do expect_no_offenses(<<~RUBY) @collection.[](index_or_key) RUBY end it 'does not register an offense when call desugared `Hash#[]` to cvar receiver' do expect_no_offenses(<<~RUBY) @@collection.[](index_or_key) RUBY end it 'does not register an offense when call desugared `Hash#[]=`' do expect_no_offenses(<<~RUBY) collection.[]=(index_or_key, value) RUBY end end context 'when assigning' do it 'registers an offense and corrects when using space between receiver and left brackets' do expect_offense(<<~RUBY) @correction [index_or_key] = :value ^ Remove the space before the opening brackets. RUBY expect_correction(<<~RUBY) @correction[index_or_key] = :value RUBY end it 'registers an offense and corrects when using space between receiver and left brackets, and a space inside left bracket' do expect_offense(<<~RUBY) @correction [ index_or_key] = :value ^ Remove the space before the opening brackets. RUBY expect_correction(<<~RUBY) @correction[ index_or_key] = :value RUBY end it 'does not register an offense when not using space between receiver and left brackets' do expect_no_offenses(<<~RUBY) @correction[index_or_key] = :value RUBY end it 'does not register an offense when space is used in left bracket' do expect_no_offenses(<<~RUBY) @collections[ index_or_key ] = :value RUBY end it 'does not register an offense when multiple spaces are inserted inside the left bracket' do expect_no_offenses(<<~RUBY) @collections[ index_or_key] = value RUBY end end it 'does not register an offense when assigning an array' do expect_no_offenses(<<~RUBY) task.options = ['--no-output'] RUBY end it 'does not register an offense when using array literal argument without parentheses' do expect_no_offenses(<<~RUBY) before_validation { to_downcase ['email'] } RUBY end it 'does not register an offense when using percent array literal argument without parentheses' do expect_no_offenses(<<~RUBY) before_validation { to_downcase %w[email] } RUBY end end
ruby
MIT
99fa0fdd0481910d7d052f4c2ed01ad36178f404
2026-01-04T15:37:41.211519Z
false
rubocop/rubocop
https://github.com/rubocop/rubocop/blob/99fa0fdd0481910d7d052f4c2ed01ad36178f404/spec/rubocop/cop/layout/hash_alignment_spec.rb
spec/rubocop/cop/layout/hash_alignment_spec.rb
# frozen_string_literal: true RSpec.describe RuboCop::Cop::Layout::HashAlignment, :config do let(:config) do RuboCop::Config.new( 'Layout/HashAlignment' => default_cop_config.merge(cop_config), 'Layout/ArgumentAlignment' => argument_alignment_config ) end let(:default_cop_config) { { 'EnforcedHashRocketStyle' => 'key', 'EnforcedColonStyle' => 'key' } } let(:argument_alignment_config) { { 'EnforcedStyle' => 'with_first_argument' } } shared_examples 'not on separate lines' do it 'accepts single line hash' do expect_no_offenses('func(a: 0, bb: 1)') end it 'accepts several pairs per line' do expect_no_offenses(<<~RUBY) func(a: 1, bb: 2, ccc: 3, dddd: 4) RUBY end it "accepts pairs that don't start a line" do expect_no_offenses(<<~RUBY) render :json => {:a => messages, :b => :json}, :status => 404 def example a( b: :c, d: e( f: g ), h: :i) end RUBY end context 'when using hash value omission', :ruby31 do it 'accepts single line hash' do expect_no_offenses('func(a:, bb:)') end it 'accepts several pairs per line' do expect_no_offenses(<<~RUBY) func(a:, bb:, ccc:, dddd:) RUBY end it "accepts pairs that don't start a line" do expect_no_offenses(<<~RUBY) render :json => {a:, b:}, :status => 404 def example a( b:, c: d( e: ), f:) end RUBY end end end context 'always inspect last argument hash' do let(:cop_config) { { 'EnforcedLastArgumentHashStyle' => 'always_inspect' } } it 'registers an offense and corrects misaligned keys in implicit hash' do expect_offense(<<~RUBY) func(a: 0, b: 1) ^^^^ Align the keys of a hash literal if they span more than one line. RUBY expect_correction(<<~RUBY) func(a: 0, b: 1) RUBY end it 'registers an offense and corrects misaligned keys in explicit hash' do expect_offense(<<~RUBY) func({a: 0, b: 1}) ^^^^ Align the keys of a hash literal if they span more than one line. RUBY expect_correction(<<~RUBY) func({a: 0, b: 1}) RUBY end it 'registers an offense and corrects misaligned keys in implicit hash for super' do expect_offense(<<~RUBY) super(a: 0, b: 1) ^^^^ Align the keys of a hash literal if they span more than one line. RUBY expect_correction(<<~RUBY) super(a: 0, b: 1) RUBY end it 'registers an offense and corrects misaligned keys in explicit hash for super' do expect_offense(<<~RUBY) super({a: 0, b: 1}) ^^^^ Align the keys of a hash literal if they span more than one line. RUBY expect_correction(<<~RUBY) super({a: 0, b: 1}) RUBY end it 'registers an offense and corrects misaligned keys in implicit hash for yield' do expect_offense(<<~RUBY) yield(a: 0, b: 1) ^^^^ Align the keys of a hash literal if they span more than one line. RUBY expect_correction(<<~RUBY) yield(a: 0, b: 1) RUBY end it 'registers an offense and corrects misaligned keys in explicit hash for yield' do expect_offense(<<~RUBY) yield({a: 0, b: 1}) ^^^^ Align the keys of a hash literal if they span more than one line. RUBY expect_correction(<<~RUBY) yield({a: 0, b: 1}) RUBY end it 'registers an offense and corrects misaligned keys in implicit hash for safe navigation' do expect_offense(<<~RUBY) foo&.bar(a: 0, b: 1) ^^^^ Align the keys of a hash literal if they span more than one line. RUBY expect_correction(<<~RUBY) foo&.bar(a: 0, b: 1) RUBY end it 'registers an offense and corrects misaligned keys in explicit hash for safe navigation' do expect_offense(<<~RUBY) foo&.bar({a: 0, b: 1}) ^^^^ Align the keys of a hash literal if they span more than one line. RUBY expect_correction(<<~RUBY) foo&.bar({a: 0, b: 1}) RUBY end context 'when using hash value omission', :ruby31 do it 'registers an offense and corrects misaligned keys in implicit hash' do expect_offense(<<~RUBY) func(a:, b:) ^^ Align the keys of a hash literal if they span more than one line. RUBY expect_correction(<<~RUBY) func(a:, b:) RUBY end it 'registers an offense and corrects misaligned keys in explicit hash' do expect_offense(<<~RUBY) func({a:, b:}) ^^ Align the keys of a hash literal if they span more than one line. RUBY expect_correction(<<~RUBY) func({a:, b:}) RUBY end end end context 'when `EnforcedStyle: with_fixed_indentation` of `ArgumentAlignment`' do let(:argument_alignment_config) { { 'EnforcedStyle' => 'with_fixed_indentation' } } it 'registers and corrects an offense' do expect_offense(<<~RUBY) THINGS = { oh: :io, hi: 'neat' ^^^^^^^^^^ Align the keys of a hash literal if they span more than one line. } RUBY expect_correction(<<~RUBY) THINGS = { oh: :io, hi: 'neat' } RUBY end it 'registers and corrects an offense when using misaligned keyword arguments' do expect_offense(<<~RUBY) config.fog_credentials_as_kwargs( provider: 'AWS', ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Align the keys of a hash literal if they span more than one line. aws_access_key_id: ENV['S3_ACCESS_KEY'], ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Align the keys of a hash literal if they span more than one line. aws_secret_access_key: ENV['S3_SECRET'], region: ENV['S3_REGION'], ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Align the keys of a hash literal if they span more than one line. ) RUBY expect_correction(<<~RUBY) config.fog_credentials_as_kwargs( provider: 'AWS', aws_access_key_id: ENV['S3_ACCESS_KEY'], aws_secret_access_key: ENV['S3_SECRET'], region: ENV['S3_REGION'], ) RUBY end context 'when using hash value omission', :ruby31 do it 'registers and corrects an offense' do expect_offense(<<~RUBY) THINGS = { oh:, hi: ^^^ Align the keys of a hash literal if they span more than one line. } RUBY expect_correction(<<~RUBY) THINGS = { oh:, hi: } RUBY end it 'does not register an offense when using aligned keyword arguments' do expect_no_offenses(<<~RUBY) config.fog_credentials_as_kwargs( provider:, aws_access_key_id:, aws_secret_access_key:, region: ) RUBY end end it 'does not register an offense using aligned hash literal' do expect_no_offenses(<<~RUBY) { oh: :io, hi: 'neat' } RUBY end it 'does not register an offense for an empty hash literal' do expect_no_offenses(<<~RUBY) foo({}) RUBY end it 'does not register an offense using aligned hash argument for `proc.()`' do expect_no_offenses(<<~RUBY) proc.(key: value) RUBY end it 'does not register an offense for a method with a positional argument' do expect_no_offenses(<<~RUBY) do_something( foo, baz: true, quux: false ) RUBY end it 'does not register an offense for a method with a positional argument that spans multiple lines' do expect_no_offenses(<<~RUBY) do_something( foo( bar ), baz: true, quux: false ) RUBY end it 'does not register an offense when a multiline hash starts on the same line as an implicit `call`' do expect_no_offenses(<<~RUBY) do_something.(foo: bar, baz: qux, quux: corge) RUBY end end context 'always ignore last argument hash' do let(:cop_config) { { 'EnforcedLastArgumentHashStyle' => 'always_ignore' } } it 'accepts misaligned keys in implicit hash' do expect_no_offenses(<<~RUBY) func(a: 0, b: 1) RUBY end it 'accepts misaligned keys in explicit hash' do expect_no_offenses(<<~RUBY) func({a: 0, b: 1}) RUBY end it 'accepts misaligned keys in implicit hash for super' do expect_no_offenses(<<~RUBY) super(a: 0, b: 1) RUBY end it 'accepts misaligned keys in explicit hash for super' do expect_no_offenses(<<~RUBY) super({a: 0, b: 1}) RUBY end it 'accepts misaligned keys in implicit hash for yield' do expect_no_offenses(<<~RUBY) yield(a: 0, b: 1) RUBY end it 'accepts misaligned keys in explicit hash for yield' do expect_no_offenses(<<~RUBY) yield({a: 0, b: 1}) RUBY end it 'accepts misaligned keys in implicit hash for safe navigation' do expect_no_offenses(<<~RUBY) foo&.bar(a: 0, b: 1) RUBY end it 'accepts misaligned keys in explicit hash for safe navigation' do expect_no_offenses(<<~RUBY) foo&.bar({a: 0, b: 1}) RUBY end end context 'ignore implicit last argument hash' do let(:cop_config) { { 'EnforcedLastArgumentHashStyle' => 'ignore_implicit' } } it 'accepts misaligned keys in implicit hash' do expect_no_offenses(<<~RUBY) func(a: 0, b: 1) RUBY end it 'registers an offense and corrects misaligned keys in explicit hash' do expect_offense(<<~RUBY) func({a: 0, b: 1}) ^^^^ Align the keys of a hash literal if they span more than one line. RUBY expect_correction(<<~RUBY) func({a: 0, b: 1}) RUBY end it 'accepts misaligned keys in implicit hash for super' do expect_no_offenses(<<~RUBY) super(a: 0, b: 1) RUBY end it 'registers an offense and corrects misaligned keys in explicit hash for super' do expect_offense(<<~RUBY) super({a: 0, b: 1}) ^^^^ Align the keys of a hash literal if they span more than one line. RUBY expect_correction(<<~RUBY) super({a: 0, b: 1}) RUBY end it 'accepts misaligned keys in implicit hash for yield' do expect_no_offenses(<<~RUBY) yield(a: 0, b: 1) RUBY end it 'registers an offense and corrects misaligned keys in explicit hash for yield' do expect_offense(<<~RUBY) yield({a: 0, b: 1}) ^^^^ Align the keys of a hash literal if they span more than one line. RUBY expect_correction(<<~RUBY) yield({a: 0, b: 1}) RUBY end it 'accepts misaligned keys in implicit hash for safe navigation' do expect_no_offenses(<<~RUBY) foo&.bar(a: 0, b: 1) RUBY end it 'registers an offense and corrects misaligned keys in explicit hash for safe navigation' do expect_offense(<<~RUBY) foo&.bar({a: 0, b: 1}) ^^^^ Align the keys of a hash literal if they span more than one line. RUBY expect_correction(<<~RUBY) foo&.bar({a: 0, b: 1}) RUBY end end context 'ignore explicit last argument hash' do let(:cop_config) { { 'EnforcedLastArgumentHashStyle' => 'ignore_explicit' } } it 'registers an offense and corrects misaligned keys in implicit hash' do expect_offense(<<~RUBY) func(a: 0, b: 1) ^^^^ Align the keys of a hash literal if they span more than one line. RUBY expect_correction(<<~RUBY) func(a: 0, b: 1) RUBY end it 'accepts misaligned keys in explicit hash' do expect_no_offenses(<<~RUBY) func({a: 0, b: 1}) RUBY end context 'when using hash value omission', :ruby31 do it 'registers an offense and corrects misaligned keys in implicit hash' do expect_offense(<<~RUBY) func(a:, b:) ^^ Align the keys of a hash literal if they span more than one line. RUBY expect_correction(<<~RUBY) func(a:, b:) RUBY end it 'accepts misaligned keys in explicit hash' do expect_no_offenses(<<~RUBY) func({a:, b:}) RUBY end end it 'registers an offense and corrects misaligned keys in implicit hash for super' do expect_offense(<<~RUBY) super(a: 0, b: 1) ^^^^ Align the keys of a hash literal if they span more than one line. RUBY expect_correction(<<~RUBY) super(a: 0, b: 1) RUBY end it 'accepts misaligned keys in explicit hash for super' do expect_no_offenses(<<~RUBY) super({a: 0, b: 1}) RUBY end it 'registers an offense and corrects misaligned keys in implicit hash for yield' do expect_offense(<<~RUBY) yield(a: 0, b: 1) ^^^^ Align the keys of a hash literal if they span more than one line. RUBY expect_correction(<<~RUBY) yield(a: 0, b: 1) RUBY end it 'accepts misaligned keys in explicit hash for yield' do expect_no_offenses(<<~RUBY) yield({a: 0, b: 1}) RUBY end it 'registers an offense and corrects misaligned keys in implicit hash for safe navigation' do expect_offense(<<~RUBY) foo&.bar(a: 0, b: 1) ^^^^ Align the keys of a hash literal if they span more than one line. RUBY expect_correction(<<~RUBY) foo&.bar(a: 0, b: 1) RUBY end it 'accepts misaligned keys in explicit hash for safe navigation' do expect_no_offenses(<<~RUBY) foo&.bar({a: 0, b: 1}) RUBY end end context 'with default configuration' do it 'registers an offense and corrects misaligned hash keys' do expect_offense(<<~RUBY) hash1 = { a: 0, bb: 1 ^^^^^ Align the keys of a hash literal if they span more than one line. } hash2 = { 'ccc' => 2, 'dddd' => 2 ^^^^^^^^^^^^^ Align the keys of a hash literal if they span more than one line. } RUBY expect_correction(<<~RUBY) hash1 = { a: 0, bb: 1 } hash2 = { 'ccc' => 2, 'dddd' => 2 } RUBY end it 'registers an offense and corrects misaligned mixed multiline hash keys' do expect_offense(<<~RUBY) hash = { a: 1, b: 2, c: 3 } ^^^^ Align the keys of a hash literal if they span more than one line. RUBY expect_correction(<<~RUBY) hash = { a: 1, b: 2, c: 3 } RUBY end it 'accepts left-aligned hash keys with single spaces' do expect_no_offenses(<<~RUBY) hash1 = { aa: 0, b: 1, } hash2 = { :a => 0, :bb => 1 } hash3 = { 'a' => 0, 'bb' => 1 } RUBY end it 'registers an offense and corrects zero or multiple spaces' do expect_offense(<<~RUBY) hash1 = { a: 0, ^^^^^^ Align the keys of a hash literal if they span more than one line. bb:1, ^^^^ Align the keys of a hash literal if they span more than one line. } hash2 = { 'ccc'=> 2, ^^^^^^^^^ Align the keys of a hash literal if they span more than one line. 'dddd' => 3 ^^^^^^^^^^^^ Align the keys of a hash literal if they span more than one line. } RUBY expect_correction(<<~RUBY) hash1 = { a: 0, bb: 1, } hash2 = { 'ccc' => 2, 'dddd' => 3 } RUBY end it 'registers an offense and corrects separator alignment' do expect_offense(<<~RUBY) hash = { 'a' => 0, 'bbb' => 1 ^^^^^^^^^^ Align the keys of a hash literal if they span more than one line. } RUBY expect_correction(<<~RUBY) hash = { 'a' => 0, 'bbb' => 1 } RUBY end it 'registers an offense and corrects table alignment' do expect_offense(<<~RUBY) hash = { 'a' => 0, ^^^^^^^^^^ Align the keys of a hash literal if they span more than one line. 'bbb' => 1 } RUBY expect_correction(<<~RUBY) hash = { 'a' => 0, 'bbb' => 1 } RUBY end it 'registers an offense and corrects multiline value starts in wrong place' do expect_offense(<<~RUBY) hash = { 'a' => ( ^^^^^^^^^ Align the keys of a hash literal if they span more than one line. ), 'bbb' => 1 } RUBY expect_correction(<<~RUBY) hash = { 'a' => ( ), 'bbb' => 1 } RUBY end it 'does not register an offense when value starts on next line' do expect_no_offenses(<<~RUBY) hash = { 'a' => 0, 'bbb' => 1 } RUBY end context 'with implicit hash as last argument' do it 'registers an offense and corrects misaligned hash keys' do expect_offense(<<~RUBY) func(a: 0, b: 1) ^^^^ Align the keys of a hash literal if they span more than one line. RUBY expect_correction(<<~RUBY) func(a: 0, b: 1) RUBY end it 'registers an offense and corrects right alignment of keys' do expect_offense(<<~RUBY) func(a: 0, bbb: 1) ^^^^^^ Align the keys of a hash literal if they span more than one line. RUBY expect_correction(<<~RUBY) func(a: 0, bbb: 1) RUBY end it 'accepts aligned hash keys' do expect_no_offenses(<<~RUBY) func(a: 0, b: 1) RUBY end it 'accepts an empty hash' do expect_no_offenses('h = {}') end end it 'registers an offense and corrects mixed hash styles' do expect_offense(<<~RUBY) hash1 = { a: 0, bb: 1, ^^^^^ Align the keys of a hash literal if they span more than one line. ccc: 2 } ^^^^^^ Align the keys of a hash literal if they span more than one line. hash2 = { :a => 0, ^^^^^^^^^ Align the keys of a hash literal if they span more than one line. :bb => 1, ^^^^^^^^^ Align the keys of a hash literal if they span more than one line. :ccc =>2 } ^^^^^^^^^ Align the keys of a hash literal if they span more than one line. hash3 = { 'a' => 0, ^^^^^^^^^^^^ Align the keys of a hash literal if they span more than one line. 'bb' => 1, ^^^^^^^^^^ Align the keys of a hash literal if they span more than one line. 'ccc' =>2 } ^^^^^^^^^^ Align the keys of a hash literal if they span more than one line. RUBY expect_correction(<<~RUBY) hash1 = { a: 0, bb: 1, ccc: 2 } hash2 = { :a => 0, :bb => 1, :ccc => 2 } hash3 = { 'a' => 0, 'bb' => 1, 'ccc' => 2 } RUBY end it 'registers an offense and corrects alignment when using double splat in an explicit hash' do expect_offense(<<~RUBY) Hash(foo: 'bar', **extra_params ^^^^^^^^^^^^^^ Align keyword splats with the rest of the hash if it spans more than one line. ) RUBY expect_correction(<<~RUBY) Hash(foo: 'bar', **extra_params ) RUBY end it 'registers an offense and corrects alignment when using double splat in braces' do expect_offense(<<~RUBY) {foo: 'bar', **extra_params ^^^^^^^^^^^^^^ Align keyword splats with the rest of the hash if it spans more than one line. } RUBY expect_correction(<<~RUBY) {foo: 'bar', **extra_params } RUBY end end it_behaves_like 'not on separate lines' context 'with table alignment configuration' do let(:cop_config) do { 'EnforcedHashRocketStyle' => 'table', 'EnforcedColonStyle' => 'table' } end it_behaves_like 'not on separate lines' it 'accepts aligned hash keys and values' do expect_no_offenses(<<~RUBY) hash1 = { 'a' => 0, 'bbb' => 1 } hash2 = { a: 0, bbb: 1 } RUBY end it 'accepts an empty hash' do expect_no_offenses('h = {}') end context 'when using hash value omission', :ruby31 do it 'accepts aligned hash keys and values' do expect_no_offenses(<<~RUBY) hash1 = { 'a' => 0, 'bbb' => 1 } hash2 = { a: 0, bbb: 1, ccc: } RUBY end end it 'accepts a multiline array of single line hashes' do expect_no_offenses(<<~RUBY) def self.scenarios_order [ { before: %w( l k ) }, { ending: %w( m l ) }, { starting: %w( m n ) }, { after: %w( n o ) } ] end RUBY end it 'accepts hashes that use different separators' do expect_no_offenses(<<~RUBY) hash = { a: 1, 'bbb' => 2 } RUBY end it 'accepts hashes that use different separators and double splats' do expect_no_offenses(<<~RUBY) hash = { a: 1, 'bbb' => 2, **foo } RUBY end it 'accepts a symbol only hash followed by a keyword splat' do expect_no_offenses(<<~RUBY) hash = { a: 1, **kw } RUBY end it 'accepts a keyword splat only hash' do expect_no_offenses(<<~RUBY) hash = { **kw } RUBY end it 'registers an offense for misaligned hash values' do expect_offense(<<~RUBY) hash1 = { 'a' => 0, ^^^^^^^^^^^ Align the keys and values of a hash literal if they span more than one line. 'bbb' => 1 } hash2 = { a: 0, bbb:1 ^^^^^ Align the keys and values of a hash literal if they span more than one line. } RUBY end it 'registers an offense and corrects for misaligned hash keys' do expect_offense(<<~RUBY) hash1 = { 'a' => 0, ^^^^^^^^^^^ Align the keys and values of a hash literal if they span more than one line. 'bbb' => 1 ^^^^^^^^^^^^ Align the keys and values of a hash literal if they span more than one line. } hash2 = { a: 0, ^^^^^ Align the keys and values of a hash literal if they span more than one line. bbb: 1 ^^^^^^ Align the keys and values of a hash literal if they span more than one line. } RUBY expect_correction(<<~RUBY) hash1 = { 'a' => 0, 'bbb' => 1 } hash2 = { a: 0, bbb: 1 } RUBY end it 'registers an offense and corrects misaligned hash rockets' do expect_offense(<<~RUBY) hash = { 'a' => 0, 'bbb' => 1 ^^^^^^^^^^^ Align the keys and values of a hash literal if they span more than one line. } RUBY expect_correction(<<~RUBY) hash = { 'a' => 0, 'bbb' => 1 } RUBY end end context 'with table+separator alignment configuration' do let(:cop_config) do { 'EnforcedHashRocketStyle' => 'table', 'EnforcedColonStyle' => 'separator' } end it 'accepts a single method argument entry with colon' do expect_no_offenses('merge(parent: nil)') end end context 'with invalid configuration' do let(:cop_config) { { 'EnforcedHashRocketStyle' => 'junk', 'EnforcedColonStyle' => 'junk' } } it 'fails' do expect do expect_offense(<<~RUBY) hash = { a: 0, bb: 1 } RUBY end.to raise_error(RuntimeError) end end context 'with separator alignment configuration' do let(:cop_config) do { 'EnforcedHashRocketStyle' => 'separator', 'EnforcedColonStyle' => 'separator' } end it 'accepts aligned hash keys' do expect_no_offenses(<<~RUBY) hash1 = { a: 0, bbb: 1 } hash2 = { 'a' => 0, 'bbb' => 1 } RUBY end context 'when using hash value omission', :ruby31 do it 'accepts aligned hash keys' do expect_no_offenses(<<~RUBY) hash1 = { a: 0, bbb: 1, ccc: } hash2 = { 'a' => 0, 'bbb' => 1 } RUBY end it 'registers an offense and corrects mixed indentation and spacing' do expect_offense(<<~RUBY) hash1 = { a: 0, bb:, ^^^ Align the separators of a hash literal if they span more than one line. ccc: 2 } ^^^^^^ Align the separators of a hash literal if they span more than one line. RUBY expect_correction(<<~RUBY) hash1 = { a: 0, bb:, ccc: 2 } RUBY end end it 'accepts an empty hash' do expect_no_offenses('h = {}') end it 'registers an offense and corrects misaligned hash values' do expect_offense(<<~RUBY) hash = { 'a' => 0, 'bbb' => 1 ^^^^^^^^^^ Align the separators of a hash literal if they span more than one line. } RUBY expect_correction(<<~RUBY) hash = { 'a' => 0, 'bbb' => 1 } RUBY end it 'registers an offense and corrects misaligned hash rockets' do expect_offense(<<~RUBY) hash = { 'a' => 0, 'bbb' => 1 ^^^^^^^^^^^ Align the separators of a hash literal if they span more than one line. } RUBY expect_correction(<<~RUBY) hash = { 'a' => 0, 'bbb' => 1 } RUBY end it 'accepts hashes with different separators' do expect_no_offenses(<<~RUBY) {a: 1, 'b' => 2, **params} RUBY end it_behaves_like 'not on separate lines' it 'registers an offense and corrects mixed indentation and spacing' do expect_offense(<<~RUBY) hash1 = { a: 0, bb: 1, ^^^^^^^^ Align the separators of a hash literal if they span more than one line. ccc: 2 } ^^^^^^ Align the separators of a hash literal if they span more than one line. hash2 = { a => 0, bb => 1, ^^^^^^^^^^ Align the separators of a hash literal if they span more than one line. ccc =>2 } ^^^^^^^^ Align the separators of a hash literal if they span more than one line. RUBY expect_correction(<<~RUBY) hash1 = { a: 0, bb: 1, ccc: 2 } hash2 = { a => 0, bb => 1, ccc => 2 } RUBY end it "doesn't break code by moving long keys too far left" do # regression test; see GH issue 2582 expect_offense(<<~RUBY) { sjtjo: sjtjo, too_ono_ilitjion_tofotono_o: too_ono_ilitjion_tofotono_o, ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Align the separators of a hash literal if they span more than one line. } RUBY expect_correction(<<~RUBY) { sjtjo: sjtjo, too_ono_ilitjion_tofotono_o: too_ono_ilitjion_tofotono_o, } RUBY end end context 'with multiple preferred(key and table) alignment configuration' do let(:cop_config) do { 'EnforcedHashRocketStyle' => %w[key table], 'EnforcedColonStyle' => %w[key table] } end it 'accepts aligned hash keys, by keys' do expect_no_offenses(<<~RUBY) hash1 = { a: 0, bbb: 1 } hash2 = { 'a' => 0, 'bbb' => 1 } RUBY end it 'accepts aligned hash keys, by table' do expect_no_offenses(<<~RUBY) hash1 = { a: 0, bbb: 1 } hash2 = { 'a' => 0, 'bbb' => 1 } RUBY end it 'accepts aligned hash keys, by both' do expect_no_offenses(<<~RUBY) hash1 = { a: 0, bbb: 1 } hash2 = { a: 0, bbb: 1 } hash3 = { 'a' => 0, 'bbb' => 1 } hash4 = { 'a' => 0, 'bbb' => 1 } RUBY end it 'accepts aligned hash keys with mixed hash style' do expect_no_offenses(<<~RUBY) headers = { "Content-Type" => 0, Authorization: 1 } RUBY end it 'accepts an empty hash' do expect_no_offenses('h = {}') end it 'registers an offense and corrects misaligned hash values' do expect_offense(<<~RUBY) hash = { 'a' => 0, ^^^^^^^^^ Align the keys of a hash literal if they span more than one line. 'bbb' => 1 ^^^^^^^^^^ Align the keys of a hash literal if they span more than one line. } RUBY expect_correction(<<~RUBY) hash = { 'a' => 0, 'bbb' => 1 } RUBY end it 'registers an offense and corrects misaligned hash values, ' \ 'prefer table when least offenses' do expect_offense(<<~RUBY) hash = { 'abcdefg' => 0, 'abcdef' => 0, 'gijk' => 0, 'a' => 0, 'b' => 1, ^^^^^^^^ Align the keys and values of a hash literal if they span more than one line. 'c' => 1 ^^^^^^^^ Align the keys and values of a hash literal if they span more than one line. } RUBY expect_correction(<<~RUBY) hash = { 'abcdefg' => 0, 'abcdef' => 0, 'gijk' => 0, 'a' => 0, 'b' => 1, 'c' => 1 } RUBY end it 'registers an offense and corrects misaligned hash values, prefer key when least offenses' do expect_offense(<<~RUBY) hash = { 'abcdefg' => 0, 'abcdef' => 0, ^^^^^^^^^^^^^^ Align the keys of a hash literal if they span more than one line.
ruby
MIT
99fa0fdd0481910d7d052f4c2ed01ad36178f404
2026-01-04T15:37:41.211519Z
true
rubocop/rubocop
https://github.com/rubocop/rubocop/blob/99fa0fdd0481910d7d052f4c2ed01ad36178f404/spec/rubocop/cop/layout/space_inside_percent_literal_delimiters_spec.rb
spec/rubocop/cop/layout/space_inside_percent_literal_delimiters_spec.rb
# frozen_string_literal: true RSpec.describe RuboCop::Cop::Layout::SpaceInsidePercentLiteralDelimiters, :config do let(:message) { 'Do not use spaces inside percent literal delimiters.' } %w[i I w W x].each do |type| [%w[{ }], %w[( )], %w([ ]), %w[! !]].each do |(ldelim, rdelim)| context "for #{type} type and #{[ldelim, rdelim]} delimiters" do define_method(:code_example) { |content| ['%', type, ldelim, content, rdelim].join } it 'registers an offense for unnecessary spaces' do expect_offense(<<~RUBY) #{code_example(' 1 2 ')} ^^ #{message} ^ #{message} RUBY expect_correction("#{code_example('1 2')}\n") end it 'registers an offense for spaces after first delimiter' do expect_offense(<<~RUBY) #{code_example(' 1 2')} ^ #{message} RUBY expect_correction("#{code_example('1 2')}\n") end it 'registers an offense for spaces before final delimiter' do expect_offense(<<~RUBY) #{code_example('1 2 ')} ^ #{message} RUBY expect_correction("#{code_example('1 2')}\n") end it 'registers an offense for literals with escaped and other spaces' do expect_offense(<<~RUBY) #{code_example(' \ a b c\ ')} ^ #{message} ^ #{message} RUBY expect_correction("#{code_example('\ a b c\ ')}\n") end it 'accepts literals without additional spaces' do expect_no_offenses(code_example('a b c')) end it 'accepts literals with escaped spaces' do expect_no_offenses(code_example('\ a b c\ ')) end it 'accepts multi-line literals' do expect_no_offenses(<<~RUBY) %#{type}( a b c ) RUBY end it 'accepts multi-line literals within a method' do expect_no_offenses(<<~RUBY) def foo %#{type}( a b c ) end RUBY end it 'accepts newlines and additional following alignment spaces' do expect_no_offenses(<<~RUBY) %#{type}(a b c) RUBY end it 'accepts spaces between entries' do expect_no_offenses(code_example('a b c')) end context 'with space in blank percent literals' do it 'registers and corrects an offense' do expect_offense(<<~RUBY) #{code_example(' ')} ^ #{message} RUBY expect_correction("#{code_example('')}\n") end end context 'with spaces in blank percent literals' do it 'registers and corrects an offense' do expect_offense(<<~RUBY) #{code_example(' ')} ^^ #{message} RUBY expect_correction("#{code_example('')}\n") end end context 'with newline in blank percent literals' do it 'registers and corrects an offense' do expect_offense(code_example("\n").lines.insert(1, " ^{} #{message}\n").join) expect_correction(code_example('').to_s) end end end end end it 'accepts other percent literals' do expect_no_offenses(<<~RUBY) %q( a b c ) %r( a b c ) %s( a b c ) RUBY end it 'accepts execute-string literals' do expect_no_offenses('` curl `') end end
ruby
MIT
99fa0fdd0481910d7d052f4c2ed01ad36178f404
2026-01-04T15:37:41.211519Z
false
rubocop/rubocop
https://github.com/rubocop/rubocop/blob/99fa0fdd0481910d7d052f4c2ed01ad36178f404/spec/rubocop/cop/layout/heredoc_indentation_spec.rb
spec/rubocop/cop/layout/heredoc_indentation_spec.rb
# frozen_string_literal: true RSpec.describe RuboCop::Cop::Layout::HeredocIndentation, :config do let(:allow_heredoc) { true } let(:other_cops) { { 'Layout/LineLength' => { 'Max' => 5, 'AllowHeredoc' => allow_heredoc } } } shared_examples 'all heredoc type' do |quote| context "quoted by #{quote}" do it 'does not register an offense when not indented but with whitespace, with `-`' do expect_no_offenses(<<-RUBY) def foo <<-#{quote}RUBY2#{quote} something RUBY2 end RUBY end it 'accepts for indented, but with `-`' do expect_no_offenses(<<~RUBY) def foo <<-#{quote}RUBY2#{quote} something RUBY2 end RUBY end it 'accepts for not indented but with whitespace' do expect_no_offenses(<<~RUBY) def foo <<#{quote}RUBY2#{quote} something RUBY2 end RUBY end it 'accepts for indented, but without `~`' do expect_no_offenses(<<~RUBY) def foo <<#{quote}RUBY2#{quote} something RUBY2 end RUBY end it 'accepts for an empty line' do expect_no_offenses(<<~RUBY) <<-#{quote}RUBY2#{quote} RUBY2 RUBY end context 'when Layout/LineLength is configured' do let(:allow_heredoc) { false } it 'accepts for long heredoc' do expect_no_offenses(<<~RUBY) <<#{quote}RUBY2#{quote} 12345678 RUBY2 RUBY end end it 'registers an offense for not indented' do expect_offense(<<~RUBY) <<~#{quote}RUBY2#{quote} something ^^^^^^^^^ Use 2 spaces for indentation in a heredoc. RUBY2 RUBY expect_correction(<<~RUBY) <<~#{quote}RUBY2#{quote} something RUBY2 RUBY end it 'registers an offense for minus level indented' do expect_offense(<<~RUBY) def foo <<~#{quote}RUBY2#{quote} something ^^^^^^^^^ Use 2 spaces for indentation in a heredoc. RUBY2 end RUBY expect_correction(<<~RUBY) def foo <<~#{quote}RUBY2#{quote} something RUBY2 end RUBY end it 'registers an offense for too deep indented' do expect_offense(<<~RUBY) <<~#{quote}RUBY2#{quote} something ^^^^^^^^^^^^^ Use 2 spaces for indentation in a heredoc. RUBY2 RUBY expect_correction(<<~RUBY) <<~#{quote}RUBY2#{quote} something RUBY2 RUBY end it 'registers an offense for not indented, without `~`' do expect_offense(<<~RUBY) <<#{quote}RUBY2#{quote} foo ^^^ Use 2 spaces for indentation in a heredoc by using `<<~` instead of `<<`. RUBY2 RUBY expect_correction(<<~RUBY) <<~#{quote}RUBY2#{quote} foo RUBY2 RUBY end it 'registers an offense for not indented, with `~`' do expect_offense(<<~RUBY) <<~#{quote}RUBY2#{quote} foo ^^^ Use 2 spaces for indentation in a heredoc. RUBY2 RUBY expect_correction(<<~RUBY) <<~#{quote}RUBY2#{quote} foo RUBY2 RUBY end it 'registers an offense for first line minus-level indented, with `-`' do expect_offense(<<~RUBY) puts <<-#{quote}RUBY2#{quote} def foo ^^^^^^^ Use 2 spaces for indentation in a heredoc by using `<<~` instead of `<<-`. bar end RUBY2 RUBY expect_correction(<<-RUBY) puts <<~#{quote}RUBY2#{quote} def foo bar end RUBY2 RUBY end it 'accepts for indented, with `~`' do expect_no_offenses(<<~RUBY) <<~#{quote}RUBY2#{quote} something RUBY2 RUBY end it 'accepts for include empty lines' do expect_no_offenses(<<~RUBY) <<~#{quote}MSG#{quote} foo bar MSG RUBY end { empty: '', whitespace: ' ' }.each do |description, line| it "registers an offense for not indented enough with #{description} line" do # Using <<- in this section makes the code more readable. # rubocop:disable Layout/HeredocIndentation expect_offense(<<-RUBY) def baz <<~#{quote}MSG#{quote} foo ^^^^^^^^^^^^^^^^^ Use 2 spaces for indentation in a heredoc. #{line} bar MSG end RUBY expect_correction(<<-RUBY) def baz <<~#{quote}MSG#{quote} foo #{line} bar MSG end RUBY end it "registers an offense for too deep indented with #{description} line" do expect_offense(<<-RUBY) <<~#{quote}RUBY2#{quote} foo ^^^^^^^^^^^^^^^^^^^^^ Use 2 spaces for indentation in a heredoc. #{line} bar RUBY2 RUBY expect_correction(<<-RUBY) <<~#{quote}RUBY2#{quote} foo #{line} bar RUBY2 RUBY end # rubocop:enable Layout/HeredocIndentation end it 'displays message to use `<<~` instead of `<<`' do expect_offense(<<~RUBY) <<RUBY2 foo ^^^ Use 2 spaces for indentation in a heredoc by using `<<~` instead of `<<`. RUBY2 RUBY end it 'displays message to use `<<~` instead of `<<-`' do expect_offense(<<~RUBY) <<-RUBY2 foo ^^^ Use 2 spaces for indentation in a heredoc by using `<<~` instead of `<<-`. RUBY2 RUBY end end context 'when `Layout/LineLength` is disabled' do let(:other_cops) { { 'Layout/LineLength' => { 'Enabled' => false } } } it 'registers an offense' do expect_offense(<<~RUBY) <<~#{quote}RUBY2#{quote} something ^^^^^^^^^ Use 2 spaces for indentation in a heredoc. RUBY2 RUBY expect_correction(<<~RUBY) <<~#{quote}RUBY2#{quote} something RUBY2 RUBY end end end context 'when Ruby >= 2.3', :ruby23 do [nil, "'", '"', '`'].each { |quote| it_behaves_like 'all heredoc type', quote } end context 'when Ruby <= 2.2', :ruby22, unsupported_on: :prism do it 'does not register an offense' do expect_no_offenses(<<~RUBY) <<-RUBY2 foo RUBY2 RUBY end end end
ruby
MIT
99fa0fdd0481910d7d052f4c2ed01ad36178f404
2026-01-04T15:37:41.211519Z
false
rubocop/rubocop
https://github.com/rubocop/rubocop/blob/99fa0fdd0481910d7d052f4c2ed01ad36178f404/spec/rubocop/cop/layout/multiline_method_parameter_line_breaks_spec.rb
spec/rubocop/cop/layout/multiline_method_parameter_line_breaks_spec.rb
# frozen_string_literal: true RSpec.describe RuboCop::Cop::Layout::MultilineMethodParameterLineBreaks, :config do context 'when one parameter on same line' do it 'does not add any offenses' do expect_no_offenses(<<~RUBY) def taz(abc) end RUBY end end context 'when there are no parameters' do it 'does not add any offenses' do expect_no_offenses(<<~RUBY) def taz end RUBY end end context 'when two parameters are on next line' do it 'does not add any offenses' do expect_no_offenses(<<~RUBY) def taz( foo, bar ) end RUBY end end context 'when many parameter are on multiple lines, two on same line' do it 'registers an offense and corrects' do expect_offense(<<~RUBY) def taz(abc, foo, bar, ^^^ Each parameter in a multi-line method definition must start on a separate line. baz ) end RUBY expect_correction(<<~RUBY) def taz(abc, foo,#{trailing_whitespace} bar, baz ) end RUBY end end context 'when many parameters are on multiple lines, three on same line' do it 'registers an offense and corrects' do expect_offense(<<~RUBY) def taz(abc, foo, bar, barz, ^^^^ Each parameter in a multi-line method definition must start on a separate line. ^^^ Each parameter in a multi-line method definition must start on a separate line. baz ) end RUBY expect_correction(<<~RUBY) def taz(abc, foo,#{trailing_whitespace} bar,#{trailing_whitespace} barz, baz ) end RUBY end end context 'when many parameters including hash are on multiple lines, three on same line' do it 'registers an offense and corrects' do expect_offense(<<~RUBY) def taz(abc, foo, bar, z: "barz", ^^^^^^^^^ Each parameter in a multi-line method definition must start on a separate line. ^^^ Each parameter in a multi-line method definition must start on a separate line. x: ) end RUBY expect_correction(<<~RUBY) def taz(abc, foo,#{trailing_whitespace} bar,#{trailing_whitespace} z: "barz", x: ) end RUBY end end context 'when parameter\'s default value starts on same line but ends on different line' do it 'registers an offense and corrects' do expect_offense(<<~RUBY) def taz(abc, foo = { ^^^^^^^ Each parameter in a multi-line method definition must start on a separate line. foo: "edf", }) end RUBY expect_correction(<<~RUBY) def taz(abc,#{trailing_whitespace} foo = { foo: "edf", }) end RUBY end end context 'when second parameter starts on same line as end of first' do it 'registers an offense and corrects' do expect_offense(<<~RUBY) def taz(abc = { foo: "edf", }, bar:) ^^^^ Each parameter in a multi-line method definition must start on a separate line. end RUBY expect_correction(<<~RUBY) def taz(abc = { foo: "edf", },#{trailing_whitespace} bar:) end RUBY end end context 'when there are multiple parameters on the first line' do it 'registers an offense and corrects starting from the 2nd argument' do expect_offense(<<~RUBY) def do_something(foo, bar, baz, ^^^ Each parameter in a multi-line method definition must start on a separate line. ^^^ Each parameter in a multi-line method definition must start on a separate line. quux) end RUBY expect_correction(<<~RUBY) def do_something(foo,#{trailing_whitespace} bar,#{trailing_whitespace} baz, quux) end RUBY end end context 'ignore last element' do let(:cop_config) { { 'AllowMultilineFinalElement' => true } } it 'ignores last parameter that value is a multiline hash' do expect_no_offenses(<<~RUBY) def foo(abc, foo, bar = { a: 1, }) end RUBY end it 'registers and corrects arguments that are multiline hashes and not the last argument' do expect_offense(<<~RUBY) def foo(abc, foo, bar = { ^^^^^^^ Each parameter in a multi-line method definition must start on a separate line. ^^^ Each parameter in a multi-line method definition must start on a separate line. a: 1, }, buz) end RUBY expect_correction(<<~RUBY) def foo(abc,#{trailing_whitespace} foo,#{trailing_whitespace} bar = { a: 1, },#{trailing_whitespace} buz) end RUBY end it 'registers and corrects last argument that starts on a new line' do expect_offense(<<~RUBY) def foo(abc, foo, ghi, ^^^ Each parameter in a multi-line method definition must start on a separate line. ^^^ Each parameter in a multi-line method definition must start on a separate line. jkl) end RUBY expect_correction(<<~RUBY) def foo(abc,#{trailing_whitespace} foo,#{trailing_whitespace} ghi, jkl) end RUBY end end context 'when a class method definition is used' do context 'when all parameters are on the same line' do it 'does not add any offenses' do expect_no_offenses(<<~RUBY) def self.taz( foo, bar ) end RUBY end end context 'when not all parameters are on separate lines' do it 'registers an offense and corrects' do expect_offense(<<~RUBY) def self.taz(abc, foo, bar, ^^^ Each parameter in a multi-line method definition must start on a separate line. baz ) end RUBY expect_correction(<<~RUBY) def self.taz(abc, foo,#{trailing_whitespace} bar, baz ) end RUBY end end end end
ruby
MIT
99fa0fdd0481910d7d052f4c2ed01ad36178f404
2026-01-04T15:37:41.211519Z
false
rubocop/rubocop
https://github.com/rubocop/rubocop/blob/99fa0fdd0481910d7d052f4c2ed01ad36178f404/spec/rubocop/cop/layout/empty_lines_around_block_body_spec.rb
spec/rubocop/cop/layout/empty_lines_around_block_body_spec.rb
# frozen_string_literal: true RSpec.describe RuboCop::Cop::Layout::EmptyLinesAroundBlockBody, :config do let(:beginning_offense_annotation) { '^{} Extra empty line detected at block body beginning.' } # Test blocks using both {} and do..end [%w[{ }], %w[do end]].each do |open, close| context "when EnforcedStyle is no_empty_lines for #{open} #{close} block" do let(:cop_config) { { 'EnforcedStyle' => 'no_empty_lines' } } it 'registers an offense for block body starting with a blank' do expect_offense(<<~RUBY) some_method #{open} #{beginning_offense_annotation} do_something #{close} RUBY expect_correction(<<~RUBY) some_method #{open} do_something #{close} RUBY end it 'registers an offense for block body ending with a blank' do expect_offense(<<~RUBY) some_method #{open} do_something ^{} Extra empty line detected at block body end. #{close} RUBY expect_correction(<<~RUBY) some_method #{open} do_something #{close} RUBY end context 'Ruby 2.7', :ruby27 do it 'registers an offense for block body ending with a blank' do expect_offense(<<~RUBY) some_method #{open} _1 ^{} Extra empty line detected at block body end. #{close} RUBY expect_correction(<<~RUBY) some_method #{open} _1 #{close} RUBY end end context 'Ruby 3.4', :ruby34 do it 'registers an offense for block body ending with a blank' do expect_offense(<<~RUBY) some_method #{open} it ^{} Extra empty line detected at block body end. #{close} RUBY expect_correction(<<~RUBY) some_method #{open} it #{close} RUBY end end it 'accepts block body starting with a line with spaces' do expect_no_offenses(<<~RUBY) some_method #{open} #{trailing_whitespace} do_something #{close} RUBY end it 'registers an offense for block body starting with a blank passed to ' \ 'a multi-line method call' do expect_offense(<<~RUBY) some_method arg, another_arg #{open} #{beginning_offense_annotation} do_something #{close} RUBY end it 'is not fooled by single line blocks' do expect_no_offenses(<<~RUBY) some_method #{open} do_something #{close} something_else RUBY end end context "when EnforcedStyle is empty_lines for #{open} #{close} block" do let(:cop_config) { { 'EnforcedStyle' => 'empty_lines' } } it 'registers an offense for block body not starting or ending with a blank' do expect_offense(<<~RUBY) some_method #{open} do_something ^ Empty line missing at block body beginning. #{close} ^ Empty line missing at block body end. RUBY expect_correction(<<~RUBY) some_method #{open} do_something #{close} RUBY end it 'ignores block with an empty body' do expect_no_offenses("some_method #{open}\n#{close}") end it 'is not fooled by single line blocks' do expect_no_offenses(<<~RUBY) some_method #{open} do_something #{close} something_else RUBY end end end end
ruby
MIT
99fa0fdd0481910d7d052f4c2ed01ad36178f404
2026-01-04T15:37:41.211519Z
false
rubocop/rubocop
https://github.com/rubocop/rubocop/blob/99fa0fdd0481910d7d052f4c2ed01ad36178f404/spec/rubocop/cop/layout/empty_comment_spec.rb
spec/rubocop/cop/layout/empty_comment_spec.rb
# frozen_string_literal: true RSpec.describe RuboCop::Cop::Layout::EmptyComment, :config do let(:cop_config) { { 'AllowBorderComment' => true, 'AllowMarginComment' => true } } it 'registers an offense and corrects using single line empty comment' do expect_offense(<<~RUBY) # ^ Source code comment is empty. RUBY expect_correction('') end it 'registers an offense and corrects using multiline empty comments' do expect_offense(<<~RUBY) # ^ Source code comment is empty. # ^ Source code comment is empty. RUBY expect_correction('') end it 'registers an offense and corrects using an empty comment next to code' do expect_offense(<<~RUBY) def foo # ^ Source code comment is empty. something end RUBY expect_correction(<<~RUBY) def foo something end RUBY end it 'registers an offense and corrects when using an empty comment next to code after comment line' do expect_offense(<<~RUBY) # comment def foo # ^ Source code comment is empty. something end RUBY expect_correction(<<~RUBY) # comment def foo something end RUBY end it 'does not register an offense when using comment text' do expect_no_offenses(<<~RUBY) # Description of `Foo` class. class Foo # Description of `hello` method. def hello end end RUBY end it 'does not register an offense when using comment text with leading and trailing blank lines' do expect_no_offenses(<<~RUBY) # # Description of `Foo` class. # class Foo # # Description of `hello` method. # def hello end end RUBY end context 'allow border comment (default)' do it 'does not register an offense when using border comment' do expect_no_offenses(<<~RUBY) ################################# RUBY end end context 'disallow border comment' do let(:cop_config) { { 'AllowBorderComment' => false } } it 'registers an offense and corrects using single line empty comment' do expect_offense(<<~RUBY) # ^ Source code comment is empty. RUBY expect_correction('') end it 'registers an offense and corrects using border comment' do expect_offense(<<~RUBY) ################################# ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Source code comment is empty. RUBY expect_correction('') end end context 'allow margin comment (default)' do it 'does not register an offense when using margin comment' do expect_no_offenses(<<~RUBY) # # Description of `hello` method. # def hello end RUBY end end context 'disallow margin comment' do let(:cop_config) { { 'AllowMarginComment' => false } } it 'registers an offense and corrects using margin comment' do expect_offense(<<~RUBY) # ^ Source code comment is empty. # Description of `hello` method. # ^ Source code comment is empty. def hello end RUBY expect_correction(<<~RUBY) # Description of `hello` method. def hello end RUBY end end it 'registers an offense and corrects an empty comment without space next to code' do expect_offense(<<~RUBY) def foo# ^ Source code comment is empty. something end RUBY expect_correction(<<~RUBY) def foo something end RUBY end it 'registers offenses and correct multiple empty comments next to code' do expect_offense(<<~RUBY) def foo # ^ Source code comment is empty. something # ^ Source code comment is empty. end RUBY expect_correction(<<~RUBY) def foo something end RUBY end it 'registers offenses and correct multiple aligned empty comments next to code' do expect_offense(<<~RUBY) def foo # ^ Source code comment is empty. something # ^ Source code comment is empty. end RUBY expect_correction(<<~RUBY) def foo something end RUBY end end
ruby
MIT
99fa0fdd0481910d7d052f4c2ed01ad36178f404
2026-01-04T15:37:41.211519Z
false
rubocop/rubocop
https://github.com/rubocop/rubocop/blob/99fa0fdd0481910d7d052f4c2ed01ad36178f404/spec/rubocop/cop/layout/space_around_operators_spec.rb
spec/rubocop/cop/layout/space_around_operators_spec.rb
# frozen_string_literal: true RSpec.describe RuboCop::Cop::Layout::SpaceAroundOperators, :config do let(:config) do RuboCop::Config .new( 'AllCops' => { 'TargetRubyVersion' => target_ruby_version }, 'Layout/HashAlignment' => { 'EnforcedHashRocketStyle' => hash_style }, 'Layout/SpaceAroundOperators' => { 'AllowForAlignment' => allow_for_alignment, 'EnforcedStyleForExponentOperator' => exponent_operator_style, 'EnforcedStyleForRationalLiterals' => rational_literals_style }, 'Layout/ExtraSpacing' => { 'Enabled' => force_equal_sign_alignment, 'ForceEqualSignAlignment' => force_equal_sign_alignment } ) end let(:target_ruby_version) { 2.5 } let(:hash_style) { 'key' } let(:allow_for_alignment) { true } let(:exponent_operator_style) { nil } let(:rational_literals_style) { nil } let(:force_equal_sign_alignment) { false } it 'accepts operator surrounded by tabs' do expect_no_offenses("a\t+\tb") end it 'accepts operator symbols' do expect_no_offenses('func(:-)') end it 'accepts ranges' do expect_no_offenses('a, b = (1..2), (1...3)') end it 'accepts rational' do expect_no_offenses('x = 2/3r') end it 'accepts multiple spaces between an operator and a tailing comment' do expect_no_offenses(<<~RUBY) foo + # comment bar RUBY end it 'accepts scope operator' do expect_no_offenses('@io.class == Zlib::GzipWriter') end it 'accepts ::Kernel::raise' do expect_no_offenses('::Kernel::raise IllegalBlockError.new') end it 'registers an offense and corrects exclamation point negation' do expect_offense(<<~RUBY) x = !a&&!b ^^ Surrounding space missing for operator `&&`. RUBY expect_correction(<<~RUBY) x = !a && !b RUBY end it 'accepts exclamation point definition' do expect_no_offenses(<<~RUBY) def ! !__getobj__ end RUBY end it 'accepts the result of the ExtraSpacing Cop' do expect_no_offenses(<<~RUBY) def batch @areas = params[:param].map do var_1 = 123_456 variable_2 = 456_123 end @another = params[:param].map do char_1 = begin variable_1_1 = 'a' variable_1_20 = 'b' variable_1_300 = 'c' # A Comment variable_1_4000 = 'd' variable_1_50000 = 'e' puts 'a non-assignment statement without a blank line' some_other_length_variable = 'f' end var_2 = 456_123 end render json: @areas end RUBY end it 'accepts a unary' do expect_no_offenses(<<~RUBY) def bm(label_width = 0, *labels, &blk) benchmark(CAPTION, label_width, FORMAT, *labels, &blk) end def each &block +11 end def self.search *args end def each *args end RUBY end it 'accepts splat operator' do expect_no_offenses('return *list if options') end it 'accepts def of operator' do expect_no_offenses(<<~RUBY) def +(other); end def self.===(other); end RUBY end it 'accepts an operator at the end of a line' do expect_no_offenses(<<~RUBY) ['Favor unless over if for negative ' + 'conditions.'] * 2 RUBY end it 'accepts an assignment with spaces' do expect_no_offenses('x = 0') end it 'accepts an assignment with the same alignment margins' do expect_no_offenses(<<~RUBY) @integer_message = 12345 @output = StringIO.new @logger = Logger.new(@output) RUBY end it 'accepts an assignment with a blank line' do expect_no_offenses(<<~RUBY) expected = posts(:welcome) tagging = Tagging.all.merge!(includes: :taggable).find(taggings(:welcome_general).id) assert_no_queries { assert_equal expected, tagging.taggable } RUBY end it 'accepts an assignment by `for` statement' do expect_no_offenses(<<~RUBY) for a in [] do; end for A in [] do; end for @a in [] do; end for @@a in [] do; end RUBY end it 'accepts vertical alignment with operator' do expect_no_offenses(<<~RUBY) down? && !migrated.include?(migration.version.to_i) up? && migrated.include?(migration.version.to_i) RUBY end it 'accepts vertical alignment with different operators that end with `=`' do expect_no_offenses(<<~RUBY) var.foo = a var.bar != b var.quux <= c var.garply >= d var.corge == e var.fred += f var.baz === g RUBY end it 'accepts an operator called with method syntax' do expect_no_offenses('Date.today.+(1).to_s') end it 'accepts operators with spaces' do expect_no_offenses(<<~RUBY) x += a + b - c * d / e % f ^ g | h & i || j y -= k && l RUBY end it "accepts some operators that are exceptions & don't need spaces" do expect_no_offenses(<<~RUBY) (1..3) ActionController::Base each { |s, t| } RUBY end it 'accepts an assignment followed by newline' do expect_no_offenses(<<~RUBY) x = 0 RUBY end it 'accepts an operator at the beginning of a line' do expect_no_offenses(<<~'RUBY') a = b \ && c RUBY end it 'registers an offense for exponent operator with spaces' do expect_offense(<<~RUBY) x = a * b ** 2 ^^ Space around operator `**` detected. y = a * b** 2 ^^ Space around operator `**` detected. RUBY expect_correction(<<~RUBY) x = a * b**2 y = a * b**2 RUBY end it 'accepts exponent operator without spaces' do expect_no_offenses('x = a * b**2') end it 'registers an offense for slash in rational literals with spaces' do expect_offense(<<~RUBY) x = a * b / 42r ^ Space around operator `/` detected. y = a * b/ 42r ^ Space around operator `/` detected. RUBY expect_correction(<<~RUBY) x = a * b/42r y = a * b/42r RUBY end it 'accepts slash in rational literals without spaces' do expect_no_offenses('x = a * b/42r') end it 'does not register an offense for slash in non rational literals without spaces' do expect_no_offenses(<<~RUBY) x = a * b / 42 RUBY end it 'registers slash in non rational literals without spaces' do expect_offense(<<~RUBY) x = a * b/42 ^ Surrounding space missing for operator `/`. y = a * b/ 42 ^ Surrounding space missing for operator `/`. RUBY expect_correction(<<~RUBY) x = a * b / 42 y = a * b / 42 RUBY end context '>= Ruby 2.7', :ruby27 do let(:target_ruby_version) { 2.7 } it 'registers an offense for alternative pattern matching syntax' do expect_offense(<<~RUBY) case foo in 0|1|2 ^ Surrounding space missing for operator `|`. ^ Surrounding space missing for operator `|`. end RUBY expect_correction(<<~RUBY) case foo in 0 | 1 | 2 end RUBY end it 'registers an offense for as pattern matching syntax' do expect_offense(<<~RUBY) case foo in bar=>baz ^^ Surrounding space missing for operator `=>`. end RUBY expect_correction(<<~RUBY) case foo in bar => baz end RUBY end it 'registers an offense for one-line alternative pattern matching syntax' do expect_offense(<<~RUBY) foo in 0|1|2 ^ Surrounding space missing for operator `|`. ^ Surrounding space missing for operator `|`. RUBY expect_correction(<<~RUBY) foo in 0 | 1 | 2 RUBY end it 'registers an offense for one-line as pattern matching syntax' do expect_offense(<<~RUBY) foo in bar=>baz ^^ Surrounding space missing for operator `=>`. RUBY expect_correction(<<~RUBY) foo in bar => baz RUBY end # NOTE: It is `Layout/SpaceAroundKeyword` cop's role to detect this offense. it 'does not register an offense for one-line pattern matching syntax (`in`)' do expect_no_offenses(<<~RUBY) ""in foo RUBY end end context '>= Ruby 3.0', :ruby30 do let(:target_ruby_version) { 3.0 } it 'registers an offense for one-line pattern matching syntax (`=>`)' do expect_offense(<<~RUBY) ""=>foo ^^ Surrounding space missing for operator `=>`. RUBY expect_correction(<<~RUBY) "" => foo RUBY end end context 'when EnforcedStyleForExponentOperator is space' do let(:exponent_operator_style) { 'space' } it 'registers an offense for exponent operator without spaces' do expect_offense(<<~RUBY) x = a * b**2 ^^ Surrounding space missing for operator `**`. RUBY expect_correction(<<~RUBY) x = a * b ** 2 RUBY end end context 'when EnforcedStyleForRationalLiterals is space' do let(:rational_literals_style) { 'space' } it 'registers an offense for rational literals without spaces' do expect_offense(<<~RUBY) x = a * b/42r ^ Surrounding space missing for operator `/`. RUBY expect_correction(<<~RUBY) x = a * b / 42r RUBY end end it 'accepts unary operators without space' do expect_no_offenses(<<~RUBY) [].map(&:size) a.(b) -3 foo::~ arr.collect { |e| -e } x = +2 RUBY end it 'accepts [arg] without space' do expect_no_offenses('files[2]') end it 'accepts [] without space' do expect_no_offenses('files[]') end it 'accepts []= without space' do expect_no_offenses('files[:key], files[:another] = method') end it 'accepts argument default values without space' do # These are handled by SpaceAroundEqualsInParameterDefault, # so SpaceAroundOperators leaves them alone. expect_no_offenses(<<~RUBY) def init(name=nil) end RUBY end it 'registers an offense and corrects singleton class operator`' do expect_offense(<<~RUBY) class<<self ^^ Surrounding space missing for operator `<<`. end RUBY expect_correction(<<~RUBY) class << self end RUBY end describe 'missing space around operators' do shared_examples 'modifier with missing space' do |keyword| it "registers an offense in presence of modifier #{keyword} statement" do expect_offense(<<~RUBY) a=1 #{keyword} condition ^ Surrounding space missing for operator `=`. c=2 ^ Surrounding space missing for operator `=`. RUBY expect_correction(<<~RUBY) a = 1 #{keyword} condition c = 2 RUBY end end it 'registers an offense for assignment without space on both sides' do expect_offense(<<~RUBY) x=0 ^ Surrounding space missing for operator `=`. y+= 0 ^^ Surrounding space missing for operator `+=`. z[0] =0 ^ Surrounding space missing for operator `=`. RUBY expect_correction(<<~RUBY) x = 0 y += 0 z[0] = 0 RUBY end context 'ternary operators' do it 'registers an offense and corrects operators with no spaces' do expect_offense(<<~RUBY) x == 0?1:2 ^ Surrounding space missing for operator `:`. ^ Surrounding space missing for operator `?`. RUBY expect_correction(<<~RUBY) x == 0 ? 1 : 2 RUBY end it 'registers an offense and corrects operators with just a trailing space' do expect_offense(<<~RUBY) x == 0? 1: 2 ^ Surrounding space missing for operator `:`. ^ Surrounding space missing for operator `?`. RUBY expect_correction(<<~RUBY) x == 0 ? 1 : 2 RUBY end it 'registers an offense and corrects operators with just a leading space' do expect_offense(<<~RUBY) x == 0 ?1 :2 ^ Surrounding space missing for operator `:`. ^ Surrounding space missing for operator `?`. RUBY expect_correction(<<~RUBY) x == 0 ? 1 : 2 RUBY end end it_behaves_like 'modifier with missing space', 'if' it_behaves_like 'modifier with missing space', 'unless' it_behaves_like 'modifier with missing space', 'while' it_behaves_like 'modifier with missing space', 'until' it 'registers an offense for binary operators that could be unary' do expect_offense(<<~RUBY) a-3 ^ Surrounding space missing for operator `-`. x&0xff ^ Surrounding space missing for operator `&`. z+0 ^ Surrounding space missing for operator `+`. RUBY expect_correction(<<~RUBY) a - 3 x & 0xff z + 0 RUBY end it 'registers an offense and corrects arguments to a method' do expect_offense(<<~RUBY) puts 1+2 ^ Surrounding space missing for operator `+`. RUBY expect_correction(<<~RUBY) puts 1 + 2 RUBY end it 'registers an offense for operators without spaces' do expect_offense(<<~RUBY) x+= a+b-c*d/e%f^g|h&i||j ^^ Surrounding space missing for operator `+=`. ^ Surrounding space missing for operator `+`. ^ Surrounding space missing for operator `-`. ^ Surrounding space missing for operator `*`. ^ Surrounding space missing for operator `/`. ^ Surrounding space missing for operator `%`. ^ Surrounding space missing for operator `^`. ^ Surrounding space missing for operator `|`. ^ Surrounding space missing for operator `&`. ^^ Surrounding space missing for operator `||`. y -=k&&l ^^ Surrounding space missing for operator `-=`. ^^ Surrounding space missing for operator `&&`. RUBY expect_correction(<<~RUBY) x += a + b - c * d / e % f ^ g | h & i || j y -= k && l RUBY end it 'registers an offense and corrects a setter call without spaces' do expect_offense(<<~RUBY) x.y=2 ^ Surrounding space missing for operator `=`. RUBY expect_correction(<<~RUBY) x.y = 2 RUBY end it 'registers an offense and corrects a setter call with implicit array without spaces' do expect_offense(<<~RUBY) x.y=2,3 ^ Surrounding space missing for operator `=`. RUBY expect_correction(<<~RUBY) x.y = 2,3 RUBY end context 'when a hash literal is on a single line' do context 'and Layout/HashAlignment:EnforcedHashRocketStyle is key' do let(:hash_style) { 'key' } it 'registers an offense and corrects a hash rocket without spaces' do expect_offense(<<~RUBY) { 1=>2, a: b } ^^ Surrounding space missing for operator `=>`. RUBY expect_correction(<<~RUBY) { 1 => 2, a: b } RUBY end end context 'and Layout/HashAlignment:EnforcedHashRocketStyle is table' do let(:hash_style) { 'table' } it 'registers an offense and corrects a hash rocket without spaces' do expect_offense(<<~RUBY) { 1=>2, a: b } ^^ Surrounding space missing for operator `=>`. RUBY expect_correction(<<~RUBY) { 1 => 2, a: b } RUBY end end context 'and Layout/HashAlignment:EnforcedHashRocketStyle is key, table' do let(:hash_style) { %w[key table] } it 'registers an offense and corrects a hash rocket without spaces' do expect_offense(<<~RUBY) { 1=>2, a: b } ^^ Surrounding space missing for operator `=>`. RUBY expect_correction(<<~RUBY) { 1 => 2, a: b } RUBY end end end context 'when a hash literal is on multiple lines' do context 'and Layout/HashAlignment:EnforcedHashRocketStyle is key' do let(:hash_style) { 'key' } it 'registers an offense and corrects a hash rocket without spaces' do expect_offense(<<~RUBY) { 1=>2, ^^ Surrounding space missing for operator `=>`. a: b } RUBY expect_correction(<<~RUBY) { 1 => 2, a: b } RUBY end end context 'and Layout/HashAlignment:EnforcedHashRocketStyle is table' do let(:hash_style) { 'table' } it "doesn't register an offense for a hash rocket without spaces" do expect_no_offenses(<<~RUBY) { 1=>2, a: b } RUBY end end context 'and Layout/HashAlignment:EnforcedHashRocketStyle is key, table' do let(:hash_style) { %w[key table] } it "doesn't register an offense for a hash rocket without spaces" do expect_no_offenses(<<~RUBY) { 1=>2, a: b } RUBY end end end it 'registers an offense and corrects match operators without space' do expect_offense(<<~RUBY) x=~/abc/ ^^ Surrounding space missing for operator `=~`. y !~/abc/ ^^ Surrounding space missing for operator `!~`. RUBY expect_correction(<<~RUBY) x =~ /abc/ y !~ /abc/ RUBY end it 'registers an offense and corrects various assignments without space' do expect_offense(<<~RUBY) x||=0 ^^^ Surrounding space missing for operator `||=`. y&&=0 ^^^ Surrounding space missing for operator `&&=`. z*=2 ^^ Surrounding space missing for operator `*=`. @a=0 ^ Surrounding space missing for operator `=`. @@a=0 ^ Surrounding space missing for operator `=`. a,b=0 ^ Surrounding space missing for operator `=`. A=0 ^ Surrounding space missing for operator `=`. x[3]=0 ^ Surrounding space missing for operator `=`. $A=0 ^ Surrounding space missing for operator `=`. A||=0 ^^^ Surrounding space missing for operator `||=`. RUBY expect_correction(<<~RUBY) x ||= 0 y &&= 0 z *= 2 @a = 0 @@a = 0 a,b = 0 A = 0 x[3] = 0 $A = 0 A ||= 0 RUBY end it 'registers an offense and corrects equality operators without space' do expect_offense(<<~RUBY) x==0 ^^ Surrounding space missing for operator `==`. y!=0 ^^ Surrounding space missing for operator `!=`. Hash===z ^^^ Surrounding space missing for operator `===`. RUBY expect_correction(<<~RUBY) x == 0 y != 0 Hash === z RUBY end it 'registers an offense and corrects `-` without space with a negative lhs operand' do expect_offense(<<~RUBY) -1-arg ^ Surrounding space missing for operator `-`. RUBY expect_correction(<<~RUBY) -1 - arg RUBY end it 'registers an offense and corrects inheritance < without space' do expect_offense(<<~RUBY) class ShowSourceTestClass<ShowSourceTestSuperClass ^ Surrounding space missing for operator `<`. end RUBY expect_correction(<<~RUBY) class ShowSourceTestClass < ShowSourceTestSuperClass end RUBY end it 'registers an offense and corrects hash rocket without space at rescue' do expect_offense(<<~RUBY) begin rescue Exception=>e ^^ Surrounding space missing for operator `=>`. end RUBY expect_correction(<<~RUBY) begin rescue Exception => e end RUBY end it 'registers an offense and corrects string concatenation without messing up new lines' do expect_offense(<<~RUBY) 'Here is a'+ ^ Surrounding space missing for operator `+`. 'joined string'+ ^ Surrounding space missing for operator `+`. 'across three lines' RUBY expect_correction(<<~RUBY) 'Here is a' + 'joined string' + 'across three lines' RUBY end it "doesn't register an offense for operators with newline on right" do expect_no_offenses(<<~RUBY) 'Here is a' + 'joined string' + 'across three lines' RUBY end end describe 'extra space around operators' do shared_examples 'modifier with extra space' do |keyword| it "registers an offense in presence of modifier #{keyword} statement" do expect_offense(<<~RUBY) a = 1 #{keyword} condition ^ Operator `=` should be surrounded by a single space. c = 2 ^ Operator `=` should be surrounded by a single space. RUBY expect_correction(<<~RUBY) a = 1 #{keyword} condition c = 2 RUBY end end it 'registers an offense and corrects assignment with too many spaces on either side' do expect_offense(<<~RUBY) x = 0 ^ Operator `=` should be surrounded by a single space. y += 0 ^^ Operator `+=` should be surrounded by a single space. z[0] = 0 ^ Operator `=` should be surrounded by a single space. RUBY expect_correction(<<~RUBY) x = 0 y += 0 z[0] = 0 RUBY end it 'registers an offense and corrects ternary operator with too many spaces' do expect_offense(<<~RUBY) x == 0 ? 1 : 2 ^ Operator `:` should be surrounded by a single space. ^ Operator `?` should be surrounded by a single space. RUBY expect_correction(<<~RUBY) x == 0 ? 1 : 2 RUBY end it_behaves_like 'modifier with extra space', 'if' it_behaves_like 'modifier with extra space', 'unless' it_behaves_like 'modifier with extra space', 'while' it_behaves_like 'modifier with extra space', 'until' it 'registers an offense and corrects binary operators that could be unary' do expect_offense(<<~RUBY) a - 3 ^ Operator `-` should be surrounded by a single space. x & 0xff ^ Operator `&` should be surrounded by a single space. z + 0 ^ Operator `+` should be surrounded by a single space. RUBY expect_correction(<<~RUBY) a - 3 x & 0xff z + 0 RUBY end it 'registers an offense and corrects arguments to a method' do expect_offense(<<~RUBY) puts 1 + 2 ^ Operator `+` should be surrounded by a single space. RUBY expect_correction(<<~RUBY) puts 1 + 2 RUBY end it 'registers an offense and corrects operators with too many spaces' do expect_offense(<<~RUBY) x += a ^^ Operator `+=` should be surrounded by a single space. a + b ^ Operator `+` should be surrounded by a single space. b - c ^ Operator `-` should be surrounded by a single space. c * d ^ Operator `*` should be surrounded by a single space. d / e ^ Operator `/` should be surrounded by a single space. e % f ^ Operator `%` should be surrounded by a single space. f ^ g ^ Operator `^` should be surrounded by a single space. g | h ^ Operator `|` should be surrounded by a single space. h & i ^ Operator `&` should be surrounded by a single space. i || j ^^ Operator `||` should be surrounded by a single space. y -= k && l ^^ Operator `-=` should be surrounded by a single space. ^^ Operator `&&` should be surrounded by a single space. RUBY expect_correction(<<~RUBY) x += a a + b b - c c * d d / e e % f f ^ g g | h h & i i || j y -= k && l RUBY end it 'registers an offense and corrects operators with too many spaces on the same line' do expect_offense(<<~RUBY) x += a + b - c * d / e % f ^ g | h & i || j ^^ Operator `||` should be surrounded by a single space. ^ Operator `&` should be surrounded by a single space. ^ Operator `|` should be surrounded by a single space. ^ Operator `^` should be surrounded by a single space. ^ Operator `%` should be surrounded by a single space. ^ Operator `/` should be surrounded by a single space. ^ Operator `*` should be surrounded by a single space. ^ Operator `-` should be surrounded by a single space. ^ Operator `+` should be surrounded by a single space. ^^ Operator `+=` should be surrounded by a single space. y -= k && l ^^ Operator `&&` should be surrounded by a single space. ^^ Operator `-=` should be surrounded by a single space. RUBY expect_correction(<<~RUBY) x += a + b - c * d / e % f ^ g | h & i || j y -= k && l RUBY end it 'registers an offense and corrects a setter call with too many spaces' do expect_offense(<<~RUBY) x.y = 2 ^ Operator `=` should be surrounded by a single space. RUBY expect_correction(<<~RUBY) x.y = 2 RUBY end it 'registers an offense and corrects a hash rocket with too many spaces' do expect_offense(<<~RUBY) { 1 => 2, a: b } ^^ Operator `=>` should be surrounded by a single space. RUBY expect_correction(<<~RUBY) { 1 => 2, a: b } RUBY end it 'registers an offense and corrects a hash rocket with an extra space on multiple line' do expect_offense(<<~RUBY) { 1 => 2 ^^ Operator `=>` should be surrounded by a single space. } RUBY expect_correction(<<~RUBY) { 1 => 2 } RUBY end it 'accepts for a hash rocket with an extra space for alignment on multiple line' do expect_no_offenses(<<~RUBY) { 1 => 2, 11 => 3 } RUBY end context 'when does not allowed for alignment' do let(:allow_for_alignment) { false } it 'registers an offense and corrects an extra space' do expect_offense(<<~RUBY) { 1 => 2, ^^ Operator `=>` should be surrounded by a single space. 11 => 3 } RUBY expect_correction(<<~RUBY) { 1 => 2, 11 => 3 } RUBY end end it 'registers an offense and corrects match operators with too many spaces' do expect_offense(<<~RUBY) x =~ /abc/ ^^ Operator `=~` should be surrounded by a single space. y !~ /abc/ ^^ Operator `!~` should be surrounded by a single space. RUBY expect_correction(<<~RUBY) x =~ /abc/ y !~ /abc/ RUBY end it 'does not register an offense match operators between `<<` and `+=`' do expect_no_offenses(<<~RUBY) x << foo yz += bar RUBY end it 'does not register an offense match operators between `+=` and `<<`' do expect_no_offenses(<<~RUBY) x += foo yz << bar RUBY end it 'registers an offense when operator is followed by aligned << inside a string' do expect_offense(<<~RUBY) x += foo ^^ Operator `+=` should be surrounded by a single space. 'yz << bar' RUBY expect_correction(<<~RUBY) x += foo 'yz << bar' RUBY end it 'registers an offense when operator is preceded by aligned << inside a string' do expect_offense(<<~RUBY) 'yz << bar' x += foo ^^ Operator `+=` should be surrounded by a single space. RUBY expect_correction(<<~RUBY) 'yz << bar' x += foo RUBY end it 'registers an offense and corrects various assignments with too many spaces' do expect_offense(<<~RUBY) x ||= 0 ^^^ Operator `||=` should be surrounded by a single space. y &&= 0 ^^^ Operator `&&=` should be surrounded by a single space. z *= 2 ^^ Operator `*=` should be surrounded by a single space. @a = 0 ^ Operator `=` should be surrounded by a single space. @@a = 0 ^ Operator `=` should be surrounded by a single space. a,b = 0 ^ Operator `=` should be surrounded by a single space. A = 0 ^ Operator `=` should be surrounded by a single space. x[3] = 0 ^ Operator `=` should be surrounded by a single space. $A = 0 ^ Operator `=` should be surrounded by a single space. A ||= 0 ^^^ Operator `||=` should be surrounded by a single space. A += 0 ^^ Operator `+=` should be surrounded by a single space. RUBY expect_correction(<<~RUBY) x ||= 0 y &&= 0 z *= 2 @a = 0 @@a = 0 a,b = 0 A = 0 x[3] = 0 $A = 0 A ||= 0 A += 0 RUBY end it 'registers an offense and corrects equality operators with too many spaces' do expect_offense(<<~RUBY) x == 0 ^^ Operator `==` should be surrounded by a single space. y != 0 ^^ Operator `!=` should be surrounded by a single space. Hash === z ^^^ Operator `===` should be surrounded by a single space. RUBY expect_correction(<<~RUBY) x == 0 y != 0 Hash === z RUBY end it 'registers an offense and corrects `-` with too many spaces with negative lhs operand' do expect_offense(<<~RUBY) -1 - arg ^ Operator `-` should be surrounded by a single space. RUBY expect_correction(<<~RUBY) -1 - arg RUBY end it 'registers an offense and corrects inheritance < with too many spaces' do expect_offense(<<~RUBY) class Foo < Bar ^ Operator `<` should be surrounded by a single space. end RUBY expect_correction(<<~RUBY) class Foo < Bar end RUBY end it 'registers an offense and corrects hash rocket with too many spaces at rescue' do expect_offense(<<~RUBY) begin rescue Exception => e ^^ Operator `=>` should be surrounded by a single space. end RUBY expect_correction(<<~RUBY) begin rescue Exception => e end RUBY end end describe 'when Layout/ExtraSpacing has `ForceEqualSignAlignment` configured to true' do let(:force_equal_sign_alignment) { true } it 'allows variables to be aligned' do expect_no_offenses(<<~RUBY)
ruby
MIT
99fa0fdd0481910d7d052f4c2ed01ad36178f404
2026-01-04T15:37:41.211519Z
true
rubocop/rubocop
https://github.com/rubocop/rubocop/blob/99fa0fdd0481910d7d052f4c2ed01ad36178f404/spec/rubocop/cop/layout/indentation_width_spec.rb
spec/rubocop/cop/layout/indentation_width_spec.rb
# frozen_string_literal: true RSpec.describe RuboCop::Cop::Layout::IndentationWidth, :config do let(:config) do RuboCop::Config.new( 'Layout/IndentationWidth' => cop_config, 'Layout/AccessModifierIndentation' => access_modifier_config, 'Layout/IndentationConsistency' => consistency_config, 'Layout/EndAlignment' => end_alignment_config, 'Layout/DefEndAlignment' => def_end_alignment_config ) end let(:access_modifier_config) { { 'EnforcedStyle' => 'indent' } } let(:consistency_config) { { 'EnforcedStyle' => 'normal' } } let(:end_alignment_config) { { 'Enabled' => true, 'EnforcedStyleAlignWith' => 'variable' } } let(:def_end_alignment_config) do { 'Enabled' => true, 'EnforcedStyleAlignWith' => 'start_of_line' } end context 'with Width set to 4' do let(:cop_config) { { 'Width' => 4 } } context 'for a file with byte order mark' do let(:bom) { "\xef\xbb\xbf" } it 'accepts correctly indented method definition' do expect_no_offenses(<<~RUBY) #{bom}class Test def method end end RUBY end end context 'with ignored patterns set' do let(:cop_config) do { 'Width' => 4, 'AllowedPatterns' => ['^\s*module', '^\s*(els)?if.*[A-Z][a-z]+'] } end it 'accepts unindented lines for those keywords' do expect_no_offenses(<<~RUBY) module Foo class Test if blah if blah == Apple puts "sweet" elsif blah == Lemon puts "sour" elsif blah == popcorn puts "salty" end end end end RUBY end end context 'with if statement' do it 'registers an offense for bad indentation of an if body' do expect_offense(<<~RUBY) if cond func ^ Use 4 (not 1) spaces for indentation. end RUBY end end it 'registers and corrects offense for bad indentation' do expect_offense(<<~RUBY) if a1 b1 ^^^ Use 4 (not 3) spaces for indentation. b1 elsif a2 b2 ^ Use 4 (not 1) spaces for indentation. else c end RUBY # The second `b1` will be corrected by IndentationConsistency. expect_correction(<<~RUBY) if a1 b1 b1 elsif a2 b2 else c end RUBY end end context 'with Width set to 2' do let(:cop_config) { { 'Width' => 2 } } context 'with if statement' do it 'registers an offense for bad indentation of an if body' do expect_offense(<<~RUBY) if cond func ^ Use 2 (not 1) spaces for indentation. end RUBY end it 'registers an offense for bad indentation of an else body' do expect_offense(<<~RUBY) if cond func1 else func2 ^ Use 2 (not 1) spaces for indentation. end RUBY end it 'registers an offense for bad indentation of an else body when if body contains no code' do expect_offense(<<~RUBY) if cond # nothing here else func2 ^ Use 2 (not 1) spaces for indentation. end RUBY end it 'registers an offense for bad indentation of an else body when if ' \ 'and elsif body contains no code' do expect_offense(<<~RUBY) if cond # nothing here elsif cond2 # nothing here either else func2 ^ Use 2 (not 1) spaces for indentation. end RUBY end it 'registers an offense for bad indentation of an elsif body' do expect_offense(<<~RUBY) if a1 b1 elsif a2 b2 ^ Use 2 (not 1) spaces for indentation. else c end RUBY end it 'registers an offense for bad indentation of ternary inside else' do expect_offense(<<~RUBY) if a b else x ? y : z ^^^^^ Use 2 (not 5) spaces for indentation. end RUBY end it 'registers an offense for bad indentation of modifier if in else' do expect_offense(<<~RUBY) if a b else x if y ^^^ Use 2 (not 3) spaces for indentation. end RUBY end it 'accepts indentation after if on new line after assignment' do expect_no_offenses(<<~RUBY) Rails.application.config.ideal_postcodes_key = if Rails.env.production? || Rails.env.staging? "AAAA-AAAA-AAAA-AAAA" end RUBY end it 'accepts `rescue` after an empty body' do expect_no_offenses(<<~RUBY) begin rescue handle_error end RUBY end it 'accepts `ensure` after an empty body' do expect_no_offenses(<<~RUBY) begin ensure something end RUBY end it 'accepts `rescue`/`ensure` after an empty body' do expect_no_offenses(<<~RUBY) begin rescue handle_error ensure something end RUBY end it 'accepts `rescue` after an empty def' do expect_no_offenses(<<~RUBY) def foo rescue handle_error end RUBY end it 'accepts `ensure` after an empty def' do expect_no_offenses(<<~RUBY) def foo ensure something end RUBY end it 'accepts `rescue`/`ensure` after an empty def' do expect_no_offenses(<<~RUBY) def foo rescue handle_error ensure something end RUBY end it 'does not raise any error with empty braces' do expect_no_offenses(<<~RUBY) if cond () else () end RUBY end it 'registers and corrects on offense for bad indentation' do expect_offense(<<~RUBY) if a1 b1 ^^^ Use 2 (not 3) spaces for indentation. b1 elsif a2 b2 ^ Use 2 (not 1) spaces for indentation. else c ^^^^ Use 2 (not 4) spaces for indentation. end RUBY # The second `b1` will be corrected by IndentationConsistency. expect_correction(<<~RUBY) if a1 b1 b1 elsif a2 b2 else c end RUBY end it 'does not correct in scopes that contain block comments' do expect_offense(<<~RUBY) module Foo # The class has a block comment within, so it's not corrected. class Bar ^{} Use 2 (not 0) spaces for indentation. =begin This is a nice long comment which spans a few lines =end # The method has no block comment inside, # but it's within a class that has a block # comment, so it's not corrected either. def baz ^{} Use 2 (not 0) spaces for indentation. do_something ^{} Use 2 (not 0) spaces for indentation. end end end RUBY expect_no_corrections end it 'does not indent heredoc strings' do expect_offense(<<~'RUBY') module Foo module Bar ^{} Use 2 (not 0) spaces for indentation. SOMETHING = <<GOO text more text foo GOO def baz do_something("#{x}") end end end RUBY expect_correction(<<~'RUBY') module Foo module Bar SOMETHING = <<GOO text more text foo GOO def baz do_something("#{x}") end end end RUBY end it 'indents parenthesized expressions' do expect_offense(<<~RUBY) var1 = nil array_list = [] if var1.attr1 != 0 || array_list.select{ |w| (w.attr2 == var1.attr2) ^^^^^^^ Use 2 (not 7) spaces for indentation. }.blank? array_list << var1 end RUBY expect_correction(<<~RUBY) var1 = nil array_list = [] if var1.attr1 != 0 || array_list.select{ |w| (w.attr2 == var1.attr2) }.blank? array_list << var1 end RUBY end it 'leaves rescue ; end unchanged' do expect_no_offenses(<<~RUBY) if variable begin do_something rescue ; end # consume any exception end RUBY end it 'leaves block unchanged if block end is not on its own line' do expect_no_offenses(<<~RUBY) a_function { # a comment result = AObject.find_by_attr(attr) if attr result || AObject.make( :attr => attr, :attr2 => Other.get_value(), :attr3 => Another.get_value()) } RUBY end it 'handles lines with only whitespace' do expect_offense(<<~RUBY) def x y ^^^^ Use 2 (not 4) spaces for indentation. rescue end RUBY expect_correction(<<~RUBY) def x y rescue end RUBY end it 'accepts a one line if statement' do expect_no_offenses('if cond then func1 else func2 end') end it 'accepts a correctly aligned if/elsif/else/end' do expect_no_offenses(<<~RUBY) if a1 b1 elsif a2 b2 else c end RUBY end it 'accepts a correctly aligned if/elsif/else/end as a method argument' do expect_no_offenses(<<~RUBY) foo( if a1 b1 elsif a2 b2 else c end ) RUBY end it 'accepts if/elsif/else/end laid out as a table' do expect_no_offenses(<<~RUBY) if @io == $stdout then str << "$stdout" elsif @io == $stdin then str << "$stdin" elsif @io == $stderr then str << "$stderr" else str << @io.class.to_s end RUBY end it 'accepts if/then/else/end laid out as another table' do expect_no_offenses(<<~RUBY) if File.exist?('config.save') then ConfigTable.load else ConfigTable.new end RUBY end it 'accepts an empty if' do expect_no_offenses(<<~RUBY) if a else end RUBY end context 'with assignment' do shared_examples 'assignment with if statement' do context 'and end is aligned with variable' do it 'accepts an if with end aligned with setter' do expect_no_offenses(<<~RUBY) foo.bar = if baz derp end RUBY end it 'accepts an if with end aligned with element assignment' do expect_no_offenses(<<~RUBY) foo[bar] = if baz derp end RUBY end it 'accepts an if with end aligned with variable' do expect_no_offenses(<<~RUBY) var = if a 0 end @var = if a 0 end $var = if a 0 end var ||= if a 0 end var &&= if a 0 end var -= if a 0 end VAR = if a 0 end RUBY end it 'accepts an if/else' do expect_no_offenses(<<~RUBY) var = if a 0 else 1 end RUBY end it 'accepts an if/else with chaining after the end' do expect_no_offenses(<<~RUBY) var = if a 0 else 1 end.abc.join("") RUBY end it 'accepts an if/else with chaining with a block after the end' do expect_no_offenses(<<~RUBY) var = if a 0 else 1 end.abc.tap {} RUBY end end context 'and end is aligned with keyword' do it 'registers an offense for an if with setter' do expect_offense(<<~RUBY) foo.bar = if baz derp ^^^^^^^^^^^^ Use 2 (not 12) spaces for indentation. end RUBY end it 'registers an offense for an if with element assignment' do expect_offense(<<~RUBY) foo[bar] = if baz derp ^^^^^^^^^^^^^ Use 2 (not 13) spaces for indentation. end RUBY end it 'registers an offense for an if' do expect_offense(<<~RUBY) var = if a 0 ^^^^^^^^ Use 2 (not 8) spaces for indentation. end RUBY end it 'accepts an if/else in assignment on next line' do expect_no_offenses(<<~RUBY) var = if a 0 else 1 end RUBY end it 'registers an offense for a while' do expect_offense(<<~RUBY) var = while a b ^^^^^^^^ Use 2 (not 8) spaces for indentation. end RUBY end it 'registers an offense for an until' do expect_offense(<<~RUBY) var = until a b ^^^^^^^^ Use 2 (not 8) spaces for indentation. end RUBY end end context 'and end is aligned randomly' do it 'registers an offense for an if' do expect_offense(<<~RUBY) var = if a 0 ^^^^^^^^^^ Use 2 (not 10) spaces for indentation. end RUBY end it 'registers an offense for a while' do expect_offense(<<~RUBY) var = while a b ^^^^^^^^^^ Use 2 (not 10) spaces for indentation. end RUBY end it 'registers an offense for an until' do expect_offense(<<~RUBY) var = until a b ^^^^^^^^^^ Use 2 (not 10) spaces for indentation. end RUBY end end end context 'when alignment style is variable' do let(:end_alignment_config) do { 'Enabled' => true, 'EnforcedStyleAlignWith' => 'variable' } end it_behaves_like 'assignment with if statement' end context 'when alignment style is start_of_line' do let(:end_alignment_config) do { 'Enabled' => true, 'EnforcedStyleAlignWith' => 'start_of_line' } end it_behaves_like 'assignment with if statement' end context 'when alignment style is keyword' do let(:end_alignment_config) do { 'Enabled' => true, 'EnforcedStyleAlignWith' => 'keyword' } end context 'and end is aligned with variable' do it 'registers an offense for an if' do expect_offense(<<~RUBY) var = if a 0 ^ Use 2 (not -4) spaces for indentation. end RUBY end it 'registers an offense for a while' do expect_offense(<<~RUBY) var = while a b ^ Use 2 (not -4) spaces for indentation. end RUBY end it 'registers and corrects bad indentation' do expect_offense(<<~RUBY) var = if a b ^ Use 2 (not -4) spaces for indentation. end var = while a b ^ Use 2 (not -4) spaces for indentation. end RUBY # Not this cop's job to fix the `end`. expect_correction(<<~RUBY) var = if a b end var = while a b end RUBY end end context 'and end is aligned with keyword' do it 'accepts an if in assignment' do expect_no_offenses(<<~RUBY) var = if a 0 end RUBY end it 'accepts an if/else in assignment' do expect_no_offenses(<<~RUBY) var = if a 0 else 1 end RUBY end it 'accepts an if/else in assignment on next line' do expect_no_offenses(<<~RUBY) var = if a 0 else 1 end RUBY end it 'accepts a while in assignment' do expect_no_offenses(<<~RUBY) var = while a b end RUBY end it 'accepts an until in assignment' do expect_no_offenses(<<~RUBY) var = until a b end RUBY end end end end it 'accepts an if/else branches with rescue clauses' do # Because of how the rescue clauses come out of Parser, these are # special and need to be tested. expect_no_offenses(<<~RUBY) if a a rescue nil else a rescue nil end RUBY end end context 'with unless' do it 'registers an offense for bad indentation of an unless body' do expect_offense(<<~RUBY) unless cond func ^ Use 2 (not 1) spaces for indentation. end RUBY end it 'accepts an empty unless' do expect_no_offenses(<<~RUBY) unless a else end RUBY end end context 'with case' do it 'registers an offense for bad indentation in a case/when body' do expect_offense(<<~RUBY) case a when b c ^ Use 2 (not 1) spaces for indentation. end RUBY end it 'registers an offense for bad indentation in a case/else body' do expect_offense(<<~RUBY) case a when b c when d e else f ^^^ Use 2 (not 3) spaces for indentation. end RUBY end it 'accepts correctly indented case/when/else' do expect_no_offenses(<<~RUBY) case a when b c c when d else f end RUBY end it 'accepts aligned values in when clause' do expect_no_offenses(<<~'RUBY') case superclass when /\A(#{NAMESPACEMATCH})(?:\s|\Z)/, /\A(Struct|OStruct)\.new/, /\ADelegateClass\((.+?)\)\s*\Z/, /\A(#{NAMESPACEMATCH})\(/ $1 when "self" namespace.path end RUBY end it 'accepts case/when/else laid out as a table' do expect_no_offenses(<<~RUBY) case sexp.loc.keyword.source when 'if' then cond, body, _else = *sexp when 'unless' then cond, _else, body = *sexp else cond, body = *sexp end RUBY end it 'accepts case/when/else with then beginning a line' do expect_no_offenses(<<~RUBY) case sexp.loc.keyword.source when 'if' then cond, body, _else = *sexp end RUBY end it 'accepts indented when/else plus indented body' do # "Indent when as deep as case" is the job of another cop. expect_no_offenses(<<~RUBY) case code_type when 'ruby', 'sql', 'plain' code_type when 'erb' 'ruby; html-script: true' when "html" 'xml' else 'plain' end RUBY end end context 'with case match', :ruby27 do it 'registers an offense for bad indentation in a case/in body' do expect_offense(<<~RUBY) case a in b c ^ Use 2 (not 1) spaces for indentation. end RUBY end it 'registers an offense for bad indentation in a case/else body' do expect_offense(<<~RUBY) case a in b c in d e else f ^^^ Use 2 (not 3) spaces for indentation. end RUBY end it 'accepts correctly indented case/in/else' do expect_no_offenses(<<~RUBY) case a in b c c in d else f end RUBY end it 'accepts aligned values in `in` clause' do expect_no_offenses(<<~RUBY) case condition in [42] foo in [43] bar end RUBY end it 'accepts aligned value in `in` clause and `else` is empty' do expect_no_offenses(<<~RUBY) case x in 42 foo else end RUBY end it 'accepts case/in/else laid out as a table' do expect_no_offenses(<<~RUBY) case sexp.loc.keyword.source in 'if' then cond, body, _else = *sexp in 'unless' then cond, _else, body = *sexp else cond, body = *sexp end RUBY end it 'accepts case/in/else with then beginning a line' do expect_no_offenses(<<~RUBY) case sexp.loc.keyword.source in 'if' then cond, body, _else = *sexp end RUBY end it 'accepts indented in/else plus indented body' do # "Indent `in` as deep as `case`" is the job of another cop. expect_no_offenses(<<~RUBY) case code_type in 'ruby' | 'sql' | 'plain' code_type in 'erb' 'ruby; html-script: true' in "html" 'xml' else 'plain' end RUBY end end context 'with while/until' do it 'registers an offense for bad indentation of a while body' do expect_offense(<<~RUBY) while cond func ^ Use 2 (not 1) spaces for indentation. end RUBY end it 'registers an offense for bad indentation of begin/end/while' do expect_offense(<<~RUBY) something = begin func1 ^ Use 2 (not 1) spaces for indentation. func2 end while cond RUBY end it 'registers an offense for bad indentation of an until body' do expect_offense(<<~RUBY) until cond func ^ Use 2 (not 1) spaces for indentation. end RUBY end it 'accepts an empty while' do expect_no_offenses(<<~RUBY) while a end RUBY end end context 'with for' do it 'registers an offense for bad indentation of a for body' do expect_offense(<<~RUBY) for var in 1..10 func ^ Use 2 (not 1) spaces for indentation. end RUBY end it 'accepts an empty for' do expect_no_offenses(<<~RUBY) for var in 1..10 end RUBY end end context 'with def/defs' do shared_examples 'without modifier on the same line' do it 'registers an offense for bad indentation of a def body' do expect_offense(<<~RUBY) def test func1 ^^^^ Use 2 (not 4) spaces for indentation. func2 # No offense registered for this. end RUBY end it 'registers an offense for bad indentation of a defs body' do expect_offense(<<~RUBY) def self.test func ^^^ Use 2 (not 3) spaces for indentation. end RUBY end it 'accepts an empty def body' do expect_no_offenses(<<~RUBY) def test end RUBY end it 'accepts an empty defs body' do expect_no_offenses(<<~RUBY) def self.test end RUBY end it 'with an assignment' do expect_no_offenses(<<~RUBY) something = def self.foo end RUBY end end context 'when end is aligned with start of line' do let(:def_end_alignment_config) do { 'Enabled' => true, 'EnforcedStyleAlignWith' => 'start_of_line' } end it_behaves_like 'without modifier on the same line' context 'when modifier and def are on the same line' do it 'accepts a correctly aligned body' do expect_no_offenses(<<~RUBY) foo def test something end RUBY end it 'registers an offense for bad indentation of a def body' do expect_offense(<<~RUBY) foo def test something ^^^^^^ Use 2 (not 6) spaces for indentation. end RUBY end it 'registers an offense for bad indentation of a defs body' do expect_offense(<<~RUBY) foo def self.test something ^^^^^^ Use 2 (not 6) spaces for indentation. end RUBY end end context 'when multiple modifiers and def are on the same line' do it 'accepts a correctly aligned body' do expect_no_offenses(<<~RUBY) public foo def test something end RUBY end it 'registers an offense for bad indentation of a def body' do expect_offense(<<~RUBY) public foo def test something ^^^^^^ Use 2 (not 6) spaces for indentation. end RUBY end it 'registers an offense for bad indentation of a defs body' do expect_offense(<<~RUBY) public foo def self.test something ^^^^^^ Use 2 (not 6) spaces for indentation. end RUBY end context 'when multiple modifiers are used in a block and' \ 'a method call is made at end of the block' do it 'accepts a correctly aligned body' do expect_no_offenses(<<~RUBY) obj = Class.new do private def private_property "That would be great." end end.new RUBY end it 'registers an offense for bad indentation of a def' do expect_offense(<<~RUBY) obj = Class.new do private def private_property ^^^^ Use 2 (not 4) spaces for indentation. end end.new RUBY end it 'registers an offense for bad indentation of a def body' do expect_offense(<<~RUBY) obj = Class.new do private def private_property "That would be great." ^^^^ Use 2 (not 4) spaces for indentation. end end.new RUBY end end end end context 'when end is aligned with def' do let(:def_end_alignment_config) do { 'Enabled' => true, 'EnforcedStyleAlignWith' => 'def' } end it_behaves_like 'without modifier on the same line' context 'when modifier and def are on the same line' do it 'accepts a correctly aligned body' do expect_no_offenses(<<~RUBY) foo def test something end RUBY end it 'registers an offense for bad indentation of a def body' do expect_offense(<<~RUBY) foo def test something ^^ Use 2 (not -2) spaces for indentation. end RUBY end it 'registers an offense for bad indentation of a defs body' do expect_offense(<<~RUBY) foo def self.test something ^^ Use 2 (not -2) spaces for indentation. end RUBY end end end end context 'with class' do it 'registers an offense for bad indentation of a class body' do expect_offense(<<~RUBY) class Test def func ^^^^ Use 2 (not 4) spaces for indentation. end end RUBY end it 'leaves body unchanged if the first body line is on the same line with class keyword' do # The class body will be corrected by IndentationConsistency. expect_no_offenses(<<~RUBY) class Test foo def func1 end def func2 end end RUBY end it 'accepts an empty class body' do expect_no_offenses(<<~RUBY) class Test end RUBY end it 'leaves body unchanged if the first body line is on the same line with an opening of singleton class' do # The class body will be corrected by IndentationConsistency. expect_no_offenses(<<~RUBY) class << self; foo def func1 end def func2 end end RUBY end context 'with access modifier' do it 'registers an offense for bad indentation of sections' do expect_offense(<<~RUBY) class Test public def e ^^^^ Use 2 (not 4) spaces for indentation. end def f ^^^^ Use 2 (not 4) spaces for indentation. end
ruby
MIT
99fa0fdd0481910d7d052f4c2ed01ad36178f404
2026-01-04T15:37:41.211519Z
true
rubocop/rubocop
https://github.com/rubocop/rubocop/blob/99fa0fdd0481910d7d052f4c2ed01ad36178f404/spec/rubocop/cop/layout/multiline_operation_indentation_spec.rb
spec/rubocop/cop/layout/multiline_operation_indentation_spec.rb
# frozen_string_literal: true RSpec.describe RuboCop::Cop::Layout::MultilineOperationIndentation, :config do let(:config) do merged = RuboCop::ConfigLoader .default_configuration['Layout/MultilineOperationIndentation'] .merge(cop_config) .merge('IndentationWidth' => cop_indent) RuboCop::Config .new('Layout/MultilineOperationIndentation' => merged, 'Layout/IndentationWidth' => { 'Width' => indentation_width }) end let(:indentation_width) { 2 } let(:cop_indent) { nil } # use indentation width from Layout/IndentationWidth shared_examples 'common' do it 'accepts unary operations' do expect_no_offenses(<<~RUBY) call a, !b RUBY end it 'accepts `[]=` operator without arguments' do expect_no_offenses(<<~RUBY) begin rescue => A[] end RUBY end it 'accepts indented operands in ordinary statement' do expect_no_offenses(<<~RUBY) a + b RUBY end it 'accepts indented operands inside and outside a block' do expect_no_offenses(<<~RUBY) a = b.map do |c| c + b + d do x + y end end RUBY end it 'registers an offense and corrects no indentation of second line' do expect_offense(<<~RUBY) a + b ^ Use 2 (not 0) spaces for indenting an expression spanning multiple lines. RUBY expect_correction(<<~RUBY) a + b RUBY end it 'registers an offense and corrects one space indentation of second line' do expect_offense(<<~RUBY) a + b ^ Use 2 (not 1) spaces for indenting an expression spanning multiple lines. RUBY expect_correction(<<~RUBY) a + b RUBY end it 'does not check method calls' do expect_no_offenses(<<~RUBY) a .(args) Foo .a .b Foo .a .b(c) Foo.&( foo, bar ) expect { Foo.new }. to change { Bar.count }. from(1).to(2) RUBY end it 'registers an offense and corrects three space indentation of second line' do expect_offense(<<~RUBY) a || b ^ Use 2 (not 3) spaces for indenting an expression spanning multiple lines. c and d ^ Use 2 (not 3) spaces for indenting an expression spanning multiple lines. RUBY expect_correction(<<~RUBY) a || b c and d RUBY end it 'registers an offense and corrects extra indentation of third line' do expect_offense(<<~RUBY) a || b || c ^ Use 2 (not 4) spaces for indenting an expression spanning multiple lines. RUBY expect_correction(<<~RUBY) a || b || c RUBY end it 'registers an offense and corrects emacs ruby-mode 1.1 indentation of ' \ 'an expression in an array' do expect_offense(<<~RUBY) [ a + b ^ Use 2 (not 0) spaces for indenting an expression spanning multiple lines. ] RUBY expect_correction(<<~RUBY) [ a + b ] RUBY end it 'accepts indented operands in an array' do expect_no_offenses(<<~RUBY) dm[i][j] = [ dm[i-1][j-1] + (this[j-1] == that[i-1] ? 0 : sub), dm[i][j-1] + ins, dm[i-1][j] + del ].min RUBY end it 'accepts two spaces indentation in assignment of local variable' do expect_no_offenses(<<~RUBY) a = 'foo' + 'bar' RUBY end it 'accepts two spaces indentation in assignment of array element' do expect_no_offenses(<<~RUBY) a['test'] = 'foo' + 'bar' RUBY end it 'accepts two spaces indentation of second line' do expect_no_offenses(<<~RUBY) a || b RUBY end it 'accepts no extra indentation of third line' do expect_no_offenses(<<~RUBY) a && b && c RUBY end it 'accepts indented operands in for body' do expect_no_offenses(<<~RUBY) for x in a something && something_else end RUBY end it 'accepts alignment inside a grouped expression' do expect_no_offenses(<<~RUBY) (a + b) RUBY end it 'accepts an expression where the first operand spans multiple lines' do expect_no_offenses(<<~RUBY) subject.each do |item| result = resolve(locale) and return result end and nil RUBY end it 'accepts any indentation of parameters to #[]' do expect_no_offenses(<<~RUBY) payment = Models::IncomingPayments[ id: input['incoming-payment-id'], user_id: @user[:id]] RUBY end it 'registers an offense and corrects an unindented multiline operation ' \ 'that is the left operand in another operation' do expect_offense(<<~RUBY) a + b < 3 ^ Use 2 (not 0) spaces for indenting an expression spanning multiple lines. RUBY expect_correction(<<~RUBY) a + b < 3 RUBY end end context 'when EnforcedStyle is aligned' do let(:cop_config) { { 'EnforcedStyle' => 'aligned' } } it_behaves_like 'common' it 'accepts aligned operands in if condition' do expect_no_offenses(<<~RUBY) if a + b something end RUBY end it 'registers an offense and corrects indented operands in if condition' do expect_offense(<<~RUBY) if a + b ^ Align the operands of a condition in an `if` statement spanning multiple lines. something end RUBY expect_correction(<<~RUBY) if a + b something end RUBY end it 'registers indented code on LHS of equality operator in the method definition' do expect_offense(<<~RUBY) def config_to_allow_offenses a + b == c ^ Use 2 (not 0) spaces for indenting an expression spanning multiple lines. end RUBY expect_correction(<<~RUBY) def config_to_allow_offenses a + b == c end RUBY end it 'registers indented code on LHS of equality operator in the modifier method definition' do expect_offense(<<~RUBY) private def config_to_allow_offenses a + b == c ^ Use 2 (not 0) spaces for indenting an expression spanning multiple lines. end RUBY expect_correction(<<~RUBY) private def config_to_allow_offenses a + b == c end RUBY end it 'accepts indented code on LHS of equality operator' do expect_no_offenses(<<~RUBY) def config_to_allow_offenses a + b == c end RUBY end it 'accepts indented operands inside block + assignment' do expect_no_offenses(<<~RUBY) a = b.map do |c| c + d end requires_interpolation = node.children.any? do |s| s.type == :dstr || s.source_range.source =~ REGEXP end RUBY end it 'accepts indented operands with ternary operators' do expect_no_offenses(<<~RUBY) one || two ? 3 : 5 RUBY end it 'registers an offense and corrects indented second part of string' do expect_offense(<<~RUBY) it "should convert " + "a to " + ^^^^^^^ Align the operands of an expression spanning multiple lines. "b" do ^^^ Align the operands of an expression spanning multiple lines. end RUBY expect_correction(<<~RUBY) it "should convert " + "a to " + "b" do end RUBY end it 'registers an offense and corrects indented operand in second argument' do expect_offense(<<~RUBY) puts a, 1 + 2 ^ Align the operands of an expression spanning multiple lines. RUBY expect_correction(<<~RUBY) puts a, 1 + 2 RUBY end it 'registers an offense and corrects misaligned string operand ' \ 'when the first operand has backslash continuation' do expect_offense(<<~'RUBY') def f flash[:error] = 'Here is a string ' \ 'That spans' << 'multiple lines' ^^^^^^^^^^^^^^^^ Align the operands of an expression in an assignment spanning multiple lines. end RUBY expect_correction(<<~'RUBY') def f flash[:error] = 'Here is a string ' \ 'That spans' << 'multiple lines' end RUBY end it 'registers an offense and corrects misaligned string operand when plus is used' do expect_offense(<<~RUBY) Error = 'Here is a string ' + 'That spans' << 'multiple lines' ^^^^^^^^^^^^^^^^ Align the operands of an expression in an assignment spanning multiple lines. RUBY expect_correction(<<~RUBY) Error = 'Here is a string ' + 'That spans' << 'multiple lines' RUBY end it 'registers an offense and corrects misaligned operands in unless condition' do expect_offense(<<~RUBY) unless a + b ^ Align the operands of a condition in an `unless` statement spanning multiple lines. something end RUBY expect_correction(<<~RUBY) unless a + b something end RUBY end [ %w[an if], %w[an unless], %w[a while], %w[an until] ].each do |article, keyword| it "registers an offense for misaligned operands in #{keyword} condition" do expect_offense(<<~RUBY) #{keyword} a or b ^ Align the operands of a condition in #{article} `#{keyword}` statement spanning multiple lines. something end RUBY end end it 'accepts aligned operands in assignment' do expect_no_offenses(<<~RUBY) a = b + c + d RUBY end it 'accepts aligned or:ed operands in assignment' do expect_no_offenses(<<~RUBY) tmp_dir = ENV['TMPDIR'] || ENV['TMP'] || ENV['TEMP'] || Etc.systmpdir || '/tmp' RUBY end it 'registers an offense and corrects unaligned operands in op-assignment' do expect_offense(<<~RUBY) bar *= Foo + a + ^ Align the operands of an expression in an assignment spanning multiple lines. b(c) RUBY expect_correction(<<~RUBY) bar *= Foo + a + b(c) RUBY end end context 'when EnforcedStyle is indented' do let(:cop_config) { { 'EnforcedStyle' => 'indented' } } it_behaves_like 'common' it 'accepts indented operands in if condition' do expect_no_offenses(<<~RUBY) if a + b something end RUBY end it 'registers an offense and corrects aligned operands in if conditions' do expect_offense(<<~RUBY) if a + b ^ Use 4 (not 3) spaces for indenting a condition in an `if` statement spanning multiple lines. something end RUBY expect_correction(<<~RUBY) if a + b something end RUBY end it 'accepts the indentation of a broken string' do expect_no_offenses(<<~'RUBY') MSG = 'Use 2 (not %d) spaces for indenting a ' \ 'broken line.' RUBY end it 'accepts normal indentation of method parameters' do expect_no_offenses(<<~RUBY) Parser::Source::Range.new(expr.source_buffer, begin_pos, begin_pos + line.length) RUBY end it 'accepts any indentation of method parameters' do expect_no_offenses(<<~RUBY) a(b + c + d) RUBY end it 'accepts normal indentation inside grouped expression' do expect_no_offenses(<<~RUBY) arg_array.size == a.size && ( arg_array == a || arg_array.map(&:children) == a.map(&:children) ) RUBY end it 'registers an offense and corrects aligned code on LHS of equality operator' do expect_offense(<<~RUBY) def config_to_allow_offenses a + b == c ^ Use 2 (not 0) spaces for indenting an expression spanning multiple lines. end RUBY expect_correction(<<~RUBY) def config_to_allow_offenses a + b == c end RUBY end [ %w[an if], %w[an unless], %w[a while], %w[an until] ].each do |article, keyword| it "accepts double indentation of #{keyword} condition" do expect_no_offenses(<<~RUBY) #{keyword} receiver.nil? && !args.empty? && FORBIDDEN_METHODS.include?(method_name) end #{keyword} receiver. nil? end RUBY end it "registers an offense for a 2 space indentation of #{keyword} condition" do expect_offense(<<~RUBY) #{keyword} receiver.nil? && !args.empty? && ^^^^^^^^^^^^ Use 4 (not 2) spaces for indenting a condition in #{article} `#{keyword}` statement spanning multiple lines. FORBIDDEN_METHODS.include?(method_name) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Use 4 (not 2) spaces for indenting a condition in #{article} `#{keyword}` statement spanning multiple lines. end RUBY end it "accepts indented operands in #{keyword} body" do expect_no_offenses(<<~RUBY) #{keyword} a something && something_else end RUBY end end %w[unless if].each do |keyword| it "accepts indentation of return #{keyword} condition" do expect_no_offenses(<<~RUBY) return #{keyword} receiver.nil? && !args.empty? && FORBIDDEN_METHODS.include?(method_name) RUBY end it "accepts indentation of next #{keyword} condition", :ruby32, unsupported_on: :prism do expect_no_offenses(<<~RUBY) next #{keyword} 5 || 7 RUBY end end it 'registers an offense and corrects wrong indentation of for expression' do expect_offense(<<~RUBY) for n in a + b ^ Use 4 (not 2) spaces for indenting a collection in a `for` statement spanning multiple lines. end RUBY expect_correction(<<~RUBY) for n in a + b end RUBY end it 'accepts special indentation of for expression' do expect_no_offenses(<<~RUBY) for n in a + b end RUBY end it 'accepts indentation of assignment' do expect_no_offenses(<<~RUBY) a = b + c + d RUBY end it 'registers an offense and corrects correct + unrecognized style' do expect_offense(<<~RUBY) a || b c and d ^ Use 2 (not 4) spaces for indenting an expression spanning multiple lines. RUBY expect_correction(<<~RUBY) a || b c and d RUBY end it 'registers an offense and corrects aligned operators in assignment' do expect_offense(<<~RUBY) a = b + c + ^ Use 2 (not 4) spaces for indenting an expression in an assignment spanning multiple lines. d ^ Use 2 (not 4) spaces for indenting an expression in an assignment spanning multiple lines. RUBY expect_correction(<<~RUBY) a = b + c + d RUBY end context 'when indentation width is overridden for this cop' do let(:cop_indent) { 6 } it 'accepts indented operands in if condition' do expect_no_offenses(<<~RUBY) if a + b something end RUBY end [ %w[an if], %w[an unless], %w[a while], %w[an until] ].each do |article, keyword| it "accepts indentation of #{keyword} condition which is offset " \ 'by a single normal indentation step' do # normal code indentation is 2 spaces, and we have configured # multiline method indentation to 6 spaces # so in this case, 8 spaces are required expect_no_offenses(<<~RUBY) #{keyword} receiver.nil? && !args.empty? && FORBIDDEN_METHODS.include?(method_name) end #{keyword} receiver. nil? end RUBY end it "registers an offense for a 4 space indentation of #{keyword} condition" do expect_offense(<<~RUBY) #{keyword} receiver.nil? && !args.empty? && ^^^^^^^^^^^^ Use 8 (not 4) spaces for indenting a condition in #{article} `#{keyword}` statement spanning multiple lines. FORBIDDEN_METHODS.include?(method_name) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Use 8 (not 4) spaces for indenting a condition in #{article} `#{keyword}` statement spanning multiple lines. end RUBY end it "accepts indented operands in #{keyword} body" do expect_no_offenses(<<~RUBY) #{keyword} a something && something_else end RUBY end end it 'registers an offense and corrects' do expect_offense(<<~RUBY) until a + b ^ Use 8 (not 6) spaces for indenting a condition in an `until` statement spanning multiple lines. something end RUBY expect_correction(<<~RUBY) until a + b something end RUBY end end end end
ruby
MIT
99fa0fdd0481910d7d052f4c2ed01ad36178f404
2026-01-04T15:37:41.211519Z
false
rubocop/rubocop
https://github.com/rubocop/rubocop/blob/99fa0fdd0481910d7d052f4c2ed01ad36178f404/spec/rubocop/cop/layout/closing_parenthesis_indentation_spec.rb
spec/rubocop/cop/layout/closing_parenthesis_indentation_spec.rb
# frozen_string_literal: true RSpec.describe RuboCop::Cop::Layout::ClosingParenthesisIndentation, :config do context 'for method calls' do context 'with line break before 1st parameter' do it 'registers an offense for misaligned )' do expect_offense(<<~RUBY) some_method( a ) ^ Indent `)` to column 0 (not 2) RUBY expect_correction(<<~RUBY) some_method( a ) RUBY end it 'accepts a correctly aligned )' do expect_no_offenses(<<~RUBY) some_method( a ) RUBY end end context 'with no line break before 1st parameter' do it 'registers an offense for misaligned )' do expect_offense(<<~RUBY) some_method(a ) ^ Align `)` with `(`. RUBY expect_correction(<<~RUBY) some_method(a ) RUBY end it 'accepts a correctly aligned )' do expect_no_offenses(<<~RUBY) some_method(a ) RUBY end it 'does not register an offense when using keyword arguments' do expect_no_offenses(<<~RUBY) some_method(x: 1, y: 2, z: 3 ) RUBY end it 'does not register an offense when using keyword splat arguments' do expect_no_offenses(<<~RUBY) some_method(x, **options ) RUBY end it 'accepts a correctly indented )' do expect_no_offenses(<<~RUBY) some_method(a, x: 1, y: 2 ) b = some_method(a, ) RUBY end it 'accepts a correctly indented ) inside a block' do expect_no_offenses(<<~RUBY) block_adds_extra_indentation do some_method(a, x: 1, y: 2 ) b = some_method(a, ) end RUBY end it 'registers an offense and corrects misindented ) when ) is aligned with the params' do expect_offense(<<~RUBY) some_method(a, x: 1, y: 2 ) ^ Indent `)` to column 0 (not 12) b = some_method(a, ) ^ Align `)` with `(`. RUBY expect_correction(<<~RUBY) some_method(a, x: 1, y: 2 ) b = some_method(a, ) RUBY end it 'does not register offense for aligned parens when first parameter is a hash' do expect_no_offenses(<<~RUBY) some_method({ foo: 1, bar: 2 }, x: 1, y: 2 ) RUBY end end context 'without arguments' do it 'accepts empty ()' do expect_no_offenses('some_method()') end it 'can handle indentation up against the left edge' do expect_no_offenses(<<~RUBY) some_method( ) RUBY end it 'accepts a correctly aligned ) against (' do expect_no_offenses(<<~RUBY) some_method( ) RUBY end end context 'with first multiline arg on new line' do it 'accepts ) on the same level as ( with args on same line' do expect_no_offenses(<<~RUBY) where( "multiline condition", second_arg ) RUBY end it 'accepts ) on the same level as ( with second arg on new line' do expect_no_offenses(<<~RUBY) where( "multiline condition", second_arg ) RUBY end end end context 'for method assignments with indented parameters' do context 'with line break before 1st parameter' do it 'registers an offense for misaligned )' do expect_offense(<<~RUBY) foo = some_method( a ) ^ Align `)` with `(`. RUBY expect_correction(<<~RUBY) foo = some_method( a ) RUBY end it 'accepts a correctly aligned )' do expect_no_offenses(<<~RUBY) foo = some_method( a ) RUBY end end context 'with no line break before 1st parameter' do it 'registers an offense for misaligned )' do expect_offense(<<~RUBY) foo = some_method(a ) ^ Align `)` with `(`. some_method(a, x: 1, y: 2 ) ^ Align `)` with `(`. RUBY expect_correction(<<~RUBY) foo = some_method(a ) some_method(a, x: 1, y: 2 ) RUBY end it 'can handle inner method calls' do expect_no_offenses(<<~RUBY) expect(response).to contain_exactly( { a: 1, b: 'x' }, { a: 2, b: 'y' } ) RUBY end it 'can handle individual arguments that are broken over lines' do expect_no_offenses(<<~RUBY) corrector.insert_before( range, "\n" + ' ' * (node.loc.keyword.column + indent_steps * configured_width) ) RUBY end it 'can handle indentation up against the left edge' do expect_no_offenses(<<~RUBY) foo( a: b ) RUBY end it 'can handle hash arguments that are not broken over lines' do expect_no_offenses(<<~RUBY) corrector.insert_before( range, arg_1: 'foo', arg_2: 'bar' ) RUBY end it 'accepts a correctly aligned )' do expect_no_offenses(<<~RUBY) foo = some_method(a ) RUBY end it 'accepts a correctly indented )' do expect_no_offenses(<<~RUBY) foo = some_method(a, x: 1, y: 2 ) b = some_method(a, ) RUBY end end context 'without arguments' do it 'accepts empty ()' do expect_no_offenses('foo = some_method()') end it 'accepts a correctly aligned ) against (' do expect_no_offenses(<<~RUBY) foo = some_method( ) RUBY end it 'can handle indentation up against the left edge' do expect_no_offenses(<<~RUBY) foo = some_method( ) RUBY end it 'can handle indentation up against the method' do expect_no_offenses(<<~RUBY) foo = some_method( ) RUBY end it 'registers an offense for misaligned )' do expect_offense(<<~RUBY) foo = some_method( ) ^ Indent `)` to column 0 (not 2) RUBY expect_correction(<<~RUBY) foo = some_method( ) RUBY end end end context 'for method chains' do it 'can handle multiple chains with differing breaks' do expect_no_offenses(<<~RUBY) good = foo.methA(:arg1, :arg2, options: :hash) .methB( :arg1, :arg2, ) .methC good = foo.methA( :arg1, :arg2, options: :hash, ) .methB( :arg1, :arg2, ) .methC RUBY end it 'registers an offense and corrects method chains' do expect_offense(<<~RUBY) good = foo.methA(:arg1, :arg2, options: :hash) .methB( :arg1, :arg2, ) ^ Indent `)` to column 9 (not 5) .methC good = foo.methA( :arg1, :arg2, options: :hash, ) ^ Indent `)` to column 9 (not 4) .methB( :arg1, :arg2, ) ^ Indent `)` to column 9 (not 7) .methC RUBY expect_correction(<<~RUBY) good = foo.methA(:arg1, :arg2, options: :hash) .methB( :arg1, :arg2, ) .methC good = foo.methA( :arg1, :arg2, options: :hash, ) .methB( :arg1, :arg2, ) .methC RUBY end context 'when using safe navigation operator' do it 'registers an offense and corrects misaligned )' do expect_offense(<<~RUBY) receiver&.some_method( a ) ^ Indent `)` to column 0 (not 2) RUBY expect_correction(<<~RUBY) receiver&.some_method( a ) RUBY end end end context 'for method definitions' do context 'with line break before 1st parameter' do it 'registers an offense for misaligned )' do expect_offense(<<~RUBY) def some_method( a ) ^ Indent `)` to column 0 (not 2) end RUBY expect_correction(<<~RUBY) def some_method( a ) end RUBY end it 'accepts a correctly aligned )' do expect_no_offenses(<<~RUBY) def some_method( a ) end RUBY end end context 'with no line break before 1st parameter' do it 'registers an offense for misaligned )' do expect_offense(<<~RUBY) def some_method(a ) ^ Align `)` with `(`. end RUBY expect_correction(<<~RUBY) def some_method(a ) end RUBY end it 'accepts a correctly aligned )' do expect_no_offenses(<<~RUBY) def some_method(a ) end RUBY end it 'accepts empty ()' do expect_no_offenses(<<~RUBY) def some_method() end RUBY end end end context 'for grouped expressions' do context 'with line break before 1st operand' do it 'registers an offense for misaligned )' do expect_offense(<<~RUBY) w = x * ( y + z ) ^ Indent `)` to column 0 (not 2) RUBY expect_correction(<<~RUBY) w = x * ( y + z ) RUBY end it 'accepts a correctly aligned )' do expect_no_offenses(<<~RUBY) w = x * ( y + z ) RUBY end end context 'with no line break before 1st operand' do it 'registers an offense for misaligned )' do expect_offense(<<~RUBY) w = x * (y + z ) ^ Align `)` with `(`. RUBY expect_correction(<<~RUBY) w = x * (y + z ) RUBY end it 'accepts a correctly aligned )' do expect_no_offenses(<<~RUBY) w = x * (y + z ) RUBY end it 'accepts ) that does not begin its line' do expect_no_offenses(<<~RUBY) w = x * (y + z + a) RUBY end end end it 'accepts begin nodes that are not grouped expressions' do expect_no_offenses(<<~RUBY) def a x y end RUBY end end
ruby
MIT
99fa0fdd0481910d7d052f4c2ed01ad36178f404
2026-01-04T15:37:41.211519Z
false
rubocop/rubocop
https://github.com/rubocop/rubocop/blob/99fa0fdd0481910d7d052f4c2ed01ad36178f404/spec/rubocop/cop/layout/space_around_block_parameters_spec.rb
spec/rubocop/cop/layout/space_around_block_parameters_spec.rb
# frozen_string_literal: true RSpec.describe RuboCop::Cop::Layout::SpaceAroundBlockParameters, :config do shared_examples 'common behavior' do it 'accepts an empty block' do expect_no_offenses('{}.each {}') end it 'skips lambda without args' do expect_no_offenses('->() { puts "a" }') end it 'skips lambda without parens' do expect_no_offenses('->a { puts a }') end end context 'when EnforcedStyleInsidePipes is no_space' do let(:cop_config) { { 'EnforcedStyleInsidePipes' => 'no_space' } } it_behaves_like 'common behavior' it 'accepts a block with spaces in the right places' do expect_no_offenses('{}.each { |x, y| puts x }') end it 'accepts a block with parameters but no body' do expect_no_offenses('{}.each { |x, y| }') end it 'accepts a block parameter without preceding space' do # This is checked by Layout/SpaceAfterComma. expect_no_offenses('{}.each { |x,y| puts x }') end it 'accepts block parameters with surrounding space that includes line breaks' do # This is checked by Layout/MultilineBlockLayout. expect_no_offenses(<<~RUBY) some_result = lambda do | so_many, parameters, it_will, be_too_long, for_one_line | do_something end RUBY end it 'accepts a lambda with spaces in the right places' do expect_no_offenses('->(x, y) { puts x }') end it 'registers an offense and corrects space before first parameter' do expect_offense(<<~RUBY) {}.each { | x| puts x } ^ Space before first block parameter detected. RUBY expect_correction(<<~RUBY) {}.each { |x| puts x } RUBY end it 'registers an offense and corrects space after last parameter' do expect_offense(<<~RUBY) {}.each { |x, y | puts x } ^^ Space after last block parameter detected. RUBY expect_correction(<<~RUBY) {}.each { |x, y| puts x } RUBY end it 'registers an offense and corrects no space after closing pipe' do expect_offense(<<~RUBY) {}.each { |x, y|puts x } ^ Space after closing `|` missing. RUBY expect_correction(<<~RUBY) {}.each { |x, y| puts x } RUBY end it 'registers an offense and corrects a lambda for space before first parameter' do expect_offense(<<~RUBY) ->( x, y) { puts x } ^ Space before first block parameter detected. RUBY expect_correction(<<~RUBY) ->(x, y) { puts x } RUBY end it 'registers an offense and corrects a lambda for space after the last parameter' do expect_offense(<<~RUBY) ->(x, y ) { puts x } ^^ Space after last block parameter detected. RUBY expect_correction(<<~RUBY) ->(x, y) { puts x } RUBY end it 'accepts line break after closing pipe' do expect_no_offenses(<<~RUBY) {}.each do |x, y| puts x end RUBY end it 'registers an offense and corrects multiple spaces before parameter' do expect_offense(<<~RUBY) {}.each { |x, y| puts x } ^^ Extra space before block parameter detected. RUBY expect_correction(<<~RUBY) {}.each { |x, y| puts x } RUBY end it 'registers an offense and corrects for space with parens' do expect_offense(<<~RUBY) {}.each { |a, (x, y), z| puts x } ^ Extra space before block parameter detected. ^ Extra space before block parameter detected. ^ Extra space before block parameter detected. RUBY expect_correction(<<~RUBY) {}.each { |a, (x, y), z| puts x } RUBY end context 'trailing comma' do it 'registers an offense for space after the last comma' do expect_offense(<<~RUBY) {}.each { |x, | puts x } ^ Space after last block parameter detected. RUBY expect_correction(<<~RUBY) {}.each { |x,| puts x } RUBY end it 'registers an offense for space before and after the last comma' do expect_offense(<<~RUBY) {}.each { |x , | puts x } ^ Space after last block parameter detected. RUBY expect_correction(<<~RUBY) {}.each { |x ,| puts x } RUBY end end it 'registers an offense and corrects all types of spacing issues' do expect_offense(<<~RUBY) {}.each { | x=5, (y,*z) |puts x } ^ Space after closing `|` missing. ^ Space after last block parameter detected. ^ Extra space before block parameter detected. ^ Extra space before block parameter detected. ^^ Space before first block parameter detected. RUBY expect_correction(<<~RUBY) {}.each { |x=5, (y,*z)| puts x } RUBY end it 'registers an offense and corrects all types of spacing issues for a lambda' do expect_offense(<<~RUBY) ->( a, b, c) { puts a } ^ Extra space before block parameter detected. ^ Extra space before block parameter detected. ^^ Space before first block parameter detected. RUBY expect_correction(<<~RUBY) ->(a, b, c) { puts a } RUBY end end context 'when EnforcedStyleInsidePipes is space' do let(:cop_config) { { 'EnforcedStyleInsidePipes' => 'space' } } it_behaves_like 'common behavior' it 'accepts a block with spaces in the right places' do expect_no_offenses('{}.each { | x, y | puts x }') end it 'accepts a block with parameters but no body' do expect_no_offenses('{}.each { | x, y | }') end it 'accepts a block parameter without preceding space' do # This is checked by Layout/SpaceAfterComma. expect_no_offenses('{}.each { | x,y | puts x }') end it 'accepts a lambda with spaces in the right places' do expect_no_offenses('->( x, y ) { puts x }') end it 'registers an offense for no space before first parameter' do expect_offense(<<~RUBY) {}.each { |x | puts x } ^ Space before first block parameter missing. RUBY expect_correction(<<~RUBY) {}.each { | x | puts x } RUBY end it 'registers an offense and corrects no space after last parameter' do expect_offense(<<~RUBY) {}.each { | x, y| puts x } ^ Space after last block parameter missing. RUBY expect_correction(<<~RUBY) {}.each { | x, y | puts x } RUBY end it 'registers an offense and corrects extra space before first parameter' do expect_offense(<<~RUBY) {}.each { | x | puts x } ^ Extra space before first block parameter detected. RUBY expect_correction(<<~RUBY) {}.each { | x | puts x } RUBY end it 'registers an offense and corrects multiple spaces after last parameter' do expect_offense(<<~RUBY) {}.each { | x, y | puts x } ^^ Extra space after last block parameter detected. RUBY expect_correction(<<~RUBY) {}.each { | x, y | puts x } RUBY end it 'registers an offense and corrects no space after closing pipe' do expect_offense(<<~RUBY) {}.each { | x, y |puts x } ^ Space after closing `|` missing. RUBY expect_correction(<<~RUBY) {}.each { | x, y | puts x } RUBY end it 'registers an offense and corrects a lambda for no space before first parameter' do expect_offense(<<~RUBY) ->(x ) { puts x } ^ Space before first block parameter missing. RUBY expect_correction(<<~RUBY) ->( x ) { puts x } RUBY end it 'registers an offense and corrects a lambda for no space after last parameter' do expect_offense(<<~RUBY) ->( x, y) { puts x } ^ Space after last block parameter missing. RUBY expect_correction(<<~RUBY) ->( x, y ) { puts x } RUBY end it 'registers an offense and corrects a lambda for extra space before first parameter' do expect_offense(<<~RUBY) ->( x ) { puts x } ^ Extra space before first block parameter detected. RUBY expect_correction(<<~RUBY) ->( x ) { puts x } RUBY end it 'registers an offense and corrects a lambda for multiple spaces after last parameter' do expect_offense(<<~RUBY) ->( x, y ) { puts x } ^^ Extra space after last block parameter detected. RUBY expect_correction(<<~RUBY) ->( x, y ) { puts x } RUBY end it 'accepts line break after closing pipe' do expect_no_offenses(<<~RUBY) {}.each do | x, y | puts x end RUBY end it 'registers an offense and corrects multiple spaces before parameter' do expect_offense(<<~RUBY) {}.each { | x, y | puts x } ^^ Extra space before block parameter detected. RUBY expect_correction(<<~RUBY) {}.each { | x, y | puts x } RUBY end it 'registers an offense and corrects space with parens at middle' do expect_offense(<<~RUBY) {}.each { |(x, y), z| puts x } ^^^^^^^ Space before first block parameter missing. ^ Extra space before block parameter detected. ^ Extra space before block parameter detected. ^ Space after last block parameter missing. RUBY expect_correction(<<~RUBY) {}.each { | (x, y), z | puts x } RUBY end context 'trailing comma' do it 'accepts space after the last comma' do expect_no_offenses('{}.each { | x, | puts x }') end it 'accepts space both before and after the last comma' do expect_no_offenses('{}.each { | x , | puts x }') end it 'registers an offense and corrects no space after the last comma' do expect_offense(<<~RUBY) {}.each { | x,| puts x } ^ Space after last block parameter missing. RUBY expect_correction(<<~RUBY, loop: false) {}.each { | x ,| puts x } RUBY end end it 'registers an offense and corrects block arguments inside Hash#each' do expect_offense(<<~RUBY) {}.each { | x=5, (y,*z)|puts x } ^ Space after closing `|` missing. ^^^^^^ Space after last block parameter missing. ^ Extra space before block parameter detected. ^ Extra space before first block parameter detected. RUBY expect_correction(<<~RUBY) {}.each { | x=5, (y,*z) | puts x } RUBY end it 'registers an offense and corrects missing space ' \ 'before first argument and after last argument' do expect_offense(<<~RUBY) {}.each { |x, z| puts x } ^ Space after last block parameter missing. ^ Space before first block parameter missing. RUBY expect_correction(<<~RUBY) {}.each { | x, z | puts x } RUBY end it 'registers an offense and corrects spacing in lambda args' do expect_offense(<<~RUBY) ->( x, y) { puts x } ^ Space after last block parameter missing. ^ Extra space before block parameter detected. ^ Extra space before first block parameter detected. RUBY expect_correction(<<~RUBY) ->( x, y ) { puts x } RUBY end end end
ruby
MIT
99fa0fdd0481910d7d052f4c2ed01ad36178f404
2026-01-04T15:37:41.211519Z
false
rubocop/rubocop
https://github.com/rubocop/rubocop/blob/99fa0fdd0481910d7d052f4c2ed01ad36178f404/spec/rubocop/cop/layout/indentation_style_spec.rb
spec/rubocop/cop/layout/indentation_style_spec.rb
# frozen_string_literal: true RSpec.describe RuboCop::Cop::Layout::IndentationStyle, :config do let(:config) do supported_styles = { 'SupportedStyles' => %w[spaces tabs] } RuboCop::Config.new( 'Layout/IndentationWidth' => { 'Width' => 2 }, 'Layout/IndentationStyle' => cop_config.merge(supported_styles) ) end context 'when EnforcedStyle is spaces' do let(:cop_config) { { 'EnforcedStyle' => 'spaces' } } it 'registers and corrects an offense for a line indented with tab' do expect_offense(<<~RUBY) \tx = 0 ^ Tab detected in indentation. RUBY expect_correction(<<-RUBY.strip_margin('|')) | x = 0 RUBY end it 'registers and corrects an offense for a line indented with multiple tabs' do expect_offense(<<~RUBY) \t\t\tx = 0 ^^^ Tab detected in indentation. RUBY expect_correction(<<-RUBY.strip_margin('|')) | x = 0 RUBY end it 'registers and corrects an offense for a line indented with mixed whitespaces' do expect_offense(<<~RUBY) \tx = 0 ^^ Tab detected in indentation. RUBY expect_correction(<<-RUBY.strip_margin('|')) | x = 0 RUBY end it 'registers offenses before __END__ but not after' do expect_offense(<<~RUBY) \tx = 0 ^ Tab detected in indentation. __END__ \tx = 0 RUBY end it 'accepts a line with a tab other than indentation' do expect_no_offenses("foo \t bar") end it 'accepts a line with a tab between string literals' do expect_no_offenses("'foo'\t'bar'") end it 'accepts a line with tab in a string' do expect_no_offenses("(x = \"\t\")") end it 'accepts a line which begins with tab in a string' do expect_no_offenses("x = '\n\thello'") end it 'accepts a line which begins with tab in a heredoc' do expect_no_offenses("x = <<HELLO\n\thello\nHELLO") end it 'accepts a line which begins with tab in a multiline heredoc' do expect_no_offenses("x = <<HELLO\n\thello\n\t\n\t\t\nhello\nHELLO") end it 'registers and corrects an offense for a line with tab in a string indented with tab' do expect_offense(<<~RUBY) \t(x = "\t") ^ Tab detected in indentation. RUBY expect_correction(<<-RUBY.strip_margin('|')) | (x = "\t") RUBY end context 'custom indentation width' do let(:cop_config) { { 'IndentationWidth' => 3, 'EnforcedStyle' => 'spaces' } } it 'uses the configured number of spaces to replace a tab' do expect_offense(<<~RUBY) \tx = 0 ^ Tab detected in indentation. RUBY expect_correction(<<-RUBY.strip_margin('|')) | x = 0 RUBY end end end context 'when EnforcedStyle is tabs' do let(:cop_config) { { 'EnforcedStyle' => 'tabs' } } it 'registers and corrects an offense for a line indented with space' do expect_offense(<<~RUBY) x = 0 ^^ Space detected in indentation. RUBY expect_correction(<<~RUBY) \tx = 0 RUBY end it 'registers and corrects an offense for a line indented with multiple spaces' do expect_offense(<<~RUBY) x = 0 ^^^^^^ Space detected in indentation. RUBY expect_correction(<<~RUBY) \t\t\tx = 0 RUBY end it 'registers an offense for a line indented with mixed whitespace' do expect_offense(<<~RUBY) \tx = 0 ^ Space detected in indentation. RUBY end it 'registers offenses before __END__ but not after' do expect_offense(<<~RUBY) x = 0 ^^ Space detected in indentation. __END__ x = 0 RUBY end it 'accepts a line a tab other than indentation' do expect_no_offenses("\tfoo \t bar") end it 'accepts a line with tabs between string literals' do expect_no_offenses("'foo'\t'bar'") end it 'accepts a line with tab in a string' do expect_no_offenses("(x = \"\t\")") end it 'accepts a line which begins with tab in a string' do expect_no_offenses("x = '\n\thello'") end it 'accepts a line which begins with tab in a heredoc' do expect_no_offenses("x = <<HELLO\n\thello\nHELLO") end it 'accepts a line which begins with tab in a multiline heredoc' do expect_no_offenses("x = <<HELLO\n\thello\n\t\n\t\t\nhello\nHELLO") end it 'registers and corrects an offense for a line indented with fractional number of' \ 'indentation groups by rounding down' do expect_offense(<<~RUBY) x = 0 ^^^ Space detected in indentation. RUBY expect_correction(<<~RUBY) \tx = 0 RUBY end it 'registers and corrects an offense for a line with tab in a string indented with space' do expect_offense(<<~RUBY) (x = "\t") ^^ Space detected in indentation. RUBY expect_correction(<<~RUBY) \t(x = "\t") RUBY end context 'custom indentation width' do let(:cop_config) { { 'IndentationWidth' => 3, 'EnforcedStyle' => 'tabs' } } it 'uses the configured number of spaces to replace with a tab' do expect_offense(<<~RUBY) x = 0 ^^^^^^ Space detected in indentation. RUBY expect_correction(<<~RUBY) \t\tx = 0 RUBY end end end end
ruby
MIT
99fa0fdd0481910d7d052f4c2ed01ad36178f404
2026-01-04T15:37:41.211519Z
false
rubocop/rubocop
https://github.com/rubocop/rubocop/blob/99fa0fdd0481910d7d052f4c2ed01ad36178f404/spec/rubocop/cop/layout/first_array_element_indentation_spec.rb
spec/rubocop/cop/layout/first_array_element_indentation_spec.rb
# frozen_string_literal: true RSpec.describe RuboCop::Cop::Layout::FirstArrayElementIndentation, :config do let(:config) do supported_styles = { 'SupportedStyles' => %w[special_inside_parentheses consistent align_brackets] } RuboCop::Config.new('Layout/FirstArrayElementIndentation' => cop_config.merge(supported_styles).merge( 'IndentationWidth' => cop_indent ), 'Layout/IndentationWidth' => { 'Width' => 2 }) end let(:cop_config) { { 'EnforcedStyle' => 'special_inside_parentheses' } } let(:cop_indent) { nil } # use indent from Layout/IndentationWidth context 'when array is operand' do it 'accepts correctly indented first element' do expect_no_offenses(<<~RUBY) a << [ 1 ] RUBY end it 'registers an offense and corrects incorrectly indented first element' do expect_offense(<<~RUBY) a << [ 1 ^ Use 2 spaces for indentation in an array, relative to the start of the line where the left square bracket is. ] RUBY expect_correction(<<~RUBY) a << [ 1 ] RUBY end it 'registers an offense and corrects incorrectly indented ]' do expect_offense(<<~RUBY) a << [ ] ^ Indent the right bracket the same as the start of the line where the left bracket is. RUBY expect_correction(<<~RUBY) a << [ ] RUBY end context 'when indentation width is overridden for this cop' do let(:cop_indent) { 4 } it 'accepts correctly indented first element' do expect_no_offenses(<<~RUBY) a << [ 1 ] RUBY end it 'registers an offense and corrects incorrectly indented 1st element' do expect_offense(<<~RUBY) a << [ 1 ^ Use 4 spaces for indentation in an array, relative to the start of the line where the left square bracket is. ] RUBY expect_correction(<<~RUBY) a << [ 1 ] RUBY end end end context 'when array is argument to setter' do it 'accepts correctly indented first element' do expect_no_offenses(<<~RUBY) config.rack_cache = [ "rails:/", "rails:/", false ] RUBY end it 'registers an offense and corrects incorrectly indented first element' do expect_offense(<<~RUBY) config.rack_cache = [ "rails:/", ^^^^^^^^^ Use 2 spaces for indentation in an array, relative to the start of the line where the left square bracket is. "rails:/", false ] RUBY expect_correction(<<~RUBY) config.rack_cache = [ "rails:/", "rails:/", false ] RUBY end end context 'when array is right hand side in assignment' do it 'registers an offense and corrects incorrectly indented first element' do expect_offense(<<~RUBY) a = [ 1, ^ Use 2 spaces for indentation in an array, relative to the start of the line where the left square bracket is. 2, 3 ] RUBY expect_correction(<<~RUBY) a = [ 1, 2, 3 ] RUBY end it 'accepts correctly indented first element' do expect_no_offenses(<<~RUBY) a = [ 1 ] RUBY end it 'accepts several elements per line' do expect_no_offenses(<<~RUBY) a = [ 1, 2 ] RUBY end it 'accepts a first element on the same line as the left bracket' do expect_no_offenses(<<~RUBY) a = [1, 2] RUBY end it 'accepts single line array' do expect_no_offenses('a = [1, 2]') end it 'accepts an empty array' do expect_no_offenses('a = []') end it 'accepts multi-assignments with brackets' do expect_no_offenses('a, b = [b, a]') end it 'accepts multi-assignments with no brackets' do expect_no_offenses('a, b = b, a') end end context 'when array is method argument' do context 'and arguments are surrounded by parentheses' do context 'and EnforcedStyle is special_inside_parentheses' do it 'accepts special indentation for first argument' do expect_no_offenses(<<~RUBY) h = [ 1 ] func([ 1 ]) func(x, [ 1 ]) h = [1 ] func([1 ]) func(x, [1 ]) RUBY end it "registers an offense and corrects 'consistent' indentation" do expect_offense(<<~RUBY) func([ 1 ^ Use 2 spaces for indentation in an array, relative to the first position after the preceding left parenthesis. ]) ^ Indent the right bracket the same as the first position after the preceding left parenthesis. RUBY expect_correction(<<~RUBY) func([ 1 ]) RUBY end context 'when using safe navigation operator' do it "registers an offense and corrects 'consistent' indentation" do expect_offense(<<~RUBY) receiver&.func([ 1 ^ Use 2 spaces for indentation in an array, relative to the first position after the preceding left parenthesis. ]) ^ Indent the right bracket the same as the first position after the preceding left parenthesis. RUBY expect_correction(<<~RUBY) receiver&.func([ 1 ]) RUBY end end it "registers an offense and corrects 'align_brackets' indentation" do expect_offense(<<~RUBY) var = [ 1 ^ Use 2 spaces for indentation in an array, relative to the start of the line where the left square bracket is. ] ^ Indent the right bracket the same as the start of the line where the left bracket is. RUBY expect_correction(<<~RUBY) var = [ 1 ] RUBY end it 'accepts special indentation for second argument' do expect_no_offenses(<<~RUBY) body.should have_tag("input", [ :name]) RUBY end it 'accepts normal indentation for array within array' do expect_no_offenses(<<~RUBY) puts( [ [1, 2] ] ) RUBY end it 'registers an offense for incorrectly indented multi-line array that is the value of a single pair hash' do expect_offense(<<~RUBY) func(x: [ :a, :b]) ^^ Use 2 spaces for indentation in an array, relative to the first position after the preceding left parenthesis. RUBY expect_correction(<<~RUBY) func(x: [ :a, :b]) RUBY end it 'registers an offense for a multi-line array that is a value of a multi pairs hash ' \ 'when the indent of its elements is not based on the hash key' do expect_offense(<<~RUBY) func(x: [ :a, ^^ Use 2 spaces for indentation in an array, relative to the parent hash key. :b ], ^ Indent the right bracket the same as the parent hash key. y: [ :c, :d ]) RUBY expect_correction(<<~RUBY) func(x: [ :a, :b ], y: [ :c, :d ]) RUBY end it 'accepts indent based on the preceding left parenthesis ' \ 'when the right bracket and its following pair is on the same line' do expect_no_offenses(<<~RUBY) func(:x, y: [ :a, :b ], z: [ :c, :d ]) RUBY end it 'accepts indent based on the left brace when the outer hash key and ' \ 'the left bracket is not on the same line' do expect_no_offenses(<<~RUBY) func(x: [ :a, :b ], y: [ :a, :b ]) RUBY end end context 'and EnforcedStyle is consistent' do let(:cop_config) { { 'EnforcedStyle' => 'consistent' } } it 'accepts normal indentation for first argument' do expect_no_offenses(<<~RUBY) h = [ 1 ] func([ 1 ]) func(x, [ 1 ]) h = [1 ] func([1 ]) func(x, [1 ]) RUBY end it 'registers an offense and corrects incorrect indentation' do expect_offense(<<~RUBY) func([ 1 ^ Use 2 spaces for indentation in an array, relative to the start of the line where the left square bracket is. ]) ^ Indent the right bracket the same as the start of the line where the left bracket is. RUBY expect_correction(<<~RUBY) func([ 1 ]) RUBY end it 'accepts normal indentation for second argument' do expect_no_offenses(<<~RUBY) body.should have_tag("input", [ :name]) RUBY end it 'registers an offense for incorrectly indented multi-line array that is the value of a single pair hash' do expect_offense(<<~RUBY) func(x: [ :a, :b]) ^^ Use 2 spaces for indentation in an array, relative to the start of the line where the left square bracket is. RUBY expect_correction(<<~RUBY) func(x: [ :a, :b]) RUBY end it 'registers an offense for a multi-line array that is a value of a multi pairs hash ' \ 'when the indent of its elements is not based on the hash key' do expect_offense(<<~RUBY) func(x: [ :a, ^^ Use 2 spaces for indentation in an array, relative to the parent hash key. :b ], ^ Indent the right bracket the same as the parent hash key. y: [ :c, :d ]) RUBY expect_correction(<<~RUBY) func(x: [ :a, :b ], y: [ :c, :d ]) RUBY end it 'accepts indent based on the start of the line where the left bracket is' \ 'when the right bracket and its following pair is on the same line' do expect_no_offenses(<<~RUBY) func(:x, y: [ :a, :b ], z: [ :c, :d ]) RUBY end it 'accepts indent based on the left brace when the outer hash key and ' \ 'the left bracket is not on the same line' do expect_no_offenses(<<~RUBY) func(x: [ :a, :b ], y: [ :a, :b ]) RUBY end end end context 'and argument are not surrounded by parentheses' do it 'accepts bracketless array' do expect_no_offenses('func 1, 2') end it 'accepts single line array with brackets' do expect_no_offenses('func x, [1, 2]') end it 'accepts a correctly indented multi-line array with brackets' do expect_no_offenses(<<~RUBY) func x, [ 1, 2] RUBY end it 'registers an offense and corrects incorrectly indented multi-line array with brackets' do expect_offense(<<~RUBY) func x, [ 1, 2] ^ Use 2 spaces for indentation in an array, relative to the start of the line where the left square bracket is. RUBY expect_correction(<<~RUBY) func x, [ 1, 2] RUBY end it 'registers an offense for incorrectly indented multi-line array that is the value of a single pair hash' do expect_offense(<<~RUBY) func x: [ :a, :b] ^^ Use 2 spaces for indentation in an array, relative to the start of the line where the left square bracket is. RUBY expect_correction(<<~RUBY) func x: [ :a, :b] RUBY end it 'registers an offense for a multi-line array that is a value of a multi pairs hash ' \ 'when the indent of its elements is not based on the hash key' do expect_offense(<<~RUBY) func x: [ :a, ^^ Use 2 spaces for indentation in an array, relative to the parent hash key. :b ], ^ Indent the right bracket the same as the parent hash key. y: [ :c, :d ] RUBY expect_correction(<<~RUBY) func x: [ :a, :b ], y: [ :c, :d ] RUBY end it 'accepts indent based on the start of the line where the left bracket is' \ 'when the right bracket and its following pair is on the same line' do expect_no_offenses(<<~RUBY) func :x, y: [ :a, :b ], z: [ :c, :d ] RUBY end it 'accepts indent based on the left bracket when the outer hash key and ' \ 'the left bracket is not on the same line' do expect_no_offenses(<<~RUBY) func x: [ :a, :b ], y: [ :a, :b ] RUBY end end end context 'when EnforcedStyle is align_brackets' do let(:cop_config) { { 'EnforcedStyle' => 'align_brackets' } } it 'accepts correctly indented first element' do expect_no_offenses(<<~RUBY) a = [ 1 ] RUBY end it 'accepts several elements per line' do expect_no_offenses(<<~RUBY) a = [ 1, 2 ] RUBY end it 'accepts a first element on the same line as the left bracket' do expect_no_offenses(<<~RUBY) a = [1, 2] RUBY end it 'accepts single line array' do expect_no_offenses('a = [1, 2]') end it 'accepts an empty array' do expect_no_offenses('a = []') end it 'accepts multi-assignments with brackets' do expect_no_offenses('a, b = [b, a]') end it 'accepts multi-assignments with no brackets' do expect_no_offenses('a, b = b, a') end context "when 'consistent' style is used" do it 'registers an offense and corrects incorrect indentation' do expect_offense(<<~RUBY) func([ 1 ^ Use 2 spaces for indentation in an array, relative to the position of the opening bracket. ]) ^ Indent the right bracket the same as the left bracket. RUBY expect_correction(<<~RUBY) func([ 1 ]) RUBY end it 'registers an offense and corrects incorrectly indented 1st element' do expect_offense(<<~RUBY) var = [ 1 ^ Use 2 spaces for indentation in an array, relative to the position of the opening bracket. ] ^ Indent the right bracket the same as the left bracket. RUBY expect_correction(<<~RUBY) var = [ 1 ] RUBY end end context "when 'special_inside_parentheses' style is used" do it 'registers an offense and corrects incorrect indentation' do expect_offense(<<~RUBY) var = [ 1 ^ Use 2 spaces for indentation in an array, relative to the position of the opening bracket. ] ^ Indent the right bracket the same as the left bracket. func([ 1 ]) RUBY expect_correction(<<~RUBY) var = [ 1 ] func([ 1 ]) RUBY end end it 'registers an offense and corrects incorrectly indented ]' do expect_offense(<<~RUBY) a << [ ] ^ Indent the right bracket the same as the left bracket. RUBY expect_correction(<<~RUBY) a << [ ] RUBY end context 'when indentation width is overridden for this cop' do let(:cop_indent) { 4 } it 'accepts correctly indented first element' do expect_no_offenses(<<~RUBY) a = [ 1 ] RUBY end it 'registers an offense and corrects indentation that does not match IndentationWidth' do expect_offense(<<~RUBY) a = [ 1 ^ Use 4 spaces for indentation in an array, relative to the position of the opening bracket. ] RUBY expect_correction(<<~RUBY) a = [ 1 ] RUBY end end end end
ruby
MIT
99fa0fdd0481910d7d052f4c2ed01ad36178f404
2026-01-04T15:37:41.211519Z
false
rubocop/rubocop
https://github.com/rubocop/rubocop/blob/99fa0fdd0481910d7d052f4c2ed01ad36178f404/spec/rubocop/cop/layout/empty_lines_around_method_body_spec.rb
spec/rubocop/cop/layout/empty_lines_around_method_body_spec.rb
# frozen_string_literal: true RSpec.describe RuboCop::Cop::Layout::EmptyLinesAroundMethodBody, :config do let(:beginning_offense_annotation) { '^{} Extra empty line detected at method body beginning.' } let(:end_offense_annotation) { '^{} Extra empty line detected at method body end.' } it 'registers an offense for method body starting with a blank' do expect_offense(<<~RUBY) def some_method #{beginning_offense_annotation} do_something end RUBY expect_correction(<<~RUBY) def some_method do_something end RUBY end # The cop only registers an offense if the extra line is completely empty. If # there is trailing whitespace, then that must be dealt with first. Having # two cops registering offense for the line with only spaces would cause # havoc in autocorrection. it 'accepts method body starting with a line with spaces' do expect_no_offenses(['def some_method', ' ', ' do_something', 'end'].join("\n")) end it 'registers an offense for class method body starting with a blank' do expect_offense(<<~RUBY) def Test.some_method #{beginning_offense_annotation} do_something end RUBY expect_correction(<<~RUBY) def Test.some_method do_something end RUBY end it 'registers an offense for method body ending with a blank' do expect_offense(<<~RUBY) def some_method do_something #{end_offense_annotation} end RUBY end it 'registers an offense for class method body ending with a blank' do expect_offense(<<~RUBY) def Test.some_method do_something #{end_offense_annotation} end RUBY end it 'is not fooled by single line methods' do expect_no_offenses(<<~RUBY) def some_method; do_something; end something_else RUBY end it 'registers an offense for methods with empty lines and arguments spanning multiple lines' do expect_offense(<<~RUBY) def some_method( arg ) #{beginning_offense_annotation} do_something end RUBY expect_correction(<<~RUBY) def some_method( arg ) do_something end RUBY end it 'registers an offense for methods with empty lines, empty arguments spanning multiple lines' do expect_offense(<<~RUBY) def some_method( ) #{beginning_offense_annotation} do_something end RUBY expect_correction(<<~RUBY) def some_method( ) do_something end RUBY end context 'endless methods', :ruby30 do it 'does not register an offense for a single-line endless method' do expect_no_offenses(<<~RUBY) def foo = quux RUBY end it 'does not register an offense for multiline endless method where the body is on the same line as the `=`' do expect_no_offenses(<<~RUBY) def foo(bar, baz) = quux RUBY end it 'does not register an offense for multiline endless method where the body is on the next line' do expect_no_offenses(<<~RUBY) def foo(bar, baz) = quux RUBY end it 'does not register an offense for multiline endless method when there is a comment before the body' do expect_no_offenses(<<~RUBY) def foo(bar, baz) = # comment quux RUBY end it 'registers an offense and corrects for multiline endless method with extra lines' do expect_offense(<<~RUBY) def foo(bar, baz) = #{beginning_offense_annotation} quux RUBY expect_correction(<<~RUBY) def foo(bar, baz) = quux RUBY end it 'registers an offense and corrects for multiline endless method with a comment and extra lines' do expect_offense(<<~RUBY) def foo(bar, baz) = #{beginning_offense_annotation} # comment quux RUBY expect_correction(<<~RUBY) def foo(bar, baz) = # comment quux RUBY end end end
ruby
MIT
99fa0fdd0481910d7d052f4c2ed01ad36178f404
2026-01-04T15:37:41.211519Z
false
rubocop/rubocop
https://github.com/rubocop/rubocop/blob/99fa0fdd0481910d7d052f4c2ed01ad36178f404/spec/rubocop/cop/layout/multiline_assignment_layout_spec.rb
spec/rubocop/cop/layout/multiline_assignment_layout_spec.rb
# frozen_string_literal: true RSpec.describe RuboCop::Cop::Layout::MultilineAssignmentLayout, :config do let(:supported_types) { %w[if] } let(:cop_config) { { 'EnforcedStyle' => enforced_style, 'SupportedTypes' => supported_types } } context 'new_line style' do let(:enforced_style) { 'new_line' } it 'registers an offense when the rhs is on the same line' do expect_offense(<<~RUBY) blarg = if true ^^^^^^^^^^^^^^^ Right hand side of multi-line assignment is on the same line as the assignment operator `=`. end RUBY expect_correction(<<~RUBY) blarg = if true end RUBY end it 'registers an offense when the rhs is on the same line in []=' do expect_offense(<<~RUBY) hash[:foo] = if true ^^^^^^^^^^^^^^^^^^^^ Right hand side of multi-line assignment is on the same line as the assignment operator `=`. end RUBY expect_correction(<<~RUBY) hash[:foo] = if true end RUBY end it 'registers an offense when the rhs is on the same line in setters' do expect_offense(<<~RUBY) foo.bar = if true ^^^^^^^^^^^^^^^^^ Right hand side of multi-line assignment is on the same line as the assignment operator `=`. end RUBY expect_correction(<<~RUBY) foo.bar = if true end RUBY end it 'ignores arrays' do expect_no_offenses(<<~RUBY) a, b = 4, 5 RUBY end context 'configured supported types' do let(:supported_types) { %w[array] } it 'registers an offense when supported types are configured' do expect_offense(<<~RUBY) a, b = 4, ^^^^^^^^^ Right hand side of multi-line assignment is on the same line as the assignment operator `=`. 5 RUBY end end it 'allows multi-line assignments on separate lines' do expect_no_offenses(<<~RUBY) blarg= if true end RUBY end it 'registers an offense for masgn with multi-line lhs' do expect_offense(<<~RUBY) a, ^^ Right hand side of multi-line assignment is on the same line as the assignment operator `=`. b = if foo end RUBY end context 'when supported types is block' do let(:supported_types) { %w[block] } it 'registers an offense when multi-line assignments ' \ 'using block definition is on the same line' do expect_offense(<<~RUBY) lambda = -> { ^^^^^^^^^^^^^ Right hand side of multi-line assignment is on the same line as the assignment operator `=`. puts 'hello' } RUBY end it 'allows multi-line assignments when using block definition on separate lines' do expect_no_offenses(<<~RUBY) lambda = -> { puts 'hello' } RUBY end it 'allows multi-line block defines on separate lines' do expect_no_offenses(<<~RUBY) default_scope -> { where(foo: "bar") } RUBY end it 'allows multi-line assignments when using shovel operator' do expect_no_offenses(<<~'RUBY') foo << items.map do |item| "#{item}!" end RUBY end end end context 'same_line style' do let(:enforced_style) { 'same_line' } it 'registers an offense when the rhs is a different line' do expect_offense(<<~RUBY) blarg = ^^^^^^^ Right hand side of multi-line assignment is not on the same line as the assignment operator `=`. if true end RUBY expect_correction(<<~RUBY) blarg = if true end RUBY end it 'registers an offense when the rhs is a different line in []=' do expect_offense(<<~RUBY) hash[:foo] = ^^^^^^^^^^^^ Right hand side of multi-line assignment is not on the same line as the assignment operator `=`. if true end RUBY expect_correction(<<~RUBY) hash[:foo] = if true end RUBY end it 'registers an offense when the rhs is a different line in setters' do expect_offense(<<~RUBY) foo.bar = ^^^^^^^^^ Right hand side of multi-line assignment is not on the same line as the assignment operator `=`. if true end RUBY expect_correction(<<~RUBY) foo.bar = if true end RUBY end it 'ignores arrays' do expect_no_offenses(<<~RUBY) a, b = 4, 5 RUBY end context 'configured supported types' do let(:supported_types) { %w[array] } it 'registers an offense when supported types are configured' do expect_offense(<<~RUBY) a, b = ^^^^^^ Right hand side of multi-line assignment is not on the same line as the assignment operator `=`. 4, 5 RUBY end end it 'allows multi-line assignments on the same line' do expect_no_offenses(<<~RUBY) blarg= if true end RUBY end it 'registers an offense for masgn with multi-line lhs' do expect_offense(<<~RUBY) a, ^^ Right hand side of multi-line assignment is not on the same line as the assignment operator `=`. b = if foo end RUBY end context 'when supported types is block' do let(:supported_types) { %w[block] } it 'allows when multi-line assignments using block definition is on the same line' do expect_no_offenses(<<~RUBY) lambda = -> { puts 'hello' } RUBY end it 'registers an offense when multi-line assignments ' \ 'using block definition on separate lines' do expect_offense(<<~RUBY) lambda = ^^^^^^^^ Right hand side of multi-line assignment is not on the same line as the assignment operator `=`. -> { puts 'hello' } RUBY end it 'allows multi-line block defines on separate lines' do expect_no_offenses(<<~RUBY) default_scope -> { where(foo: "bar") } RUBY end it 'allows multi-line assignments when using shovel operator' do expect_no_offenses(<<~'RUBY') foo << items.map do |item| "#{item}!" end RUBY end end end end
ruby
MIT
99fa0fdd0481910d7d052f4c2ed01ad36178f404
2026-01-04T15:37:41.211519Z
false
rubocop/rubocop
https://github.com/rubocop/rubocop/blob/99fa0fdd0481910d7d052f4c2ed01ad36178f404/spec/rubocop/cop/layout/end_alignment_spec.rb
spec/rubocop/cop/layout/end_alignment_spec.rb
# frozen_string_literal: true RSpec.describe RuboCop::Cop::Layout::EndAlignment, :config do let(:cop_config) { { 'EnforcedStyleAlignWith' => 'keyword', 'AutoCorrect' => true } } it_behaves_like 'aligned', "\xef\xbb\xbfclass", 'Test', 'end' it_behaves_like 'aligned', 'class', 'Test', 'end' it_behaves_like 'aligned', 'class << self;', 'Test', 'end' it_behaves_like 'aligned', 'module', 'Test', 'end' it_behaves_like 'aligned', 'if', 'test', 'end' it_behaves_like 'aligned', 'unless', 'test', 'end' it_behaves_like 'aligned', 'while', 'test', 'end' it_behaves_like 'aligned', 'until', 'test', 'end' it_behaves_like 'aligned', 'case', 'a when b', 'end' context 'Ruby >= 2.7', :ruby27 do # rubocop:disable RSpec/RepeatedExampleGroupDescription it_behaves_like 'aligned', 'case', 'a; in b', 'end' end it_behaves_like 'misaligned', <<~RUBY, false puts 1; class Test end ^^^ `end` at 2, 2 is not aligned with `class` at 1, 8. class Test end ^^^ `end` at 2, 2 is not aligned with `class` at 1, 0. puts 1; class << self end ^^^ `end` at 2, 2 is not aligned with `class` at 1, 8. class << self end ^^^ `end` at 2, 2 is not aligned with `class` at 1, 0. puts 1; module Test end ^^^ `end` at 2, 2 is not aligned with `module` at 1, 8. module Test end ^^^ `end` at 2, 2 is not aligned with `module` at 1, 0. puts 1; if test end ^^^ `end` at 2, 2 is not aligned with `if` at 1, 8. if test end ^^^ `end` at 2, 2 is not aligned with `if` at 1, 0. puts 1; unless test end ^^^ `end` at 2, 2 is not aligned with `unless` at 1, 8. unless test end ^^^ `end` at 2, 2 is not aligned with `unless` at 1, 0. puts 1; while test end ^^^ `end` at 2, 2 is not aligned with `while` at 1, 8. while test end ^^^ `end` at 2, 2 is not aligned with `while` at 1, 0. puts 1; until test end ^^^ `end` at 2, 2 is not aligned with `until` at 1, 8. until test end ^^^ `end` at 2, 2 is not aligned with `until` at 1, 0. puts 1; case a when b end ^^^ `end` at 2, 2 is not aligned with `case` at 1, 8. case a when b end ^^^ `end` at 2, 2 is not aligned with `case` at 1, 0. RUBY context 'Ruby >= 2.7', :ruby27 do # rubocop:disable RSpec/RepeatedExampleGroupDescription it_behaves_like 'misaligned', <<~RUBY, false puts 1; case a; in b end ^^^ `end` at 2, 2 is not aligned with `case` at 1, 8. case a; in b end ^^^ `end` at 2, 2 is not aligned with `case` at 1, 0. RUBY end it_behaves_like 'aligned', 'puts 1; class', 'Test', ' end' it_behaves_like 'aligned', 'puts 1; class << self;', 'Test', ' end' it_behaves_like 'aligned', 'puts 1; module', 'Test', ' end' it_behaves_like 'aligned', 'puts 1; if', 'Test', ' end' it_behaves_like 'aligned', 'puts 1; unless', 'Test', ' end' it_behaves_like 'aligned', 'puts 1; while', 'Test', ' end' it_behaves_like 'aligned', 'puts 1; until', 'Test', ' end' it_behaves_like 'aligned', 'puts 1; case', 'a when b', ' end' context 'Ruby >= 2.7', :ruby27 do # rubocop:disable RSpec/RepeatedExampleGroupDescription it_behaves_like 'aligned', 'puts 1; case', 'a; in b', ' end' end it 'can handle ternary if' do expect_no_offenses('a = cond ? x : y') end it 'can handle modifier if' do expect_no_offenses('a = x if cond') end context 'when EnforcedStyleAlignWith is start_of_line' do let(:cop_config) { { 'EnforcedStyleAlignWith' => 'start_of_line', 'AutoCorrect' => true } } it_behaves_like 'aligned', 'puts 1; class', 'Test', 'end' it_behaves_like 'aligned', 'puts 1; class << self;', 'Test', 'end' it_behaves_like 'aligned', 'puts 1; module', 'Test', 'end' it_behaves_like 'aligned', 'puts 1; if', 'test', 'end' it_behaves_like 'aligned', 'puts 1; unless', 'test', 'end' it_behaves_like 'aligned', 'puts 1; while', 'test', 'end' it_behaves_like 'aligned', 'puts 1; until', 'test', 'end' it_behaves_like 'aligned', 'puts 1; case', 'a when b', 'end' context 'Ruby >= 2.7', :ruby27 do # rubocop:disable RSpec/RepeatedExampleGroupDescription it_behaves_like 'aligned', 'puts 1; case', 'a; in b', 'end' end it_behaves_like 'misaligned', <<~RUBY, false puts 1; class Test end ^^^ `end` at 2, 2 is not aligned with `puts 1; class Test` at 1, 0. class Test end ^^^ `end` at 2, 2 is not aligned with `class Test` at 1, 0. puts 1; module Test end ^^^ `end` at 2, 2 is not aligned with `puts 1; module Test` at 1, 0. module Test end ^^^ `end` at 2, 2 is not aligned with `module Test` at 1, 0. puts 1; if test end ^^^ `end` at 2, 2 is not aligned with `puts 1; if test` at 1, 0. if test end ^^^ `end` at 2, 2 is not aligned with `if test` at 1, 0. puts 1; unless test end ^^^ `end` at 2, 2 is not aligned with `puts 1; unless test` at 1, 0. unless test end ^^^ `end` at 2, 2 is not aligned with `unless test` at 1, 0. puts 1; while test end ^^^ `end` at 2, 2 is not aligned with `puts 1; while test` at 1, 0. while test end ^^^ `end` at 2, 2 is not aligned with `while test` at 1, 0. puts 1; until test end ^^^ `end` at 2, 2 is not aligned with `puts 1; until test` at 1, 0. until test end ^^^ `end` at 2, 2 is not aligned with `until test` at 1, 0. puts 1; case a when b end ^^^ `end` at 2, 2 is not aligned with `puts 1; case a when b` at 1, 0. case a when b end ^^^ `end` at 2, 2 is not aligned with `case a when b` at 1, 0. RUBY context 'Ruby >= 2.7', :ruby27 do # rubocop:disable RSpec/RepeatedExampleGroupDescription it_behaves_like 'misaligned', <<~RUBY, false case a in b end ^^^ `end` at 3, 2 is not aligned with `case a` at 1, 0. RUBY end it_behaves_like 'misaligned', <<~RUBY, :keyword puts(if test end) ^^^ `end` at 2, 5 is not aligned with `puts(if test` at 1, 0. RUBY it_behaves_like 'misaligned', <<~RUBY, :keyword var = if test end ^^^ `end` at 2, 6 is not aligned with `var = if test` at 1, 0. var = unless test end ^^^ `end` at 2, 6 is not aligned with `var = unless test` at 1, 0. var = while test end ^^^ `end` at 2, 6 is not aligned with `var = while test` at 1, 0. var = until test end ^^^ `end` at 2, 6 is not aligned with `var = until test` at 1, 0. var = case a when b end ^^^ `end` at 2, 6 is not aligned with `var = case a when b` at 1, 0. var << if test end ^^^ `end` at 2, 7 is not aligned with `var << if test` at 1, 0. var << unless test end ^^^ `end` at 2, 7 is not aligned with `var << unless test` at 1, 0. var << while test end ^^^ `end` at 2, 7 is not aligned with `var << while test` at 1, 0. var << until test end ^^^ `end` at 2, 7 is not aligned with `var << until test` at 1, 0. var << case a when b end ^^^ `end` at 2, 7 is not aligned with `var << case a when b` at 1, 0. RUBY context 'Ruby >= 2.7', :ruby27 do # rubocop:disable RSpec/RepeatedExampleGroupDescription it_behaves_like 'misaligned', <<~RUBY, :keyword var << case a in b end ^^^ `end` at 3, 7 is not aligned with `var << case a` at 1, 0. RUBY end it_behaves_like 'misaligned', <<~RUBY, false var = if test end ^^^ `end` at 3, 0 is not aligned with `if test` at 2, 2. RUBY it_behaves_like 'aligned', 'var = if', 'test', 'end' it_behaves_like 'aligned', 'var = unless', 'test', 'end' it_behaves_like 'aligned', 'var = while', 'test', 'end' it_behaves_like 'aligned', 'var << while', 'test', 'end' it_behaves_like 'aligned', 'var = until', 'test', 'end' it_behaves_like 'aligned', 'var = case', 'a when b', 'end' it_behaves_like 'aligned', "var =\n if", 'test', ' end' end context 'when EnforcedStyleAlignWith is variable' do # same as 'EnforcedStyleAlignWith' => 'keyword', # as long as assignments or `case` are not involved let(:cop_config) { { 'EnforcedStyleAlignWith' => 'variable', 'AutoCorrect' => true } } it_behaves_like 'misaligned', <<~RUBY, false class Test end ^^^ `end` at 2, 2 is not aligned with `class` at 1, 0. module Test end ^^^ `end` at 2, 7 is not aligned with `module` at 1, 0. class << self end ^^^ `end` at 2, 2 is not aligned with `class` at 1, 0. if test end ^^^ `end` at 2, 2 is not aligned with `if` at 1, 0. unless test end ^^^ `end` at 2, 2 is not aligned with `unless` at 1, 0. while test end ^^^ `end` at 2, 2 is not aligned with `while` at 1, 0. until test end ^^^ `end` at 2, 2 is not aligned with `until` at 1, 0. case a when b end ^^^ `end` at 2, 2 is not aligned with `case` at 1, 0. RUBY it_behaves_like 'aligned', 'class', 'Test', 'end' it_behaves_like 'aligned', 'class << self;', 'Test', 'end' it_behaves_like 'aligned', 'module', 'Test', 'end' it_behaves_like 'aligned', 'if', 'test', 'end' it_behaves_like 'aligned', 'unless', 'test', 'end' it_behaves_like 'aligned', 'while', 'test', 'end' it_behaves_like 'aligned', 'until', 'test', 'end' it_behaves_like 'aligned', 'case', 'a when b', 'end' context 'Ruby >= 2.7', :ruby27 do # rubocop:disable RSpec/RepeatedExampleGroupDescription it_behaves_like 'aligned', 'case', 'a; in b', 'end' end it_behaves_like 'misaligned', <<~RUBY, :start_of_line puts 1; class Test end ^^^ `end` at 2, 0 is not aligned with `class` at 1, 8. puts 1; module Test end ^^^ `end` at 2, 0 is not aligned with `module` at 1, 8. puts 1; class << self end ^^^ `end` at 2, 0 is not aligned with `class` at 1, 8. puts 1; if test end ^^^ `end` at 2, 0 is not aligned with `if` at 1, 8. puts 1; unless test end ^^^ `end` at 2, 0 is not aligned with `unless` at 1, 8. puts 1; while test end ^^^ `end` at 2, 0 is not aligned with `while` at 1, 8. puts 1; until test end ^^^ `end` at 2, 0 is not aligned with `until` at 1, 8. puts 1; case a when b end ^^^ `end` at 2, 0 is not aligned with `case` at 1, 8. RUBY context 'Ruby >= 2.7', :ruby27 do # rubocop:disable RSpec/RepeatedExampleGroupDescription it_behaves_like 'misaligned', <<~RUBY, :start_of_line puts 1; case a in b end ^^^ `end` at 3, 0 is not aligned with `case` at 1, 8. RUBY end it_behaves_like 'aligned', 'puts 1; class', 'Test', ' end' it_behaves_like 'aligned', 'puts 1; class << self;', 'Test', ' end' it_behaves_like 'aligned', 'puts 1; module', 'Test', ' end' it_behaves_like 'aligned', 'puts 1; if', 'Test', ' end' it_behaves_like 'aligned', 'puts 1; unless', 'Test', ' end' it_behaves_like 'aligned', 'puts 1; while', 'Test', ' end' it_behaves_like 'aligned', 'puts 1; until', 'Test', ' end' it_behaves_like 'aligned', 'puts 1; case', 'a when b', ' end' it 'registers an offense when using `+` operator method and `end` is not aligned' do expect_offense(<<~RUBY) variable + if condition foo else bar end ^^^ `end` at 5, 11 is not aligned with `variable + if` at 1, 0. RUBY expect_correction(<<~RUBY) variable + if condition foo else bar end RUBY end it 'registers an offense when using `-` operator method and `end` is not aligned' do expect_offense(<<~RUBY) variable - if condition foo else bar end ^^^ `end` at 5, 11 is not aligned with `variable - if` at 1, 0. RUBY expect_correction(<<~RUBY) variable - if condition foo else bar end RUBY end it 'registers an offense when using `+` operator method with `if` inside `begin` and `end` is not aligned' do expect_offense(<<~RUBY) variable + (if bar baz end) ^^^ `end` at 3, 12 is not aligned with `variable + (if` at 1, 0. RUBY expect_correction(<<~RUBY) variable + (if bar baz end) RUBY end it 'registers an offense when using `+` operator method with `if` inside multiple `begin`s and `end` is not aligned' do expect_offense(<<~RUBY) variable + ((if bar baz end)) ^^^ `end` at 3, 12 is not aligned with `variable + ((if` at 1, 0. RUBY expect_correction(<<~RUBY) variable + ((if bar baz end)) RUBY end it 'registers an offense when using a conditional statement in a method argument and `end` is not aligned' do expect_offense(<<~RUBY) format( case condition when foo bar else baz end, qux ^^^ `end` at 7, 0 is not aligned with `case` at 2, 2. ) RUBY expect_correction(<<~RUBY) format( case condition when foo bar else baz end, qux ) RUBY end it 'does not register an offense when using a conditional assignment on the same line and `end` with method call is aligned' do expect_no_offenses(<<~RUBY) value = if condition do_something end.method_call RUBY end it 'does not register an offense when using a conditional assignment in a method argument on the same line and `end` with safe navigation method call is aligned' do expect_no_offenses(<<~RUBY) value = if condition do_something end&.method_call RUBY end it 'registers an offense when using a conditional statement in a method argument on the same line and `end` with method call is not aligned' do expect_offense(<<~RUBY) do_something case condition when expr end.method_call ^^^ `end` at 3, 13 is not aligned with `do_something case` at 1, 0. RUBY expect_correction(<<~RUBY) do_something case condition when expr end.method_call RUBY end it 'registers an offense when using a pattern matching in a method argument and `end` is not aligned', :ruby27 do expect_offense(<<~RUBY) format( case pattern in foo bar else baz end, qux ^^^ `end` at 7, 0 is not aligned with `case` at 2, 2. ) RUBY expect_correction(<<~RUBY) format( case pattern in foo bar else baz end, qux ) RUBY end end context 'correct + opposite' do it 'registers an offense and corrects' do expect_offense(<<~RUBY) x = if a a1 end y = if b b1 end ^^^ `end` at 6, 0 is not aligned with `if` at 4, 4. RUBY expect_correction(<<~RUBY) x = if a a1 end y = if b b1 end RUBY end end context 'when end is preceded by something else than whitespace' do it 'registers an offense and corrects' do expect_offense(<<~RUBY) module A puts a end ^^^ `end` at 2, 7 is not aligned with `module` at 1, 0. RUBY expect_correction(<<~RUBY) module A puts a#{trailing_whitespace} end RUBY end end context 'when end is on the same line as `else` body' do it 'registers an offense and corrects' do expect_offense(<<~RUBY) if test foo else bar end ^^^ `end` at 4, 6 is not aligned with `if` at 1, 0. RUBY expect_correction(<<~RUBY) if test foo else bar#{trailing_whitespace} end RUBY end end context 'case as argument' do context 'when EnforcedStyleAlignWith is keyword' do let(:cop_config) { { 'EnforcedStyleAlignWith' => 'keyword', 'AutoCorrect' => true } } it_behaves_like 'aligned', 'test case', 'a when b', ' end' it_behaves_like 'misaligned', <<~RUBY, :start_of_line test case a when b end ^^^ `end` at 2, 0 is not aligned with `case` at 1, 5. RUBY end context 'when EnforcedStyleAlignWith is variable' do let(:cop_config) { { 'EnforcedStyleAlignWith' => 'variable', 'AutoCorrect' => true } } it_behaves_like 'aligned', 'test case', 'a when b', 'end' it_behaves_like 'misaligned', <<~RUBY, :keyword test case a when b end ^^^ `end` at 2, 5 is not aligned with `test case` at 1, 0. RUBY end context 'when EnforcedStyleAlignWith is start_of_line' do let(:cop_config) { { 'EnforcedStyleAlignWith' => 'start_of_line', 'AutoCorrect' => true } } it_behaves_like 'aligned', 'test case a when b', '', 'end' it_behaves_like 'misaligned', <<~RUBY, :keyword test case a when b end ^^^ `end` at 2, 5 is not aligned with `test case a when b` at 1, 0. RUBY end end context 'case-match as argument', :ruby27 do context 'when EnforcedStyleAlignWith is keyword' do let(:cop_config) { { 'EnforcedStyleAlignWith' => 'keyword', 'AutoCorrect' => true } } it_behaves_like 'aligned', 'test case', 'a; in b', ' end' it_behaves_like 'misaligned', <<~RUBY, :start_of_line test case a; in b end ^^^ `end` at 2, 0 is not aligned with `case` at 1, 5. RUBY end context 'when EnforcedStyleAlignWith is variable' do let(:cop_config) { { 'EnforcedStyleAlignWith' => 'variable', 'AutoCorrect' => true } } it_behaves_like 'aligned', 'test case', 'a; in b', 'end' it_behaves_like 'misaligned', <<~RUBY, :keyword test case a; in b end ^^^ `end` at 2, 5 is not aligned with `test case` at 1, 0. RUBY end context 'when EnforcedStyleAlignWith is start_of_line' do let(:cop_config) { { 'EnforcedStyleAlignWith' => 'start_of_line', 'AutoCorrect' => true } } it_behaves_like 'aligned', 'test case a; in b', '', 'end' it_behaves_like 'misaligned', <<~RUBY, :keyword test case a; in b end ^^^ `end` at 2, 5 is not aligned with `test case a; in b` at 1, 0. RUBY end end context 'regarding assignment' do context 'when EnforcedStyleAlignWith is keyword' do it_behaves_like 'misaligned', <<~RUBY, :start_of_line var = if test end ^^^ `end` at 2, 0 is not aligned with `if` at 1, 6. var = unless test end ^^^ `end` at 2, 0 is not aligned with `unless` at 1, 6. var = while test end ^^^ `end` at 2, 0 is not aligned with `while` at 1, 6. var = until test end ^^^ `end` at 2, 0 is not aligned with `until` at 1, 6. var << until test end ^^^ `end` at 2, 0 is not aligned with `until` at 1, 7. RUBY it_behaves_like 'aligned', 'var = if', 'test', ' end' it_behaves_like 'aligned', 'var = unless', 'test', ' end' it_behaves_like 'aligned', 'var = while', 'test', ' end' it_behaves_like 'aligned', 'var = until', 'test', ' end' it_behaves_like 'aligned', 'var = case', 'a when b', ' end' it_behaves_like 'aligned', 'var[0] = case', 'a when b', ' end' context 'Ruby >= 2.7', :ruby27 do it_behaves_like 'aligned', 'var = case', 'a; in b', ' end' it_behaves_like 'aligned', 'var[0] = case', 'a; in b', ' end' end end context 'when EnforcedStyleAlignWith is variable' do let(:cop_config) { { 'EnforcedStyleAlignWith' => 'variable', 'AutoCorrect' => true } } it_behaves_like 'aligned', 'var << if', 'test', 'end' it_behaves_like 'aligned', 'var = if', 'test', 'end' it_behaves_like 'aligned', 'var = unless', 'test', 'end' it_behaves_like 'aligned', 'var = while', 'test', 'end' it_behaves_like 'aligned', 'var = until', 'test', 'end' it_behaves_like 'aligned', 'var = until', 'test', 'end.ab.join("")' it_behaves_like 'aligned', 'var = until', 'test', 'end.ab.tap {}' it_behaves_like 'aligned', 'var = until', 'test', 'end.ab.tap { _1 }' context 'Ruby >= 3.4', :ruby34 do it_behaves_like 'aligned', 'var = until', 'test', 'end.ab.tap { it }' end it_behaves_like 'aligned', 'var = case', 'a when b', 'end' it_behaves_like 'aligned', "var =\n if", 'test', ' end' context 'Ruby >= 2.7', :ruby27 do it_behaves_like 'aligned', 'var = case', 'a; in b', 'end' end it_behaves_like 'misaligned', <<~RUBY, :keyword var = if test end ^^^ `end` at 2, 6 is not aligned with `var = if` at 1, 0. var = unless test end ^^^ `end` at 2, 6 is not aligned with `var = unless` at 1, 0. var = while test end ^^^ `end` at 2, 6 is not aligned with `var = while` at 1, 0. var = until test end ^^^ `end` at 2, 6 is not aligned with `var = until` at 1, 0. var << if test end ^^^ `end` at 2, 7 is not aligned with `var << if` at 1, 0. var << unless test end ^^^ `end` at 2, 7 is not aligned with `var << unless` at 1, 0. var[x] = while test end ^^^ `end` at 2, 9 is not aligned with `var[x] = while` at 1, 0. var << until test end ^^^ `end` at 2, 7 is not aligned with `var << until` at 1, 0. RUBY # If there's a line break after = we align with the keyword even if the # style is `variable`. it_behaves_like 'misaligned', <<~RUBY, false var = if test end ^^^ `end` at 3, 0 is not aligned with `if` at 2, 2. var = unless test end ^^^ `end` at 3, 1 is not aligned with `unless` at 2, 2. var = # comment while test end ^^^ `end` at 4, 3 is not aligned with `while` at 3, 2. var = until test do_something end ^^^ `end` at 4, 4 is not aligned with `until` at 2, 2. RUBY it_behaves_like 'misaligned', <<~RUBY, :keyword var = until test end.j ^^^ `end` at 2, 6 is not aligned with `var = until` at 1, 0. RUBY it_behaves_like 'aligned', '@var = if', 'test', 'end' it_behaves_like 'aligned', '@@var = if', 'test', 'end' it_behaves_like 'aligned', '$var = if', 'test', 'end' it_behaves_like 'aligned', 'CNST = if', 'test', 'end' it_behaves_like 'aligned', 'a, b = if', 'test', 'end' it_behaves_like 'aligned', 'var ||= if', 'test', 'end' it_behaves_like 'aligned', 'var &&= if', 'test', 'end' it_behaves_like 'aligned', 'var += if', 'test', 'end' it_behaves_like 'aligned', 'h[k] = if', 'test', 'end' it_behaves_like 'aligned', 'h.k = if', 'test', 'end' it_behaves_like 'misaligned', <<~RUBY, :keyword var = case a when b end ^^^ `end` at 2, 6 is not aligned with `var = case` at 1, 0. @var = if test end ^^^ `end` at 2, 7 is not aligned with `@var = if` at 1, 0. @@var = if test end ^^^ `end` at 2, 8 is not aligned with `@@var = if` at 1, 0. $var = if test end ^^^ `end` at 2, 7 is not aligned with `$var = if` at 1, 0. CNST = if test end ^^^ `end` at 2, 7 is not aligned with `CNST = if` at 1, 0. a, b = if test end ^^^ `end` at 2, 7 is not aligned with `a, b = if` at 1, 0. var ||= if test end ^^^ `end` at 2, 8 is not aligned with `var ||= if` at 1, 0. var &&= if test end ^^^ `end` at 2, 8 is not aligned with `var &&= if` at 1, 0. var += if test end ^^^ `end` at 2, 7 is not aligned with `var += if` at 1, 0. h[k] = if test end ^^^ `end` at 2, 7 is not aligned with `h[k] = if` at 1, 0. var << case a when b end ^^^ `end` at 2, 7 is not aligned with `var << case` at 1, 0. @var << if test end ^^^ `end` at 2, 8 is not aligned with `@var << if` at 1, 0. @@var << if test end ^^^ `end` at 2, 9 is not aligned with `@@var << if` at 1, 0. $var << if test end ^^^ `end` at 2, 8 is not aligned with `$var << if` at 1, 0. CNST << if test end ^^^ `end` at 2, 8 is not aligned with `CNST << if` at 1, 0. h[k] << if test end ^^^ `end` at 2, 8 is not aligned with `h[k] << if` at 1, 0. RUBY it_behaves_like 'misaligned', <<~RUBY, false h.k = if test end ^^^ `end` at 2, 9 is not aligned with `h.k = if` at 1, 0. RUBY context 'Ruby 2.7', :ruby27 do it_behaves_like 'misaligned', <<~RUBY, :keyword var = case a; in b end ^^^ `end` at 2, 6 is not aligned with `var = case` at 1, 0. RUBY end end end end
ruby
MIT
99fa0fdd0481910d7d052f4c2ed01ad36178f404
2026-01-04T15:37:41.211519Z
false
rubocop/rubocop
https://github.com/rubocop/rubocop/blob/99fa0fdd0481910d7d052f4c2ed01ad36178f404/spec/rubocop/cop/layout/space_after_semicolon_spec.rb
spec/rubocop/cop/layout/space_after_semicolon_spec.rb
# frozen_string_literal: true RSpec.describe RuboCop::Cop::Layout::SpaceAfterSemicolon, :config do let(:config) { RuboCop::Config.new('Layout/SpaceInsideBlockBraces' => brace_config) } let(:brace_config) { {} } it 'registers an offense and corrects semicolon without space after it' do expect_offense(<<~RUBY) x = 1;y = 2 ^ Space missing after semicolon. RUBY expect_correction(<<~RUBY) x = 1; y = 2 RUBY end it 'does not crash if semicolon is the last character of the file' do expect_no_offenses('x = 1;') end it 'does not register an offense when semicolons appear consecutively' do expect_no_offenses(<<~RUBY) foo;; bar RUBY end context 'inside block braces' do shared_examples 'common behavior' do it 'accepts a space between a semicolon and a closing brace' do expect_no_offenses('test { ; }') end it 'accepts no space between a semicolon and a closing brace of string interpolation' do expect_no_offenses(<<~'RUBY') "#{ ;}" RUBY end end context 'when EnforcedStyle for SpaceInsideBlockBraces is space' do let(:brace_config) { { 'Enabled' => true, 'EnforcedStyle' => 'space' } } it_behaves_like 'common behavior' it 'registers an offense and corrects no space between a semicolon and a closing brace' do expect_offense(<<~RUBY) test { ;} ^ Space missing after semicolon. RUBY expect_correction(<<~RUBY) test { ; } RUBY end end context 'when EnforcedStyle for SpaceInsideBlockBraces is no_space' do let(:brace_config) { { 'Enabled' => true, 'EnforcedStyle' => 'no_space' } } it_behaves_like 'common behavior' it 'accepts no space between a semicolon and a closing brace' do expect_no_offenses('test { ;}') end end end end
ruby
MIT
99fa0fdd0481910d7d052f4c2ed01ad36178f404
2026-01-04T15:37:41.211519Z
false
rubocop/rubocop
https://github.com/rubocop/rubocop/blob/99fa0fdd0481910d7d052f4c2ed01ad36178f404/spec/rubocop/cop/layout/space_inside_hash_literal_braces_spec.rb
spec/rubocop/cop/layout/space_inside_hash_literal_braces_spec.rb
# frozen_string_literal: true RSpec.describe RuboCop::Cop::Layout::SpaceInsideHashLiteralBraces, :config do let(:cop_config) { { 'EnforcedStyle' => 'space' } } context 'with space inside empty braces not allowed' do let(:cop_config) { { 'EnforcedStyleForEmptyBraces' => 'no_space' } } it 'accepts empty braces with no space inside' do expect_no_offenses('h = {}') end it 'registers an offense for empty braces with space inside' do expect_offense(<<~RUBY) h = { } ^ Space inside empty hash literal braces detected. RUBY expect_correction(<<~RUBY) h = {} RUBY end end context 'with newline inside empty braces not allowed' do let(:cop_config) { { 'EnforcedStyleForEmptyBraces' => 'no_space' } } it 'registers an offense' do expect_offense(<<~RUBY) h = { ^{} Space inside empty hash literal braces detected. } RUBY expect_correction(<<~RUBY) h = {} RUBY end end context 'with space inside empty braces allowed' do let(:cop_config) { { 'EnforcedStyleForEmptyBraces' => 'space' } } it 'accepts empty braces with space inside' do expect_no_offenses('h = { }') end it 'registers an offense for empty braces with no space inside' do expect_offense(<<~RUBY) h = {} ^ Space inside empty hash literal braces missing. RUBY expect_correction(<<~RUBY) h = { } RUBY end context 'when using method argument that both key and value are hash literals' do it 'registers hashes with no spaces' do expect_offense(<<~RUBY) foo({key: value} => {key: value}) ^ Space inside } missing. ^ Space inside { missing. ^ Space inside } missing. ^ Space inside { missing. RUBY expect_correction(<<~RUBY) foo({ key: value } => { key: value }) RUBY end end end it 'registers an offense for hashes with no spaces if so configured' do expect_offense(<<~RUBY) h = {a: 1, b: 2} ^ Space inside { missing. ^ Space inside } missing. h = {a => 1} ^ Space inside { missing. ^ Space inside } missing. RUBY expect_correction(<<~RUBY) h = { a: 1, b: 2 } h = { a => 1 } RUBY end it 'registers an offense for correct + opposite' do expect_offense(<<~RUBY) h = { a: 1} ^ Space inside } missing. RUBY expect_correction(<<~RUBY) h = { a: 1 } RUBY end it 'handles "{" as final hash value' do expect_offense(<<~RUBY) h = {a: '{'} ^ Space inside } missing. ^ Space inside { missing. RUBY expect_correction(<<~RUBY) h = { a: '{' } RUBY end context 'when using hash pattern matching', :ruby27 do it 'registers an offense when hash pattern with no spaces' do expect_offense(<<~RUBY) case foo in {k1: 0, k2: 1} ^ Space inside } missing. ^ Space inside { missing. end RUBY expect_correction(<<~RUBY) case foo in { k1: 0, k2: 1 } end RUBY end it 'does not register an offense when hash pattern with spaces' do expect_no_offenses(<<~RUBY) case foo in { k1: 0, k2: 1 } end RUBY end end context 'when using one-line hash `in` pattern matching', :ruby27 do it 'registers an offense when hash pattern with no spaces' do expect_offense(<<~RUBY) foo in {k1: 0, k2: 1} ^ Space inside } missing. ^ Space inside { missing. RUBY expect_correction(<<~RUBY) foo in { k1: 0, k2: 1 } RUBY end it 'does not register an offense when hash pattern with spaces' do expect_no_offenses(<<~RUBY) foo in { k1: 0, k2: 1 } RUBY end end context 'when using one-line hash `=>` pattern matching', :ruby30 do it 'registers an offense when hash pattern with no spaces' do expect_offense(<<~RUBY) foo => {k1: 0, k2: 1} ^ Space inside } missing. ^ Space inside { missing. RUBY expect_correction(<<~RUBY) foo => { k1: 0, k2: 1 } RUBY end it 'does not register an offense when hash pattern with spaces' do expect_no_offenses(<<~RUBY) foo => { k1: 0, k2: 1 } RUBY end end context 'when EnforcedStyle is no_space' do let(:cop_config) { { 'EnforcedStyle' => 'no_space' } } it 'registers an offense for hashes with spaces' do expect_offense(<<~RUBY) h = { a: 1, b: 2 } ^ Space inside { detected. ^ Space inside } detected. RUBY expect_correction(<<~RUBY) h = {a: 1, b: 2} RUBY end it 'registers an offense for opposite + correct' do expect_offense(<<~RUBY) h = {a: 1 } ^ Space inside } detected. RUBY expect_correction(<<~RUBY) h = {a: 1} RUBY end it 'handles "{" as final hash value' do expect_offense(<<~RUBY) h = { a: '{' } ^ Space inside } detected. ^ Space inside { detected. RUBY expect_correction(<<~RUBY) h = {a: '{'} RUBY end it 'accepts hashes with no spaces' do expect_no_offenses(<<~RUBY) h = {a: 1, b: 2} h = {a => 1} RUBY end it 'accepts multiline hash' do expect_no_offenses(<<~RUBY) h = { a: 1, b: 2, } RUBY end it 'accepts multiline hash with comment' do expect_no_offenses(<<~RUBY) h = { # Comment a: 1, b: 2, } RUBY end context 'when using method argument that both key and value are hash literals' do it 'accepts hashes with no spaces' do expect_no_offenses(<<~RUBY) foo({key: value} => {key: value}) RUBY end end context 'when using hash pattern matching', :ruby27 do it 'registers an offense when hash with spaces' do expect_offense(<<~RUBY) case foo in { k1: 0, k2: 1 } ^ Space inside } detected. ^ Space inside { detected. end RUBY expect_correction(<<~RUBY) case foo in {k1: 0, k2: 1} end RUBY end it 'does not register an offense when hash pattern with no spaces' do expect_no_offenses(<<~RUBY) case foo in {k1: 0, k2: 1} end RUBY end end context 'when using one-line hash `in` pattern matching', :ruby27 do it 'registers an offense when hash with spaces' do expect_offense(<<~RUBY) foo in { k1: 0, k2: 1 } ^ Space inside } detected. ^ Space inside { detected. RUBY expect_correction(<<~RUBY) foo in {k1: 0, k2: 1} RUBY end it 'does not register an offense when hash with no spaces' do expect_no_offenses(<<~RUBY) foo in {k1: 0, k2: 1} RUBY end end context 'when using one-line hash `=>` pattern matching', :ruby30 do it 'registers an offense when hash with spaces' do expect_offense(<<~RUBY) foo => { k1: 0, k2: 1 } ^ Space inside } detected. ^ Space inside { detected. RUBY expect_correction(<<~RUBY) foo => {k1: 0, k2: 1} RUBY end it 'does not register an offense when hash with no spaces' do expect_no_offenses(<<~RUBY) foo => {k1: 0, k2: 1} RUBY end end end context 'when EnforcedStyle is compact' do let(:cop_config) { { 'EnforcedStyle' => 'compact' } } it "doesn't register an offense for non-nested hashes with spaces" do expect_no_offenses(<<~RUBY) h = { a: 1, b: 2 } RUBY end it 'registers an offense for nested hashes with spaces' do expect_offense(<<~RUBY) h = { a: { a: 1, b: 2 } } ^ Space inside } detected. RUBY expect_correction(<<~RUBY) h = { a: { a: 1, b: 2 }} RUBY end it 'registers an offense for opposite + correct' do expect_offense(<<~RUBY) h = {a: 1 } ^ Space inside { missing. RUBY expect_correction(<<~RUBY) h = { a: 1 } RUBY end it 'registers offenses for hashes with no spaces' do expect_offense(<<~RUBY) h = {a: 1, b: 2} ^ Space inside } missing. ^ Space inside { missing. h = {a => 1} ^ Space inside } missing. ^ Space inside { missing. RUBY expect_correction(<<~RUBY) h = { a: 1, b: 2 } h = { a => 1 } RUBY end it 'accepts multiline hash' do expect_no_offenses(<<~RUBY) h = { a: 1, b: 2, } RUBY end it 'accepts multiline hash with comment' do expect_no_offenses(<<~RUBY) h = { # Comment a: 1, b: 2, } RUBY end end it 'accepts hashes with spaces by default' do expect_no_offenses(<<~RUBY) h = { a: 1, b: 2 } h = { a => 1 } RUBY end it 'accepts hash literals with no braces' do expect_no_offenses('x(a: b.c)') end it 'can handle interpolation in a braceless hash literal' do # A tricky special case where the closing brace of the # interpolation risks getting confused for a hash literal brace. expect_no_offenses('f(get: "#{x}")') end context 'on Hash[{ x: 1 } => [1]]' do # regression test; see GH issue 2436 it 'does not register an offense' do expect_no_offenses('Hash[{ x: 1 } => [1]]') end end context 'on { key: "{" }' do # regression test; see GH issue 3958 it 'does not register an offense' do expect_no_offenses('{ key: "{" }') end end context 'offending hash following empty hash' do # regression test; see GH issue 8642 it 'registers an offense on both sides' do expect_offense(<<~RUBY) {} {key: 1} ^ Space inside { missing. ^ Space inside } missing. RUBY expect_correction(<<~RUBY) {} { key: 1 } RUBY end end end
ruby
MIT
99fa0fdd0481910d7d052f4c2ed01ad36178f404
2026-01-04T15:37:41.211519Z
false
rubocop/rubocop
https://github.com/rubocop/rubocop/blob/99fa0fdd0481910d7d052f4c2ed01ad36178f404/spec/rubocop/cop/layout/block_alignment_spec.rb
spec/rubocop/cop/layout/block_alignment_spec.rb
# frozen_string_literal: true RSpec.describe RuboCop::Cop::Layout::BlockAlignment, :config do let(:cop_config) { { 'EnforcedStyleAlignWith' => 'either' } } context 'when the block has no arguments' do it 'registers an offense for mismatched block end' do expect_offense(<<~RUBY) test do end ^^^ `end` at 2, 2 is not aligned with `test do` at 1, 0. RUBY expect_correction(<<~RUBY) test do end RUBY end end context 'when the block has arguments' do it 'registers an offense for mismatched block end' do expect_offense(<<~RUBY) test do |ala| end ^^^ `end` at 2, 2 is not aligned with `test do |ala|` at 1, 0. RUBY expect_correction(<<~RUBY) test do |ala| end RUBY end end it 'accepts a block end that does not begin its line' do expect_no_offenses(<<~RUBY) scope :bar, lambda { joins(:baz) .distinct } RUBY end context 'when the block is a logical operand' do it 'accepts a correctly aligned block end' do expect_no_offenses(<<~RUBY) (value.is_a? Array) && value.all? do |subvalue| type_check_value(subvalue, array_type) end a || b do end RUBY end end it 'accepts end aligned with a variable' do expect_no_offenses(<<~RUBY) variable = test do |ala| end RUBY end context 'when there is an assignment chain' do it 'registers an offense for an end aligned with the 2nd variable' do expect_offense(<<~RUBY) a = b = c = test do |ala| end ^^^ `end` at 2, 4 is not aligned with `a = b = c = test do |ala|` at 1, 0. RUBY expect_correction(<<~RUBY) a = b = c = test do |ala| end RUBY end it 'accepts end aligned with the first variable' do expect_no_offenses(<<~RUBY) a = b = c = test do |ala| end RUBY end end context 'and the block is an operand' do it 'accepts end aligned with a variable' do expect_no_offenses(<<~RUBY) b = 1 + preceding_line.reduce(0) do |a, e| a + e.length + newline_length end + 1 RUBY end end it 'registers an offense for mismatched block end with a variable' do expect_offense(<<~RUBY) variable = test do |ala| end ^^^ `end` at 2, 2 is not aligned with `variable = test do |ala|` at 1, 0. RUBY expect_correction(<<~RUBY) variable = test do |ala| end RUBY end context 'when the block is defined on the next line' do it 'accepts end aligned with the block expression' do expect_no_offenses(<<~RUBY) variable = a_long_method_that_dont_fit_on_the_line do |v| v.foo end RUBY end it 'registers an offense for mismatched end alignment' do expect_offense(<<~RUBY) variable = a_long_method_that_dont_fit_on_the_line do |v| v.foo end ^^^ `end` at 4, 0 is not aligned with `a_long_method_that_dont_fit_on_the_line do |v|` at 2, 2. RUBY expect_correction(<<~RUBY) variable = a_long_method_that_dont_fit_on_the_line do |v| v.foo end RUBY end end context 'when the method part is a call chain that spans several lines' do # Example from issue 346 of rubocop/rubocop on github: it 'accepts pretty alignment style' do expect_no_offenses(<<~RUBY) def foo(bar) bar.get_stuffs .reject do |stuff| stuff.with_a_very_long_expression_that_doesnt_fit_the_line end.select do |stuff| stuff.another_very_long_expression_that_doesnt_fit_the_line end .select do |stuff| stuff.another_very_long_expression_that_doesnt_fit_the_line end end RUBY end it 'registers offenses for misaligned ends' do expect_offense(<<~RUBY) def foo(bar) bar.get_stuffs .reject do |stuff| stuff.with_a_very_long_expression_that_doesnt_fit_the_line end.select do |stuff| ^^^ `end` at 5, 8 is not aligned with `bar.get_stuffs` at 2, 2 or `.reject do |stuff|` at 3, 6. stuff.another_very_long_expression_that_doesnt_fit_the_line end ^^^ `end` at 7, 4 is not aligned with `bar.get_stuffs` at 2, 2 or `end.select do |stuff|` at 5, 8. .select do |stuff| stuff.another_very_long_expression_that_doesnt_fit_the_line end ^^^ `end` at 10, 8 is not aligned with `bar.get_stuffs` at 2, 2 or `.select do |stuff|` at 8, 6. end RUBY expect_correction(<<~RUBY) def foo(bar) bar.get_stuffs .reject do |stuff| stuff.with_a_very_long_expression_that_doesnt_fit_the_line end.select do |stuff| stuff.another_very_long_expression_that_doesnt_fit_the_line end .select do |stuff| stuff.another_very_long_expression_that_doesnt_fit_the_line end end RUBY end # Example from issue 393 of rubocop/rubocop on github: it 'accepts end indented as the start of the block' do expect_no_offenses(<<~RUBY) my_object.chaining_this_very_long_method(with_a_parameter) .and_one_with_a_block do do_something end # Other variant: my_object.chaining_this_very_long_method( with_a_parameter).and_one_with_a_block do do_something end RUBY end # Example from issue 447 of rubocop/rubocop on github: it 'accepts two kinds of end alignment' do expect_no_offenses(<<~RUBY) # Aligned with start of line where do is: params = default_options.merge(options) .delete_if { |k, v| v.nil? } .each_with_object({}) do |(k, v), new_hash| new_hash[k.to_s] = v.to_s end # Aligned with start of the whole expression: params = default_options.merge(options) .delete_if { |k, v| v.nil? } .each_with_object({}) do |(k, v), new_hash| new_hash[k.to_s] = v.to_s end RUBY end end context 'when variables of a mass assignment spans several lines' do it 'accepts end aligned with the variables' do expect_no_offenses(<<~RUBY) e, f = [5, 6].map do |i| i - 5 end RUBY end it 'registers an offense for end aligned with the block' do expect_offense(<<~RUBY) e, f = [5, 6].map do |i| i - 5 end ^^^ `end` at 4, 4 is not aligned with `e,` at 1, 0 or `f = [5, 6].map do |i|` at 2, 0. RUBY expect_correction(<<~RUBY) e, f = [5, 6].map do |i| i - 5 end RUBY end end it 'accepts end aligned with an instance variable' do expect_no_offenses(<<~RUBY) @variable = test do |ala| end RUBY end it 'registers an offense for mismatched block end with an instance variable' do expect_offense(<<~RUBY) @variable = test do |ala| end ^^^ `end` at 2, 2 is not aligned with `@variable = test do |ala|` at 1, 0. RUBY expect_correction(<<~RUBY) @variable = test do |ala| end RUBY end it 'accepts end aligned with a class variable' do expect_no_offenses(<<~RUBY) @@variable = test do |ala| end RUBY end it 'registers an offense for mismatched block end with a class variable' do expect_offense(<<~RUBY) @@variable = test do |ala| end ^^^ `end` at 2, 2 is not aligned with `@@variable = test do |ala|` at 1, 0. RUBY expect_correction(<<~RUBY) @@variable = test do |ala| end RUBY end it 'accepts end aligned with a global variable' do expect_no_offenses(<<~RUBY) $variable = test do |ala| end RUBY end it 'registers an offense for mismatched block end with a global variable' do expect_offense(<<~RUBY) $variable = test do |ala| end ^^^ `end` at 2, 2 is not aligned with `$variable = test do |ala|` at 1, 0. RUBY expect_correction(<<~RUBY) $variable = test do |ala| end RUBY end it 'accepts end aligned with a constant' do expect_no_offenses(<<~RUBY) CONSTANT = test do |ala| end RUBY end it 'registers an offense for mismatched block end with a constant' do expect_offense(<<~RUBY) Module::CONSTANT = test do |ala| end ^^^ `end` at 2, 2 is not aligned with `Module::CONSTANT = test do |ala|` at 1, 0. RUBY expect_correction(<<~RUBY) Module::CONSTANT = test do |ala| end RUBY end it 'accepts end aligned with a method call' do expect_no_offenses(<<~RUBY) parser.children << lambda do |token| token << 1 end RUBY end it 'registers an offense for mismatched block end with a method call' do expect_offense(<<~RUBY) parser.children << lambda do |token| token << 1 end ^^^ `end` at 3, 2 is not aligned with `parser.children << lambda do |token|` at 1, 0. RUBY expect_correction(<<~RUBY) parser.children << lambda do |token| token << 1 end RUBY end it 'accepts end aligned with a method call with arguments' do expect_no_offenses(<<~RUBY) @h[:f] = f.each_pair.map do |f, v| v = 1 end RUBY end it 'registers an offense for mismatched end with a method call with arguments' do expect_offense(<<~RUBY) @h[:f] = f.each_pair.map do |f, v| v = 1 end ^^^ `end` at 3, 2 is not aligned with `@h[:f] = f.each_pair.map do |f, v|` at 1, 0. RUBY expect_correction(<<~RUBY) @h[:f] = f.each_pair.map do |f, v| v = 1 end RUBY end it 'does not raise an error for nested block in a method call' do expect_no_offenses('expect(arr.all? { |o| o.valid? })') end it 'accepts end aligned with the block when the block is a method argument' do expect_no_offenses(<<~RUBY) expect(arr.all? do |o| o.valid? end) RUBY end it 'registers an offense for mismatched end not aligned with the block that is an argument' do expect_offense(<<~RUBY) expect(arr.all? do |o| o.valid? end) ^^^ `end` at 3, 2 is not aligned with `arr.all? do |o|` at 1, 7 or `expect(arr.all? do |o|` at 1, 0. RUBY expect_correction(<<~RUBY) expect(arr.all? do |o| o.valid? end) RUBY end it 'accepts end aligned with an op-asgn (+=, -=)' do expect_no_offenses(<<~RUBY) rb += files.select do |file| file << something end RUBY end it 'registers an offense for mismatched block end with an op-asgn (+=, -=)' do expect_offense(<<~RUBY) rb += files.select do |file| file << something end ^^^ `end` at 3, 2 is not aligned with `rb` at 1, 0. RUBY expect_correction(<<~RUBY) rb += files.select do |file| file << something end RUBY end it 'accepts end aligned with an and-asgn (&&=)' do expect_no_offenses(<<~RUBY) variable &&= test do |ala| end RUBY end it 'registers an offense for mismatched block end with an and-asgn (&&=)' do expect_offense(<<~RUBY) variable &&= test do |ala| end ^^^ `end` at 2, 2 is not aligned with `variable &&= test do |ala|` at 1, 0. RUBY expect_correction(<<~RUBY) variable &&= test do |ala| end RUBY end it 'accepts end aligned with an or-asgn (||=)' do expect_no_offenses(<<~RUBY) variable ||= test do |ala| end RUBY end it 'registers an offense for mismatched block end with an or-asgn (||=)' do expect_offense(<<~RUBY) variable ||= test do |ala| end ^^^ `end` at 2, 2 is not aligned with `variable ||= test do |ala|` at 1, 0. RUBY expect_correction(<<~RUBY) variable ||= test do |ala| end RUBY end it 'accepts end aligned with a mass assignment' do expect_no_offenses(<<~RUBY) var1, var2 = lambda do |test| [1, 2] end RUBY end it 'accepts end aligned with a call chain left hand side' do expect_no_offenses(<<~RUBY) parser.diagnostics.consumer = lambda do |diagnostic| diagnostics << diagnostic end RUBY end it 'registers an offense for mismatched block end with a mass assignment' do expect_offense(<<~RUBY) var1, var2 = lambda do |test| [1, 2] end ^^^ `end` at 3, 2 is not aligned with `var1, var2` at 1, 0. RUBY expect_correction(<<~RUBY) var1, var2 = lambda do |test| [1, 2] end RUBY end context 'when multiple similar-looking blocks have misaligned ends' do it 'registers an offense for each of them' do expect_offense(<<~RUBY) a = test do end ^^^ `end` at 2, 1 is not aligned with `a = test do` at 1, 0. b = test do end ^^^ `end` at 4, 1 is not aligned with `b = test do` at 3, 0. RUBY expect_correction(<<~RUBY) a = test do end b = test do end RUBY end end context 'on a splatted method call' do it 'aligns end with the splat operator' do expect_no_offenses(<<~RUBY) def get_gems_by_name @gems ||= Hash[*get_latest_gems.map { |gem| [gem.name, gem, gem.full_name, gem] }.flatten] end RUBY end it 'registers an offense and corrects misaligned end braces' do expect_offense(<<~RUBY) def get_gems_by_name @gems ||= Hash[*get_latest_gems.map { |gem| [gem.name, gem, gem.full_name, gem] }.flatten] ^ `}` at 4, 14 is not aligned with `*get_latest_gems.map { |gem|` at 2, 17 or `@gems ||= Hash[*get_latest_gems.map { |gem|` at 2, 2. end RUBY expect_correction(<<~RUBY) def get_gems_by_name @gems ||= Hash[*get_latest_gems.map { |gem| [gem.name, gem, gem.full_name, gem] }.flatten] end RUBY end end context 'on a bit-flipped method call' do it 'aligns end with the ~ operator' do expect_no_offenses(<<~RUBY) def abc @abc ||= A[~xyz { |x| x }.flatten] end RUBY end it 'registers an offense and corrects misaligned end brace' do expect_offense(<<~RUBY) def abc @abc ||= A[~xyz { |x| x }.flatten] ^ `}` at 4, 24 is not aligned with `~xyz { |x|` at 2, 13 or `@abc ||= A[~xyz { |x|` at 2, 2. end RUBY expect_correction(<<~RUBY) def abc @abc ||= A[~xyz { |x| x }.flatten] end RUBY end end context 'on a logically negated method call' do it 'aligns end with the ! operator' do expect_no_offenses(<<~RUBY) def abc @abc ||= A[!xyz { |x| x }.flatten] end RUBY end it 'registers an offense and corrects' do expect_offense(<<~RUBY) def abc @abc ||= A[!xyz { |x| x }.flatten] ^ `}` at 4, 0 is not aligned with `!xyz { |x|` at 2, 13 or `@abc ||= A[!xyz { |x|` at 2, 2. end RUBY expect_correction(<<~RUBY) def abc @abc ||= A[!xyz { |x| x }.flatten] end RUBY end end context 'on an arithmetically negated method call' do it 'aligns end with the - operator' do expect_no_offenses(<<~RUBY) def abc @abc ||= A[-xyz { |x| x }.flatten] end RUBY end it 'registers an offense and corrects' do expect_offense(<<~RUBY) def abc @abc ||= A[-xyz { |x| x }.flatten] ^ `}` at 4, 18 is not aligned with `-xyz { |x|` at 2, 13 or `@abc ||= A[-xyz { |x|` at 2, 2. end RUBY expect_correction(<<~RUBY) def abc @abc ||= A[-xyz { |x| x }.flatten] end RUBY end end context 'when the block is terminated by }' do it 'mentions } (not end) in the message' do expect_offense(<<~RUBY) test { } ^ `}` at 2, 2 is not aligned with `test {` at 1, 0. RUBY expect_correction(<<~RUBY) test { } RUBY end end context 'with `EnforcedStyle: start_of_line`' do let(:cop_config) { { 'EnforcedStyleAlignWith' => 'start_of_line' } } it 'allows when start_of_line aligned' do expect_no_offenses(<<~RUBY) foo.bar .each do baz end RUBY end it 'errors when do aligned' do expect_offense(<<~RUBY) foo.bar .each do baz end ^^^ `end` at 4, 2 is not aligned with `foo.bar` at 1, 0. RUBY expect_correction(<<~RUBY) foo.bar .each do baz end RUBY end context 'inside a non-endless method' do it 'does not register an offense when `end` is aligned with the block start' do expect_no_offenses(<<~RUBY) def foo bar do baz end end RUBY end it 'registers an offense and corrects when `end` is aligned with the method start' do expect_offense(<<~RUBY) def foo bar do baz end ^^^ `end` at 4, 0 is not aligned with `bar do` at 2, 2. end RUBY expect_correction(<<~RUBY) def foo bar do baz end end RUBY end end context 'inside a non-endless singleton method' do it 'does not register an offense when `end` is aligned with the block start' do expect_no_offenses(<<~RUBY) def self.foo bar do baz end end RUBY end it 'registers an offense and corrects when `end` is aligned with the method start' do expect_offense(<<~RUBY) def self.foo bar do baz end ^^^ `end` at 4, 0 is not aligned with `bar do` at 2, 2. end RUBY expect_correction(<<~RUBY) def self.foo bar do baz end end RUBY end end context 'with endless methods', :ruby30 do it 'does not register an offense when `end` is aligned with the start of the method definition' do expect_no_offenses(<<~RUBY) def foo = bar do baz end RUBY end it 'does not register an offense when `end` is aligned with the start of a multiline method definition' do expect_no_offenses(<<~RUBY) def foo = bar(123, 456) do baz end RUBY end it 'registers an offense when `end` is not aligned with the start of the method definition' do expect_offense(<<~RUBY) def foo = bar do baz end ^^^ `end` at 3, 4 is not aligned with `def foo = bar do` at 1, 0. RUBY expect_correction(<<~RUBY) def foo = bar do baz end RUBY end it 'does not register an offense when `end` is aligned with the start of a singleton method definition' do expect_no_offenses(<<~RUBY) def self.foo = bar do baz end RUBY end it 'does not register an offense when `end` is aligned with the start of a multiline singleton method definition' do expect_no_offenses(<<~RUBY) def self.foo = bar(123, 456) do baz end RUBY end it 'registers an offense when `end` is not aligned with the start of a singleton method definition' do expect_offense(<<~RUBY) def self.foo = bar do baz end ^^^ `end` at 3, 4 is not aligned with `def self.foo = bar do` at 1, 0. RUBY expect_correction(<<~RUBY) def self.foo = bar do baz end RUBY end end end context 'with `EnforcedStyle: start_of_block`' do let(:cop_config) { { 'EnforcedStyleAlignWith' => 'start_of_block' } } it 'allows when do aligned' do expect_no_offenses(<<~RUBY) foo.bar .each do baz end RUBY end it 'errors when start_of_line aligned' do expect_offense(<<~RUBY) foo.bar .each do baz end ^^^ `end` at 4, 0 is not aligned with `.each do` at 2, 2. RUBY expect_correction(<<~RUBY) foo.bar .each do baz end RUBY end context 'inside a non-endless method' do it 'does not register an offense when `end` is aligned with the block start' do expect_no_offenses(<<~RUBY) def foo bar do baz end end RUBY end it 'registers an offense and corrects when `end` is aligned with the method start' do expect_offense(<<~RUBY) def foo bar do baz end ^^^ `end` at 4, 0 is not aligned with `bar do` at 2, 2. end RUBY expect_correction(<<~RUBY) def foo bar do baz end end RUBY end end context 'inside a non-endless singleton method' do it 'does not register an offense when `end` is aligned with the block start' do expect_no_offenses(<<~RUBY) def self.foo bar do baz end end RUBY end it 'registers an offense and corrects when `end` is aligned with the method start' do expect_offense(<<~RUBY) def self.foo bar do baz end ^^^ `end` at 4, 0 is not aligned with `bar do` at 2, 2. end RUBY expect_correction(<<~RUBY) def self.foo bar do baz end end RUBY end end context 'with endless methods', :ruby30 do it 'does not register an offense when `end` is aligned with the start of the method definition' do expect_no_offenses(<<~RUBY) def foo = bar do baz end RUBY end it 'registers an offense when `end` is aligned with the start of a multiline method definition' do expect_offense(<<~RUBY) def foo = bar(123, 456) do baz end ^^^ `end` at 4, 0 is not aligned with `456) do` at 2, 14. RUBY expect_correction(<<~RUBY) def foo = bar(123, 456) do baz end RUBY end it 'registers an offense when `end` is not aligned with the start of the method definition' do expect_offense(<<~RUBY) def foo = bar do baz end ^^^ `end` at 3, 4 is not aligned with `def foo = bar do` at 1, 0. RUBY expect_correction(<<~RUBY) def foo = bar do baz end RUBY end it 'does not register an offense when `end` is aligned with the start of a singleton method definition' do expect_no_offenses(<<~RUBY) def self.foo = bar do baz end RUBY end it 'registers an offense when `end` is aligned with the start of a multiline singleton method definition' do expect_offense(<<~RUBY) def self.foo = bar(123, 456) do baz end ^^^ `end` at 4, 0 is not aligned with `456) do` at 2, 19. RUBY expect_correction(<<~RUBY) def self.foo = bar(123, 456) do baz end RUBY end it 'registers an offense when `end` is not aligned with the start of a singleton method definition' do expect_offense(<<~RUBY) def self.foo = bar do baz end ^^^ `end` at 3, 4 is not aligned with `def self.foo = bar do` at 1, 0. RUBY expect_correction(<<~RUBY) def self.foo = bar do baz end RUBY end end end context 'Ruby 2.7', :ruby27 do it 'accepts end aligned with a call chain left hand side' do expect_no_offenses(<<~RUBY) parser.diagnostics.consumer = lambda do _1 << diagnostic end RUBY end it 'registers an offense for mismatched block end with a mass assignment' do expect_offense(<<~RUBY) var1, var2 = lambda do [_1, _2] end ^^^ `end` at 3, 2 is not aligned with `var1, var2` at 1, 0. RUBY end end context 'Ruby 3.4', :ruby34 do it 'accepts end aligned with a call chain left hand side' do expect_no_offenses(<<~RUBY) parser.diagnostics.consumer = lambda do it << diagnostic end RUBY end end end
ruby
MIT
99fa0fdd0481910d7d052f4c2ed01ad36178f404
2026-01-04T15:37:41.211519Z
false
rubocop/rubocop
https://github.com/rubocop/rubocop/blob/99fa0fdd0481910d7d052f4c2ed01ad36178f404/spec/rubocop/cop/layout/trailing_whitespace_spec.rb
spec/rubocop/cop/layout/trailing_whitespace_spec.rb
# frozen_string_literal: true RSpec.describe RuboCop::Cop::Layout::TrailingWhitespace, :config do let(:cop_config) { { 'AllowInHeredoc' => false } } it 'registers an offense for a line ending with space' do expect_offense(<<~RUBY) x = 0#{trailing_whitespace} ^ Trailing whitespace detected. RUBY end it 'registers an offense for a blank line with space' do expect_offense(<<~RUBY) #{trailing_whitespace * 2} ^^ Trailing whitespace detected. RUBY end it 'registers an offense for a line ending with tab' do expect_offense(<<~RUBY) x = 0\t ^ Trailing whitespace detected. RUBY end it 'registers an offense for a line ending with full-width space' do expect_offense(<<~RUBY) x = :a\u3000 ^ Trailing whitespace detected. RUBY end it 'registers an offense for trailing whitespace in a heredoc string' do expect_offense(<<~RUBY) x = <<HEREDOC Hi#{trailing_whitespace * 3} ^^^ Trailing whitespace detected. HEREDOC RUBY end it 'registers an offense for a tab in a heredoc' do expect_offense(<<~RUBY) <<~X \t ^ Trailing whitespace detected. X RUBY end it 'registers an offense for a full-width space in a heredoc' do expect_offense(<<~RUBY) <<~X \u3000 ^ Trailing whitespace detected. X RUBY end it 'registers offenses before __END__ but not after' do expect_offense(<<~RUBY) x = 0\t ^ Trailing whitespace detected. #{trailing_whitespace} ^ Trailing whitespace detected. __END__ x = 0\t RUBY end it 'is not fooled by __END__ within a documentation comment' do expect_offense(<<~RUBY) x = 0\t ^ Trailing whitespace detected. =begin __END__ =end x = 0\t ^ Trailing whitespace detected. RUBY end it 'is not fooled by heredoc containing __END__' do expect_offense(<<~RUBY) x1 = <<HEREDOC#{trailing_whitespace} ^ Trailing whitespace detected. __END__ x2 = 0\t ^ Trailing whitespace detected. HEREDOC x3 = 0\t ^ Trailing whitespace detected. RUBY end it 'is not fooled by heredoc containing __END__ within a doc comment' do expect_offense(<<~RUBY) x1 = <<HEREDOC#{trailing_whitespace} ^ Trailing whitespace detected. =begin#{trailing_whitespace * 2} ^^ Trailing whitespace detected. __END__ =end x2 = 0\t ^ Trailing whitespace detected. HEREDOC x3 = 0\t ^ Trailing whitespace detected. RUBY end it 'accepts a line without trailing whitespace' do expect_no_offenses('x = 0') end it 'autocorrects unwanted space' do expect_offense(<<~RUBY) x = 0#{trailing_whitespace} ^ Trailing whitespace detected. x = 0\t ^ Trailing whitespace detected. x = :a\u3000 ^ Trailing whitespace detected. RUBY expect_correction(<<~RUBY) x = 0 x = 0 x = :a RUBY end context 'when `AllowInHeredoc` is set to true' do let(:cop_config) { { 'AllowInHeredoc' => true } } it 'accepts trailing whitespace in a heredoc string' do expect_no_offenses(<<~RUBY) x = <<HEREDOC Hi#{trailing_whitespace * 3} HEREDOC RUBY end it 'registers an offense for trailing whitespace at the heredoc begin' do expect_offense(<<~RUBY) x = <<HEREDOC#{trailing_whitespace} ^ Trailing whitespace detected. Hi#{trailing_whitespace * 3} HEREDOC RUBY end end context 'when `AllowInHeredoc` is set to false' do let(:cop_config) { { 'AllowInHeredoc' => false } } it 'corrects safely trailing whitespace in a heredoc string' do expect_offense(<<~RUBY) x = <<~EXAMPLE has trailing #{trailing_whitespace} ^^^^ Trailing whitespace detected. no trailing EXAMPLE RUBY expect_correction(<<~RUBY) x = <<~EXAMPLE has trailing\#{' '} no trailing EXAMPLE RUBY end it 'corrects by removing trailing whitespace used for indentation in a heredoc string' do expect_offense(<<~RUBY) x = <<~EXAMPLE no trailing #{trailing_whitespace} ^^ Trailing whitespace detected. no trailing #{trailing_whitespace} ^ Trailing whitespace detected. no trailing EXAMPLE RUBY expect_correction(<<~RUBY) x = <<~EXAMPLE no trailing no trailing no trailing EXAMPLE RUBY end it 'corrects a whitespace line in a heredoc string that is longer than the indentation' do expect_offense(<<~RUBY) x = <<~EXAMPLE no trailing #{trailing_whitespace} ^^^ Trailing whitespace detected. no trailing EXAMPLE RUBY expect_correction(<<~RUBY) x = <<~EXAMPLE no trailing \#{' '} no trailing EXAMPLE RUBY end it 'does not correct trailing whitespace in a static heredoc string' do expect_offense(<<~RUBY) x = <<~'EXAMPLE' has trailing#{trailing_whitespace} ^ Trailing whitespace detected. no trailing EXAMPLE RUBY expect_no_corrections end end end
ruby
MIT
99fa0fdd0481910d7d052f4c2ed01ad36178f404
2026-01-04T15:37:41.211519Z
false
rubocop/rubocop
https://github.com/rubocop/rubocop/blob/99fa0fdd0481910d7d052f4c2ed01ad36178f404/spec/rubocop/cop/layout/initial_indentation_spec.rb
spec/rubocop/cop/layout/initial_indentation_spec.rb
# frozen_string_literal: true RSpec.describe RuboCop::Cop::Layout::InitialIndentation, :config do it 'registers an offense for indented method definition' do expect_offense(<<-RUBY.strip_margin('|')) | def f | ^^^ Indentation of first line in file detected. | end RUBY expect_correction(<<~RUBY) def f end RUBY end it 'accepts unindented method definition' do expect_no_offenses(<<~RUBY) def f end RUBY end context 'for a file with byte order mark' do it 'accepts unindented method call' do expect_no_offenses('puts 1') end it 'registers an offense and corrects indented method call' do expect_offense(<<~RUBY)  puts 1 ^^^^ Indentation of first line in file detected. RUBY expect_correction(<<~RUBY) puts 1 RUBY end it 'registers an offense and corrects indented method call after comment' do expect_offense(<<~RUBY) # comment puts 1 ^^^^ Indentation of first line in file detected. RUBY expect_correction(<<~RUBY) # comment puts 1 RUBY end end it 'accepts empty file' do expect_no_offenses('') end it 'registers an offense and corrects indented assignment disregarding comment' do expect_offense(<<-RUBY.strip_margin('|')) | # comment | x = 1 | ^ Indentation of first line in file detected. RUBY expect_correction(<<~RUBY) # comment x = 1 RUBY end it 'accepts unindented comment + assignment' do expect_no_offenses(<<~RUBY) # comment x = 1 RUBY end end
ruby
MIT
99fa0fdd0481910d7d052f4c2ed01ad36178f404
2026-01-04T15:37:41.211519Z
false
rubocop/rubocop
https://github.com/rubocop/rubocop/blob/99fa0fdd0481910d7d052f4c2ed01ad36178f404/spec/rubocop/cop/layout/space_inside_array_literal_brackets_spec.rb
spec/rubocop/cop/layout/space_inside_array_literal_brackets_spec.rb
# frozen_string_literal: true RSpec.describe RuboCop::Cop::Layout::SpaceInsideArrayLiteralBrackets, :config do let(:no_space_in_empty_message) { 'Do not use space inside empty array brackets.' } let(:no_space_message) { 'Do not use space inside array brackets.' } let(:one_space_message) { 'Use one space inside empty array brackets.' } let(:use_space_message) { 'Use space inside array brackets.' } it 'does not register offense for any kind of reference brackets' do expect_no_offenses(<<~RUBY) a[1] b[ 3] c[ foo ] d[index, 2] RUBY end context 'with space inside empty brackets not allowed' do let(:cop_config) { { 'EnforcedStyleForEmptyBrackets' => 'no_space' } } it 'accepts empty brackets with no space inside' do expect_no_offenses('a = []') end it 'registers an offense and corrects empty brackets with 1 space inside' do expect_offense(<<~RUBY) a = [ ] ^^^ #{no_space_in_empty_message} RUBY expect_correction(<<~RUBY) a = [] RUBY end it 'registers an offense and corrects empty brackets with multiple spaces inside' do expect_offense(<<~RUBY) a = [ ] ^^^^^^^ #{no_space_in_empty_message} RUBY expect_correction(<<~RUBY) a = [] RUBY end it 'registers an offense and corrects multiline spaces' do expect_offense(<<~RUBY) a = [ ^ #{no_space_in_empty_message} ] RUBY expect_correction(<<~RUBY) a = [] RUBY end end context 'with space inside empty braces allowed' do let(:cop_config) { { 'EnforcedStyleForEmptyBrackets' => 'space' } } it 'accepts empty brackets with space inside' do expect_no_offenses('a = [ ]') end it 'registers an offense and corrects empty brackets with no space inside' do expect_offense(<<~RUBY) a = [] ^^ #{one_space_message} RUBY expect_correction(<<~RUBY) a = [ ] RUBY end it 'registers an offense and corrects empty brackets with more than one space inside' do expect_offense(<<~RUBY) a = [ ] ^^^^^^^^ #{one_space_message} RUBY expect_correction(<<~RUBY) a = [ ] RUBY end end context 'when EnforcedStyle is no_space' do let(:cop_config) { { 'EnforcedStyle' => 'no_space' } } it 'does not register offense for arrays with no spaces' do expect_no_offenses(<<~RUBY) [1, 2, 3] [foo, bar] ["qux", "baz"] [[1, 2], [3, 4]] [{ foo: 1 }, { bar: 2}] RUBY end it 'does not register offense for arrays using ref brackets' do expect_no_offenses(<<~RUBY) [1, 2, 3][0] [foo, bar][ 1] ["qux", "baz"][ -1 ] [[1, 2], [3, 4]][1 ] [{ foo: 1 }, { bar: 2}][0] RUBY end it 'does not register offense when 2 arrays on one line' do expect_no_offenses(<<~RUBY) [2,3,4] + [5,6,7] RUBY end it 'does not register offense for array when brackets get own line' do expect_no_offenses(<<~RUBY) stuff = [ a, b ] RUBY end it 'does not register offense for indented array ' \ 'when bottom bracket gets its own line & is misaligned' do expect_no_offenses(<<~RUBY) def do_stuff a = [ 1, 2 ] end RUBY end it 'does not register offense when bottom bracket gets its own line & has trailing method' do expect_no_offenses(<<~RUBY) a = [ 1, 2, nil ].compact RUBY end it 'does not register offense when bottom bracket gets its own line indented with tabs' do expect_no_offenses(<<~RUBY) a = \t[ \t1, 2, nil \t].compact RUBY end it 'does not register offense for valid multiline array' do expect_no_offenses(<<~RUBY) ['Encoding:', ' Enabled: false'] RUBY end it 'does not register offense for valid 2-dimensional array' do expect_no_offenses(<<~RUBY) [1, [2,3,4], [5,6,7]] RUBY end it 'accepts space inside array brackets if with comment' do expect_no_offenses(<<~RUBY) a = [ # Comment 1, 2 ] RUBY end it 'accepts square brackets as method name' do expect_no_offenses(<<~RUBY) def Vector.[](*array) end RUBY end it 'does not register offense when contains an array literal as ' \ 'an argument after a heredoc is started' do expect_no_offenses(<<~RUBY) ActiveRecord::Base.connection.execute(<<-SQL, [self.class.to_s]).first["count"] SELECT COUNT(widgets.id) FROM widgets WHERE widget_type = $1 SQL RUBY end it 'accepts square brackets called with method call syntax' do expect_no_offenses('subject.[](0)') end it 'registers an offense and corrects array brackets with leading whitespace' do expect_offense(<<~RUBY) [ 2, 3, 4] ^ #{no_space_message} RUBY expect_correction(<<~RUBY) [2, 3, 4] RUBY end it 'registers an offense and corrects array brackets with trailing whitespace' do expect_offense(<<~RUBY) [b, c, d ] ^^^ #{no_space_message} RUBY expect_correction(<<~RUBY) [b, c, d] RUBY end it 'registers an offense and corrects an array when two on one line' do expect_offense(<<~RUBY) ['qux', 'baz' ] - ['baz'] ^^ #{no_space_message} RUBY expect_correction(<<~RUBY) ['qux', 'baz'] - ['baz'] RUBY end it 'registers an offense and corrects multiline array on end bracket' do expect_offense(<<~RUBY) ['ok', 'still good', 'not good' ] ^ #{no_space_message} RUBY expect_correction(<<~RUBY) ['ok', 'still good', 'not good'] RUBY end it 'registers an offense and corrects multiline array on end bracket with trailing method' do expect_offense(<<~RUBY) [:good, :bad ].compact ^^ #{no_space_message} RUBY expect_correction(<<~RUBY) [:good, :bad].compact RUBY end it 'registers an offense and corrects 2 arrays on one line' do expect_offense(<<~RUBY) [2,3,4] - [ 3,4] ^ #{no_space_message} RUBY expect_correction(<<~RUBY) [2,3,4] - [3,4] RUBY end it 'registers an offense and corrects an array literal as ' \ 'an argument with trailing whitespace after a heredoc is started' do expect_offense(<<~RUBY) ActiveRecord::Base.connection.execute(<<-SQL, [self.class.to_s ]).first["count"] ^ #{no_space_message} SELECT COUNT(widgets.id) FROM widgets WHERE widget_type = $1 SQL RUBY expect_correction(<<~RUBY) ActiveRecord::Base.connection.execute(<<-SQL, [self.class.to_s]).first["count"] SELECT COUNT(widgets.id) FROM widgets WHERE widget_type = $1 SQL RUBY end it 'accepts a multiline array with whitespace before end bracket' do expect_no_offenses(<<~RUBY) stuff = [ a, b ] RUBY end context 'when using array pattern matching', :ruby27 do it 'registers an offense when array pattern with spaces' do expect_offense(<<~RUBY) case foo in [ bar, baz ] ^ Do not use space inside array brackets. ^ Do not use space inside array brackets. end RUBY expect_correction(<<~RUBY) case foo in [bar, baz] end RUBY end it 'does not register an offense when array pattern with no spaces' do expect_no_offenses(<<~RUBY) case foo in [bar, baz] end RUBY end end context 'when using one-line array `in` pattern matching', :ruby27 do it 'registers an offense when array pattern with spaces' do expect_offense(<<~RUBY) foo in [ bar, baz ] ^ Do not use space inside array brackets. ^ Do not use space inside array brackets. RUBY expect_correction(<<~RUBY) foo in [bar, baz] RUBY end it 'does not register an offense when array pattern with no spaces' do expect_no_offenses(<<~RUBY) foo in [bar, baz] RUBY end end context 'when using one-line array `=>` pattern matching', :ruby30 do it 'registers an offense when array pattern with spaces' do expect_offense(<<~RUBY) foo => [ bar, baz ] ^ Do not use space inside array brackets. ^ Do not use space inside array brackets. RUBY expect_correction(<<~RUBY) foo => [bar, baz] RUBY end it 'does not register an offense when array pattern with no spaces' do expect_no_offenses(<<~RUBY) foo => [bar, baz] RUBY end end context 'when using constant pattern matching', :ruby27 do it 'registers an offense for empty pattern with spaces' do expect_offense(<<~RUBY) case value in ADT[ ] ^^^ #{no_space_in_empty_message} end RUBY expect_correction(<<~RUBY) case value in ADT[] end RUBY end it 'registers an offense for empty nested pattern with spaces' do expect_offense(<<~RUBY) case value in ADT[*head, ADT[ ]] ^^^ #{no_space_in_empty_message} end RUBY expect_correction(<<~RUBY) case value in ADT[*head, ADT[]] end RUBY end it 'registers an offense for pattern with spaces' do expect_offense(<<~RUBY) case value in ADT[ *head, tail ] ^ Do not use space inside array brackets. ^ Do not use space inside array brackets. end RUBY expect_correction(<<~RUBY) case value in ADT[*head, tail] end RUBY end it 'registers an offense for nested constant with spaces' do expect_offense(<<~RUBY) case value in ADT[ *head, ADT[ *headhead, tail ] ] ^ Do not use space inside array brackets. ^ Do not use space inside array brackets. ^ Do not use space inside array brackets. ^ Do not use space inside array brackets. end RUBY expect_correction(<<~RUBY) case value in ADT[*head, ADT[*headhead, tail]] end RUBY end it 'does not register an offense for pattern with no spaces' do expect_no_offenses(<<~RUBY) case value in ADT[*head, ADT[*headhead, tail]] end RUBY end end end shared_examples 'space inside arrays' do it 'does not register offense for arrays with spaces' do expect_no_offenses(<<~RUBY) [ 1, 2, 3 ] [ foo, bar ] [ "qux", "baz" ] [ { foo: 1 }, { bar: 2} ] RUBY end it 'does not register offense for arrays using ref brackets' do expect_no_offenses(<<~RUBY) [ 1, 2, 3 ][0] [ foo, bar ][ 1] [ "qux", "baz" ][ -1 ] [ { foo: 1 }, { bar: 2} ][0] RUBY end it 'does not register offense when 2 arrays on one line' do expect_no_offenses(<<~RUBY) [ 2,3,4 ] + [ 5,6,7 ] RUBY end it 'does not register offense for array when brackets get their own line' do expect_no_offenses(<<~RUBY) stuff = [ a, b ] RUBY end it 'does not register offense for indented array ' \ 'when bottom bracket gets its own line & is misaligned' do expect_no_offenses(<<~RUBY) def do_stuff a = [ 1, 2 ] end RUBY end it 'does not register offense when bottom bracket gets its own line & has trailing method' do expect_no_offenses(<<~RUBY) a = [ 1, 2, nil ].compact RUBY end it 'does not register offense for valid multiline array' do expect_no_offenses(<<~RUBY) [ 'Encoding:', 'Enabled: false' ] RUBY end it 'accepts space inside array brackets with comment' do expect_no_offenses(<<~RUBY) a = [ # Comment 1, 2 ] RUBY end it 'accepts square brackets as method name' do expect_no_offenses(<<~RUBY) def Vector.[](*array) end RUBY end it 'accepts square brackets called with method call syntax' do expect_no_offenses('subject.[](0)') end it 'registers an offense and corrects array brackets with no leading whitespace' do expect_offense(<<~RUBY) [2, 3, 4 ] ^ #{use_space_message} RUBY expect_correction(<<~RUBY) [ 2, 3, 4 ] RUBY end it 'registers an offense and corrects array brackets with no trailing whitespace' do expect_offense(<<~RUBY) [ b, c, d] ^ #{use_space_message} RUBY expect_correction(<<~RUBY) [ b, c, d ] RUBY end it 'registers an offense and corrects an array missing whitespace ' \ 'when there is more than one array on a line' do expect_offense(<<~RUBY) [ 'qux', 'baz'] - [ 'baz' ] ^ #{use_space_message} RUBY expect_correction(<<~RUBY) [ 'qux', 'baz' ] - [ 'baz' ] RUBY end it 'registers an offense and corrects multiline array on end bracket' do expect_offense(<<~RUBY) [ 'ok', 'still good', 'not good'] ^ #{use_space_message} RUBY expect_correction(<<~RUBY) [ 'ok', 'still good', 'not good' ] RUBY end it 'registers an offense and corrects multiline array on end bracket with trailing method' do expect_offense(<<~RUBY) [ :good, :bad].compact ^ #{use_space_message} RUBY expect_correction(<<~RUBY) [ :good, :bad ].compact RUBY end it 'registers an offense and corrects when 2 arrays are on one line' do expect_offense(<<~RUBY) [ 2, 3, 4 ] - [3, 4 ] ^ #{use_space_message} RUBY expect_correction(<<~RUBY) [ 2, 3, 4 ] - [ 3, 4 ] RUBY end end shared_examples 'array pattern without brackets' do it 'does not register an offense' do expect_no_offenses(<<~RUBY) case ary in a, b, c, d end RUBY end end context 'when EnforcedStyle is space' do let(:cop_config) { { 'EnforcedStyle' => 'space' } } it_behaves_like 'space inside arrays' it_behaves_like 'array pattern without brackets' it 'does not register offense for valid 2-dimensional array' do expect_no_offenses(<<~RUBY) [ 1, [ 2,3,4 ], [ 5,6,7 ] ] RUBY end context 'when using array pattern matching', :ruby27 do it 'registers an offense when array pattern with no spaces' do expect_offense(<<~RUBY) case foo in [bar, baz] ^ Use space inside array brackets. ^ Use space inside array brackets. end RUBY expect_correction(<<~RUBY) case foo in [ bar, baz ] end RUBY end it 'does not register an offense when array pattern with spaces' do expect_no_offenses(<<~RUBY) case foo in [ bar, baz ] end RUBY end end context 'when using one-line array `in` pattern matching', :ruby27 do it 'registers an offense when array pattern with no spaces' do expect_offense(<<~RUBY) foo in [bar, baz] ^ Use space inside array brackets. ^ Use space inside array brackets. RUBY expect_correction(<<~RUBY) foo in [ bar, baz ] RUBY end it 'does not register an offense when array pattern with spaces' do expect_no_offenses(<<~RUBY) foo in [ bar, baz ] RUBY end end context 'when using one-line array `=>` pattern matching', :ruby30 do it 'registers an offense when array pattern with no spaces' do expect_offense(<<~RUBY) foo => [bar, baz] ^ Use space inside array brackets. ^ Use space inside array brackets. RUBY expect_correction(<<~RUBY) foo => [ bar, baz ] RUBY end it 'does not register an offense when array pattern with spaces' do expect_no_offenses(<<~RUBY) foo => [ bar, baz ] RUBY end end context 'when using constant pattern matching', :ruby27 do it 'registers an offense for pattern with no spaces' do expect_offense(<<~RUBY) case value in ADT[*head, tail] ^ #{use_space_message} ^ #{use_space_message} end RUBY expect_correction(<<~RUBY) case value in ADT[ *head, tail ] end RUBY end it 'registers an offense for nested constant with no spaces' do expect_offense(<<~RUBY) case value in ADT[*head, ADT[*headhead, tail]] ^ #{use_space_message} ^ #{use_space_message} ^ #{use_space_message} ^ #{use_space_message} end RUBY expect_correction(<<~RUBY) case value in ADT[ *head, ADT[ *headhead, tail ] ] end RUBY end it 'does not register an offense for pattern with spaces' do expect_no_offenses(<<~RUBY) case value in ADT[ *head, ADT[ *headhead, tail ] ] end RUBY end end end context 'when EnforcedStyle is compact' do let(:cop_config) { { 'EnforcedStyle' => 'compact' } } it_behaves_like 'space inside arrays' it_behaves_like 'array pattern without brackets' it 'does not register offense for valid 2-dimensional array' do expect_no_offenses(<<~RUBY) [ 1, [ 2,3,4 ], [ 5,6,7 ]] RUBY end it 'does not register offense for valid 3-dimensional array' do expect_no_offenses(<<~RUBY) [[ 2, 3, [ 4 ]]] RUBY end it 'does not register offense for valid 4-dimensional array' do expect_no_offenses(<<~RUBY) [[[[ boom ]]]] RUBY end it 'registers an offense and corrects space between 2 closing brackets' do expect_offense(<<~RUBY) [ 1, [ 2,3,4 ], [ 5,6,7 ] ] ^ #{no_space_message} RUBY expect_correction(<<~RUBY) [ 1, [ 2,3,4 ], [ 5,6,7 ]] RUBY end it 'registers an offense and corrects space between 2 opening brackets' do expect_offense(<<~RUBY) [ [ 2,3,4 ], [ 5,6,7 ], 8 ] ^ #{no_space_message} RUBY expect_correction(<<~RUBY) [[ 2,3,4 ], [ 5,6,7 ], 8 ] RUBY end it 'accepts multiline array' do expect_no_offenses(<<~RUBY) array = [[ a ], [ b, c ]] RUBY end context 'multiline, 2-dimensional array with spaces' do it 'registers an offense and corrects at the beginning of array' do expect_offense(<<~RUBY) multiline = [ [ 1, 2, 3, 4 ], ^ #{no_space_message} [ 3, 4, 5, 6 ]] RUBY expect_correction(<<~RUBY) multiline = [[ 1, 2, 3, 4 ], [ 3, 4, 5, 6 ]] RUBY end it 'registers an offense and corrects at the end of array' do expect_offense(<<~RUBY) multiline = [[ 1, 2, 3, 4 ], [ 3, 4, 5, 6 ] ] ^ #{no_space_message} RUBY expect_correction(<<~RUBY) multiline = [[ 1, 2, 3, 4 ], [ 3, 4, 5, 6 ]] RUBY end end context 'multiline, 2-dimensional array with newlines' do it 'registers an offense and corrects at the beginning of array' do expect_offense(<<~RUBY) multiline = [ ^{} #{no_space_message} [ 1, 2, 3, 4 ], [ 3, 4, 5, 6 ]] RUBY expect_correction(<<~RUBY) multiline = [[ 1, 2, 3, 4 ], [ 3, 4, 5, 6 ]] RUBY end it 'registers an offense and corrects at the end of array' do expect_offense(<<~RUBY) multiline = [[ 1, 2, 3, 4 ], [ 3, 4, 5, 6 ] ] ^{} #{no_space_message} RUBY expect_correction(<<~RUBY) multiline = [[ 1, 2, 3, 4 ], [ 3, 4, 5, 6 ]] RUBY end end it 'registers an offense and corrects 2-dimensional array with extra spaces' do expect_offense(<<~RUBY) [ [ a, b ], [ 1, 7 ] ] ^ #{no_space_message} ^ #{no_space_message} RUBY expect_correction(<<~RUBY) [[ a, b ], [ 1, 7 ]] RUBY end it 'registers an offense and corrects 3-dimensional array with extra spaces' do expect_offense(<<~RUBY) [ [a, b ], [foo, [bar, baz] ] ] ^ #{no_space_message} ^ #{no_space_message} ^ #{use_space_message} ^ #{use_space_message} ^ #{use_space_message} ^ #{use_space_message} ^ #{no_space_message} RUBY expect_correction(<<~RUBY) [[ a, b ], [ foo, [ bar, baz ]]] RUBY end context 'when using constant pattern matching', :ruby27 do it 'registers an offense for pattern with no spaces' do expect_offense(<<~RUBY) case value in ADT[*head, tail] ^ #{use_space_message} ^ #{use_space_message} end RUBY expect_correction(<<~RUBY) case value in ADT[ *head, tail ] end RUBY end it 'registers an offense for nested constant pattern with no spaces' do expect_offense(<<~RUBY) case value in ADT[*head, ADT[*headhead, tail]] ^ #{use_space_message} ^ #{use_space_message} ^ #{use_space_message} end RUBY expect_correction(<<~RUBY) case value in ADT[ *head, ADT[ *headhead, tail ]] end RUBY end it 'does not register an offense for pattern with spaces' do expect_no_offenses(<<~RUBY) case value in ADT[ *head, ADT[ *headhead, tail ]] end RUBY end end end end
ruby
MIT
99fa0fdd0481910d7d052f4c2ed01ad36178f404
2026-01-04T15:37:41.211519Z
false
rubocop/rubocop
https://github.com/rubocop/rubocop/blob/99fa0fdd0481910d7d052f4c2ed01ad36178f404/spec/rubocop/cop/layout/space_inside_block_braces_spec.rb
spec/rubocop/cop/layout/space_inside_block_braces_spec.rb
# frozen_string_literal: true RSpec.describe RuboCop::Cop::Layout::SpaceInsideBlockBraces, :config do let(:supported_styles) { %w[space no_space] } let(:cop_config) do { 'EnforcedStyle' => 'space', 'SupportedStyles' => supported_styles, 'SpaceBeforeBlockParameters' => true } end context 'with space inside empty braces not allowed' do let(:cop_config) { { 'EnforcedStyleForEmptyBraces' => 'no_space' } } it 'accepts empty braces with no space inside' do expect_no_offenses('each {}') end it 'accepts braces with something inside' do expect_no_offenses('each { "f" }') end it 'accepts multiline braces with content' do expect_no_offenses(<<~RUBY) each { %( ) } RUBY end it 'accepts empty braces with comment and line break inside' do expect_no_offenses(<<~RUBY) each { # Comment } RUBY end it 'accepts empty braces with line break inside' do expect_no_offenses(<<-RUBY.strip_margin('|')) | each { | } RUBY end it 'registers an offense and corrects empty braces with space inside' do expect_offense(<<~RUBY) each { } ^ Space inside empty braces detected. RUBY expect_correction(<<~RUBY) each {} RUBY end it 'accepts braces that are not empty' do expect_no_offenses(<<~RUBY) a { b } RUBY end end context 'with space inside empty braces allowed' do let(:cop_config) { { 'EnforcedStyleForEmptyBraces' => 'space' } } it 'accepts empty braces with space inside' do expect_no_offenses('each { }') end it 'registers an offense and corrects empty braces with no space inside' do expect_offense(<<~RUBY) each {} ^^ Space missing inside empty braces. RUBY expect_correction(<<~RUBY) each { } RUBY end end context 'with invalid value for EnforcedStyleForEmptyBraces' do let(:cop_config) { { 'EnforcedStyleForEmptyBraces' => 'unknown' } } it 'fails with an error' do expect { expect_no_offenses('each { }') } .to raise_error('Unknown EnforcedStyleForEmptyBraces selected!') end end context 'Ruby >= 2.7', :ruby27 do it 'registers an offense for numblocks without inner space' do expect_offense(<<~RUBY) [1, 2, 3].each {_1 * 2} ^ Space missing inside {. ^ Space missing inside }. RUBY expect_correction(<<~RUBY) [1, 2, 3].each { _1 * 2 } RUBY end end context 'Ruby >= 3.4', :ruby34 do it 'registers an offense for itblocks without inner space' do expect_offense(<<~RUBY) [1, 2, 3].each {it * 2} ^ Space missing inside {. ^ Space missing inside }. RUBY expect_correction(<<~RUBY) [1, 2, 3].each { it * 2 } RUBY end end it 'accepts braces surrounded by spaces' do expect_no_offenses('each { puts }') end it 'accepts left brace without outer space' do expect_no_offenses('each{ puts }') end it 'registers an offense and corrects left brace without inner space' do expect_offense(<<~RUBY) each {puts } ^ Space missing inside {. RUBY expect_correction(<<~RUBY) each { puts } RUBY end it 'registers an offense and corrects right brace without inner space' do expect_offense(<<~RUBY) each { puts} ^ Space missing inside }. RUBY expect_correction(<<~RUBY) each { puts } RUBY end it 'registers an offense and corrects both left and right brace without inner space after success' do expect_offense(<<~RUBY) each { puts } each {puts} ^ Space missing inside {. ^ Space missing inside }. RUBY expect_correction(<<~RUBY) each { puts } each { puts } RUBY end it 'registers offenses and correct both braces without inner space' do expect_offense(<<~RUBY) a {} b { } ^ Space inside empty braces detected. each {puts} ^ Space missing inside {. ^ Space missing inside }. RUBY expect_correction(<<~RUBY) a {} b {} each { puts } RUBY end context 'with passed in parameters' do context 'for single-line blocks' do it 'accepts left brace with inner space' do expect_no_offenses('each { |x| puts }') end it 'registers an offense and corrects left brace without inner space' do expect_offense(<<~RUBY) each {|x| puts } ^^ Space between { and | missing. RUBY expect_correction(<<~RUBY) each { |x| puts } RUBY end end context 'for multi-line blocks' do it 'accepts left brace with inner space' do expect_no_offenses(<<~RUBY) each { |x| puts } RUBY end it 'registers an offense and corrects left brace without inner space' do expect_offense(<<~RUBY) each {|x| ^^ Space between { and | missing. puts } RUBY expect_correction(<<~RUBY) each { |x| puts } RUBY end end it 'accepts new lambda syntax' do expect_no_offenses('->(x) { x }') end context 'and BlockDelimiters cop enabled' do let(:config) do RuboCop::Config.new('Style/BlockDelimiters' => { 'Enabled' => true }, 'Layout/SpaceInsideBlockBraces' => cop_config) end it 'registers an offense and corrects for single-line blocks' do expect_offense(<<~RUBY) each {|x| puts} ^ Space missing inside }. ^^ Space between { and | missing. RUBY expect_correction(<<~RUBY) each { |x| puts } RUBY end it 'registers an offense and corrects multi-line blocks' do expect_offense(<<~RUBY) each {|x| ^^ Space between { and | missing. puts } RUBY expect_correction(<<~RUBY) each { |x| puts } RUBY end end context 'and space before block parameters not allowed' do let(:cop_config) do { 'EnforcedStyle' => 'space', 'SupportedStyles' => supported_styles, 'SpaceBeforeBlockParameters' => false } end it 'registers an offense and corrects left brace with inner space' do expect_offense(<<~RUBY) each { |x| puts } ^ Space between { and | detected. RUBY expect_correction(<<~RUBY) each {|x| puts } RUBY end it 'accepts new lambda syntax' do expect_no_offenses('->(x) { x }') end it 'accepts left brace without inner space' do expect_no_offenses('each {|x| puts }') end end end context 'configured with no_space' do let(:cop_config) do { 'EnforcedStyle' => 'no_space', 'SupportedStyles' => supported_styles, 'SpaceBeforeBlockParameters' => true } end it 'accepts braces without spaces inside' do expect_no_offenses('each {puts}') end it 'registers an offense and corrects left brace with inner space' do expect_offense(<<~RUBY) each { puts} ^ Space inside { detected. RUBY expect_correction(<<~RUBY) each {puts} RUBY end it 'registers an offense and corrects right brace with inner space' do expect_offense(<<~RUBY) each {puts } ^ Space inside } detected. RUBY expect_correction(<<~RUBY) each {puts} RUBY end it 'registers an offense and corrects both left and right brace with inner space after success' do expect_offense(<<~RUBY) each {puts} each { puts } ^ Space inside { detected. ^ Space inside } detected. RUBY expect_correction(<<~RUBY) each {puts} each {puts} RUBY end it 'accepts left brace without outer space' do expect_no_offenses('each{puts}') end it 'accepts when a method call with a multiline block is used as an argument' do expect_no_offenses(<<~RUBY) foo bar { |arg| baz(arg) } RUBY end context 'with passed in parameters' do context 'and space before block parameters allowed' do it 'accepts left brace with inner space' do expect_no_offenses('each { |x| puts}') end it 'registers an offense and corrects left brace without inner space' do expect_offense(<<~RUBY) each {|x| puts} ^^ Space between { and | missing. RUBY expect_correction(<<~RUBY) each { |x| puts} RUBY end it 'accepts new lambda syntax' do expect_no_offenses('->(x) {x}') end end context 'and space before block parameters not allowed' do let(:cop_config) do { 'EnforcedStyle' => 'no_space', 'SupportedStyles' => supported_styles, 'SpaceBeforeBlockParameters' => false } end it 'registers an offense and corrects left brace with inner space' do expect_offense(<<~RUBY) each { |x| puts} ^ Space between { and | detected. RUBY expect_correction(<<~RUBY) each {|x| puts} RUBY end it 'accepts new lambda syntax' do expect_no_offenses('->(x) {x}') end it 'accepts when braces are aligned in multiline block' do expect_no_offenses(<<~RUBY) items.map {|item| item.do_something } RUBY end it 'registers an offense when braces are not aligned in multiline block' do expect_offense(<<~RUBY) items.map {|item| item.do_something } ^^ Space inside } detected. RUBY expect_correction(<<~RUBY) items.map {|item| item.do_something } RUBY end it 'accepts when braces are aligned in multiline block with bracket' do expect_no_offenses(<<~RUBY) foo {[ bar ]} RUBY end it 'registers an offense when braces are not aligned in multiline block with bracket' do expect_offense(<<~RUBY) foo {[ bar ]} ^^ Space inside } detected. RUBY expect_correction(<<~RUBY) foo {[ bar ]} RUBY end end end end end
ruby
MIT
99fa0fdd0481910d7d052f4c2ed01ad36178f404
2026-01-04T15:37:41.211519Z
false
rubocop/rubocop
https://github.com/rubocop/rubocop/blob/99fa0fdd0481910d7d052f4c2ed01ad36178f404/spec/rubocop/cop/layout/empty_lines_around_class_body_spec.rb
spec/rubocop/cop/layout/empty_lines_around_class_body_spec.rb
# frozen_string_literal: true RSpec.describe RuboCop::Cop::Layout::EmptyLinesAroundClassBody, :config do let(:extra_begin) { 'Extra empty line detected at class body beginning.' } let(:extra_end) { 'Extra empty line detected at class body end.' } let(:missing_begin) { 'Empty line missing at class body beginning.' } let(:missing_end) { 'Empty line missing at class body end.' } let(:missing_def) { 'Empty line missing before first def definition' } let(:missing_type) { 'Empty line missing before first class definition' } context 'when EnforcedStyle is no_empty_lines' do let(:cop_config) { { 'EnforcedStyle' => 'no_empty_lines' } } it 'registers an offense for class body starting with a blank' do expect_offense(<<~RUBY) class SomeClass ^{} #{extra_begin} do_something end RUBY expect_correction(<<~RUBY) class SomeClass do_something end RUBY end it 'registers an offense for class body ending with a blank' do expect_offense(<<~RUBY) class SomeClass do_something ^{} #{extra_end} end RUBY expect_correction(<<~RUBY) class SomeClass do_something end RUBY end it 'autocorrects singleton class body containing only a blank' do expect_offense(<<~RUBY) class << self ^{} #{extra_begin} end RUBY expect_correction(<<~RUBY) class << self end RUBY end it 'registers an offense for singleton class body ending with a blank' do expect_offense(<<~RUBY) class << self do_something ^{} #{extra_end} end RUBY expect_correction(<<~RUBY) class << self do_something end RUBY end it 'registers an offense when a class body starts with a blank line and defines a multiline superclass' do expect_offense(<<~RUBY) class SomeClass < Struct.new( :attr, keyword_init: true ) ^{} #{extra_begin} do_something end RUBY expect_correction(<<~RUBY) class SomeClass < Struct.new( :attr, keyword_init: true ) do_something end RUBY end end context 'when EnforcedStyle is empty_lines' do let(:cop_config) { { 'EnforcedStyle' => 'empty_lines' } } it 'does not register offenses' do expect_no_offenses(<<~RUBY) class Foo def do_something end end RUBY end it 'does not register offenses when specifying a superclass that breaks the line' do expect_no_offenses(<<~RUBY) class Foo < Bar def do_something end end RUBY end it 'registers an offense for class body not starting or ending with a blank' do expect_offense(<<~RUBY) class SomeClass do_something ^ #{missing_begin} end ^ #{missing_end} RUBY expect_correction(<<~RUBY) class SomeClass do_something end RUBY end it 'accepts classes with an empty body' do expect_no_offenses("class SomeClass\nend") end it 'registers an offense for singleton class body not starting or ending with a blank' do expect_offense(<<~RUBY) class << self do_something ^ #{missing_begin} end ^ #{missing_end} RUBY expect_correction(<<~RUBY) class << self do_something end RUBY end it 'accepts singleton classes with an empty body' do expect_no_offenses("class << self\nend") end end context 'when EnforcedStyle is empty_lines_except_namespace' do let(:cop_config) { { 'EnforcedStyle' => 'empty_lines_except_namespace' } } context 'when only child is class' do it 'requires no empty lines for namespace' do expect_no_offenses(<<~RUBY) class Parent < Base class Child do_something end end RUBY end it 'registers an offense for namespace body starting with a blank' do expect_offense(<<~RUBY) class Parent ^{} #{extra_begin} class Child do_something end end RUBY end it 'registers an offense for namespace body ending with a blank' do expect_offense(<<~RUBY) class Parent class Child do_something end ^{} #{extra_end} end RUBY end it 'registers offenses for namespaced class body not starting with a blank' do expect_offense(<<~RUBY) class Parent class Child do_something ^ #{missing_begin} end end RUBY end it 'registers offenses for namespaced class body not ending with a blank' do expect_offense(<<~RUBY) class Parent class Child do_something end ^ #{missing_end} end RUBY end it 'autocorrects beginning and end' do expect_offense(<<~RUBY) class Parent < Base ^{} #{extra_begin} class Child do_something ^ #{missing_begin} end ^ #{missing_end} ^{} #{extra_end} end RUBY expect_correction(<<~RUBY) class Parent < Base class Child do_something end end RUBY end end context 'when only child is module' do it 'requires no empty lines for namespace' do expect_no_offenses(<<~RUBY) class Parent module Child do_something end end RUBY end it 'registers an offense for namespace body starting with a blank' do expect_offense(<<~RUBY) class Parent ^{} #{extra_begin} module Child do_something end end RUBY end it 'registers an offense for namespace body ending with a blank' do expect_offense(<<~RUBY) class Parent module Child do_something end ^{} #{extra_end} end RUBY end end context 'when has multiple child classes' do it 'requires empty lines for namespace' do expect_no_offenses(<<~RUBY) class Parent class Mom do_something end class Dad end end RUBY end it 'registers offenses for namespace body starting and ending without a blank' do expect_offense(<<~RUBY) class Parent class Mom ^ #{missing_begin} do_something ^ #{missing_begin} end ^ #{missing_end} class Dad end end ^ #{missing_end} RUBY end end end context 'when EnforcedStyle is beginning_only' do let(:cop_config) { { 'EnforcedStyle' => 'beginning_only' } } it 'ignores empty lines at the beginning of a class' do expect_no_offenses(<<~RUBY) class SomeClass do_something end RUBY end it 'registers an offense for an empty line at the end of a class' do expect_offense(<<~RUBY) class SomeClass do_something ^{} #{extra_end} end RUBY expect_correction(<<~RUBY) class SomeClass do_something end RUBY end end context 'when EnforcedStyle is ending_only' do let(:cop_config) { { 'EnforcedStyle' => 'ending_only' } } it 'ignores empty lines at the beginning of a class' do expect_no_offenses(<<~RUBY) class SomeClass do_something end RUBY end it 'registers an offense for an empty line at the end of a class' do expect_offense(<<~RUBY) class SomeClass ^{} #{extra_begin} do_something end RUBY expect_correction(<<~RUBY) class SomeClass do_something end RUBY end end it_behaves_like 'empty_lines_around_class_or_module_body', 'class' end
ruby
MIT
99fa0fdd0481910d7d052f4c2ed01ad36178f404
2026-01-04T15:37:41.211519Z
false
rubocop/rubocop
https://github.com/rubocop/rubocop/blob/99fa0fdd0481910d7d052f4c2ed01ad36178f404/spec/rubocop/cop/layout/condition_position_spec.rb
spec/rubocop/cop/layout/condition_position_spec.rb
# frozen_string_literal: true RSpec.describe RuboCop::Cop::Layout::ConditionPosition, :config do %w[if unless while until].each do |keyword| it 'registers an offense and corrects for condition on the next line' do expect_offense(<<~RUBY) #{keyword} x == 10 ^^^^^^^ Place the condition on the same line as `#{keyword}`. end RUBY expect_correction(<<~RUBY) #{keyword} x == 10 end RUBY end it 'accepts condition on the same line' do expect_no_offenses(<<~RUBY) #{keyword} x == 10 bala end RUBY end it 'accepts condition on a different line for modifiers' do expect_no_offenses(<<~RUBY) do_something #{keyword} something && something_else RUBY end end it 'registers an offense and corrects for elsif condition on the next line' do expect_offense(<<~RUBY) if something test elsif something ^^^^^^^^^ Place the condition on the same line as `elsif`. test end RUBY expect_correction(<<~RUBY) if something test elsif something test end RUBY end it 'accepts ternary ops' do expect_no_offenses('x ? a : b') end end
ruby
MIT
99fa0fdd0481910d7d052f4c2ed01ad36178f404
2026-01-04T15:37:41.211519Z
false
rubocop/rubocop
https://github.com/rubocop/rubocop/blob/99fa0fdd0481910d7d052f4c2ed01ad36178f404/spec/rubocop/cop/layout/space_before_semicolon_spec.rb
spec/rubocop/cop/layout/space_before_semicolon_spec.rb
# frozen_string_literal: true RSpec.describe RuboCop::Cop::Layout::SpaceBeforeSemicolon, :config do let(:config) { RuboCop::Config.new('Layout/SpaceInsideBlockBraces' => brace_config) } let(:brace_config) { {} } it 'registers an offense and corrects space before semicolon' do expect_offense(<<~RUBY) x = 1 ; y = 2 ^ Space found before semicolon. RUBY expect_correction(<<~RUBY) x = 1; y = 2 RUBY end it 'does not register an offense for no space before semicolons' do expect_no_offenses('x = 1; y = 2') end it 'registers an offense and corrects more than one space before a semicolon' do expect_offense(<<~RUBY) x = 1 ; y = 2 ^^ Space found before semicolon. RUBY expect_correction(<<~RUBY) x = 1; y = 2 RUBY end context 'inside block braces' do shared_examples 'common behavior' do it 'accepts no space between an opening brace and a semicolon' do expect_no_offenses('test {; }') end end context 'when EnforcedStyle for SpaceInsideBlockBraces is space' do let(:brace_config) { { 'Enabled' => true, 'EnforcedStyle' => 'space' } } it_behaves_like 'common behavior' it 'accepts a space between an opening brace and a semicolon' do expect_no_offenses('test { ; }') end it 'accepts a space between an opening lambda brace and a semicolon' do expect_no_offenses('-> { ; }') end end context 'when EnforcedStyle for SpaceInsideBlockBraces is no_space' do let(:brace_config) { { 'Enabled' => true, 'EnforcedStyle' => 'no_space' } } it_behaves_like 'common behavior' it 'registers an offense and corrects a space between an opening brace and a semicolon' do expect_offense(<<~RUBY) test { ; } ^ Space found before semicolon. RUBY expect_correction(<<~RUBY) test {; } RUBY end end end context 'heredocs' do it 'registers an offense and corrects' do expect_offense(<<~RUBY) <<~STR ; x = 1 ^ Space found before semicolon. text STR RUBY expect_correction(<<~RUBY) <<~STR; x = 1 text STR RUBY end end end
ruby
MIT
99fa0fdd0481910d7d052f4c2ed01ad36178f404
2026-01-04T15:37:41.211519Z
false
rubocop/rubocop
https://github.com/rubocop/rubocop/blob/99fa0fdd0481910d7d052f4c2ed01ad36178f404/spec/rubocop/cop/layout/case_indentation_spec.rb
spec/rubocop/cop/layout/case_indentation_spec.rb
# frozen_string_literal: true RSpec.describe RuboCop::Cop::Layout::CaseIndentation, :config do let(:config) do merged = RuboCop::ConfigLoader.default_configuration['Layout/CaseIndentation'].merge(cop_config) RuboCop::Config.new('Layout/CaseIndentation' => merged, 'Layout/IndentationWidth' => { 'Width' => 2 }) end context 'with EnforcedStyle: case' do context 'with IndentOneStep: false' do let(:cop_config) { { 'EnforcedStyle' => 'case', 'IndentOneStep' => false } } describe '`case` ... `when`' do context 'with everything on a single line' do it 'does not register an offense' do expect_no_offenses('case foo; when :bar then 1; else 0; end') end end context 'regarding assignment where the right hand side is a `case`' do it 'accepts a correctly indented assignment' do expect_no_offenses(<<~RUBY) output = case variable when 'value1' 'output1' else 'output2' end RUBY end it 'registers an offense and corrects assignment indented as end' do expect_offense(<<~RUBY) output = case variable when 'value1' ^^^^ Indent `when` as deep as `case`. 'output1' else 'output2' end RUBY expect_correction(<<~RUBY) output = case variable when 'value1' 'output1' else 'output2' end RUBY end it 'registers an offense and corrects assignment indented some other way' do expect_offense(<<~RUBY) output = case variable when 'value1' ^^^^ Indent `when` as deep as `case`. 'output1' else 'output2' end RUBY expect_correction(<<~RUBY) output = case variable when 'value1' 'output1' else 'output2' end RUBY end it 'registers an offense and corrects correct + opposite style' do expect_offense(<<~RUBY) output = case variable when 'value1' 'output1' else 'output2' end output = case variable when 'value1' ^^^^ Indent `when` as deep as `case`. 'output1' else 'output2' end RUBY expect_correction(<<~RUBY) output = case variable when 'value1' 'output1' else 'output2' end output = case variable when 'value1' 'output1' else 'output2' end RUBY end end it 'registers an offense and corrects a `when` clause that is indented deeper than `case`' do expect_offense(<<~RUBY) case a when 0 then return ^^^^ Indent `when` as deep as `case`. else case b when 1 then return ^^^^ Indent `when` as deep as `case`. end end RUBY expect_correction(<<~RUBY) case a when 0 then return else case b when 1 then return end end RUBY end it "accepts a `when` clause that's equally indented with `case`" do expect_no_offenses(<<~RUBY) y = case a when 0 then raise when 0 then return else z = case b when 1 then return when 1 then raise end end case c when 2 then encoding end RUBY end it "doesn't get confused by strings with `case` in them" do expect_no_offenses(<<~RUBY) a = "case" case x when 0 end RUBY end it "doesn't get confused by symbols named `case` or `when`" do expect_no_offenses(<<~RUBY) KEYWORDS = { :case => true, :when => true } case type when 0 ParameterNode when 1 MethodCallNode end RUBY end it 'accepts correctly indented whens in complex combinations' do expect_no_offenses(<<~RUBY) each { case state when 0 case name when :a end when 1 loop { case name when :b end } end } case s when Array end RUBY end end describe '`case` ... `in`', :ruby27 do context 'with everything on a single line' do it 'does not register an offense' do expect_no_offenses('case foo; in pattern then 1; else 0; end') end end context 'regarding assignment where the right hand side is a `case`' do it 'accepts a correctly indented assignment' do expect_no_offenses(<<~RUBY) output = case variable in pattern 'output1' else 'output2' end RUBY end it 'registers an offense and corrects assignment indented as `end`' do expect_offense(<<~RUBY) output = case variable in pattern ^^ Indent `in` as deep as `case`. 'output1' else 'output2' end RUBY expect_correction(<<~RUBY) output = case variable in pattern 'output1' else 'output2' end RUBY end it 'registers an offense and corrects assignment indented some other way' do expect_offense(<<~RUBY) output = case variable in pattern ^^ Indent `in` as deep as `case`. 'output1' else 'output2' end RUBY expect_correction(<<~RUBY) output = case variable in pattern 'output1' else 'output2' end RUBY end it 'registers an offense and corrects correct + opposite style' do expect_offense(<<~RUBY) output = case variable in pattern 'output1' else 'output2' end output = case variable in pattern ^^ Indent `in` as deep as `case`. 'output1' else 'output2' end RUBY expect_correction(<<~RUBY) output = case variable in pattern 'output1' else 'output2' end output = case variable in pattern 'output1' else 'output2' end RUBY end end it 'registers an offense and corrects an `in` clause that is indented deeper than `case`' do expect_offense(<<~RUBY) case a in 0 then return ^^ Indent `in` as deep as `case`. else case b in 1 then return ^^ Indent `in` as deep as `case`. end end RUBY expect_correction(<<~RUBY) case a in 0 then return else case b in 1 then return end end RUBY end it "accepts an `in` clause that's equally indented with `case`" do expect_no_offenses(<<~RUBY) y = case a in 0 then raise in 0 then return else z = case b in 1 then return in 1 then raise end end case c in 2 then encoding end RUBY end it "doesn't get confused by strings with `case` in them" do expect_no_offenses(<<~RUBY) a = "case" case x when 0 end RUBY end it "doesn't get confused by symbols named `case` or `in`" do expect_no_offenses(<<~RUBY) KEYWORDS = { :case => true, :in => true } case type in 0 ParameterNode in 1 MethodCallNode end RUBY end it 'accepts correctly indented whens in complex combinations' do expect_no_offenses(<<~RUBY) each { case state in 0 case name in :a end in 1 loop { case name in :b end } end } case s in Array end RUBY end end end context 'with IndentOneStep: true' do let(:cop_config) { { 'EnforcedStyle' => 'case', 'IndentOneStep' => true } } describe '`case` ... `when`' do context 'with everything on a single line' do it 'does not register an offense' do expect_no_offenses('case foo; when :bar then 1; else 0; end') end end context 'regarding assignment where the right hand side is a `case`' do it 'accepts a correctly indented assignment' do expect_no_offenses(<<~RUBY) output = case variable when 'value1' 'output1' else 'output2' end RUBY end it 'registers an offense and corrects an assignment indented some other way' do expect_offense(<<~RUBY) output = case variable when 'value1' ^^^^ Indent `when` one step more than `case`. 'output1' else 'output2' end RUBY expect_correction(<<~RUBY) output = case variable when 'value1' 'output1' else 'output2' end RUBY end end it "accepts a `when` clause that's 2 spaces deeper than `case`" do expect_no_offenses(<<~RUBY) case a when 0 then return else case b when 1 then return end end RUBY end it 'registers an offense and corrects a `when` clause that is equally indented with `case`' do expect_offense(<<~RUBY) y = case a when 0 then raise ^^^^ Indent `when` one step more than `case`. when 0 then return ^^^^ Indent `when` one step more than `case`. z = case b when 1 then return ^^^^ Indent `when` one step more than `case`. when 1 then raise ^^^^ Indent `when` one step more than `case`. end end case c when 2 then encoding ^^^^ Indent `when` one step more than `case`. end RUBY expect_correction(<<~RUBY) y = case a when 0 then raise when 0 then return z = case b when 1 then return when 1 then raise end end case c when 2 then encoding end RUBY end context 'when indentation width is overridden for this cop only' do let(:cop_config) do { 'EnforcedStyle' => 'case', 'IndentOneStep' => true, 'IndentationWidth' => 5 } end it 'respects cop-specific IndentationWidth' do expect_no_offenses(<<~RUBY) output = case variable when 'value1' 'output1' else 'output2' end RUBY end end end describe '`case` ... `in`', :ruby27 do context 'with everything on a single line' do it 'does not register an offense' do expect_no_offenses('case foo; in pattern then 1; else 0; end') end end context 'regarding assignment where the right hand side is a `case`' do it 'accepts a correctly indented assignment' do expect_no_offenses(<<~RUBY) output = case variable in pattern 'output1' else 'output2' end RUBY end it 'registers an offense and corrects an assignment indented some other way' do expect_offense(<<~RUBY) output = case variable in pattern ^^ Indent `in` one step more than `case`. 'output1' else 'output2' end RUBY expect_correction(<<~RUBY) output = case variable in pattern 'output1' else 'output2' end RUBY end end it "accepts an `in` clause that's 2 spaces deeper than `case`" do expect_no_offenses(<<~RUBY) case a in 0 then return else case b in 1 then return end end RUBY end it 'registers an offense and corrects an `in` clause that is equally indented with `case`' do expect_offense(<<~RUBY) y = case a in 0 then raise ^^ Indent `in` one step more than `case`. in 0 then return ^^ Indent `in` one step more than `case`. z = case b in 1 then return ^^ Indent `in` one step more than `case`. in 1 then raise ^^ Indent `in` one step more than `case`. end end case c in 2 then encoding ^^ Indent `in` one step more than `case`. end RUBY expect_correction(<<~RUBY) y = case a in 0 then raise in 0 then return z = case b in 1 then return in 1 then raise end end case c in 2 then encoding end RUBY end context 'when indentation width is overridden for this cop only' do let(:cop_config) do { 'EnforcedStyle' => 'case', 'IndentOneStep' => true, 'IndentationWidth' => 5 } end it 'respects cop-specific IndentationWidth' do expect_no_offenses(<<~RUBY) output = case variable in pattern 'output1' else 'output2' end RUBY end end end end end context 'with EnforcedStyle: end' do context 'with IndentOneStep: false' do let(:cop_config) { { 'EnforcedStyle' => 'end', 'IndentOneStep' => false } } describe '`case` ... `when`' do context 'with everything on a single line' do it 'does not register an offense' do expect_no_offenses('case foo; when :bar then 1; else 0; end') end end context '`else` and `end` same line' do it 'does not register an offense' do expect_no_offenses(<<~RUBY) case variable when 'value1' when 'value2' else 'value3' end RUBY end end context '`when` and `end` same line' do it 'does not register an offense' do expect_no_offenses(<<~RUBY) case variable when 'value1' then 'then1' when 'value2' then 'then2' end RUBY end end context 'regarding assignment where the right hand side is a `case`' do it 'accepts a correctly indented assignment' do expect_no_offenses(<<~RUBY) output = case variable when 'value1' 'output1' else 'output2' end RUBY end it 'registers an offense and corrects an assignment indented some other way' do expect_offense(<<~RUBY) output = case variable when 'value1' ^^^^ Indent `when` as deep as `end`. 'output1' else 'output2' end RUBY expect_correction(<<~RUBY) output = case variable when 'value1' 'output1' else 'output2' end RUBY end end end describe '`case` ... `in`', :ruby27 do context 'with everything on a single line' do it 'does not register an offense' do expect_no_offenses('case foo; in pattern then 1; else 0; end') end end context '`in` and `end` same line' do it 'does not register an offense' do expect_no_offenses(<<~RUBY) case variable in pattern then 'output1' in pattern then 'output2' end RUBY end end context 'regarding assignment where the right hand side is a `case`' do it 'accepts a correctly indented assignment' do expect_no_offenses(<<~RUBY) output = case variable in pattern 'output1' else 'output2' end RUBY end it 'registers an offense and corrects an assignment indented some other way' do expect_offense(<<~RUBY) output = case variable in pattern ^^ Indent `in` as deep as `end`. 'output1' else 'output2' end RUBY expect_correction(<<~RUBY) output = case variable in pattern 'output1' else 'output2' end RUBY end end end end context 'with IndentOneStep: true' do let(:cop_config) { { 'EnforcedStyle' => 'end', 'IndentOneStep' => true } } describe '`case` ... `when`' do context 'with everything on a single line' do it 'does not register an offense' do expect_no_offenses('case foo; when :bar then 1; else 0; end') end end context 'regarding assignment where the right hand side is a `case`' do it 'accepts a correctly indented assignment' do expect_no_offenses(<<~RUBY) output = case variable when 'value1' 'output1' else 'output2' end RUBY end it 'registers an offense and corrects an assignment indented as `case`' do expect_offense(<<~RUBY) output = case variable when 'value1' ^^^^ Indent `when` one step more than `end`. 'output1' else 'output2' end RUBY expect_correction(<<~RUBY) output = case variable when 'value1' 'output1' else 'output2' end RUBY end it 'registers an offense and corrects an assignment indented some other way' do expect_offense(<<~RUBY) output = case variable when 'value1' ^^^^ Indent `when` one step more than `end`. 'output1' else 'output2' end RUBY expect_correction(<<~RUBY) output = case variable when 'value1' 'output1' else 'output2' end RUBY end end end describe '`case` ... `in`', :ruby27 do context 'with everything on a single line' do it 'does not register an offense' do expect_no_offenses('case foo; in pattern then 1; else 0; end') end end context 'regarding assignment where the right hand side is a `case`' do it 'accepts a correctly indented assignment' do expect_no_offenses(<<~RUBY) output = case variable in pattern 'output1' else 'output2' end RUBY end it 'registers an offense and corrects an assignment indented as `case`' do expect_offense(<<~RUBY) output = case variable in pattern ^^ Indent `in` one step more than `end`. 'output1' else 'output2' end RUBY expect_correction(<<~RUBY) output = case variable in pattern 'output1' else 'output2' end RUBY end it 'registers an offense and corrects an assignment indented some other way' do expect_offense(<<~RUBY) output = case variable in pattern ^^ Indent `in` one step more than `end`. 'output1' else 'output2' end RUBY expect_correction(<<~RUBY) output = case variable in pattern 'output1' else 'output2' end RUBY end end end end end context 'when `when` is on the same line as `case`' do let(:cop_config) { {} } it 'registers an offense but does not autocorrect' do expect_offense(<<~RUBY) case test when something ^^^^ Indent `when` as deep as `case`. end RUBY expect_no_corrections end end end
ruby
MIT
99fa0fdd0481910d7d052f4c2ed01ad36178f404
2026-01-04T15:37:41.211519Z
false
rubocop/rubocop
https://github.com/rubocop/rubocop/blob/99fa0fdd0481910d7d052f4c2ed01ad36178f404/spec/rubocop/cop/layout/extra_spacing_spec.rb
spec/rubocop/cop/layout/extra_spacing_spec.rb
# frozen_string_literal: true RSpec.describe RuboCop::Cop::Layout::ExtraSpacing, :config do shared_examples 'common behavior' do it 'registers an offense and corrects alignment with token not preceded by space' do # The = and the ( are on the same column, but this is not for alignment, # it's just a mistake. expect_offense(<<~RUBY) website("example.org") name = "Jill" ^^ Unnecessary spacing detected. RUBY expect_correction(<<~RUBY) website("example.org") name = "Jill" RUBY end it 'accepts aligned values of an implicit hash literal' do expect_no_offenses(<<~RUBY) register(street1: '1 Market', street2: '#200', :city => 'Some Town', state: 'CA', postal_code:'99999-1111') RUBY end it 'accepts space between key and value in a hash with hash rockets' do expect_no_offenses(<<~RUBY) ospf_h = { 'ospfTest' => { 'foo' => { area: '0.0.0.0', cost: 10, hello: 30, pass: true }, 'longname' => { area: '1.1.1.38', pass: false }, 'vlan101' => { area: '2.2.2.101', cost: 5, hello: 20, pass: true } }, 'TestOspfInt' => { 'x' => { area: '0.0.0.19' }, 'vlan290' => { area: '2.2.2.29', cost: 200, hello: 30, pass: true }, 'port-channel100' => { area: '3.2.2.29', cost: 25, hello: 50, pass: false } } } RUBY end context 'when spaces are present in a single-line hash literal' do it 'registers an offense and corrects hashes with symbol keys' do expect_offense(<<~RUBY) hash = {a: 1, b: 2} ^^ Unnecessary spacing detected. ^ Unnecessary spacing detected. ^^^ Unnecessary spacing detected. RUBY expect_correction(<<~RUBY) hash = {a: 1, b: 2} RUBY end it 'registers an offense and corrects hashes with hash rockets' do expect_offense(<<~RUBY) let(:single_line_hash) { {"a" => "1", "b" => "2"} ^^ Unnecessary spacing detected. } RUBY expect_correction(<<~RUBY) let(:single_line_hash) { {"a" => "1", "b" => "2"} } RUBY end end it 'registers an offense and corrects extra space before a float' do expect_offense(<<~RUBY) {:a => "a", :b => [nil, 2.5]} ^ Unnecessary spacing detected. RUBY expect_correction(<<~RUBY) {:a => "a", :b => [nil, 2.5]} RUBY end it 'registers an offense and corrects extra spacing before a unary plus in an argument list' do expect_offense(<<~RUBY) assert_difference(MyModel.count, +2, 3, +3, # Extra spacing only here. ^ Unnecessary spacing detected. 4,+4) RUBY expect_correction(<<~RUBY) assert_difference(MyModel.count, +2, 3, +3, # Extra spacing only here. 4,+4) RUBY end it 'registers an offense and corrects double extra spacing in variable assignment' do expect_offense(<<~RUBY) m = "hello" ^^^ Unnecessary spacing detected. RUBY expect_correction(<<~RUBY) m = "hello" RUBY end it 'ignores whitespace at the beginning of the line' do expect_no_offenses(' m = "hello"') end it 'ignores whitespace inside a string' do expect_no_offenses('m = "hello this"') end it 'ignores trailing whitespace' do expect_no_offenses([' class Benchmarker < Performer ', ' end'].join("\n")) end it 'registers an offense and corrects extra spacing in class inheritance' do expect_offense(<<~RUBY) class A < String ^^ Unnecessary spacing detected. end RUBY expect_correction(<<~RUBY) class A < String end RUBY end end sources = { 'lining up assignments' => <<~RUBY, website = "example.org" name = "Jill" ^^^ Unnecessary spacing detected. RUBY 'lining up assignments with empty lines and comments in between' => <<~RUBY, a += 1 ^^ Unnecessary spacing detected. # Comment aa = 2 ^^ Unnecessary spacing detected. bb = 3 ^^ Unnecessary spacing detected. a ||= 1 ^ Unnecessary spacing detected. RUBY 'aligning with the same character' => <<~RUBY, y, m = (year * 12 + (mon - 1) + n).divmod(12) m, = (m + 1) .divmod(1) ^^^^^^^^^^^^^^^^^^^ Unnecessary spacing detected. ^^ Unnecessary spacing detected. RUBY 'lining up different kinds of assignments' => <<~RUBY, type_name ||= value.class.name if value type_name = type_name.to_s if type_name ^^ Unnecessary spacing detected. ^^ Unnecessary spacing detected. type_name = value.class.name if value ^^^^ Unnecessary spacing detected. ^ Unnecessary spacing detected. type_name += type_name.to_s unless type_name ^^ Unnecessary spacing detected. a += 1 ^ Unnecessary spacing detected. aa -= 2 RUBY 'aligning comments on non-adjacent lines' => <<~RUBY, include_examples 'aligned', 'var = until', 'test' ^ Unnecessary spacing detected. ^^ Unnecessary spacing detected. include_examples 'unaligned', "var = if", 'test' ^^^^ Unnecessary spacing detected. RUBY 'aligning tokens with empty line between' => <<~RUBY, unless nochdir Dir.chdir "/" # Release old working directory. ^^^ Unnecessary spacing detected. end File.umask 0000 # Ensure sensible umask. ^^^ Unnecessary spacing detected. RUBY 'aligning long assignment expressions that include line breaks' => <<~RUBY, size_attribute_name = FactoryGirl.create(:attribute, ^^^ Unnecessary spacing detected. name: 'Size', values: %w{small large}) carrier_attribute_name = FactoryGirl.create(:attribute, name: 'Carrier', values: %w{verizon}) RUBY 'aligning = on lines where there are trailing comments' => <<~RUBY, a_long_var_name = 100 # this is 100 short_name1 = 2 ^^^^ Unnecessary spacing detected. clear short_name2 = 2 ^^^^ Unnecessary spacing detected. a_long_var_name = 100 # this is 100 clear short_name3 = 2 # this is 2 ^^^^ Unnecessary spacing detected. a_long_var_name = 100 # this is 100 RUBY 'aligning trailing comments' => <<~RUBY a_long_var_name = 2 # this is 2 ^^ Unnecessary spacing detected. a_long_var_name = 100 # this is 100 RUBY }.freeze context 'when AllowForAlignment is true' do let(:cop_config) { { 'AllowForAlignment' => true, 'ForceEqualSignAlignment' => false } } it_behaves_like 'common behavior' context 'with extra spacing for alignment purposes' do sources.each do |reason, src| context "such as #{reason}" do it 'allows it' do src_without_annotations = src.gsub(/^ +\^.+\n/, '') expect_no_offenses(src_without_annotations) end end end end it 'registers an offense and corrects when a character is vertically aligned' do expect_offense(<<~RUBY) d_is_vertically_aligned do ^ Unnecessary spacing detected. _______________________d end RUBY expect_correction(<<~RUBY) d_is_vertically_aligned do _______________________d end RUBY end end context 'when AllowForAlignment is false' do let(:cop_config) { { 'AllowForAlignment' => false, 'ForceEqualSignAlignment' => false } } it_behaves_like 'common behavior' context 'with extra spacing for alignment purposes' do sources.each do |reason, src| context "such as #{reason}" do it 'registers offense(s)' do expect_offense(src) end end end end end context 'when AllowBeforeTrailingComments is' do let(:allow_alignment) { false } let(:cop_config) do { 'AllowForAlignment' => allow_alignment, 'AllowBeforeTrailingComments' => allow_comments } end context 'true' do let(:allow_comments) { true } it 'allows it' do expect_no_offenses(<<~RUBY) object.method(argument) # this is a comment RUBY end context "doesn't interfere with AllowForAlignment" do context 'being true' do let(:allow_alignment) { true } sources.each do |reason, src| context "such as #{reason}" do it 'allows it' do src_without_annotations = src.gsub(/^ +\^.+\n/, '') expect_no_offenses(src_without_annotations) end end end end context 'being false' do sources.each do |reason, src| context "such as #{reason}" do # In these specific test cases, the extra space in question # is to align comments, so it would be allowed by EITHER ONE # being true. Yes, that means technically it interferes a bit, # but specifically in the way it was intended to. if ['aligning tokens with empty line between', 'aligning trailing comments'].include?(reason) it 'does not register an offense' do src_without_annotations = src.gsub(/^ +\^.+\n/, '') expect_no_offenses(src_without_annotations) end else it 'registers offense(s)' do expect_offense(src) end end end end end end end context 'false' do let(:allow_comments) { false } it 'registers an offense' do expect_offense(<<~RUBY) object.method(argument) # this is a comment ^ Unnecessary spacing detected. RUBY end it 'does not trigger on only one space before comment' do expect_no_offenses(<<~RUBY) object.method(argument) # this is a comment RUBY end end end context 'when ForceEqualSignAlignment is true' do let(:cop_config) { { 'AllowForAlignment' => true, 'ForceEqualSignAlignment' => true } } it 'does not register offenses for multiple complex nested assignments' do expect_no_offenses(<<~RUBY) def batch @areas = params[:param].map { var_1 = 123_456 variable_2 = 456_123 } @another = params[:param].map { char_1 = begin variable_1_1 = 'a' variable_1_20 = 'b' variable_1_300 = 'c' # A Comment variable_1_4000 = 'd' variable_1_50000 = 'e' puts 'a non-assignment statement without a blank line' some_other_length_variable = 'f' end var_2 = 456_123 } render json: @areas end RUBY end it 'does not register an offense if assignments are separated by blanks' do expect_no_offenses(<<~RUBY) a = 1 bb = 2 ccc = 3 RUBY end it 'does not register an offense if assignments are aligned' do expect_no_offenses(<<~RUBY) a = 1 bb = 2 ccc = 3 RUBY end it 'aligns the first assignment with the following assignment' do expect_no_offenses(<<~RUBY) # comment a = 1 bb = 2 RUBY end it 'does not register alignment errors on outdented lines' do expect_no_offenses(<<~RUBY) @areas = params[:param].map do |ca_params| ca_params = ActionController::Parameters.new(stuff) end RUBY end it 'registers an offense and corrects consecutive assignments that are not aligned' do expect_offense(<<~RUBY) a = 1 bb = 2 ^ `=` is not aligned with the preceding assignment. ccc = 3 ^ `=` is not aligned with the preceding assignment. abcde = 1 a = 2 ^ `=` is not aligned with the preceding assignment. abc = 3 ^ `=` is not aligned with the preceding assignment. RUBY expect_correction(<<~RUBY) a = 1 bb = 2 ccc = 3 abcde = 1 a = 2 abc = 3 RUBY end it 'registers offenses and correct consecutive operator assignments which are not aligned' do expect_offense(<<~RUBY) a += 1 bb = 2 ccc <<= 3 ^^^ `=` is not aligned with the preceding assignment. abcde = 1 a *= 2 ^^ `=` is not aligned with the preceding assignment. abc ||= 3 ^^^ `=` is not aligned with the preceding assignment. RUBY expect_correction(<<~RUBY) a += 1 bb = 2 ccc <<= 3 abcde = 1 a *= 2 abc ||= 3 RUBY end it 'registers an offense and corrects consecutive aref assignments which are not aligned' do expect_offense(<<~RUBY) a[1] = 1 bb[2,3] = 2 ^ `=` is not aligned with the preceding assignment. ccc[:key] = 3 ^ `=` is not aligned with the preceding assignment. abcde[0] = 1 a = 2 ^ `=` is not aligned with the preceding assignment. abc += 3 ^^ `=` is not aligned with the preceding assignment. RUBY expect_correction(<<~RUBY) a[1] = 1 bb[2,3] = 2 ccc[:key] = 3 abcde[0] = 1 a = 2 abc += 3 RUBY end it 'registers offenses and correct consecutive attribute assignments which are not aligned' do expect_offense(<<~RUBY) a.attr = 1 bb &&= 2 ^^^ `=` is not aligned with the preceding assignment. ccc.s = 3 ^ `=` is not aligned with the preceding assignment. abcde.blah = 1 a.attribute_name = 2 ^ `=` is not aligned with the preceding assignment. abc[1] = 3 ^ `=` is not aligned with the preceding assignment. RUBY expect_correction(<<~RUBY) a.attr = 1 bb &&= 2 ccc.s = 3 abcde.blah = 1 a.attribute_name = 2 abc[1] = 3 RUBY end it 'registers offenses and correct complex nested assignments' do expect_offense(<<~RUBY) def batch @areas = params[:param].map { var_1 = 123_456 variable_2 = 456_123 } ^ `=` is not aligned with the preceding assignment. @another = params[:param].map { ^ `=` is not aligned with the preceding assignment. char_1 = begin variable_1_1 = 'a' variable_1_20 = 'b' ^ `=` is not aligned with the preceding assignment. variable_1_300 = 'c' # A Comment variable_1_4000 = 'd' ^ `=` is not aligned with the preceding assignment. variable_1_50000 = 'e' puts 'a non-assignment statement without a blank line' some_other_length_variable = 'f' ^ `=` is not aligned with the preceding assignment. end var_2 = 456_123 } ^ `=` is not aligned with the preceding assignment. render json: @areas end RUBY expect_correction(<<~RUBY, loop: false) def batch @areas = params[:param].map { var_1 = 123_456 variable_2 = 456_123 } @another = params[:param].map { char_1 = begin variable_1_1 = 'a' variable_1_20 = 'b' variable_1_300 = 'c' # A Comment variable_1_4000 = 'd' variable_1_50000 = 'e' puts 'a non-assignment statement without a blank line' some_other_length_variable = 'f' end var_2 = 456_123 } render json: @areas end RUBY end it 'does not register an offense when optarg equals is not aligned with ' \ 'assignment equals sign' do expect_no_offenses(<<~RUBY) def method(arg = 1) var = arg end RUBY end it 'registers an offense and corrects when there is = in a string after assignment' do expect_offense(<<~RUBY) e, f = val.split('=') opt.ssh_config[e] = f ^ `=` is not aligned with the preceding assignment. RUBY expect_correction(<<~RUBY) e, f = val.split('=') opt.ssh_config[e] = f RUBY end it 'registers an offense and corrects with op-asgn when there is = in a string after assignment' do expect_offense(<<~RUBY) xy &&= val.split('=') opt.ssh_config[e] = f ^ `=` is not aligned with the preceding assignment. RUBY expect_correction(<<~RUBY) xy &&= val.split('=') opt.ssh_config[e] = f RUBY end context 'endless methods', :ruby30 do it 'does not register an offense when not aligned' do expect_no_offenses(<<~RUBY) def deleted = do_something def updated = do_something def added = do_something RUBY end it 'does not register an offense with optional values' do expect_no_offenses(<<~RUBY) def deleted(x = true) = do_something(x) def updated(x = true) = do_something(x) def added(x = true) = do_something(x) RUBY end end end context 'when exactly two comments have extra spaces' do context 'and they are aligned' do it 'does not register an offense' do expect_no_offenses(<<~RUBY) one # comment one two # comment two RUBY end end context 'and they are not aligned' do it 'registers an offense' do expect_offense(<<~RUBY) one # comment one ^ Unnecessary spacing detected. two # comment two ^^ Unnecessary spacing detected. RUBY expect_correction(<<~RUBY) one # comment one two # comment two RUBY end end end context 'when multiple comments have extra spaces' do it 'registers offenses for all comments' do expect_offense(<<~RUBY) class Foo def require(p) # rubocop:disable Naming/MethodParameterName ^ Unnecessary spacing detected. end def load(p) # rubocop:disable Naming/MethodParameterName ^ Unnecessary spacing detected. end def join(*ps) # rubocop:disable Naming/MethodParameterName ^ Unnecessary spacing detected. end def exist?(*ps) # rubocop:disable Naming/MethodParameterName ^ Unnecessary spacing detected. end end RUBY expect_correction(<<~RUBY) class Foo def require(p) # rubocop:disable Naming/MethodParameterName end def load(p) # rubocop:disable Naming/MethodParameterName end def join(*ps) # rubocop:disable Naming/MethodParameterName end def exist?(*ps) # rubocop:disable Naming/MethodParameterName end end RUBY end end end
ruby
MIT
99fa0fdd0481910d7d052f4c2ed01ad36178f404
2026-01-04T15:37:41.211519Z
false
rubocop/rubocop
https://github.com/rubocop/rubocop/blob/99fa0fdd0481910d7d052f4c2ed01ad36178f404/spec/rubocop/cop/layout/first_hash_element_line_break_spec.rb
spec/rubocop/cop/layout/first_hash_element_line_break_spec.rb
# frozen_string_literal: true RSpec.describe RuboCop::Cop::Layout::FirstHashElementLineBreak, :config do it 'registers an offense and corrects elements listed on the first line' do expect_offense(<<~RUBY) a = { a: 1, ^^^^ Add a line break before the first element of a multi-line hash. b: 2 } RUBY expect_correction(<<~RUBY) a = {#{trailing_whitespace} a: 1, b: 2 } RUBY end it 'registers an offense and corrects hash nested in a method call' do expect_offense(<<~RUBY) method({ foo: 1, ^^^^^^ Add a line break before the first element of a multi-line hash. bar: 2 }) RUBY expect_correction(<<~RUBY) method({#{trailing_whitespace} foo: 1, bar: 2 }) RUBY end it 'registers an offense and corrects single element multi-line hash' do expect_offense(<<~RUBY) { foo: { ^^^^^^ Add a line break before the first element of a multi-line hash. bar: 2, } } RUBY expect_correction(<<~RUBY) {#{trailing_whitespace} foo: { bar: 2, } } RUBY end it 'ignores implicit hashes in method calls with parens' do expect_no_offenses(<<~RUBY) method( foo: 1, bar: 2) RUBY end it 'ignores implicit hashes in method calls without parens' do expect_no_offenses(<<~RUBY) method foo: 1, bar: 2 RUBY end it 'ignores implicit hashes in method calls that are improperly formatted' do # These are covered by Style/FirstMethodArgumentLineBreak expect_no_offenses(<<~RUBY) method(foo: 1, bar: 2) RUBY end it 'ignores elements listed on a single line' do expect_no_offenses(<<~RUBY) b = { a: 1, b: 2 } RUBY end context 'last element can be multiline' do let(:cop_config) { { 'AllowMultilineFinalElement' => true } } it 'ignores last argument that is a multiline Hash' do expect_no_offenses(<<~RUBY) h = {a: b, c: { d: e }} RUBY end it 'ignores single value that is a multiline hash' do expect_no_offenses(<<~RUBY) h = {a: { b: c }} RUBY end it 'registers and corrects values that are multiline hashes and not the last value' do expect_offense(<<~RUBY) h = {a: b, c: { ^^^^ Add a line break before the first element of a multi-line hash. d: e, }, f: g} RUBY expect_correction(<<~RUBY) h = { a: b, c: { d: e, }, f: g} RUBY end it 'registers and corrects last value that starts on another line' do expect_offense(<<~RUBY) h = {a: b, c: d, ^^^^ Add a line break before the first element of a multi-line hash. e: f} RUBY expect_correction(<<~RUBY) h = { a: b, c: d, e: f} RUBY end end end
ruby
MIT
99fa0fdd0481910d7d052f4c2ed01ad36178f404
2026-01-04T15:37:41.211519Z
false
rubocop/rubocop
https://github.com/rubocop/rubocop/blob/99fa0fdd0481910d7d052f4c2ed01ad36178f404/spec/rubocop/cop/layout/space_around_method_call_operator_spec.rb
spec/rubocop/cop/layout/space_around_method_call_operator_spec.rb
# frozen_string_literal: true RSpec.describe RuboCop::Cop::Layout::SpaceAroundMethodCallOperator, :config do shared_examples 'offense' do |name, offense, correction| it "registers an offense and corrects when #{name}" do expect_offense(offense) expect_correction(correction) end end context 'dot operator' do it_behaves_like 'offense', 'space after method call', <<~OFFENSE, <<~CORRECTION foo. bar ^ Avoid using spaces around a method call operator. OFFENSE foo.bar CORRECTION it_behaves_like 'offense', 'space before method call', <<~OFFENSE, <<~CORRECTION foo .bar ^ Avoid using spaces around a method call operator. OFFENSE foo.bar CORRECTION it_behaves_like 'offense', 'spaces before method call', <<~OFFENSE, <<~CORRECTION foo .bar ^^ Avoid using spaces around a method call operator. OFFENSE foo.bar CORRECTION it_behaves_like 'offense', 'spaces after method call', <<~OFFENSE, <<~CORRECTION foo. bar ^^ Avoid using spaces around a method call operator. OFFENSE foo.bar CORRECTION it_behaves_like 'offense', 'spaces around method call', <<~OFFENSE, <<~CORRECTION foo . bar ^ Avoid using spaces around a method call operator. ^ Avoid using spaces around a method call operator. OFFENSE foo.bar CORRECTION it_behaves_like 'offense', 'spaces after `Proc#call` shorthand call', <<~OFFENSE, <<~CORRECTION foo. () ^ Avoid using spaces around a method call operator. OFFENSE foo.() CORRECTION context 'when multi line method call' do it_behaves_like 'offense', 'space before method call', <<~OFFENSE, <<~CORRECTION foo . bar ^ Avoid using spaces around a method call operator. OFFENSE foo .bar CORRECTION it_behaves_like 'offense', 'space before method call in suffix chaining', <<~OFFENSE, <<~CORRECTION foo . ^ Avoid using spaces around a method call operator. bar OFFENSE foo. bar CORRECTION it 'does not register an offense when no space after the `.`' do expect_no_offenses(<<~RUBY) foo .bar RUBY end end it 'does not register an offense when no space around method call' do expect_no_offenses(<<~RUBY) 'foo'.bar RUBY end it_behaves_like 'offense', 'space after last method call operator', <<~OFFENSE, <<~CORRECTION foo.bar. buzz ^ Avoid using spaces around a method call operator. OFFENSE foo.bar.buzz CORRECTION it_behaves_like 'offense', 'space after first method call operator', <<~OFFENSE, <<~CORRECTION foo. bar.buzz ^ Avoid using spaces around a method call operator. OFFENSE foo.bar.buzz CORRECTION it_behaves_like 'offense', 'space before first method call operator', <<~OFFENSE, <<~CORRECTION foo .bar.buzz ^ Avoid using spaces around a method call operator. OFFENSE foo.bar.buzz CORRECTION it_behaves_like 'offense', 'space before last method call operator', <<~OFFENSE, <<~CORRECTION foo.bar .buzz ^ Avoid using spaces around a method call operator. OFFENSE foo.bar.buzz CORRECTION it_behaves_like 'offense', 'space around intermediate method call operator', <<~OFFENSE, <<~CORRECTION foo.bar .buzz.bat ^ Avoid using spaces around a method call operator. OFFENSE foo.bar.buzz.bat CORRECTION it_behaves_like 'offense', 'space around multiple method call operator', <<~OFFENSE, <<~CORRECTION foo. bar. buzz.bat ^ Avoid using spaces around a method call operator. ^ Avoid using spaces around a method call operator. OFFENSE foo.bar.buzz.bat CORRECTION it 'does not register an offense when no space around any `.` operators' do expect_no_offenses(<<~RUBY) foo.bar.buzz RUBY end context 'when there is a space between `.` operator and a comment' do it 'does not register an offense when there is not a space before `.`' do expect_no_offenses(<<~RUBY) foo. # comment bar.baz RUBY end it 'registers an offense when there is a space before `.`' do expect_offense(<<~RUBY) foo . # comment ^ Avoid using spaces around a method call operator. bar.baz RUBY expect_correction(<<~RUBY) foo. # comment bar.baz RUBY end end end context 'safe navigation operator' do it_behaves_like 'offense', 'space after method call', <<~OFFENSE, <<~CORRECTION foo&. bar ^ Avoid using spaces around a method call operator. OFFENSE foo&.bar CORRECTION it_behaves_like 'offense', 'space before method call', <<~OFFENSE, <<~CORRECTION foo &.bar ^ Avoid using spaces around a method call operator. OFFENSE foo&.bar CORRECTION it_behaves_like 'offense', 'spaces before method call', <<~OFFENSE, <<~CORRECTION foo &.bar ^^ Avoid using spaces around a method call operator. OFFENSE foo&.bar CORRECTION it_behaves_like 'offense', 'spaces after method call', <<~OFFENSE, <<~CORRECTION foo&. bar ^^ Avoid using spaces around a method call operator. OFFENSE foo&.bar CORRECTION it_behaves_like 'offense', 'spaces around method call', <<~OFFENSE, <<~CORRECTION foo &. bar ^ Avoid using spaces around a method call operator. ^ Avoid using spaces around a method call operator. OFFENSE foo&.bar CORRECTION context 'when multi line method call' do it_behaves_like 'offense', 'space before method call', <<~OFFENSE, <<~CORRECTION foo &. bar ^ Avoid using spaces around a method call operator. OFFENSE foo &.bar CORRECTION it_behaves_like 'offense', 'space before method call in suffix chaining', <<~OFFENSE, <<~CORRECTION foo &. ^ Avoid using spaces around a method call operator. bar OFFENSE foo&. bar CORRECTION it 'does not register an offense when no space after the `&.`' do expect_no_offenses(<<~RUBY) foo &.bar RUBY end end it 'does not register an offense when no space around method call' do expect_no_offenses(<<~RUBY) foo&.bar RUBY end it_behaves_like 'offense', 'space after last method call operator', <<~OFFENSE, <<~CORRECTION foo&.bar&. buzz ^ Avoid using spaces around a method call operator. OFFENSE foo&.bar&.buzz CORRECTION it_behaves_like 'offense', 'space after first method call operator', <<~OFFENSE, <<~CORRECTION foo&. bar&.buzz ^ Avoid using spaces around a method call operator. OFFENSE foo&.bar&.buzz CORRECTION it_behaves_like 'offense', 'space before first method call operator', <<~OFFENSE, <<~CORRECTION foo &.bar&.buzz ^ Avoid using spaces around a method call operator. OFFENSE foo&.bar&.buzz CORRECTION it_behaves_like 'offense', 'space before last method call operator', <<~OFFENSE, <<~CORRECTION foo&.bar &.buzz ^ Avoid using spaces around a method call operator. OFFENSE foo&.bar&.buzz CORRECTION it_behaves_like 'offense', 'space around intermediate method call operator', <<~OFFENSE, <<~CORRECTION foo&.bar &.buzz&.bat ^ Avoid using spaces around a method call operator. OFFENSE foo&.bar&.buzz&.bat CORRECTION it_behaves_like 'offense', 'space around multiple method call operator', <<~OFFENSE, <<~CORRECTION foo&. bar&. buzz&.bat ^ Avoid using spaces around a method call operator. ^ Avoid using spaces around a method call operator. OFFENSE foo&.bar&.buzz&.bat CORRECTION it 'does not register an offense when no space around any `.` operators' do expect_no_offenses(<<~RUBY) foo&.bar&.buzz RUBY end end context ':: operator' do it_behaves_like 'offense', 'space after method call', <<~OFFENSE, <<~CORRECTION RuboCop:: Cop ^ Avoid using spaces around a method call operator. OFFENSE RuboCop::Cop CORRECTION it_behaves_like 'offense', 'spaces after method call', <<~OFFENSE, <<~CORRECTION RuboCop:: Cop ^^ Avoid using spaces around a method call operator. OFFENSE RuboCop::Cop CORRECTION context 'when multi line method call' do it_behaves_like 'offense', 'space before method call', <<~OFFENSE, <<~CORRECTION RuboCop :: Cop ^ Avoid using spaces around a method call operator. OFFENSE RuboCop ::Cop CORRECTION it 'does not register an offense when no space after the `::`' do expect_no_offenses(<<~RUBY) RuboCop ::Cop RUBY end end it 'does not register an offense when no space around method call' do expect_no_offenses(<<~RUBY) RuboCop::Cop RUBY end it_behaves_like 'offense', 'space after last method call operator', <<~OFFENSE, <<~CORRECTION RuboCop::Cop:: Cop ^ Avoid using spaces around a method call operator. OFFENSE RuboCop::Cop::Cop CORRECTION it_behaves_like 'offense', 'space around intermediate method call operator', <<~OFFENSE, <<~CORRECTION RuboCop::Cop:: Cop::Cop ^ Avoid using spaces around a method call operator. OFFENSE RuboCop::Cop::Cop::Cop CORRECTION it_behaves_like 'offense', 'space around multiple method call operator', <<~OFFENSE, <<~CORRECTION :: RuboCop:: Cop:: Cop::Cop ^ Avoid using spaces around a method call operator. ^ Avoid using spaces around a method call operator. ^ Avoid using spaces around a method call operator. OFFENSE ::RuboCop::Cop::Cop::Cop CORRECTION it_behaves_like 'offense', 'space after first operator with assignment', <<~OFFENSE, <<~CORRECTION klass = :: RuboCop::Cop ^ Avoid using spaces around a method call operator. OFFENSE klass = ::RuboCop::Cop CORRECTION it 'does not register an offense when no space around any `.` operators' do expect_no_offenses(<<~RUBY) RuboCop::Cop::Cop RUBY end it 'does not register an offense if no space before `::` operator with assignment' do expect_no_offenses(<<~RUBY) klass = ::RuboCop::Cop RUBY end it 'does not register an offense if no space before `::` operator with inheritance' do expect_no_offenses(<<~RUBY) class Test < ::RuboCop::Cop end RUBY end it 'does not register an offense if no space with conditionals' do expect_no_offenses(<<~RUBY) ::RuboCop::Cop || ::RuboCop RUBY end it_behaves_like 'offense', 'multiple spaces with assignment', <<~OFFENSE, <<~CORRECTION :: RuboCop:: Cop || :: RuboCop ^ Avoid using spaces around a method call operator. ^ Avoid using spaces around a method call operator. ^ Avoid using spaces around a method call operator. OFFENSE ::RuboCop::Cop || ::RuboCop CORRECTION end it 'does not register an offense when no method call operator' do expect_no_offenses(<<~RUBY) 'foo' + 'bar' RUBY end it 'does not register an offense when using `__ENCODING__`' do expect_no_offenses(<<~RUBY) __ENCODING__ RUBY end end
ruby
MIT
99fa0fdd0481910d7d052f4c2ed01ad36178f404
2026-01-04T15:37:41.211519Z
false
rubocop/rubocop
https://github.com/rubocop/rubocop/blob/99fa0fdd0481910d7d052f4c2ed01ad36178f404/spec/rubocop/cop/layout/assignment_indentation_spec.rb
spec/rubocop/cop/layout/assignment_indentation_spec.rb
# frozen_string_literal: true RSpec.describe RuboCop::Cop::Layout::AssignmentIndentation, :config do let(:config) do RuboCop::Config.new('Layout/AssignmentIndentation' => { 'IndentationWidth' => cop_indent }, 'Layout/IndentationWidth' => { 'Width' => 2 }) end let(:cop_indent) { nil } # use indentation with from Layout/IndentationWidth it 'registers an offense for incorrectly indented rhs' do expect_offense(<<~RUBY) a = if b ; end ^^^^^^^^^^ Indent the first line of the right-hand-side of a multi-line assignment. RUBY expect_correction(<<~RUBY) a = if b ; end RUBY end it 'allows assignments that do not start on a newline' do expect_no_offenses(<<~RUBY) a = if b foo end RUBY end it 'allows a properly indented rhs' do expect_no_offenses(<<~RUBY) a = if b ; end RUBY end it 'allows a properly indented rhs with fullwidth characters' do expect_no_offenses(<<~RUBY) f 'Ruby', a = b RUBY end it 'registers an offense for multi-lhs' do expect_offense(<<~RUBY) a, b = if b ; end ^^^^^^^^^^ Indent the first line of the right-hand-side of a multi-line assignment. RUBY expect_correction(<<~RUBY) a, b = if b ; end RUBY end it 'ignores comparison operators' do expect_no_offenses(<<~RUBY) a === if b ; end RUBY end context 'when indentation width is overridden for this cop only' do let(:cop_indent) { 7 } it 'allows a properly indented rhs' do expect_no_offenses(<<~RUBY) a = if b ; end RUBY end it 'autocorrects indentation' do expect_offense(<<~RUBY) a = if b ; end ^^^^^^^^^^ Indent the first line of the right-hand-side of a multi-line assignment. RUBY expect_correction(<<~RUBY) a = if b ; end RUBY end end it 'registers an offense for incorrectly indented rhs when multiple assignment' do expect_offense(<<~RUBY) foo = bar = baz = '' ^^^^^^^^ Indent the first line of the right-hand-side of a multi-line assignment. RUBY expect_correction(<<~RUBY) foo = bar = baz = '' RUBY end it 'registers an offense for incorrectly indented rhs when' \ 'multiple assignment with line breaks on each line' do expect_offense(<<~RUBY) foo = bar = baz = 42 ^^^^^^^^ Indent the first line of the right-hand-side of a multi-line assignment. RUBY expect_correction(<<~RUBY) foo = bar = baz = 42 RUBY end end
ruby
MIT
99fa0fdd0481910d7d052f4c2ed01ad36178f404
2026-01-04T15:37:41.211519Z
false
rubocop/rubocop
https://github.com/rubocop/rubocop/blob/99fa0fdd0481910d7d052f4c2ed01ad36178f404/spec/rubocop/cop/layout/leading_empty_lines_spec.rb
spec/rubocop/cop/layout/leading_empty_lines_spec.rb
# frozen_string_literal: true RSpec.describe RuboCop::Cop::Layout::LeadingEmptyLines, :config do it 'allows an empty input' do expect_no_offenses('') end it 'allows blank lines without any comments or code' do expect_no_offenses("\n") end it 'accepts not having a blank line before a class' do expect_no_offenses(<<~RUBY) class Foo end RUBY end it 'accepts not having a blank line before code' do expect_no_offenses(<<~RUBY) puts 1 RUBY end it 'accepts not having a blank line before a comment' do expect_no_offenses(<<~RUBY) # something RUBY end it 'registers an offense and corrects a new line before a class' do expect_offense(<<~RUBY) class Foo ^^^^^ Unnecessary blank line at the beginning of the source. end RUBY expect_correction(<<~RUBY) class Foo end RUBY end it 'registers an offense and corrects a new line before code' do expect_offense(<<~RUBY) puts 1 ^^^^ Unnecessary blank line at the beginning of the source. RUBY expect_correction(<<~RUBY) puts 1 RUBY end it 'registers an offense and corrects a new line before a comment' do expect_offense(<<~RUBY) # something ^^^^^^^^^^^ Unnecessary blank line at the beginning of the source. RUBY expect_correction(<<~RUBY) # something RUBY end it 'registers an offense and corrects multiple new lines before a class' do expect_offense(<<~RUBY) class Foo ^^^^^ Unnecessary blank line at the beginning of the source. end RUBY expect_correction(<<~RUBY) class Foo end RUBY end context 'autocorrect' do context 'in collaboration' do let(:config) do RuboCop::Config.new('Layout/SpaceAroundEqualsInParameterDefault' => { 'SupportedStyles' => %i[space no_space], 'EnforcedStyle' => :space }) end let(:cops) do cop_classes = [described_class, RuboCop::Cop::Layout::SpaceAroundEqualsInParameterDefault] RuboCop::Cop::Registry.new(cop_classes) end it 'does not invoke conflicts with other cops' do source_with_offenses = <<~RUBY def bar(arg =1); end RUBY options = { autocorrect: true, stdin: true } team = RuboCop::Cop::Team.mobilize(cops, config, options) team.inspect_file(parse_source(source_with_offenses, nil)) new_source = options[:stdin] expect(new_source).to eq(<<~RUBY) def bar(arg = 1); end RUBY end end end end
ruby
MIT
99fa0fdd0481910d7d052f4c2ed01ad36178f404
2026-01-04T15:37:41.211519Z
false
rubocop/rubocop
https://github.com/rubocop/rubocop/blob/99fa0fdd0481910d7d052f4c2ed01ad36178f404/spec/rubocop/cop/layout/multiline_hash_brace_layout_spec.rb
spec/rubocop/cop/layout/multiline_hash_brace_layout_spec.rb
# frozen_string_literal: true RSpec.describe RuboCop::Cop::Layout::MultilineHashBraceLayout, :config do let(:cop_config) { { 'EnforcedStyle' => 'symmetrical' } } it 'ignores implicit hashes' do expect_no_offenses(<<~RUBY) foo(a: 1, b: 2) RUBY end it 'ignores single-line hashes' do expect_no_offenses('{a: 1, b: 2}') end it 'ignores empty hashes' do expect_no_offenses('{}') end it_behaves_like 'multiline literal brace layout' do let(:open) { '{' } let(:close) { '}' } let(:a) { 'a: 1' } let(:b) { 'b: 2' } let(:multi_prefix) { 'b: ' } let(:multi) do <<~RUBY.chomp [ 1 ] RUBY end end it_behaves_like 'multiline literal brace layout method argument' do let(:open) { '{' } let(:close) { '}' } let(:a) { 'a: 1' } let(:b) { 'b: 2' } end it_behaves_like 'multiline literal brace layout trailing comma' do let(:open) { '{' } let(:close) { '}' } let(:a) { 'a: 1' } let(:b) { 'b: 2' } let(:same_line_message) do 'Closing hash brace must be on the same line as the last hash element ' \ 'when opening [...]' end let(:always_same_line_message) do 'Closing hash brace must be on the same line as the last hash element.' end end end
ruby
MIT
99fa0fdd0481910d7d052f4c2ed01ad36178f404
2026-01-04T15:37:41.211519Z
false
rubocop/rubocop
https://github.com/rubocop/rubocop/blob/99fa0fdd0481910d7d052f4c2ed01ad36178f404/spec/rubocop/cop/layout/multiline_method_definition_brace_layout_spec.rb
spec/rubocop/cop/layout/multiline_method_definition_brace_layout_spec.rb
# frozen_string_literal: true RSpec.describe RuboCop::Cop::Layout::MultilineMethodDefinitionBraceLayout, :config do let(:cop_config) { { 'EnforcedStyle' => 'symmetrical' } } it 'ignores implicit defs' do expect_no_offenses(<<~RUBY) def foo a: 1, b: 2 end RUBY end it 'ignores single-line defs' do expect_no_offenses(<<~RUBY) def foo(a,b) end RUBY end it 'ignores defs without params' do expect_no_offenses(<<~RUBY) def foo end RUBY end it_behaves_like 'multiline literal brace layout' do let(:prefix) { 'def foo' } let(:suffix) { 'end' } let(:open) { '(' } let(:close) { ')' } let(:multi_prefix) { 'b: ' } end end
ruby
MIT
99fa0fdd0481910d7d052f4c2ed01ad36178f404
2026-01-04T15:37:41.211519Z
false
rubocop/rubocop
https://github.com/rubocop/rubocop/blob/99fa0fdd0481910d7d052f4c2ed01ad36178f404/spec/rubocop/cop/layout/space_inside_reference_brackets_spec.rb
spec/rubocop/cop/layout/space_inside_reference_brackets_spec.rb
# frozen_string_literal: true RSpec.describe RuboCop::Cop::Layout::SpaceInsideReferenceBrackets, :config do context 'with space inside empty brackets not allowed' do let(:cop_config) { { 'EnforcedStyleForEmptyBrackets' => 'no_space' } } it 'accepts empty brackets with no space inside' do expect_no_offenses('a[]') end it 'registers an offense and corrects empty brackets with 1 space inside' do expect_offense(<<~RUBY) foo[ ] ^^^ Do not use space inside empty reference brackets. RUBY expect_correction(<<~RUBY) foo[] RUBY end it 'registers an offense and corrects empty brackets with multiple spaces inside' do expect_offense(<<~RUBY) a[ ] ^^^^^^^ Do not use space inside empty reference brackets. RUBY expect_correction(<<~RUBY) a[] RUBY end it 'registers an offense and corrects empty brackets with newline inside' do expect_offense(<<~RUBY) a[ ^ Do not use space inside empty reference brackets. ] RUBY expect_correction(<<~RUBY) a[] RUBY end end context 'with space inside empty braces allowed' do let(:cop_config) { { 'EnforcedStyleForEmptyBrackets' => 'space' } } it 'accepts empty brackets with space inside' do expect_no_offenses('a[ ]') end it 'registers an offense and corrects empty brackets with no space inside' do expect_offense(<<~RUBY) foo[] ^^ Use one space inside empty reference brackets. RUBY expect_correction(<<~RUBY) foo[ ] RUBY end it 'registers an offense and corrects empty brackets with more than one space inside' do expect_offense(<<~RUBY) a[ ] ^^^^^^^^ Use one space inside empty reference brackets. RUBY expect_correction(<<~RUBY) a[ ] RUBY end it 'registers an offense and corrects empty brackets with newline inside' do expect_offense(<<~RUBY) a[ ^ Use one space inside empty reference brackets. ] RUBY expect_correction(<<~RUBY) a[ ] RUBY end end context 'when EnforcedStyle is no_space' do let(:cop_config) { { 'EnforcedStyle' => 'no_space' } } it 'does not register offense for array literals' do expect_no_offenses(<<~RUBY) a = [1, 2 ] b = [ 3, 4] c = [5, 6] d = [ 7, 8 ] RUBY end it 'does not register offense for reference brackets with no spaces' do expect_no_offenses(<<~RUBY) a[1] b[index, 2] c["foo"] d[:bar] e[] RUBY end it 'does not register offense for ref bcts with no spaces that assign' do expect_no_offenses(<<~RUBY) a[1] = 2 b[345] = [ 678, var, "", nil] c["foo"] = "qux" d[:bar] = var e[] = foo RUBY end it 'does not register offense for non-empty brackets with newline inside' do expect_no_offenses(<<~RUBY) foo[ bar ] RUBY end it 'registers an offense and corrects when a reference bracket with a ' \ 'leading whitespace is assigned by another reference bracket' do expect_offense(<<~RUBY) a[ "foo"] = b["something"] ^ Do not use space inside reference brackets. RUBY expect_correction(<<~RUBY) a["foo"] = b["something"] RUBY end it 'registers an offense and corrects when a reference bracket with a ' \ 'trailing whitespace is assigned by another reference bracket' do expect_offense(<<~RUBY) a["foo" ] = b["something"] ^ Do not use space inside reference brackets. RUBY expect_correction(<<~RUBY) a["foo"] = b["something"] RUBY end it 'registers an offense and corrects when a reference bracket is ' \ 'assigned by another reference bracket with trailing whitespace' do expect_offense(<<~RUBY) a["foo"] = b["something" ] ^ Do not use space inside reference brackets. RUBY expect_correction(<<~RUBY) a["foo"] = b["something"] RUBY end it 'accepts square brackets as method name' do expect_no_offenses(<<~RUBY) def Vector.[](*array) end RUBY end it 'accepts square brackets called with method call syntax' do expect_no_offenses('subject.[](0)') end it 'accepts an array as a reference object' do expect_no_offenses('a[[ 1, 2 ]]') end it 'registers an offense and corrects ref brackets with leading whitespace' do expect_offense(<<~RUBY) a[ :key] ^^ Do not use space inside reference brackets. RUBY expect_correction(<<~RUBY) a[:key] RUBY end it 'registers an offense and corrects ref brackets with trailing whitespace' do expect_offense(<<~RUBY) b[:key ] ^ Do not use space inside reference brackets. RUBY expect_correction(<<~RUBY) b[:key] RUBY end it 'registers an offense and corrects second ref brackets with leading whitespace' do expect_offense(<<~RUBY) a[:key][ "key"] ^ Do not use space inside reference brackets. RUBY expect_correction(<<~RUBY) a[:key]["key"] RUBY end it 'registers an offense and corrects second ref brackets with trailing whitespace' do expect_offense(<<~RUBY) a[1][:key ] ^^^ Do not use space inside reference brackets. RUBY expect_correction(<<~RUBY) a[1][:key] RUBY end it 'registers an offense and corrects third ref brackets with leading whitespace' do expect_offense(<<~RUBY) a[:key][3][ :key] ^ Do not use space inside reference brackets. RUBY expect_correction(<<~RUBY) a[:key][3][:key] RUBY end it 'registers an offense and corrects third ref brackets with trailing whitespace' do expect_offense(<<~RUBY) a[var]["key", 3][:key ] ^ Do not use space inside reference brackets. RUBY expect_correction(<<~RUBY) a[var]["key", 3][:key] RUBY end it 'registers multiple offenses and corrects one set of ref brackets' do expect_offense(<<~RUBY) b[ 89 ] ^ Do not use space inside reference brackets. ^^ Do not use space inside reference brackets. RUBY expect_correction(<<~RUBY) b[89] RUBY end it 'registers multiple offenses and corrects multiple sets of ref brackets' do expect_offense(<<~RUBY) a[ :key]["foo" ][ 0 ] ^ Do not use space inside reference brackets. ^^ Do not use space inside reference brackets. ^^^ Do not use space inside reference brackets. ^ Do not use space inside reference brackets. RUBY expect_correction(<<~RUBY) a[:key]["foo"][0] RUBY end it 'registers an offense and corrects outer ref brackets' do expect_offense(<<~RUBY) record[ options[:attribute] ] ^ Do not use space inside reference brackets. ^ Do not use space inside reference brackets. RUBY expect_correction(<<~RUBY) record[options[:attribute]] RUBY end it 'registers and correct multiple offenses for multiple sets of ref brackets' do expect_offense(<<~RUBY) b[ :key]["foo" ][ 0 ] ^ Do not use space inside reference brackets. ^^ Do not use space inside reference brackets. ^^^ Do not use space inside reference brackets. ^ Do not use space inside reference brackets. RUBY expect_correction(<<~RUBY) b[:key]["foo"][0] RUBY end it 'registers an offense when extra spacing in array brackets' do expect_offense(<<~RUBY) j[ "pop"] = [89, nil, "" ] ^ Do not use space inside reference brackets. RUBY expect_correction(<<~RUBY) j["pop"] = [89, nil, "" ] RUBY end end context 'when EnforcedStyle is space' do let(:cop_config) do { 'EnforcedStyle' => 'space', 'EnforcedStyleForEmptyBrackets' => 'space' } end it 'does not register offense for array literals' do expect_no_offenses(<<~RUBY) a = [1, 2 ] b = [ 3, 4] c = [5, 6] d = [ 7, 8 ] RUBY end it 'does not register offense for reference brackets with spaces' do expect_no_offenses(<<~RUBY) a[ 1 ] b[ index, 3 ] c[ "foo" ] d[ :bar ] e[ ] RUBY end it 'does not register offense for ref bcts with spaces that assign' do expect_no_offenses(<<~RUBY) a[ 1 ] = 2 b[ 345 ] = [ 678, var, "", nil] c[ "foo" ] = "qux" d[ :bar ] = var e[ ] = baz RUBY end it 'registers an offense and corrects when a reference bracket with no ' \ 'leading whitespace is assigned by another reference bracket' do expect_offense(<<~RUBY) a["foo" ] = b[ "something" ] ^ Use space inside reference brackets. RUBY expect_correction(<<~RUBY) a[ "foo" ] = b[ "something" ] RUBY end it 'registers an offense and corrects when a reference bracket with no ' \ 'trailing whitespace is assigned by another reference bracket' do expect_offense(<<~RUBY) a[ "foo"] = b[ "something" ] ^ Use space inside reference brackets. RUBY expect_correction(<<~RUBY) a[ "foo" ] = b[ "something" ] RUBY end it 'registers an offense and corrects when a reference bracket is ' \ 'assigned by another reference bracket with no trailing whitespace' do expect_offense(<<~RUBY) a[ "foo" ] = b[ "something"] ^ Use space inside reference brackets. RUBY expect_correction(<<~RUBY) a[ "foo" ] = b[ "something" ] RUBY end it 'accepts square brackets as method name' do expect_no_offenses(<<~RUBY) def Vector.[](*array) end RUBY end it 'accepts square brackets called with method call syntax' do expect_no_offenses('subject.[](0)') end it 'accepts an array as a reference object' do expect_no_offenses('a[ [1, 2] ]') end it 'registers an offense and corrects ref brackets with no leading whitespace' do expect_offense(<<~RUBY) a[:key ] ^ Use space inside reference brackets. RUBY expect_correction(<<~RUBY) a[ :key ] RUBY end it 'registers an offense and corrects ref brackets with no trailing whitespace' do expect_offense(<<~RUBY) b[ :key] ^ Use space inside reference brackets. RUBY expect_correction(<<~RUBY) b[ :key ] RUBY end it 'registers an offense and corrects second ref brackets with no leading whitespace' do expect_offense(<<~RUBY) a[ :key ]["key" ] ^ Use space inside reference brackets. RUBY expect_correction(<<~RUBY) a[ :key ][ "key" ] RUBY end it 'registers an offense and corrects second ref brackets with no trailing whitespace' do expect_offense(<<~RUBY) a[ 5, 1 ][ :key] ^ Use space inside reference brackets. RUBY expect_correction(<<~RUBY) a[ 5, 1 ][ :key ] RUBY end it 'registers an offense and corrects third ref brackets with no leading whitespace' do expect_offense(<<~RUBY) a[ :key ][ 3 ][:key ] ^ Use space inside reference brackets. RUBY expect_correction(<<~RUBY) a[ :key ][ 3 ][ :key ] RUBY end it 'registers an offense and correct third ref brackets with no trailing whitespace' do expect_offense(<<~RUBY) a[ var ][ "key" ][ :key] ^ Use space inside reference brackets. RUBY expect_correction(<<~RUBY) a[ var ][ "key" ][ :key ] RUBY end it 'registers and corrects multiple offenses in one set of ref brackets' do expect_offense(<<~RUBY) b[89] ^ Use space inside reference brackets. ^ Use space inside reference brackets. RUBY expect_correction(<<~RUBY) b[ 89 ] RUBY end it 'registers and corrects multiple offenses for multiple sets of ref brackets' do expect_offense(<<~RUBY) a[:key]["foo" ][0] ^ Use space inside reference brackets. ^ Use space inside reference brackets. ^ Use space inside reference brackets. ^ Use space inside reference brackets. ^ Use space inside reference brackets. RUBY expect_correction(<<~RUBY) a[ :key ][ "foo" ][ 0 ] RUBY end it 'accepts spaces in array brackets' do expect_no_offenses(<<~RUBY) j = [89, nil, "" ] RUBY end end end
ruby
MIT
99fa0fdd0481910d7d052f4c2ed01ad36178f404
2026-01-04T15:37:41.211519Z
false
rubocop/rubocop
https://github.com/rubocop/rubocop/blob/99fa0fdd0481910d7d052f4c2ed01ad36178f404/spec/rubocop/cop/layout/multiline_method_argument_line_breaks_spec.rb
spec/rubocop/cop/layout/multiline_method_argument_line_breaks_spec.rb
# frozen_string_literal: true RSpec.describe RuboCop::Cop::Layout::MultilineMethodArgumentLineBreaks, :config do context 'with one argument on same line as the method call' do it 'does not add any offenses' do expect_no_offenses(<<~RUBY) taz("abc") RUBY end end context 'when bracket hash assignment on multiple lines' do it 'does not add any offenses' do expect_no_offenses(<<~RUBY) class Thing def call bar['foo'] = ::Time.zone.at( huh['foo'], ) end end RUBY end end context 'when bracket hash assignment key on multiple lines' do it 'does not add any offenses' do expect_no_offenses(<<~RUBY) a['b', 'c', 'd'] = e RUBY end end context 'when two arguments are on next line' do it 'does not add any offenses' do expect_no_offenses(<<~RUBY) taz( "abc", "foo" ) RUBY end end context 'when many arguments are on multiple lines, two on same line' do it 'registers an offense and corrects' do expect_offense(<<~RUBY) taz("abc", "foo", "bar", ^^^^^ Each argument in a multi-line method call must start on a separate line. "baz" ) RUBY expect_correction(<<~RUBY) taz("abc", "foo",\s "bar", "baz" ) RUBY end end context 'when many arguments are on multiple lines, three on same line' do it 'registers an offense and corrects' do expect_offense(<<~RUBY) taz("abc", "foo", "bar", "barz", ^^^^^^ Each argument in a multi-line method call must start on a separate line. ^^^^^ Each argument in a multi-line method call must start on a separate line. "baz" ) RUBY expect_correction(<<~RUBY) taz("abc", "foo",\s "bar",\s "barz", "baz" ) RUBY end end context 'when many arguments including hash are on multiple lines, three on same line' do it 'registers an offense and corrects' do expect_offense(<<~RUBY) taz("abc", "foo", "bar", z: "barz", ^^^^^^^^^ Each argument in a multi-line method call must start on a separate line. ^^^^^ Each argument in a multi-line method call must start on a separate line. x: "baz" ) RUBY expect_correction(<<~RUBY) taz("abc", "foo",\s "bar",\s z: "barz", x: "baz" ) RUBY end end context 'when argument starts on same line but ends on different line' do it 'registers an offense and corrects' do expect_offense(<<~RUBY) taz("abc", { ^ Each argument in a multi-line method call must start on a separate line. foo: "edf", }) RUBY expect_correction(<<~RUBY) taz("abc",\s { foo: "edf", }) RUBY end end context 'when second argument starts on same line as end of first' do it 'registers an offense and corrects' do expect_offense(<<~RUBY) taz({ foo: "edf", }, "abc") ^^^^^ Each argument in a multi-line method call must start on a separate line. RUBY expect_correction(<<~RUBY) taz({ foo: "edf", },\s "abc") RUBY end end context 'when there are multiple arguments on the first line' do it 'registers an offense and corrects starting from the 2nd argument' do expect_offense(<<~RUBY) do_something(foo, bar, baz, ^^^ Each argument in a multi-line method call must start on a separate line. ^^^ Each argument in a multi-line method call must start on a separate line. quux) RUBY expect_correction(<<~RUBY) do_something(foo,#{trailing_whitespace} bar,#{trailing_whitespace} baz, quux) RUBY end end context 'with safe navigation' do context 'with one argument on the same line as the method call' do it 'does not register an offense' do expect_no_offenses(<<~RUBY) foo&.bar(baz) RUBY end end context 'with multiple arguments on the same line as the method call' do it 'does not register an offense' do expect_no_offenses(<<~RUBY) foo&.bar(baz, quux) RUBY end end context 'with arguments over multiple lines and more than one argument on a single line' do it 'registers an offense and corrects' do expect_offense(<<~RUBY) foo&.bar(baz, quux, ^^^^ Each argument in a multi-line method call must start on a separate line. corge) RUBY expect_correction(<<~RUBY) foo&.bar(baz,#{trailing_whitespace} quux, corge) RUBY end end context 'with []=' do it 'allows a multiline assignment' do expect_no_offenses(<<~RUBY) foo&.bar['foo'] = ::Time.zone.at( huh['foo'], ) RUBY end end context 'with AllowMultilineFinalElement: true' do let(:cop_config) { { 'AllowMultilineFinalElement' => true } } it 'ignores last argument that is a multiline hash' do expect_no_offenses(<<~RUBY) foo&.bar(1, 2, 3, { a: 1, }) RUBY end end end context 'ignore last element' do let(:cop_config) { { 'AllowMultilineFinalElement' => true } } it 'ignores last argument that is a multiline hash' do expect_no_offenses(<<~RUBY) foo(1, 2, 3, { a: 1, }) RUBY end it 'registers and corrects arguments that are multiline hashes and not the last argument' do expect_offense(<<~RUBY) foo(1, 2, 3, { ^ Each argument in a multi-line method call must start on a separate line. ^ Each argument in a multi-line method call must start on a separate line. ^ Each argument in a multi-line method call must start on a separate line. a: 1, }, 4) RUBY expect_correction(<<~RUBY) foo(1,#{trailing_whitespace} 2,#{trailing_whitespace} 3,#{trailing_whitespace} { a: 1, },#{trailing_whitespace} 4) RUBY end it 'registers and corrects last argument that starts on a new line' do expect_offense(<<~RUBY) foo(1, 2, 3, ^ Each argument in a multi-line method call must start on a separate line. ^ Each argument in a multi-line method call must start on a separate line. 4) RUBY expect_correction(<<~RUBY) foo(1,#{trailing_whitespace} 2,#{trailing_whitespace} 3, 4) RUBY end end end
ruby
MIT
99fa0fdd0481910d7d052f4c2ed01ad36178f404
2026-01-04T15:37:41.211519Z
false
rubocop/rubocop
https://github.com/rubocop/rubocop/blob/99fa0fdd0481910d7d052f4c2ed01ad36178f404/spec/rubocop/cop/layout/begin_end_alignment_spec.rb
spec/rubocop/cop/layout/begin_end_alignment_spec.rb
# frozen_string_literal: true RSpec.describe RuboCop::Cop::Layout::BeginEndAlignment, :config do let(:cop_config) { { 'EnforcedStyleAlignWith' => 'begin' } } it_behaves_like 'aligned', 'begin', '', 'end' it_behaves_like 'misaligned', <<~RUBY, false begin end ^^^ `end` at 2, 2 is not aligned with `begin` at 1, 0. RUBY it_behaves_like 'aligned', 'puts 1; begin', '', ' end' context 'when EnforcedStyleAlignWith is start_of_line' do let(:cop_config) { { 'EnforcedStyleAlignWith' => 'start_of_line' } } it_behaves_like 'aligned', 'puts 1; begin', '', 'end' it_behaves_like 'misaligned', <<~RUBY, false begin end ^^^ `end` at 2, 2 is not aligned with `begin` at 1, 0. RUBY it_behaves_like 'misaligned', <<~RUBY, :begin var << begin end ^^^ `end` at 2, 7 is not aligned with `var << begin` at 1, 0. RUBY it_behaves_like 'aligned', 'var = begin', '', 'end' end end
ruby
MIT
99fa0fdd0481910d7d052f4c2ed01ad36178f404
2026-01-04T15:37:41.211519Z
false
rubocop/rubocop
https://github.com/rubocop/rubocop/blob/99fa0fdd0481910d7d052f4c2ed01ad36178f404/spec/rubocop/cop/layout/empty_lines_around_module_body_spec.rb
spec/rubocop/cop/layout/empty_lines_around_module_body_spec.rb
# frozen_string_literal: true RSpec.describe RuboCop::Cop::Layout::EmptyLinesAroundModuleBody, :config do let(:extra_begin) { 'Extra empty line detected at module body beginning.' } let(:extra_end) { 'Extra empty line detected at module body end.' } let(:missing_begin) { 'Empty line missing at module body beginning.' } let(:missing_end) { 'Empty line missing at module body end.' } let(:missing_def) { 'Empty line missing before first def definition' } let(:missing_type) { 'Empty line missing before first module definition' } context 'when EnforcedStyle is no_empty_lines' do let(:cop_config) { { 'EnforcedStyle' => 'no_empty_lines' } } it 'registers an offense for module body starting with a blank' do expect_offense(<<~RUBY) module SomeModule ^{} #{extra_begin} do_something end RUBY end it 'registers an offense for module body ending with a blank' do expect_offense(<<~RUBY) module SomeModule do_something ^{} #{extra_end} end RUBY end it 'autocorrects beginning and end' do expect_offense(<<~RUBY) module SomeModule ^{} #{extra_begin} do_something ^{} #{extra_end} end RUBY expect_correction(<<~RUBY) module SomeModule do_something end RUBY end end context 'when EnforcedStyle is empty_lines' do let(:cop_config) { { 'EnforcedStyle' => 'empty_lines' } } it 'registers an offense for module body not starting or ending with a blank' do expect_offense(<<~RUBY) module SomeModule do_something ^ #{missing_begin} end ^ #{missing_end} RUBY end it 'registers an offense for module body not ending with a blank' do expect_offense(<<~RUBY) module SomeModule do_something end ^ #{missing_end} RUBY end it 'autocorrects beginning and end' do expect_offense(<<~RUBY) module SomeModule do_something ^ #{missing_begin} end ^ #{missing_end} RUBY expect_correction(<<~RUBY) module SomeModule do_something end RUBY end it 'accepts modules with an empty body' do expect_no_offenses(<<~RUBY) module A end RUBY end end context 'when EnforcedStyle is empty_lines_except_namespace' do let(:cop_config) { { 'EnforcedStyle' => 'empty_lines_except_namespace' } } context 'when only child is module' do it 'requires no empty lines for namespace' do expect_no_offenses(<<~RUBY) module Parent module Child do_something end end RUBY end it 'registers an offense for namespace body starting with a blank' do expect_offense(<<~RUBY) module Parent ^{} #{extra_begin} module Child do_something end end RUBY end it 'registers an offense for namespace body ending with a blank' do expect_offense(<<~RUBY) module Parent module Child do_something end ^{} #{extra_end} end RUBY end it 'registers offenses for namespaced module body not starting with a blank' do expect_offense(<<~RUBY) module Parent module Child do_something ^ #{missing_begin} end end RUBY end it 'registers offenses for namespaced module body not ending with a blank' do expect_offense(<<~RUBY) module Parent module Child do_something end ^ #{missing_end} end RUBY end it 'autocorrects beginning and end' do expect_offense(<<~RUBY) module Parent ^{} #{extra_begin} module Child do_something ^ #{missing_begin} end ^ #{missing_end} ^{} #{extra_end} end RUBY expect_correction(<<~RUBY) module Parent module Child do_something end end RUBY end end context 'when only child is class' do it 'requires no empty lines for namespace' do expect_no_offenses(<<~RUBY) module Parent class SomeClass do_something end end RUBY end it 'registers an offense for namespace body starting with a blank' do expect_offense(<<~RUBY) module Parent ^{} #{extra_begin} class SomeClass do_something end end RUBY end it 'registers an offense for namespace body ending with a blank' do expect_offense(<<~RUBY) module Parent class SomeClass do_something end ^{} #{extra_end} end RUBY end end context 'when has multiple child modules' do it 'requires empty lines for namespace' do expect_no_offenses(<<~RUBY) module Parent module Mom do_something end module Dad end end RUBY end it 'registers offenses for namespace body starting and ending without a blank' do expect_offense(<<~RUBY) module Parent module Mom ^ #{missing_begin} do_something end module Dad end end ^ #{missing_end} RUBY end end end it_behaves_like 'empty_lines_around_class_or_module_body', 'module' end
ruby
MIT
99fa0fdd0481910d7d052f4c2ed01ad36178f404
2026-01-04T15:37:41.211519Z
false
rubocop/rubocop
https://github.com/rubocop/rubocop/blob/99fa0fdd0481910d7d052f4c2ed01ad36178f404/spec/rubocop/cop/layout/empty_lines_around_attribute_accessor_spec.rb
spec/rubocop/cop/layout/empty_lines_around_attribute_accessor_spec.rb
# frozen_string_literal: true RSpec.describe RuboCop::Cop::Layout::EmptyLinesAroundAttributeAccessor, :config do it 'registers an offense and corrects for code that immediately follows accessor' do expect_offense(<<~RUBY) attr_accessor :foo ^^^^^^^^^^^^^^^^^^ Add an empty line after attribute accessor. def do_something end RUBY expect_correction(<<~RUBY) attr_accessor :foo def do_something end RUBY end it 'registers an offense and corrects for code that immediately follows accessor with comment' do expect_offense(<<~RUBY) attr_accessor :foo # comment ^^^^^^^^^^^^^^^^^^ Add an empty line after attribute accessor. def do_something end RUBY expect_correction(<<~RUBY) attr_accessor :foo # comment def do_something end RUBY end it 'registers an offense and corrects for an attribute accessor and comment line' do expect_offense(<<~RUBY) attr_accessor :foo ^^^^^^^^^^^^^^^^^^ Add an empty line after attribute accessor. # comment def do_something end RUBY expect_correction(<<~RUBY) attr_accessor :foo # comment def do_something end RUBY end it 'registers an offense and corrects for some attribute accessors and comment line' do expect_offense(<<~RUBY) attr_accessor :foo attr_reader :bar attr_writer :baz ^^^^^^^^^^^^^^^^ Add an empty line after attribute accessor. # comment def do_something end RUBY expect_correction(<<~RUBY) attr_accessor :foo attr_reader :bar attr_writer :baz # comment def do_something end RUBY end it 'registers an offense and corrects for an attribute accessor and some comment line' do expect_offense(<<~RUBY) attr_accessor :foo ^^^^^^^^^^^^^^^^^^ Add an empty line after attribute accessor. # comment # comment def do_something end RUBY expect_correction(<<~RUBY) attr_accessor :foo # comment # comment def do_something end RUBY end it 'registers an offense and corrects for an attribute accessor and `rubocop:disable` ' \ 'comment line' do expect_offense(<<~RUBY) attr_accessor :foo ^^^^^^^^^^^^^^^^^^ Add an empty line after attribute accessor. # rubocop:disable Department/Cop def do_something end RUBY expect_correction(<<~RUBY) attr_accessor :foo # rubocop:disable Department/Cop def do_something end RUBY end it 'registers an offense and corrects for an attribute accessor and `rubocop:enable` ' \ 'comment line' do expect_offense(<<~RUBY) # rubocop:disable Department/Cop attr_accessor :foo ^^^^^^^^^^^^^^^^^^ Add an empty line after attribute accessor. # rubocop:enable Department/Cop def do_something end RUBY expect_correction(<<~RUBY) # rubocop:disable Department/Cop attr_accessor :foo # rubocop:enable Department/Cop def do_something end RUBY end it 'registers an offense and corrects for an attribute accessor and `rubocop:enable` ' \ 'comment line and other comment' do expect_offense(<<~RUBY) # rubocop:disable Department/Cop attr_accessor :foo ^^^^^^^^^^^^^^^^^^ Add an empty line after attribute accessor. # rubocop:enable Department/Cop # comment def do_something end RUBY expect_correction(<<~RUBY) # rubocop:disable Department/Cop attr_accessor :foo # rubocop:enable Department/Cop # comment def do_something end RUBY end it 'accepts code that separates an attribute accessor from the code with a newline' do expect_no_offenses(<<~RUBY) attr_accessor :foo def do_something end RUBY end it 'accepts code that separates an attribute accessor from the code and `rubocop:enable` ' \ 'comment line with a newline' do expect_no_offenses(<<~RUBY) # rubocop:disable Department/Cop attr_accessor :foo # rubocop:enable Department/Cop def do_something end RUBY end it 'accepts code that where the attr_accessor is the last line' do expect_no_offenses('attr_accessor :foo') end it 'accepts code that separates attribute accessors from the code with a newline' do expect_no_offenses(<<~RUBY) attr_accessor :foo attr_reader :bar attr_writer :baz def do_something end RUBY end it 'accepts code that separates attribute accessors from the code and comment line with a newline' do expect_no_offenses(<<~RUBY) attr_accessor :foo attr_reader :bar attr_writer :baz # comment def do_something end RUBY end it 'accepts code when used in class definition' do expect_no_offenses(<<~RUBY) class Foo attr_accessor :foo end RUBY end it 'accepts code when attribute method is method chained' do expect_no_offenses(<<~RUBY) class Foo attr.foo end RUBY end it 'does not register an offense when using `if` ... `else` branches' do expect_no_offenses(<<~RUBY) if condition attr_reader :foo else do_something end RUBY end context 'when `AllowAliasSyntax: true`' do let(:cop_config) { { 'AllowAliasSyntax' => true } } it 'does not register an offense for code that immediately `alias` syntax after accessor' do expect_no_offenses(<<~RUBY) attr_accessor :foo alias foo? foo def do_something end RUBY end end context 'when `AllowAliasSyntax: false`' do let(:cop_config) { { 'AllowAliasSyntax' => false } } it 'registers an offense for code that immediately `alias` syntax after accessor' do expect_offense(<<~RUBY) attr_accessor :foo ^^^^^^^^^^^^^^^^^^ Add an empty line after attribute accessor. alias foo? foo def do_something end RUBY end end context 'when `AllowedMethods: private`' do let(:cop_config) { { 'AllowedMethods' => ['private'] } } it 'does not register an offense for code that immediately ignored methods after accessor' do expect_no_offenses(<<~RUBY) attr_accessor :foo private :foo def do_something end RUBY end end context 'when `AllowedMethods: []`' do let(:cop_config) { { 'AllowedMethods' => [] } } it 'registers an offense for code that immediately ignored methods after accessor' do expect_offense(<<~RUBY) attr_accessor :foo ^^^^^^^^^^^^^^^^^^ Add an empty line after attribute accessor. private :foo def do_something end RUBY end end end
ruby
MIT
99fa0fdd0481910d7d052f4c2ed01ad36178f404
2026-01-04T15:37:41.211519Z
false
rubocop/rubocop
https://github.com/rubocop/rubocop/blob/99fa0fdd0481910d7d052f4c2ed01ad36178f404/spec/rubocop/cop/layout/empty_lines_around_begin_body_spec.rb
spec/rubocop/cop/layout/empty_lines_around_begin_body_spec.rb
# frozen_string_literal: true RSpec.describe RuboCop::Cop::Layout::EmptyLinesAroundBeginBody, :config do shared_examples 'accepts' do |name, code| it "accepts #{name}" do expect_no_offenses(code) end end it 'registers an offense for begin body starting with a blank' do expect_offense(<<~RUBY) begin ^{} Extra empty line detected at `begin` body beginning. foo end RUBY expect_correction(<<~RUBY) begin foo end RUBY end it 'registers an offense for ensure body ending' do expect_offense(<<~RUBY) begin foo ensure bar ^{} Extra empty line detected at `begin` body end. end RUBY expect_correction(<<~RUBY) begin foo ensure bar end RUBY end it 'registers an offense for begin body ending with a blank' do expect_offense(<<~RUBY) begin foo ^{} Extra empty line detected at `begin` body end. end RUBY expect_correction(<<~RUBY) begin foo end RUBY end it 'registers an offense for begin body starting in method' do expect_offense(<<~RUBY) def bar begin ^{} Extra empty line detected at `begin` body beginning. foo end end RUBY expect_correction(<<~RUBY) def bar begin foo end end RUBY end it 'registers an offense for begin body ending in method' do expect_offense(<<~RUBY) def bar begin foo ^{} Extra empty line detected at `begin` body end. end end RUBY expect_correction(<<~RUBY) def bar begin foo end end RUBY end it 'registers an offense for begin body starting with rescue' do expect_offense(<<~RUBY) begin ^{} Extra empty line detected at `begin` body beginning. foo rescue bar end RUBY expect_correction(<<~RUBY) begin foo rescue bar end RUBY end it 'registers an offense for rescue body ending' do expect_offense(<<~RUBY) begin foo rescue bar ^{} Extra empty line detected at `begin` body end. end RUBY expect_correction(<<~RUBY) begin foo rescue bar end RUBY end it 'registers an offense for else body ending' do expect_offense(<<~RUBY) begin foo rescue bar else baz ^{} Extra empty line detected at `begin` body end. end RUBY expect_correction(<<~RUBY) begin foo rescue bar else baz end RUBY end it 'registers many offenses with complex begin-end' do expect_offense(<<~RUBY) begin ^{} Extra empty line detected at `begin` body beginning. do_something1 rescue RuntimeError do_something2 rescue ArgumentError => ex do_something3 rescue do_something3 else do_something4 ensure do_something4 ^{} Extra empty line detected at `begin` body end. end RUBY expect_correction(<<~RUBY) begin do_something1 rescue RuntimeError do_something2 rescue ArgumentError => ex do_something3 rescue do_something3 else do_something4 ensure do_something4 end RUBY end it_behaves_like 'accepts', 'begin block without empty line', <<~RUBY begin foo end RUBY it_behaves_like 'accepts', 'begin block without empty line in a method', <<~RUBY def foo begin bar end end RUBY end
ruby
MIT
99fa0fdd0481910d7d052f4c2ed01ad36178f404
2026-01-04T15:37:41.211519Z
false
rubocop/rubocop
https://github.com/rubocop/rubocop/blob/99fa0fdd0481910d7d052f4c2ed01ad36178f404/spec/rubocop/cop/layout/line_continuation_spacing_spec.rb
spec/rubocop/cop/layout/line_continuation_spacing_spec.rb
# frozen_string_literal: true RSpec.describe RuboCop::Cop::Layout::LineContinuationSpacing, :config do context 'EnforcedStyle: space' do let(:cop_config) { { 'EnforcedStyle' => 'space' } } it 'registers an offense when no space in front of backslash' do expect_offense(<<~'RUBY') if 2 + 2\ ^ Use one space in front of backslash. == 4 foo end RUBY expect_correction(<<~'RUBY') if 2 + 2 \ == 4 foo end RUBY end it 'registers an offense when too much space in front of backslash' do expect_offense(<<~'RUBY') if 2 + 2 \ ^^^ Use one space in front of backslash. == 4 foo end RUBY expect_correction(<<~'RUBY') if 2 + 2 \ == 4 foo end RUBY end it 'marks the offense correctly when offense is not in first line' do expect_offense(<<~'RUBY') foo bar baz if 2 + 2 \ ^^^^^ Use one space in front of backslash. == 4 foo end RUBY expect_correction(<<~'RUBY') foo bar baz if 2 + 2 \ == 4 foo end RUBY end it 'registers an offense when too much space in front of backslash in array literals' do expect_offense(<<~'RUBY') [ :foo \ ^^^ Use one space in front of backslash. ] RUBY expect_correction(<<~'RUBY') [ :foo \ ] RUBY end it 'registers no offense with one space in front of backslash' do expect_no_offenses(<<~'RUBY') if 2 + 2 \ == 4 foo end RUBY end it 'ignores heredocs and comments' do expect_no_offenses(<<~'RUBY') # this\ <<-X is\ ok X RUBY end it 'ignores percent literals' do expect_no_offenses(<<~'RUBY') %i[ foo \ ] RUBY end it 'ignores when too much space in front of backslash after `__END__`' do expect_no_offenses(<<~'RUBY') foo bar __END__ baz \ qux RUBY end it 'ignores empty code' do expect_no_offenses('') end it 'does not register an offense for a continuation inside a multiline string' do expect_no_offenses(<<~'RUBY') 'foo\ bar' RUBY end it 'does not register an offense for a continuation inside a %q string' do expect_no_offenses(<<~'RUBY') %q{foo\ bar} RUBY end it 'does not register an offense for a continuation inside a regexp' do expect_no_offenses(<<~'RUBY') /foo\ bar/ RUBY end it 'does not register an offense for a continuation inside a %r regexp' do expect_no_offenses(<<~'RUBY') %r{foo\ bar} RUBY end it 'does not register an offense for a continuation inside an xstr' do expect_no_offenses(<<~'RUBY') `foo\ bar` RUBY end it 'does not register an offense for a continuation inside an %x xstr' do expect_no_offenses(<<~'RUBY') %x{foo\ bar} RUBY end end context 'EnforcedStyle: no_space' do let(:cop_config) { { 'EnforcedStyle' => 'no_space' } } it 'registers an offense when one space in front of backslash' do expect_offense(<<~'RUBY') if 2 + 2 \ ^^ Use zero spaces in front of backslash. == 4 foo end RUBY expect_correction(<<~'RUBY') if 2 + 2\ == 4 foo end RUBY end it 'registers an offense when many spaces in front of backslash' do expect_offense(<<~'RUBY') if 2 + 2 \ ^^^ Use zero spaces in front of backslash. == 4 foo end RUBY expect_correction(<<~'RUBY') if 2 + 2\ == 4 foo end RUBY end it 'marks the offense correctly when offense is not in first line' do expect_offense(<<~'RUBY') foo bar baz if 2 + 2 \ ^^^^^ Use zero spaces in front of backslash. == 4 foo end RUBY expect_correction(<<~'RUBY') foo bar baz if 2 + 2\ == 4 foo end RUBY end it 'registers no offense with zero spaces in front of backslash' do expect_no_offenses(<<~'RUBY') if 2 + 2\ == 4 foo end RUBY end it 'registers an offense when too much space in front of backslash in array literals' do expect_offense(<<~'RUBY') [ :foo \ ^^^ Use zero spaces in front of backslash. ] RUBY expect_correction(<<~'RUBY') [ :foo\ ] RUBY end it 'ignores heredocs and comments' do expect_no_offenses(<<~'RUBY') # this \ <<-X is \ ok X RUBY end it 'ignores percent literals' do expect_no_offenses(<<~'RUBY') %i[ foo \ ] RUBY end it 'ignores when too much space in front of backslash after `__END__`' do expect_no_offenses(<<~'RUBY') foo bar __END__ baz \ qux RUBY end it 'ignores empty code' do expect_no_offenses('') end it 'does not register an offense for a continuation inside a multiline string' do expect_no_offenses(<<~'RUBY') 'foo \ bar' RUBY end it 'does not register an offense for a continuation inside a %q string' do expect_no_offenses(<<~'RUBY') %q{foo \ bar} RUBY end it 'does not register an offense for a continuation inside a multiline string with interpolation' do expect_no_offenses(<<~'RUBY') "#{foo} \ bar" RUBY end it 'does not register an offense for a continuation inside a regexp' do expect_no_offenses(<<~'RUBY') /foo \ bar/ RUBY end it 'does not register an offense for a continuation inside a %r regexp' do expect_no_offenses(<<~'RUBY') %r{foo \ bar} RUBY end it 'does not register an offense for a continuation inside an xstr' do expect_no_offenses(<<~'RUBY') `foo \ bar` RUBY end it 'does not register an offense for a continuation inside an %x xstr' do expect_no_offenses(<<~'RUBY') %x{foo \ bar} RUBY end end end
ruby
MIT
99fa0fdd0481910d7d052f4c2ed01ad36178f404
2026-01-04T15:37:41.211519Z
false
rubocop/rubocop
https://github.com/rubocop/rubocop/blob/99fa0fdd0481910d7d052f4c2ed01ad36178f404/spec/rubocop/cop/layout/empty_lines_around_access_modifier_spec.rb
spec/rubocop/cop/layout/empty_lines_around_access_modifier_spec.rb
# frozen_string_literal: true RSpec.describe RuboCop::Cop::Layout::EmptyLinesAroundAccessModifier, :config do context 'EnforcedStyle is `around`' do let(:cop_config) { { 'EnforcedStyle' => 'around' } } %w[private protected public module_function].each do |access_modifier| it "requires blank line before #{access_modifier}" do expect_offense(<<~RUBY) class Test something #{access_modifier} #{'^' * access_modifier.size} Keep a blank line before and after `#{access_modifier}`. def test; end end RUBY expect_correction(<<~RUBY) class Test something #{access_modifier} def test; end end RUBY end it "requires blank line after #{access_modifier}" do expect_offense(<<~RUBY) class Test something #{access_modifier} #{'^' * access_modifier.size} Keep a blank line before and after `#{access_modifier}`. def test; end end RUBY expect_correction(<<~RUBY) class Test something #{access_modifier} def test; end end RUBY end it "ignores comment line before #{access_modifier}" do expect_no_offenses(<<~RUBY) class Test something # This comment is fine #{access_modifier} def test; end end RUBY end it "ignores #{access_modifier} inside a method call" do expect_no_offenses(<<~RUBY) class Test def #{access_modifier}? #{access_modifier} end end RUBY end it "ignores an accessor with the same name as #{access_modifier} " \ 'above a method definition' do expect_no_offenses(<<~RUBY) class Test attr_reader #{access_modifier} def foo end end RUBY end it "ignores #{access_modifier} deep inside a method call" do expect_no_offenses(<<~RUBY) class Test def #{access_modifier}? if true #{access_modifier} end end end RUBY end it "ignores #{access_modifier} with a right-hand-side condition" do expect_no_offenses(<<~RUBY) class Test def #{access_modifier}? #{access_modifier} if true end end RUBY end it "ignores #{access_modifier} with block argument" do expect_no_offenses(<<~RUBY) def foo #{access_modifier} { do_something } end RUBY end it 'autocorrects blank line after #{access_modifier} with comment' do expect_offense(<<~RUBY) class Test something #{access_modifier} # let's modify the rest #{'^' * access_modifier.size} Keep a blank line before and after `#{access_modifier}`. def test; end end RUBY expect_correction(<<~RUBY) class Test something #{access_modifier} # let's modify the rest def test; end end RUBY end it 'accepts missing blank line when at the beginning of class' do expect_no_offenses(<<~RUBY) class Test #{access_modifier} def test; end end RUBY end it 'accepts missing blank line when at the beginning of module' do expect_no_offenses(<<~RUBY) module Test #{access_modifier} def test; end end RUBY end it 'accepts missing blank line when at the beginning of sclass' do expect_no_offenses(<<~RUBY) class << self #{access_modifier} def test; end end RUBY end it 'accepts missing blank line when specifying a superclass that breaks the line' do expect_no_offenses(<<~RUBY) class Foo < Bar #{access_modifier} def do_something end end RUBY end it 'accepts missing blank line when specifying `self` that breaks the line' do expect_no_offenses(<<~RUBY) class << self #{access_modifier} def do_something end end RUBY end it 'accepts missing blank line when at the beginning of file' \ 'when specifying a superclass that breaks the line' do expect_no_offenses(<<~RUBY) #{access_modifier} def do_something end RUBY end it "requires blank line after, but not before, #{access_modifier} " \ 'when at the beginning of class/module' do expect_offense(<<~RUBY) class Test #{access_modifier} #{'^' * access_modifier.size} Keep a blank line after `#{access_modifier}`. def test end end RUBY end it 'accepts missing blank line when at the beginning of file and preceded by a comment' do expect_no_offenses(<<~RUBY) # comment #{access_modifier} def do_something end RUBY end context 'at the beginning of block' do context 'for blocks defined with do' do it 'accepts missing blank line' do expect_no_offenses(<<~RUBY) included do #{access_modifier} def test; end end RUBY end it 'accepts missing blank line with arguments' do expect_no_offenses(<<~RUBY) included do |foo| #{access_modifier} def test; end end RUBY end it "requires blank line after, but not before, #{access_modifier}" do expect_offense(<<~RUBY) included do #{access_modifier} #{'^' * access_modifier.size} Keep a blank line after `#{access_modifier}`. def test end end RUBY end end context 'for blocks defined with {}' do it 'accepts missing blank line' do expect_no_offenses(<<~RUBY) included { #{access_modifier} def test; end } RUBY end it 'accepts missing blank line with arguments' do expect_no_offenses(<<~RUBY) included { |foo| #{access_modifier} def test; end } RUBY end end end it 'accepts missing blank line when at the end of block' do expect_no_offenses(<<~RUBY) class Test def test; end #{access_modifier} end RUBY end it 'accepts missing blank line when at the end of specifying a superclass' do expect_no_offenses(<<~RUBY) class Test < Base def test; end #{access_modifier} end RUBY end it 'accepts missing blank line when at the end of specifying `self`' do expect_no_offenses(<<~RUBY) class << self def test; end #{access_modifier} end RUBY end it 'requires blank line when next line started with end' do expect_offense(<<~RUBY) class Test #{access_modifier} #{'^' * access_modifier.size} Keep a blank line after `#{access_modifier}`. end_this! end RUBY end it 'recognizes blank lines with DOS style line endings' do expect_no_offenses(<<~RUBY) class Test\r \r #{access_modifier}\r \r def test; end\r end\r RUBY end it 'accepts only using access modifier' do expect_no_offenses(<<~RUBY) #{access_modifier} RUBY end it 'accepts when an access modifier and an expression are on the same line' do expect_no_offenses(<<~RUBY) #{access_modifier}; foo .bar RUBY end context 'inside an implicit `begin` node' do it 'registers an offense and corrects' do expect_offense(<<~RUBY, access_modifier: access_modifier) foo %{access_modifier} ^{access_modifier} Keep a blank line before and after `%{access_modifier}`. bar RUBY expect_correction(<<~RUBY) foo #{access_modifier} bar RUBY end end context 'when `Layout/EmptyLinesAroundBlockBody` is configured with `EnforcedStyle: no_empty_lines`' do let(:other_cops) do { 'Layout/EmptyLinesAroundBlockBody' => { 'EnforcedStyle' => 'no_empty_lines' } } end context 'access modifier is the only child of the block' do it 'registers an offense but does not correct' do expect_offense(<<~RUBY, access_modifier: access_modifier) Module.new do %{access_modifier} ^{access_modifier} Keep a blank line after `%{access_modifier}`. end RUBY expect_no_corrections end end context 'access modifier is the first child of the block' do it 'registers an offense and corrects' do expect_offense(<<~RUBY, access_modifier: access_modifier) Module.new do %{access_modifier} ^{access_modifier} Keep a blank line after `%{access_modifier}`. foo end RUBY expect_correction(<<~RUBY) Module.new do #{access_modifier} foo end RUBY end end context 'access modifier is the last child of the block' do it 'registers an offense and partially corrects' do expect_offense(<<~RUBY, access_modifier: access_modifier) Module.new do foo %{access_modifier} ^{access_modifier} Keep a blank line before and after `%{access_modifier}`. end RUBY expect_correction(<<~RUBY) Module.new do foo #{access_modifier} end RUBY end end end context 'when `Layout/EmptyLinesAroundBlockBody` is configured with `EnforcedStyle: empty_lines`' do let(:other_cops) do { 'Layout/EmptyLinesAroundBlockBody' => { 'EnforcedStyle' => 'empty_lines' } } end context 'access modifier is the only child of the block' do it 'registers an offense and corrects' do expect_offense(<<~RUBY, access_modifier: access_modifier) Module.new do %{access_modifier} ^{access_modifier} Keep a blank line after `%{access_modifier}`. end RUBY expect_correction(<<~RUBY) Module.new do #{access_modifier} end RUBY end end context 'access modifier is the first child of the block' do it 'registers an offense and corrects' do expect_offense(<<~RUBY, access_modifier: access_modifier) Module.new do %{access_modifier} ^{access_modifier} Keep a blank line after `%{access_modifier}`. foo end RUBY expect_correction(<<~RUBY) Module.new do #{access_modifier} foo end RUBY end end context 'access modifier is the last child of the block' do it 'registers an offense and corrects' do expect_offense(<<~RUBY, access_modifier: access_modifier) Module.new do foo %{access_modifier} ^{access_modifier} Keep a blank line before and after `%{access_modifier}`. end RUBY expect_correction(<<~RUBY) Module.new do foo #{access_modifier} end RUBY end end end end end context 'EnforcedStyle is `only_before`' do let(:cop_config) { { 'EnforcedStyle' => 'only_before' } } %w[private protected].each do |access_modifier| it "accepts missing blank line after #{access_modifier}" do expect_no_offenses(<<~RUBY) class Test something #{access_modifier} def test; end end RUBY end it "registers an offense for blank line after #{access_modifier}" do expect_offense(<<~RUBY) class Test something #{access_modifier} #{'^' * access_modifier.size} Remove a blank line after `#{access_modifier}`. def test; end end RUBY expect_correction(<<~RUBY) class Test something #{access_modifier} def test; end end RUBY end it "does not register an offense when `end` immediately after #{access_modifier}" do expect_no_offenses(<<~RUBY) class Test #{access_modifier} end RUBY end end %w[public module_function].each do |access_modifier| it "accepts blank line after #{access_modifier}" do expect_no_offenses(<<~RUBY) module Kernel #{access_modifier} def do_something end end RUBY end end %w[private protected public module_function].each do |access_modifier| it "registers an offense for missing blank line before #{access_modifier}" do expect_offense(<<~RUBY) class Test something #{access_modifier} #{'^' * access_modifier.size} Keep a blank line before `#{access_modifier}`. def test; end end RUBY expect_correction(<<~RUBY) class Test something #{access_modifier} def test; end end RUBY end it 'does not register an offense when modifier is on the last line' do expect_no_offenses(<<~RUBY) #{access_modifier} RUBY end end end context 'Ruby 2.7', :ruby27 do %w[private protected public module_function].each do |access_modifier| it "registers an offense for missing around line before #{access_modifier}" do expect_offense(<<~RUBY) included do _1 #{access_modifier} #{'^' * access_modifier.size} Keep a blank line before and after `#{access_modifier}`. def test; end end RUBY expect_correction(<<~RUBY) included do _1 #{access_modifier} def test; end end RUBY end it "ignores #{access_modifier} with numblock argument" do expect_no_offenses(<<~RUBY) def foo #{access_modifier} { _1 } end RUBY end end end context 'Ruby 3.4', :ruby34 do %w[private protected public module_function].each do |access_modifier| it "registers an offense for missing around line before #{access_modifier}" do expect_offense(<<~RUBY) included do it #{access_modifier} #{'^' * access_modifier.size} Keep a blank line before and after `#{access_modifier}`. def test; end end RUBY expect_correction(<<~RUBY) included do it #{access_modifier} def test; end end RUBY end it "ignores #{access_modifier} with itblock argument" do expect_no_offenses(<<~RUBY) def foo #{access_modifier} { it } end RUBY end end end end
ruby
MIT
99fa0fdd0481910d7d052f4c2ed01ad36178f404
2026-01-04T15:37:41.211519Z
false
rubocop/rubocop
https://github.com/rubocop/rubocop/blob/99fa0fdd0481910d7d052f4c2ed01ad36178f404/spec/rubocop/cop/layout/first_array_element_line_break_spec.rb
spec/rubocop/cop/layout/first_array_element_line_break_spec.rb
# frozen_string_literal: true RSpec.describe RuboCop::Cop::Layout::FirstArrayElementLineBreak, :config do context 'elements listed on the first line' do it 'registers and corrects the offense' do expect_offense(<<~RUBY) a = [:a, ^^ Add a line break before the first element of a multi-line array. :b] RUBY # Alignment for the first element is set by IndentationWidth cop, # the rest of the elements should be aligned using the ArrayAlignment cop. expect_correction(<<~RUBY) a = [ :a, :b] RUBY end end context 'word arrays' do it 'registers and corrects the offense' do expect_offense(<<~RUBY) %w(a b ^ Add a line break before the first element of a multi-line array. c d) RUBY expect_correction(<<~RUBY) %w( a b c d) RUBY end end context 'array nested in a method call' do it 'registers and corrects the offense' do expect_offense(<<~RUBY) method([:foo, ^^^^ Add a line break before the first element of a multi-line array. :bar]) RUBY expect_correction(<<~RUBY) method([ :foo, :bar]) RUBY end end context 'masgn implicit arrays' do it 'registers and corrects the offense' do expect_offense(<<~RUBY) a, b, c = 1, ^ Add a line break before the first element of a multi-line array. 2, 3 RUBY expect_correction(<<~RUBY) a, b, c =#{trailing_whitespace} 1, 2, 3 RUBY end end context 'send implicit arrays' do it 'registers and corrects the offense' do expect_offense(<<~RUBY) a .c = 1, ^ Add a line break before the first element of a multi-line array. 2, 3 RUBY expect_correction(<<~RUBY) a .c =#{trailing_whitespace} 1, 2, 3 RUBY end end it 'ignores properly formatted implicit arrays' do expect_no_offenses(<<~RUBY) a, b, c = 1, 2, 3 RUBY end it 'ignores elements listed on a single line' do expect_no_offenses(<<~RUBY) b = [ :a, :b] RUBY end context 'last element can be multiline' do let(:cop_config) { { 'AllowMultilineFinalElement' => true } } it 'ignores last argument that is a multiline Hash' do expect_no_offenses(<<~RUBY) [a, b, { c: d }] RUBY end it 'ignores single value that is a multiline hash' do expect_no_offenses(<<~RUBY) [{ a: b }] RUBY end it 'registers and corrects values that are multiline hashes and not the last value' do expect_offense(<<~RUBY) [a, { ^ Add a line break before the first element of a multi-line array. b: c }, d] RUBY expect_correction(<<~RUBY) [ a, { b: c }, d] RUBY end it 'registers and corrects last value that starts on another line' do expect_offense(<<~RUBY) [a, b, ^ Add a line break before the first element of a multi-line array. c] RUBY expect_correction(<<~RUBY) [ a, b, c] RUBY end end end
ruby
MIT
99fa0fdd0481910d7d052f4c2ed01ad36178f404
2026-01-04T15:37:41.211519Z
false
rubocop/rubocop
https://github.com/rubocop/rubocop/blob/99fa0fdd0481910d7d052f4c2ed01ad36178f404/spec/rubocop/cop/layout/space_before_block_braces_spec.rb
spec/rubocop/cop/layout/space_before_block_braces_spec.rb
# frozen_string_literal: true RSpec.describe RuboCop::Cop::Layout::SpaceBeforeBlockBraces, :config do let(:cop_config) { { 'EnforcedStyle' => 'space' } } context 'when EnforcedStyle is space' do it 'accepts braces surrounded by spaces' do expect_no_offenses('each { puts }') end it 'registers an offense and corrects left brace without outer space' do expect_offense(<<~RUBY) each{ puts } ^ Space missing to the left of {. RUBY expect(cop.config_to_allow_offenses).to eq('EnforcedStyle' => 'no_space') expect_correction(<<~RUBY) each { puts } RUBY end it 'registers an offense and corrects opposite + correct style' do expect_offense(<<~RUBY) each{ puts } ^ Space missing to the left of {. each { puts } RUBY expect(cop.config_to_allow_offenses).to eq('Enabled' => false) expect_correction(<<~RUBY) each { puts } each { puts } RUBY end it 'registers an offense and corrects multiline block where the left ' \ 'brace has no outer space' do expect_offense(<<~RUBY) foo.map{ |a| ^ Space missing to the left of {. a.bar.to_s } RUBY expect_correction(<<~RUBY) foo.map { |a| a.bar.to_s } RUBY end context 'Ruby 2.7', :ruby27 do it 'registers an offense and corrects opposite + correct style' do expect_offense(<<~RUBY) each{ _1 } ^ Space missing to the left of {. each { _1 } RUBY expect(cop.config_to_allow_offenses).to eq('Enabled' => false) expect_correction(<<~RUBY) each { _1 } each { _1 } RUBY end it 'registers an offense and corrects multiline block where the left ' \ 'brace has no outer space' do expect_offense(<<~RUBY) foo.map{ ^ Space missing to the left of {. _1.bar.to_s } RUBY expect_correction(<<~RUBY) foo.map { _1.bar.to_s } RUBY end end context 'Ruby 3.4', :ruby34 do it 'registers an offense and corrects opposite + correct style' do expect_offense(<<~RUBY) each{ it } ^ Space missing to the left of {. each { it } RUBY expect(cop.config_to_allow_offenses).to eq('Enabled' => false) expect_correction(<<~RUBY) each { it } each { it } RUBY end it 'registers an offense and corrects multiline block where the left ' \ 'brace has no outer space' do expect_offense(<<~RUBY) foo.map{ ^ Space missing to the left of {. it.bar.to_s } RUBY expect_correction(<<~RUBY) foo.map { it.bar.to_s } RUBY end end end context 'when EnforcedStyle is no_space' do let(:cop_config) { { 'EnforcedStyle' => 'no_space' } } it 'registers an offense and corrects braces surrounded by spaces' do expect_offense(<<~RUBY) each { puts } ^ Space detected to the left of {. RUBY expect(cop.config_to_allow_offenses).to eq('EnforcedStyle' => 'space') expect_correction(<<~RUBY) each{ puts } RUBY end it 'registers an offense and corrects correct + opposite style' do expect_offense(<<~RUBY) each{ puts } each { puts } ^ Space detected to the left of {. RUBY expect(cop.config_to_allow_offenses).to eq('Enabled' => false) expect_correction(<<~RUBY) each{ puts } each{ puts } RUBY end it 'accepts left brace without outer space' do expect_no_offenses('each{ puts }') end context 'Ruby 2.7', :ruby27 do it 'registers an offense and corrects correct + opposite style' do expect_offense(<<~RUBY) each{ _1 } each { _1 } ^ Space detected to the left of {. RUBY expect(cop.config_to_allow_offenses).to eq('Enabled' => false) expect_correction(<<~RUBY) each{ _1 } each{ _1 } RUBY end end context 'with `EnforcedStyle` of `Style/BlockDelimiters`' do let(:config) do merged_config = RuboCop::ConfigLoader.default_configuration[ 'Layout/SpaceBeforeBlockBraces' ].merge(cop_config) RuboCop::Config.new( 'Layout/SpaceBeforeBlockBraces' => merged_config, 'Style/BlockDelimiters' => { 'EnforcedStyle' => 'line_count_based' } ) end it 'accepts left brace without outer space' do expect_no_offenses(<<~RUBY) let(:foo){{foo: 1, bar: 2}} RUBY end end end context 'with space before empty braces not allowed' do let(:cop_config) do { 'EnforcedStyle' => 'space', 'EnforcedStyleForEmptyBraces' => 'no_space' } end it 'accepts empty braces without outer space' do expect_no_offenses('->{}') end it 'registers an offense and corrects empty braces' do expect_offense(<<~RUBY) -> {} ^ Space detected to the left of {. RUBY expect(cop.config_to_allow_offenses).to eq('EnforcedStyleForEmptyBraces' => 'space') expect_correction(<<~RUBY) ->{} RUBY end end context 'with space before empty braces allowed' do let(:cop_config) do { 'EnforcedStyle' => 'no_space', 'EnforcedStyleForEmptyBraces' => 'space' } end it 'accepts empty braces with outer space' do expect_no_offenses('-> {}') end it 'registers an offense and corrects empty braces' do expect_offense(<<~RUBY) ->{} ^ Space missing to the left of {. RUBY expect(cop.config_to_allow_offenses).to eq('EnforcedStyleForEmptyBraces' => 'no_space') expect_correction(<<~RUBY) -> {} RUBY end end context 'with invalid value for EnforcedStyleForEmptyBraces' do let(:cop_config) { { 'EnforcedStyleForEmptyBraces' => 'unknown' } } it 'fails with an error' do expect { expect_no_offenses('each {}') } .to raise_error('Unknown EnforcedStyleForEmptyBraces selected!') end end end
ruby
MIT
99fa0fdd0481910d7d052f4c2ed01ad36178f404
2026-01-04T15:37:41.211519Z
false
rubocop/rubocop
https://github.com/rubocop/rubocop/blob/99fa0fdd0481910d7d052f4c2ed01ad36178f404/spec/rubocop/cop/layout/empty_line_between_defs_spec.rb
spec/rubocop/cop/layout/empty_line_between_defs_spec.rb
# frozen_string_literal: true RSpec.describe RuboCop::Cop::Layout::EmptyLineBetweenDefs, :config do let(:cop_config) { { 'AllowAdjacentOneLineDefs' => false } } it 'finds offenses in inner classes' do expect_offense(<<~RUBY) class K def m end class J def n end def o ^^^^^ Expected 1 empty line between method definitions; found 0. end end # checks something def p end end RUBY end context 'when there are only comments between defs' do it 'registers an offense' do expect_offense(<<~RUBY) class J def n end # n-related # checks something o-related # and more def o ^^^^^ Expected 1 empty line between method definitions; found 0. end end RUBY expect_correction(<<~RUBY) class J def n end # n-related # checks something o-related # and more def o end end RUBY end end context 'conditional method definitions' do it 'accepts defs inside a conditional without blank lines in between' do expect_no_offenses(<<~RUBY) if condition def foo true end else def foo false end end RUBY end it 'registers an offense for consecutive defs inside a conditional' do expect_offense(<<~RUBY) if condition def foo true end def bar ^^^^^^^ Expected 1 empty line between method definitions; found 0. true end else def foo false end end RUBY end end context 'class methods' do context 'adjacent class methods' do it 'registers an offense for missing blank line between methods' do expect_offense(<<~RUBY) class Test def self.foo true end def self.bar ^^^^^^^^^^^^ Expected 1 empty line between method definitions; found 0. true end end RUBY expect_correction(<<~RUBY) class Test def self.foo true end def self.bar true end end RUBY end end context 'mixed instance and class methods' do it 'registers an offense for missing blank line between methods' do expect_offense(<<~RUBY) class Test def foo true end def self.bar ^^^^^^^^^^^^ Expected 1 empty line between method definitions; found 0. true end end RUBY expect_correction(<<~RUBY) class Test def foo true end def self.bar true end end RUBY end end end # Only one def, so rule about empty line *between* defs does not # apply. it 'accepts a def that follows a line with code' do expect_no_offenses(<<~RUBY) x = 0 def m end RUBY end # Only one def, so rule about empty line *between* defs does not # apply. it 'accepts a def that follows code and a comment' do expect_no_offenses(<<~RUBY) x = 0 # 123 def m end RUBY end it 'accepts the first def without leading empty line in a class' do expect_no_offenses(<<~RUBY) class K def m end end RUBY end it 'accepts a def that follows an empty line and then a comment' do expect_no_offenses(<<~RUBY) class A # calculates value def m end private # calculates size def n end end RUBY end it 'accepts a def that is the first of a module' do expect_no_offenses(<<~RUBY) module Util public # def html_escape(s) end end RUBY end it 'accepts a nested def' do expect_no_offenses(<<~RUBY) def mock_model(*attributes) Class.new do def initialize(attrs) end end end RUBY end it 'registers an offense for adjacent one-liners by default' do expect_offense(<<~RUBY) def a; end def b; end ^^^^^ Expected 1 empty line between method definitions; found 0. RUBY expect_correction(<<~RUBY) def a; end def b; end RUBY end it 'registers an offense when two method definitions are on the same line separated by a semicolon' do expect_offense(<<~RUBY) def a end;def b ^^^^^ Expected 1 empty line between method definitions; found 0. end RUBY expect_correction(<<~RUBY) def a end; def b end RUBY end it 'autocorrects when there are too many new lines' do expect_offense(<<~RUBY) def a; end def b; end ^^^^^ Expected 1 empty line between method definitions; found 3. RUBY expect_correction(<<~RUBY) def a; end def b; end RUBY end it 'treats lines with whitespaces as blank' do expect_no_offenses(<<~RUBY) class J def n end def o end end RUBY end it "doesn't allow more than the required number of newlines" do expect_offense(<<~RUBY) class A def n end def o ^^^^^ Expected 1 empty line between method definitions; found 2. end end RUBY expect_correction(<<~RUBY) class A def n end def o end end RUBY end it 'registers an offense for multiple one-liners on the same line' do expect_offense(<<~RUBY) def a; end; def b; end ^^^^^ Expected 1 empty line between method definitions; found 0. RUBY expect_correction(<<~RUBY) def a; end;#{trailing_whitespace} def b; end RUBY end context 'when AllowAdjacentOneLineDefs is enabled' do let(:cop_config) { { 'AllowAdjacentOneLineDefs' => true } } it 'accepts adjacent one-liners' do expect_no_offenses(<<~RUBY) def a; end def b; end RUBY end it 'registers an offense for adjacent defs if some are multi-line' do expect_offense(<<~RUBY) def a; end def b; end def c # Not a one-liner, so this is an offense. ^^^^^ Expected 1 empty line between method definitions; found 0. end def d; end # Also an offense since previous was multi-line: ^^^^^ Expected 1 empty line between method definitions; found 0. RUBY expect_correction(<<~RUBY) def a; end def b; end def c # Not a one-liner, so this is an offense. end def d; end # Also an offense since previous was multi-line: RUBY end end context 'when a maximum of empty lines is specified' do let(:cop_config) { { 'NumberOfEmptyLines' => [0, 1] } } it 'finds no offense for no empty line' do expect_no_offenses(<<~RUBY) def n end def o end RUBY end it 'finds no offense for one empty line' do expect_no_offenses(<<~RUBY) def n end def o end RUBY end it 'finds an offense for two empty lines' do expect_offense(<<~RUBY) def n end def o ^^^^^ Expected 0..1 empty lines between method definitions; found 2. end RUBY expect_correction(<<~RUBY) def n end def o end RUBY end end context 'when multiple lines between defs are allowed' do let(:cop_config) { { 'NumberOfEmptyLines' => 2 } } it 'treats lines with whitespaces as blank' do expect_offense(<<~RUBY) def n end def o ^^^^^ Expected 2 empty lines between method definitions; found 1. end RUBY expect_correction(<<~RUBY) def n end def o end RUBY end it 'registers an offense and corrects when there are too many new lines' do expect_offense(<<~RUBY) def n end def o ^^^^^ Expected 2 empty lines between method definitions; found 4. end RUBY expect_correction(<<~RUBY) def n end def o end RUBY end end context 'EmptyLineBetweenClassDefs' do it 'registers an offense when no empty lines between class and method definitions' do expect_offense(<<~RUBY) class Foo end class Baz ^^^^^^^^^ Expected 1 empty line between class definitions; found 0. end def example ^^^^^^^^^^^ Expected 1 empty line between method definitions; found 0. end RUBY expect_correction(<<~RUBY) class Foo end class Baz end def example end RUBY end context 'when disabled' do let(:cop_config) { { 'EmptyLineBetweenClassDefs' => false } } it 'does not register offense' do expect_no_offenses(<<~RUBY) class Foo end class Baz end def example end RUBY end end context 'with AllowAdjacentOneLineDefs enabled' do let(:cop_config) { { 'AllowAdjacentOneLineDefs' => true } } it 'does not register offense' do expect_no_offenses(<<~RUBY) class Foo; end class Baz; end RUBY end end end context 'EmptyLineBetweenModuleDefs' do it 'registers an offense when no empty lines between module and method definitions' do expect_offense(<<~RUBY) module Foo end module Baz ^^^^^^^^^^ Expected 1 empty line between module definitions; found 0. end def example ^^^^^^^^^^^ Expected 1 empty line between method definitions; found 0. end RUBY expect_correction(<<~RUBY) module Foo end module Baz end def example end RUBY end context 'when disabled' do let(:cop_config) { { 'EmptyLineBetweenModuleDefs' => false } } it 'does not register offense' do expect_no_offenses(<<~RUBY) module Foo end module Baz end def example end RUBY end end end context 'when empty lines between classes and modules together' do it 'registers an offense when no empty lines between module and method definitions' do expect_offense(<<~RUBY) class Foo end module Baz ^^^^^^^^^^ Expected 1 empty line between module definitions; found 0. end def example ^^^^^^^^^^^ Expected 1 empty line between method definitions; found 0. end RUBY expect_correction(<<~RUBY) class Foo end module Baz end def example end RUBY end end context 'endless methods', :ruby30 do context 'between endless and regular methods' do it 'registers an offense and corrects' do expect_offense(<<~RUBY) def foo() = x def bar ^^^^^^^ Expected 1 empty line between method definitions; found 0. y end RUBY expect_correction(<<~RUBY) def foo() = x def bar y end RUBY end end context 'between regular and endless methods' do it 'registers an offense and corrects' do expect_offense(<<~RUBY) def foo x end def bar() = y ^^^^^^^ Expected 1 empty line between method definitions; found 0. RUBY expect_correction(<<~RUBY) def foo x end def bar() = y RUBY end end context 'between endless class method and regular methods' do it 'registers an offense and corrects' do expect_offense(<<~RUBY) def self.foo = x def bar ^^^^^^^ Expected 1 empty line between method definitions; found 0. y end RUBY expect_correction(<<~RUBY) def self.foo = x def bar y end RUBY end end context 'between endless class method and regular class methods' do it 'registers an offense and corrects' do expect_offense(<<~RUBY) def self.foo = x def self.bar ^^^^^^^^^^^^ Expected 1 empty line between method definitions; found 0. y end RUBY expect_correction(<<~RUBY) def self.foo = x def self.bar y end RUBY end end context 'with AllowAdjacentOneLineDefs: false' do it 'registers an offense and corrects' do expect_offense(<<~RUBY) def foo() = x def bar() = y ^^^^^^^ Expected 1 empty line between method definitions; found 0. RUBY expect_correction(<<~RUBY) def foo() = x def bar() = y RUBY end end context 'with AllowAdjacentOneLineDefs: true' do let(:cop_config) { { 'AllowAdjacentOneLineDefs' => true } } it 'does not register an offense' do expect_no_offenses(<<~RUBY) def foo() = x def bar() = y RUBY end end end context 'DefLikeMacros: [\'foo\']' do let(:allow_adjacent_one_line_defs) { true } let(:cop_config) do { 'DefLikeMacros' => ['foo'], 'AllowAdjacentOneLineDefs' => allow_adjacent_one_line_defs } end it 'registers an offense' do expect_offense(<<~RUBY) foo 'first foo' do #foo body end foo 'second foo' do ^^^^^^^^^^^^^^^^^^^ Expected 1 empty line between block definitions; found 0. #foo body end RUBY expect_correction(<<~RUBY) foo 'first foo' do #foo body end foo 'second foo' do #foo body end RUBY end it 'registers an offense if next to method' do expect_offense(<<~RUBY) def foo_first_foo #foo body end foo 'second foo' do ^^^^^^^^^^^^^^^^^^^ Expected 1 empty line between block definitions; found 0. #foo body end RUBY expect_correction(<<~RUBY) def foo_first_foo #foo body end foo 'second foo' do #foo body end RUBY end it 'registers an offense if next to numblock' do expect_offense(<<~RUBY) foo 'first foo' do #foo body end foo 'second foo' do ^^^^^^^^^^^^^^^^^^^ Expected 1 empty line between block definitions; found 0. _1 end RUBY expect_correction(<<~RUBY) foo 'first foo' do #foo body end foo 'second foo' do _1 end RUBY end it 'registers an offense if next to itblock', :ruby34 do expect_offense(<<~RUBY) foo 'first foo' do #foo body end foo 'second foo' do ^^^^^^^^^^^^^^^^^^^ Expected 1 empty line between block definitions; found 0. it end RUBY expect_correction(<<~RUBY) foo 'first foo' do #foo body end foo 'second foo' do it end RUBY end it 'does not register offense' do expect_no_offenses(<<~RUBY) foo 'first foo' do #foo body end foo 'second foo' do #foo body end RUBY end it 'does not register offense for non registered macro names' do expect_no_offenses(<<~RUBY) bar "bar" do #bar body end foo 'first foo' do #foo body end sig {void} foo 'second foo' do #foo body end RUBY end it 'does not register an offense for single-line macros' do expect_no_offenses(<<~RUBY) foo :first_attribute foo :second_attribute RUBY end context 'and AllowAdjacentOneLineDefs: false' do let(:allow_adjacent_one_line_defs) { false } it 'registers an offense for macros that take no block' do expect_offense(<<~RUBY) foo :first_attribute foo :second_attribute ^^^^^^^^^^^^^^^^^^^^^ Expected 1 empty line between send definitions; found 0. RUBY expect_correction(<<~RUBY) foo :first_attribute foo :second_attribute RUBY end end end end
ruby
MIT
99fa0fdd0481910d7d052f4c2ed01ad36178f404
2026-01-04T15:37:41.211519Z
false
rubocop/rubocop
https://github.com/rubocop/rubocop/blob/99fa0fdd0481910d7d052f4c2ed01ad36178f404/spec/rubocop/cop/layout/closing_heredoc_indentation_spec.rb
spec/rubocop/cop/layout/closing_heredoc_indentation_spec.rb
# frozen_string_literal: true RSpec.describe RuboCop::Cop::Layout::ClosingHeredocIndentation, :config do let(:config) { RuboCop::Config.new('Layout/ClosingHeredocIndentation' => cop_config) } let(:cop_config) { { 'Enabled' => true } } it 'accepts correctly indented closing heredoc' do expect_no_offenses(<<~RUBY) class Test def foo <<-SQL bar SQL end end RUBY end it 'accepts correctly indented closing heredoc when heredoc contents is after closing heredoc' do expect_no_offenses(<<~RUBY) include_examples :offense, <<-EOS foo bar EOS RUBY end it 'accepts correctly indented closing heredoc when heredoc contents with blank line' do expect_no_offenses(<<~RUBY) def_node_matcher :eval_without_location?, <<~PATTERN { (send $(send _ $:sort ...) ${:[] :at :slice} {(int 0) (int -1)}) (send $(send _ $:sort_by _) ${:last :first}) } PATTERN RUBY end it 'accepts correctly indented closing heredoc when aligned at ' \ 'the beginning of method definition' do expect_no_offenses(<<~RUBY) include_examples :offense, <<-EOS bar EOS RUBY end it 'accepts correctly indented closing heredoc when aligned at ' \ 'the beginning of method definition and using `strip_indent`' do expect_no_offenses(<<~RUBY) include_examples :offense, <<-EOS.strip_indent bar EOS RUBY end it 'accepts correctly indented closing heredoc when aligned at ' \ 'the beginning of method definition and content is empty' do expect_no_offenses(<<~RUBY) let(:source) { <<~EOS } EOS RUBY end it 'accepts correctly indented closing heredoc when heredoc contents is before closing heredoc' do expect_no_offenses(<<~RUBY) include_examples :offense, <<-EOS foo bar baz EOS RUBY end it 'registers an offense for bad indentation of a closing heredoc' do expect_offense(<<~RUBY) class Test def foo <<-SQL bar SQL ^^^^^ `SQL` is not aligned with `<<-SQL`. end end RUBY expect_correction(<<~RUBY) class Test def foo <<-SQL bar SQL end end RUBY end it 'registers an offense for incorrectly indented empty heredocs' do expect_offense(<<~RUBY) def foo <<-NIL NIL ^^^^^^^ `NIL` is not aligned with `<<-NIL`. end RUBY expect_correction(<<~RUBY) def foo <<-NIL NIL end RUBY end it 'does not register an offense for correctly indented empty heredocs' do expect_no_offenses(<<~RUBY) def foo <<-NIL NIL end RUBY end it 'does not register an offense for a << heredoc' do expect_no_offenses(<<~RUBY) def foo <<NIL NIL end RUBY end end
ruby
MIT
99fa0fdd0481910d7d052f4c2ed01ad36178f404
2026-01-04T15:37:41.211519Z
false
rubocop/rubocop
https://github.com/rubocop/rubocop/blob/99fa0fdd0481910d7d052f4c2ed01ad36178f404/spec/rubocop/cop/layout/space_before_comment_spec.rb
spec/rubocop/cop/layout/space_before_comment_spec.rb
# frozen_string_literal: true RSpec.describe RuboCop::Cop::Layout::SpaceBeforeComment, :config do it 'registers an offense and corrects missing space before an EOL comment' do expect_offense(<<~RUBY) a += 1# increment ^^^^^^^^^^^ Put a space before an end-of-line comment. RUBY expect_correction(<<~RUBY) a += 1 # increment RUBY end it 'accepts an EOL comment with a preceding space' do expect_no_offenses('a += 1 # increment') end it 'accepts a comment that begins a line' do expect_no_offenses('# comment') end it 'accepts a doc comment' do expect_no_offenses(<<~RUBY) =begin Doc comment =end RUBY end it 'registers an offense and corrects after a heredoc' do expect_offense(<<~RUBY) <<~STR# my string ^^^^^^^^^^^ Put a space before an end-of-line comment. text STR RUBY expect_correction(<<~RUBY) <<~STR # my string text STR RUBY end end
ruby
MIT
99fa0fdd0481910d7d052f4c2ed01ad36178f404
2026-01-04T15:37:41.211519Z
false
rubocop/rubocop
https://github.com/rubocop/rubocop/blob/99fa0fdd0481910d7d052f4c2ed01ad36178f404/spec/rubocop/cop/layout/first_method_parameter_line_break_spec.rb
spec/rubocop/cop/layout/first_method_parameter_line_break_spec.rb
# frozen_string_literal: true RSpec.describe RuboCop::Cop::Layout::FirstMethodParameterLineBreak, :config do it 'registers an offense and corrects params listed on the first line' do expect_offense(<<~RUBY) def foo(bar, ^^^ Add a line break before the first parameter of a multi-line method parameter list. baz) do_something end RUBY expect_correction(<<~RUBY) def foo( bar, baz) do_something end RUBY end it 'registers an offense and corrects params on first line of singleton method' do expect_offense(<<~RUBY) def self.foo(bar, ^^^ Add a line break before the first parameter of a multi-line method parameter list. baz) do_something end RUBY expect_correction(<<~RUBY) def self.foo( bar, baz) do_something end RUBY end it 'accepts params listed on a single line' do expect_no_offenses(<<~RUBY) def foo(bar, baz, bing) do_something end RUBY end it 'accepts params without parens' do expect_no_offenses(<<~RUBY) def foo bar, baz do_something end RUBY end it 'accepts single-line methods' do expect_no_offenses('def foo(bar, baz) ; bing ; end') end it 'accepts methods without params' do expect_no_offenses(<<~RUBY) def foo bing end RUBY end it 'registers an offense and corrects params with default values' do expect_offense(<<~RUBY) def foo(bar = [], ^^^^^^^^ Add a line break before the first parameter of a multi-line method parameter list. baz = 2) do_something end RUBY expect_correction(<<~RUBY) def foo( bar = [], baz = 2) do_something end RUBY end context 'last element can be multiline' do let(:cop_config) { { 'AllowMultilineFinalElement' => true } } it 'ignores last argument that value is a multiline Hash' do expect_no_offenses(<<~RUBY) def foo(bar, baz = { a: b }) do_something end RUBY end it 'ignores single argument that value is a multiline hash' do expect_no_offenses(<<~RUBY) def foo(bar = { a: b }) do_something end RUBY end it 'registers and corrects parameters that value is a multiline hashes and is not the last parameter' do expect_offense(<<~RUBY) def foo(bar, baz = { ^^^ Add a line break before the first parameter of a multi-line method parameter list. a: b }, qux = false) do_something end RUBY expect_correction(<<~RUBY) def foo( bar, baz = { a: b }, qux = false) do_something end RUBY end it 'registers and corrects last parameter that starts on another line' do expect_offense(<<~RUBY) def foo(bar, baz, ^^^ Add a line break before the first parameter of a multi-line method parameter list. qux = false) do_something end RUBY expect_correction(<<~RUBY) def foo( bar, baz, qux = false) do_something end RUBY end end end
ruby
MIT
99fa0fdd0481910d7d052f4c2ed01ad36178f404
2026-01-04T15:37:41.211519Z
false
rubocop/rubocop
https://github.com/rubocop/rubocop/blob/99fa0fdd0481910d7d052f4c2ed01ad36178f404/spec/rubocop/cop/layout/heredoc_argument_closing_parenthesis_spec.rb
spec/rubocop/cop/layout/heredoc_argument_closing_parenthesis_spec.rb
# frozen_string_literal: true RSpec.describe RuboCop::Cop::Layout::HeredocArgumentClosingParenthesis, :config do shared_examples 'correct cases' do |dot| context 'correct cases' do it 'accepts simple correct case' do expect_no_offenses(<<~RUBY) foo#{dot}bar(<<-SQL) foo SQL RUBY end it 'accepts double correct case' do expect_no_offenses(<<~RUBY) foo#{dot}bar(<<-SQL, <<-NOSQL) foo SQL bar NOSQL RUBY end it 'accepts method chain with heredoc argument correct case' do expect_no_offenses(<<~RUBY) do_something( Model .foo#{dot}bar(<<~CODE) code CODE .baz(<<~CODE)) code CODE RUBY end it 'accepts method chain with safe navigation with heredoc argument correct case' do expect_no_offenses(<<~RUBY) do_something( Model .foo#{dot}bar(<<~CODE) code CODE &.baz(<<~CODE)) code CODE RUBY end it 'accepts method with heredoc argument of proc correct case' do expect_no_offenses(<<~RUBY) outer_method(-> { inner_method#{dot}chain(<<~CODE) code CODE }) RUBY end it 'accepts double correct case nested' do expect_no_offenses(<<~RUBY) baz(bar(foo#{dot}bar(<<-SQL, <<-NOSQL))) foo SQL bar NOSQL RUBY end it 'accepts double correct case new line' do expect_no_offenses(<<~RUBY) foo#{dot}bar( <<-SQL, <<-NOSQL) foo SQL bar NOSQL RUBY end it 'accepts when there is an argument between a heredoc argument and the closing parentheses' do expect_no_offenses(<<~RUBY) foo#{dot}bar(<<~TEXT, Lots of Lovely Text TEXT some_arg: { foo: "bar" } ) RUBY end it 'accepts when heredoc is a method argument in a parenthesized block argument' do expect_no_offenses(<<~RUBY) foo(bar do baz#{dot}quux <<~EOS EOS end) RUBY end it 'accepts when heredoc is a branch body in a method argument of a parenthesized argument' do expect_no_offenses(<<~RUBY) foo(unless condition bar#{dot}baz(<<~EOS) text EOS end) RUBY end it 'accepts when heredoc is a branch body in a nested method argument of a parenthesized argument' do expect_no_offenses(<<~RUBY) foo(unless condition bar(baz#{dot}quux(<<~EOS)) text EOS end) RUBY end it 'accepts correct case with other param after' do expect_no_offenses(<<~RUBY) foo#{dot}bar(<<-SQL, 123) foo SQL RUBY end it 'accepts correct case with other param before' do expect_no_offenses(<<~RUBY) foo#{dot}bar(123, <<-SQL) foo SQL RUBY end it 'accepts hash correct case' do expect_no_offenses(<<~RUBY) foo#{dot}bar(foo: <<-SQL) foo SQL RUBY end context 'invocation after the HEREDOC' do it 'ignores tr' do expect_no_offenses(<<~RUBY) foo#{dot}bar( <<-SQL.tr("z", "t")) baz SQL RUBY end it 'ignores random call' do expect_no_offenses(<<~RUBY) foo#{dot}bar( <<-TEXT.foo) foobarbaz TEXT RUBY end it 'ignores random call after' do expect_no_offenses(<<~RUBY) foo#{dot}bar( <<-TEXT foobarbaz TEXT .foo ) RUBY end end end end shared_examples 'incorrect cases' do |dot| context 'incorrect cases' do context 'simple incorrect case' do it 'registers an offense' do expect_offense(<<~RUBY) foo#{dot}bar(<<-SQL foo SQL ) ^ Put the closing parenthesis for a method call with a HEREDOC parameter on the same line as the HEREDOC opening. RUBY expect_correction(<<~RUBY) foo#{dot}bar(<<-SQL) foo SQL RUBY end end context 'simple incorrect case with call after' do it 'registers an offense' do expect_offense(<<~RUBY) foo#{dot}bar(<<~SQL foo SQL ).baz ^ Put the closing parenthesis for a method call with a HEREDOC parameter on the same line as the HEREDOC opening. RUBY expect_correction(<<~RUBY) foo#{dot}bar(<<~SQL) foo SQL .baz RUBY end end context 'simple incorrect case with call after with safe navigation' do it 'registers an offense' do expect_offense(<<~RUBY) foo#{dot}bar(<<~SQL foo SQL )&.baz ^ Put the closing parenthesis for a method call with a HEREDOC parameter on the same line as the HEREDOC opening. RUBY expect_correction(<<~RUBY) foo#{dot}bar(<<~SQL) foo SQL &.baz RUBY end end context 'simple incorrect case with call after trailing comma' do it 'registers an offense' do expect_offense(<<~RUBY) foo#{dot}bar(<<~SQL, foo SQL ).baz ^ Put the closing parenthesis for a method call with a HEREDOC parameter on the same line as the HEREDOC opening. RUBY expect_correction(<<~RUBY) foo#{dot}bar(<<~SQL) foo SQL .baz RUBY end end context 'simple incorrect case hash' do it 'registers an offense' do expect_offense(<<~RUBY) foo#{dot}bar(foo: <<-SQL foo SQL ) ^ Put the closing parenthesis for a method call with a HEREDOC parameter on the same line as the HEREDOC opening. RUBY expect_correction(<<~RUBY) foo#{dot}bar(foo: <<-SQL) foo SQL RUBY end end context 'nested incorrect case' do it 'registers an offense' do expect_offense(<<~RUBY) foo(foo#{dot}bar(<<-SQL) foo SQL ) ^ Put the closing parenthesis for a method call with a HEREDOC parameter on the same line as the HEREDOC opening. RUBY expect_correction(<<~RUBY) foo(foo#{dot}bar(<<-SQL)) foo SQL RUBY end end context 'simple incorrect case squiggles' do it 'registers an offense' do expect_offense(<<~RUBY) foo#{dot}bar(<<~SQL foo SQL ) ^ Put the closing parenthesis for a method call with a HEREDOC parameter on the same line as the HEREDOC opening. RUBY expect_correction(<<~RUBY) foo#{dot}bar(<<~SQL) foo SQL RUBY end end context 'simple incorrect case comma' do it 'registers an offense' do expect_offense(<<~RUBY) foo#{dot}bar(<<-SQL, foo SQL ) ^ Put the closing parenthesis for a method call with a HEREDOC parameter on the same line as the HEREDOC opening. RUBY expect_correction(<<~RUBY) foo#{dot}bar(<<-SQL) foo SQL RUBY end end context 'simple incorrect case comma with spaces' do it 'registers an offense' do expect_offense(<<~RUBY) foo#{dot}bar(<<-SQL , foo SQL ) ^ Put the closing parenthesis for a method call with a HEREDOC parameter on the same line as the HEREDOC opening. RUBY expect_correction(<<~RUBY) foo#{dot}bar(<<-SQL) foo SQL RUBY end end context 'simple incorrect case comma with spaces and comma in heredoc' do it 'registers an offense' do expect_offense(<<~RUBY) foo#{dot}bar(<<-SQL , foo, SQL ) ^ Put the closing parenthesis for a method call with a HEREDOC parameter on the same line as the HEREDOC opening. RUBY expect_correction(<<~RUBY) foo#{dot}bar(<<-SQL) foo, SQL RUBY end end context 'double incorrect case' do it 'registers an offense' do expect_offense(<<~RUBY) foo#{dot}bar(<<-SQL, <<-NOSQL foo SQL bar NOSQL ) ^ Put the closing parenthesis for a method call with a HEREDOC parameter on the same line as the HEREDOC opening. RUBY expect_correction(<<~RUBY) foo#{dot}bar(<<-SQL, <<-NOSQL) foo SQL bar NOSQL RUBY end end context 'double incorrect case new line chained calls' do it 'registers an offense' do expect_offense(<<~RUBY) foo#{dot}bar(<<-SQL, <<-NOSQL foo SQL bar NOSQL ).baz(123).quux(456) ^ Put the closing parenthesis for a method call with a HEREDOC parameter on the same line as the HEREDOC opening. RUBY expect_correction(<<~RUBY) foo#{dot}bar(<<-SQL, <<-NOSQL) foo SQL bar NOSQL .baz(123).quux(456) RUBY end end context 'incorrect case with other param after' do it 'registers an offense' do expect_offense(<<~RUBY) foo#{dot}bar(<<-SQL, 123 foo SQL ) ^ Put the closing parenthesis for a method call with a HEREDOC parameter on the same line as the HEREDOC opening. RUBY expect_correction(<<~RUBY) foo#{dot}bar(<<-SQL, 123) foo SQL RUBY end end context 'incorrect case with other param before' do it 'registers an offense' do expect_offense(<<~RUBY) foo#{dot}bar(123, <<-SQL foo SQL ) ^ Put the closing parenthesis for a method call with a HEREDOC parameter on the same line as the HEREDOC opening. RUBY expect_correction(<<~RUBY) foo#{dot}bar(123, <<-SQL) foo SQL RUBY end end context 'incorrect case with other param before constructor' do it 'registers an offense' do expect_offense(<<~RUBY) Foo.new(123, <<-SQL foo SQL ) ^ Put the closing parenthesis for a method call with a HEREDOC parameter on the same line as the HEREDOC opening. RUBY expect_correction(<<~RUBY) Foo.new(123, <<-SQL) foo SQL RUBY end end context 'incorrect case with other param before constructor and raise call' do it 'registers an offense' do expect_offense(<<~RUBY) raise Foo.new(123, <<-SQL foo SQL ) ^ Put the closing parenthesis for a method call with a HEREDOC parameter on the same line as the HEREDOC opening. RUBY expect_correction(<<~RUBY) raise Foo.new(123, <<-SQL) foo SQL RUBY end end context 'incorrect case nested method call with comma' do it 'registers an offense' do expect_offense(<<~RUBY) bar( foo#{dot}bar(123, <<-SQL foo SQL ), ^ Put the closing parenthesis for a method call with a HEREDOC parameter on the same line as the HEREDOC opening. 456, 789, ) RUBY expect_correction(<<~RUBY, loop: false) bar( foo#{dot}bar(123, <<-SQL), foo SQL 456, 789, ) RUBY end end context 'incorrect case in array with spaced out comma' do it 'registers an offense' do expect_offense(<<~RUBY) [ foo#{dot}bar(123, <<-SQL foo SQL ) , ^ Put the closing parenthesis for a method call with a HEREDOC parameter on the same line as the HEREDOC opening. 456, 789, ] RUBY expect_correction(<<~RUBY) [ foo#{dot}bar(123, <<-SQL), foo SQL 456, 789, ] RUBY end end context 'incorrect case in array with double heredoc and spaced out comma' do it 'registers an offense' do expect_offense(<<~RUBY) [ foo#{dot}bar(123, <<-SQL, 456, 789, <<-NOSQL, foo SQL bar NOSQL ) , ^ Put the closing parenthesis for a method call with a HEREDOC parameter on the same line as the HEREDOC opening. 456, 789, ] RUBY expect_correction(<<~RUBY) [ foo#{dot}bar(123, <<-SQL, 456, 789, <<-NOSQL), foo SQL bar NOSQL 456, 789, ] RUBY end end context 'incorrect case in array with nested calls and double heredoc and spaced out comma' do it 'registers an offense' do expect_offense(<<~RUBY) [ foo(foo(foo#{dot}bar(123, <<-SQL, 456, 789, <<-NOSQL), 456), 400, foo SQL bar NOSQL ) , ^ Put the closing parenthesis for a method call with a HEREDOC parameter on the same line as the HEREDOC opening. 456, 789, ] RUBY expect_correction(<<~RUBY) [ foo(foo(foo#{dot}bar(123, <<-SQL, 456, 789, <<-NOSQL), 456), 400), foo SQL bar NOSQL 456, 789, ] RUBY end end context 'complex incorrect case with multiple calls' do it 'detects and fixes the first' do expect_offense(<<~RUBY) query.order(foo#{dot}bar(<<-SQL, foo SQL )) ^ Put the closing parenthesis for a method call with a HEREDOC parameter on the same line as the HEREDOC opening. RUBY expect_correction(<<~RUBY, loop: false) query.order(foo#{dot}bar(<<-SQL) foo SQL ) RUBY end it 'detects and fixes the second' do expect_offense(<<~RUBY) query.order(foo#{dot}bar(<<-SQL) foo SQL ) ^ Put the closing parenthesis for a method call with a HEREDOC parameter on the same line as the HEREDOC opening. RUBY expect_correction(<<~RUBY) query.order(foo#{dot}bar(<<-SQL)) foo SQL RUBY end end context 'complex chained incorrect case with multiple calls' do it 'detects and fixes the first' do expect_offense(<<~RUBY) query.joins({ foo: [] }).order(foo#{dot}bar(<<-SQL), bar SQL ) ^ Put the closing parenthesis for a method call with a HEREDOC parameter on the same line as the HEREDOC opening. RUBY expect_correction(<<~RUBY) query.joins({ foo: [] }).order(foo#{dot}bar(<<-SQL)) bar SQL RUBY end end context 'double case new line' do it 'detects and fixes' do expect_offense(<<~RUBY) foo#{dot}bar( <<-SQL, <<-NOSQL foo SQL bar NOSQL ) ^ Put the closing parenthesis for a method call with a HEREDOC parameter on the same line as the HEREDOC opening. RUBY expect_correction(<<~RUBY) foo#{dot}bar( <<-SQL, <<-NOSQL) foo SQL bar NOSQL RUBY end end end end it_behaves_like 'correct cases', '.' it_behaves_like 'correct cases', '&.' it_behaves_like 'incorrect cases', '.' it_behaves_like 'incorrect cases', '&.' end
ruby
MIT
99fa0fdd0481910d7d052f4c2ed01ad36178f404
2026-01-04T15:37:41.211519Z
false
rubocop/rubocop
https://github.com/rubocop/rubocop/blob/99fa0fdd0481910d7d052f4c2ed01ad36178f404/spec/rubocop/cop/layout/first_method_argument_line_break_spec.rb
spec/rubocop/cop/layout/first_method_argument_line_break_spec.rb
# frozen_string_literal: true RSpec.describe RuboCop::Cop::Layout::FirstMethodArgumentLineBreak, :config do context 'args listed on the first line' do it 'registers an offense and corrects' do expect_offense(<<~RUBY) foo(bar, ^^^ Add a line break before the first argument of a multi-line method argument list. baz) RUBY expect_correction(<<~RUBY) foo( bar, baz) RUBY end it 'registers an offense and corrects using `super`' do expect_offense(<<~RUBY) super(bar, ^^^ Add a line break before the first argument of a multi-line method argument list. baz) RUBY expect_correction(<<~RUBY) super( bar, baz) RUBY end it 'registers an offense and corrects using safe navigation operator' do expect_offense(<<~RUBY) receiver&.foo(bar, ^^^ Add a line break before the first argument of a multi-line method argument list. baz) RUBY expect_correction(<<~RUBY) receiver&.foo( bar, baz) RUBY end end it 'registers an offense and corrects hash arg spanning multiple lines' do expect_offense(<<~RUBY) something(3, bar: 1, ^ Add a line break before the first argument of a multi-line method argument list. baz: 2) RUBY expect_correction(<<~RUBY) something( 3, bar: 1, baz: 2) RUBY end it 'registers an offense and corrects hash arg without a line break before the first pair' do expect_offense(<<~RUBY) something(bar: 1, ^^^^^^ Add a line break before the first argument of a multi-line method argument list. baz: 2) RUBY expect_correction(<<~RUBY) something( bar: 1, baz: 2) RUBY end it 'ignores arguments listed on a single line' do expect_no_offenses('foo(bar, baz, bing)') end it 'ignores kwargs listed on a single line when the arguments are used in `super`' do expect_no_offenses('super(foo: 1, bar: 2)') end it 'ignores arguments without parens' do expect_no_offenses(<<~RUBY) foo bar, baz RUBY end it 'ignores methods without arguments' do expect_no_offenses('foo') end context 'last element can be multiline' do let(:cop_config) { { 'AllowMultilineFinalElement' => true } } it 'ignores last argument that is a multiline Hash' do expect_no_offenses(<<~RUBY) foo(bar, { a: b }) RUBY end it 'ignores single argument that is a multiline hash' do expect_no_offenses(<<~RUBY) foo({ a: b }) RUBY end it 'registers and corrects values that are multiline hashes and not the last value' do expect_offense(<<~RUBY) foo(bar, { ^^^ Add a line break before the first argument of a multi-line method argument list. a: b }, baz) RUBY expect_correction(<<~RUBY) foo( bar, { a: b }, baz) RUBY end it 'registers and corrects last argument that starts on another line' do expect_offense(<<~RUBY) foo(bar, ^^^ Add a line break before the first argument of a multi-line method argument list. baz) RUBY expect_correction(<<~RUBY) foo( bar, baz) RUBY end end context 'AllowedMethods' do let(:cop_config) { { 'AllowedMethods' => %w[something] } } it 'does not register an offense for an allowed method' do expect_no_offenses(<<~RUBY) something(3, bar: 1, baz: 2) RUBY end end end
ruby
MIT
99fa0fdd0481910d7d052f4c2ed01ad36178f404
2026-01-04T15:37:41.211519Z
false
rubocop/rubocop
https://github.com/rubocop/rubocop/blob/99fa0fdd0481910d7d052f4c2ed01ad36178f404/spec/rubocop/cop/layout/line_continuation_leading_space_spec.rb
spec/rubocop/cop/layout/line_continuation_leading_space_spec.rb
# frozen_string_literal: true RSpec.describe RuboCop::Cop::Layout::LineContinuationLeadingSpace, :config do context 'EnforcedStyle: trailing' do let(:cop_config) { { 'EnforcedStyle' => 'trailing' } } it 'registers an offense when 2nd line has one leading space' do expect_offense(<<~'RUBY') 'this text is too' \ ' long' ^ Move leading spaces to the end of the previous line. RUBY expect_correction(<<~'RUBY') 'this text is too ' \ 'long' RUBY end it 'puts the offense message in correct position also on indented line' do expect_offense(<<~'RUBY') 'this text is too' \ ' long' ^ Move leading spaces to the end of the previous line. RUBY expect_correction(<<~'RUBY') 'this text is too ' \ 'long' RUBY end it 'registers an offense when 2nd line has multiple leading spaces' do expect_offense(<<~'RUBY') 'this text contains a lot of' \ ' spaces' ^^^^^^^^^^^^^^^ Move leading spaces to the end of the previous line. RUBY expect_correction(<<~'RUBY') 'this text contains a lot of ' \ 'spaces' RUBY end it 'registers offenses when 2nd and 3rd line has leading spaces' do expect_offense(<<~'RUBY') 'this text is too' \ ' long' \ ^ Move leading spaces to the end of the previous line. ' long long' ^^ Move leading spaces to the end of the previous line. RUBY expect_correction(<<~'RUBY') 'this text is too ' \ 'long ' \ 'long long' RUBY end it 'registers an offense in the right location when 1st line is not the string' do expect_offense(<<~'RUBY') something_unrelated_to_the_line_continuation_below 'this text is too' \ ' long' ^ Move leading spaces to the end of the previous line. RUBY expect_correction(<<~'RUBY') something_unrelated_to_the_line_continuation_below 'this text is too ' \ 'long' RUBY end it 'marks the correct range when string is a block method argument' do expect_offense(<<~'RUBY') long_method_name 'this text is too' \ ' long' do ^ Move leading spaces to the end of the previous line. end RUBY expect_correction(<<~'RUBY') long_method_name 'this text is too ' \ 'long' do end RUBY end it 'marks the correct range when string is a positional method argument' do expect_offense(<<~'RUBY') long_method_name( 'this text is too' \ ' long' ^ Move leading spaces to the end of the previous line. ) RUBY expect_correction(<<~'RUBY') long_method_name( 'this text is too ' \ 'long' ) RUBY end it 'registers no offense for multiline string with backslash' do expect_no_offenses(<<~'RUBY') '"this text is too" \ " long"' RUBY end it 'registers no offense when multiline string ends with a line continuation' do expect_no_offenses(<<~'RUBY') "" "foo bar\ " RUBY end it 'registers an offense when multiline string ends on 1st line' do expect_offense(<<~'RUBY') "foo bar" \ " baz" ^ Move leading spaces to the end of the previous line. RUBY expect_correction(<<~'RUBY') "foo bar " \ "baz" RUBY end describe 'interpolated strings' do it 'registers no offense on interpolated string alone' do expect_no_offenses(<<~'RUBY') "foo #{bar}" RUBY end it 'registers no offense on doubly interpolated string alone' do expect_no_offenses(<<~'RUBY') "foo #{bar} baz #{qux}" RUBY end it 'registers offenses when 2nd line has leading spaces and 1st line is interpolated' do expect_offense(<<~'RUBY') "foo #{bar}" \ ' long' ^ Move leading spaces to the end of the previous line. RUBY expect_correction(<<~'RUBY') "foo #{bar} " \ 'long' RUBY end it 'registers offenses when 2nd line has leading spaces and 2nd line is interpolated' do expect_offense(<<~'RUBY') 'this line is' \ " #{foo}" ^ Move leading spaces to the end of the previous line. RUBY expect_correction(<<~'RUBY') 'this line is ' \ "#{foo}" RUBY end it 'registers no offense for correctly formatted multiline interpolation' do expect_no_offenses(<<~'RUBY') "five is #{2 + \ 3}" RUBY end it 'registers no offense for correctly formatted multiline interpolated string' do expect_no_offenses(<<~'RUBY') "foo #{'bar ' \ 'baz'}" RUBY end it 'registers an offense for incorrectly formatted multiline interpolated string' do expect_offense(<<~'RUBY') "foo #{'bar' \ ' baz'}" ^ Move leading spaces to the end of the previous line. RUBY expect_correction(<<~'RUBY') "foo #{'bar ' \ 'baz'}" RUBY end end end context 'EnforcedStyle: leading' do let(:cop_config) { { 'EnforcedStyle' => 'leading' } } it 'registers an offense when 1st line has one trailing space' do expect_offense(<<~'RUBY') 'this text is too ' \ ^ Move trailing spaces to the start of the next line. 'long' RUBY expect_correction(<<~'RUBY') 'this text is too' \ ' long' RUBY end it 'puts the offense message in correct position also on indented line' do expect_offense(<<~'RUBY') 'this text is too' \ ' very ' \ ^ Move trailing spaces to the start of the next line. 'long' RUBY expect_correction(<<~'RUBY') 'this text is too' \ ' very' \ ' long' RUBY end it 'registers an offense when 1st line has multiple trailing spaces' do expect_offense(<<~'RUBY') 'this text contains a lot of ' \ ^^^^^^^^^^^^^^^ Move trailing spaces to the start of the next line. 'spaces' RUBY expect_correction(<<~'RUBY') 'this text contains a lot of' \ ' spaces' RUBY end it 'registers offenses when 1st and 2nd line has trailing spaces' do expect_offense(<<~'RUBY') 'this text is too ' \ ^ Move trailing spaces to the start of the next line. 'long ' \ ^^ Move trailing spaces to the start of the next line. 'long long' RUBY expect_correction(<<~'RUBY') 'this text is too' \ ' long' \ ' long long' RUBY end it 'registers an offense in the right location when 1st line is not the string' do expect_offense(<<~'RUBY') something_unrelated_to_the_line_continuation_below 'this text is too ' \ ^ Move trailing spaces to the start of the next line. 'long' RUBY expect_correction(<<~'RUBY') something_unrelated_to_the_line_continuation_below 'this text is too' \ ' long' RUBY end it 'marks the correct range when string is a block method argument' do expect_offense(<<~'RUBY') long_method_name 'this text is too' \ ' very ' \ ^ Move trailing spaces to the start of the next line. 'long' do end RUBY expect_correction(<<~'RUBY') long_method_name 'this text is too' \ ' very' \ ' long' do end RUBY end it 'marks the correct range when string is a positional method argument' do expect_offense(<<~'RUBY') long_method_name( 'this text is too ' \ ^ Move trailing spaces to the start of the next line. 'long' ) RUBY expect_correction(<<~'RUBY') long_method_name( 'this text is too' \ ' long' ) RUBY end it 'registers no offense for multiline string with backslash' do expect_no_offenses(<<~'RUBY') '"this text is too " \ "long"' RUBY end describe 'interpolated strings' do it 'registers no offense on interpolated string alone' do expect_no_offenses(<<~'RUBY') "foo #{bar}" RUBY end it 'registers no offense on doubly interpolated string alone' do expect_no_offenses(<<~'RUBY') "foo #{bar} baz #{qux}" RUBY end it 'registers offenses when 1st line has trailing spaces and 2nd line is interpolated' do expect_offense(<<~'RUBY') 'foo ' \ ^ Move trailing spaces to the start of the next line. "#{bar} long" RUBY expect_correction(<<~'RUBY') 'foo' \ " #{bar} long" RUBY end it 'registers offenses when 1st line has leading spaces and 1st line is interpolated' do expect_offense(<<~'RUBY') "this #{foo} is " \ ^ Move trailing spaces to the start of the next line. 'long' RUBY expect_correction(<<~'RUBY') "this #{foo} is" \ ' long' RUBY end it 'registers no offense for correctly formatted multiline interpolation' do expect_no_offenses(<<~'RUBY') "five is #{2 + \ 3}" RUBY end it 'registers no offense for correctly formatted multiline interpolated string' do expect_no_offenses(<<~'RUBY') "foo #{'bar' \ ' baz'}" RUBY end it 'registers an offense for incorrectly formatted multiline interpolated string' do expect_offense(<<~'RUBY') "foo #{'bar ' \ ^ Move trailing spaces to the start of the next line. 'baz'}" RUBY expect_correction(<<~'RUBY') "foo #{'bar' \ ' baz'}" RUBY end end end end
ruby
MIT
99fa0fdd0481910d7d052f4c2ed01ad36178f404
2026-01-04T15:37:41.211519Z
false
rubocop/rubocop
https://github.com/rubocop/rubocop/blob/99fa0fdd0481910d7d052f4c2ed01ad36178f404/spec/rubocop/cop/layout/space_inside_string_interpolation_spec.rb
spec/rubocop/cop/layout/space_inside_string_interpolation_spec.rb
# frozen_string_literal: true RSpec.describe RuboCop::Cop::Layout::SpaceInsideStringInterpolation, :config do context 'when EnforcedStyle is no_space' do let(:cop_config) { { 'EnforcedStyle' => 'no_space' } } context 'for ill-formatted string interpolations' do it 'registers offenses and autocorrects' do expect_offense(<<~'RUBY') "#{ var}" ^ Do not use space inside string interpolation. "#{var }" ^ Do not use space inside string interpolation. "#{ var }" ^^^ Do not use space inside string interpolation. ^^^ Do not use space inside string interpolation. "#{var }" ^ Do not use space inside string interpolation. "#{ var }" ^ Do not use space inside string interpolation. ^ Do not use space inside string interpolation. "#{ var}" ^ Do not use space inside string interpolation. "#{ var }" ^^^ Do not use space inside string interpolation. ^^^^ Do not use space inside string interpolation. RUBY expect_correction(<<~'RUBY') "#{var}" "#{var}" "#{var}" "#{var}" "#{var}" "#{var}" "#{var}" RUBY end it 'finds interpolations in string-like contexts' do expect_offense(<<~'RUBY') /regexp #{ var}/ ^ Do not use space inside string interpolation. `backticks #{ var}` ^ Do not use space inside string interpolation. :"symbol #{ var}" ^ Do not use space inside string interpolation. RUBY expect_correction(<<~'RUBY') /regexp #{var}/ `backticks #{var}` :"symbol #{var}" RUBY end end context 'for "space" style formatted string interpolations' do it 'registers offenses and autocorrects' do expect_offense(<<~'RUBY') "#{ var }" ^ Do not use space inside string interpolation. ^ Do not use space inside string interpolation. RUBY expect_correction(<<~'RUBY') "#{var}" RUBY end end it 'does not touch spaces inside the interpolated expression' do expect_offense(<<~'RUBY') "#{ a; b }" ^ Do not use space inside string interpolation. ^ Do not use space inside string interpolation. RUBY expect_correction(<<~'RUBY') "#{a; b}" RUBY end context 'for well-formatted string interpolations' do it 'accepts excess literal spacing' do expect_no_offenses(<<~'RUBY') "Variable is #{var} " " Variable is #{var}" RUBY end end it 'accepts empty interpolation' do expect_no_offenses("\"\#{}\"") end context 'when interpolation starts or ends with a line break' do it 'does not register an offense' do expect_no_offenses(<<~'RUBY') "#{ code }" RUBY end it 'ignores comments and whitespace when looking for line breaks' do expect_no_offenses(<<~'RUBY') def foo "#{ # comment code }" end RUBY end end end context 'when EnforcedStyle is space' do let(:cop_config) { { 'EnforcedStyle' => 'space' } } context 'for ill-formatted string interpolations' do it 'registers offenses and autocorrects' do expect_offense(<<~'RUBY') "#{ var}" ^ Use space inside string interpolation. "#{var }" ^^ Use space inside string interpolation. "#{ var }" "#{var }" ^^ Use space inside string interpolation. "#{ var }" "#{ var}" ^ Use space inside string interpolation. "#{ var }" RUBY # Extra space is handled by ExtraSpace cop. expect_correction(<<~'RUBY') "#{ var }" "#{ var }" "#{ var }" "#{ var }" "#{ var }" "#{ var }" "#{ var }" RUBY end end context 'for "no_space" style formatted string interpolations' do it 'registers offenses and autocorrects' do expect_offense(<<~'RUBY') "#{var}" ^^ Use space inside string interpolation. ^ Use space inside string interpolation. RUBY expect_correction(<<~'RUBY') "#{ var }" RUBY end end context 'for well-formatted string interpolations' do it 'does not register an offense for excess literal spacing' do expect_no_offenses(<<~'RUBY') "Variable is #{ var } " " Variable is #{ var }" RUBY end end it 'accepts empty interpolation' do expect_no_offenses("\"\#{}\"") end end end
ruby
MIT
99fa0fdd0481910d7d052f4c2ed01ad36178f404
2026-01-04T15:37:41.211519Z
false
rubocop/rubocop
https://github.com/rubocop/rubocop/blob/99fa0fdd0481910d7d052f4c2ed01ad36178f404/spec/rubocop/cop/layout/multiline_hash_key_line_breaks_spec.rb
spec/rubocop/cop/layout/multiline_hash_key_line_breaks_spec.rb
# frozen_string_literal: true RSpec.describe RuboCop::Cop::Layout::MultilineHashKeyLineBreaks, :config do context 'with line break after opening bracket' do context 'when on different lines than brackets but keys on one' do it 'does not add any offenses' do expect_no_offenses(<<~RUBY) { foo: 1, bar: "2" } RUBY end end context 'when on all keys on one line different than brackets' do it 'does not add any offenses' do expect_no_offenses(<<~RUBY) { foo => 1, bar => "2" } RUBY end end it 'registers an offense and corrects when key starts on same line as another' do expect_offense(<<~RUBY) { foo: 1, baz: 3, bar: "2"} ^^^^^^^^ Each key in a multi-line hash must start on a separate line. RUBY expect_correction(<<~RUBY) { foo: 1, baz: 3,\s bar: "2"} RUBY end context 'when key starts on same line as another with rockets' do it 'adds an offense' do expect_offense(<<~RUBY) { foo => 1, baz => 3, bar: "2"} ^^^^^^^^ Each key in a multi-line hash must start on a separate line. RUBY expect_correction(<<~RUBY) { foo => 1, baz => 3,\s bar: "2"} RUBY end end end context 'without line break after opening bracket' do context 'when on same line' do it 'does not add any offenses' do expect_no_offenses(<<~RUBY) {foo: 1, bar: "2"} RUBY end end it 'registers an offense and corrects when key starts on same line as another' do expect_offense(<<~RUBY) {foo: 1, baz: 3, bar: "2"} ^^^^^^^^ Each key in a multi-line hash must start on a separate line. RUBY expect_correction(<<~RUBY) {foo: 1, baz: 3,\s bar: "2"} RUBY end it 'registers an offense and corrects nested hashes' do expect_offense(<<~RUBY) {foo: 1, baz: { as: 12, }, bar: "2"} ^^^^^^^^ Each key in a multi-line hash must start on a separate line. RUBY expect_correction(<<~RUBY) {foo: 1, baz: { as: 12, },\s bar: "2"} RUBY end context 'ignore last element' do let(:cop_config) { { 'AllowMultilineFinalElement' => true } } it 'ignores last value that is a multiline hash' do expect_no_offenses(<<~RUBY) {foo: 1, bar: { a: 1 }} RUBY end it 'registers and corrects values that are multiline hashes and not the last value' do expect_offense(<<~RUBY) {foo: 1, bar: { ^^^^^^ Each key in a multi-line hash must start on a separate line. a: 1, }, baz: 3} RUBY expect_correction(<<~RUBY) {foo: 1,#{trailing_whitespace} bar: { a: 1, },#{trailing_whitespace} baz: 3} RUBY end it 'registers and corrects last value that is on a new line' do expect_offense(<<~RUBY) {foo: 1, bar: 2, ^^^^^^ Each key in a multi-line hash must start on a separate line. baz: 3} RUBY expect_correction(<<~RUBY) {foo: 1,#{trailing_whitespace} bar: 2, baz: 3} RUBY end end end end
ruby
MIT
99fa0fdd0481910d7d052f4c2ed01ad36178f404
2026-01-04T15:37:41.211519Z
false
rubocop/rubocop
https://github.com/rubocop/rubocop/blob/99fa0fdd0481910d7d052f4c2ed01ad36178f404/spec/rubocop/cop/layout/redundant_line_break_spec.rb
spec/rubocop/cop/layout/redundant_line_break_spec.rb
# frozen_string_literal: true RSpec.describe RuboCop::Cop::Layout::RedundantLineBreak, :config do let(:config) do RuboCop::Config.new('Layout/LineLength' => { 'Enabled' => line_length_enabled, 'Max' => max_line_length }, 'Layout/RedundantLineBreak' => { 'InspectBlocks' => inspect_blocks }, 'Layout/SingleLineBlockChain' => { 'Enabled' => single_line_block_chain_enabled }) end let(:line_length_enabled) { true } let(:max_line_length) { 31 } let(:inspect_blocks) { false } let(:single_line_block_chain_enabled) { true } shared_examples 'common behavior' do context 'when Layout/SingleLineBlockChain is disabled' do let(:single_line_block_chain_enabled) { false } let(:max_line_length) { 90 } it 'reports an offense for a method call chained onto a single line block' do expect_offense(<<~RUBY) e.select { |i| i.cond? } ^^^^^^^^^^^^^^^^^^^^^^^^ Redundant line break detected. .join a = e.select { |i| i.cond? } ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Redundant line break detected. .join e.select { |i| i.cond? } ^^^^^^^^^^^^^^^^^^^^^^^^ Redundant line break detected. .join + [] RUBY expect_correction(<<~RUBY) e.select { |i| i.cond? }.join a = e.select { |i| i.cond? }.join e.select { |i| i.cond? }.join + [] RUBY end it 'reports an offense for a safe navigation method call chained onto a single line block' do expect_offense(<<~RUBY) e&.select { |i| i.cond? } ^^^^^^^^^^^^^^^^^^^^^^^^^ Redundant line break detected. &.join a = e&.select { |i| i.cond? } ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Redundant line break detected. &.join e&.select { |i| i.cond? } ^^^^^^^^^^^^^^^^^^^^^^^^^ Redundant line break detected. &.join + [] RUBY expect_correction(<<~RUBY) e&.select { |i| i.cond? }&.join a = e&.select { |i| i.cond? }&.join e&.select { |i| i.cond? }&.join + [] RUBY end end context 'when Layout/SingleLineBlockChain is enabled' do let(:single_line_block_chain_enabled) { true } let(:max_line_length) { 90 } it 'accepts a method call chained onto a single line block' do expect_no_offenses(<<~RUBY) e.select { |i| i.cond? } .join a = e.select { |i| i.cond? } .join e.select { |i| i.cond? } .join + [] RUBY end it 'accepts a method call chained onto a single line numbered block', :ruby27 do expect_no_offenses(<<~RUBY) e.select { _1.cond? } .join a = e.select { _1.cond? } .join e.select { _1.cond? } .join + [] RUBY end it 'accepts a method call chained onto a single line `it` block', :ruby34 do expect_no_offenses(<<~RUBY) e.select { it.cond? } .join a = e.select { it.cond? } .join e.select { it.cond? } .join + [] RUBY end end context 'for an expression that fits on a single line' do it 'accepts an assignment containing an if expression' do expect_no_offenses(<<~RUBY) a = if x 1 else 2 end RUBY end it 'accepts an assignment containing a case expression' do expect_no_offenses(<<~RUBY) a = case x when :a 1 else 2 end RUBY end it 'accepts a binary expression containing an if expression' do expect_no_offenses(<<~RUBY) a + if x 1 else 2 end RUBY end it 'accepts a method call with a block' do expect_no_offenses(<<~RUBY) a do x y end RUBY end it 'accepts an assignment containing a begin-end expression' do expect_no_offenses(<<~RUBY) a ||= begin x y end RUBY end it 'accepts a modified singleton method definition' do expect_no_offenses(<<~RUBY) x def self.y z end RUBY end it 'accepts a method call on a single line' do expect_no_offenses(<<~RUBY) my_method(1, 2, "x") RUBY end it 'registers an offense for a method call on multiple lines with backslash' do expect_offense(<<~RUBY) my_method(1) \\ ^^^^^^^^^^^^^^ Redundant line break detected. [:a] RUBY expect_correction(<<~RUBY) my_method(1) [:a] RUBY end it 'does not register an offense for index access call chained on multiple lines with backslash' do expect_no_offenses(<<~RUBY) hash[:foo] \\ [:bar] RUBY end it 'registers an offense for index access call chained on multiline hash literal' do expect_offense(<<~RUBY) { ^ Redundant line break detected. key: value }[key] RUBY expect_correction(<<~RUBY) { key: value }[key] RUBY end it 'registers an offense when using `&&` before a backslash newline' do expect_offense(<<~RUBY) foo && \\ ^^^^^^^^ Redundant line break detected. bar RUBY expect_correction(<<~RUBY) foo && bar RUBY end it 'does not register an offense when using `&&` after a backslash newline' do expect_no_offenses(<<~RUBY) foo \\ && bar RUBY end it 'registers an offense when using `||` before a backslash newline' do expect_offense(<<~RUBY) foo || \\ ^^^^^^^^ Redundant line break detected. bar RUBY expect_correction(<<~RUBY) foo || bar RUBY end it 'does not register an offense when using `||` after a backslash newline' do expect_no_offenses(<<~RUBY) foo \\ || bar RUBY end context 'with LineLength Max 100' do let(:max_line_length) { 100 } it 'registers an offense for a method without parentheses on multiple lines' do expect_offense(<<~RUBY) def resolve_inheritance_from_gems(hash) gems = hash.delete('inherit_gem') (gems || {}).each_pair do |gem_name, config_path| if gem_name == 'rubocop' raise ArgumentError, ^^^^^^^^^^^^^^^^^^^^ Redundant line break detected. "can't inherit configuration from the rubocop gem" end hash['inherit_from'] = Array(hash['inherit_from']) Array(config_path).reverse_each do |path| # Put gem configuration first so local configuration overrides it. hash['inherit_from'].unshift gem_config_path(gem_name, path) end end end RUBY expect_correction(<<~RUBY) def resolve_inheritance_from_gems(hash) gems = hash.delete('inherit_gem') (gems || {}).each_pair do |gem_name, config_path| if gem_name == 'rubocop' raise ArgumentError, "can't inherit configuration from the rubocop gem" end hash['inherit_from'] = Array(hash['inherit_from']) Array(config_path).reverse_each do |path| # Put gem configuration first so local configuration overrides it. hash['inherit_from'].unshift gem_config_path(gem_name, path) end end end RUBY end end it 'registers an offense for a method call on multiple lines' do expect_offense(<<~RUBY) my_method(1, ^^^^^^^^^^^^ Redundant line break detected. 2, "x") RUBY expect_correction(<<~RUBY) my_method(1, 2, "x") RUBY end it 'registers an offense for a method call on multiple lines inside a block' do expect_offense(<<~RUBY) some_array.map do |something| my_method( ^^^^^^^^^^ Redundant line break detected. something, ) end RUBY expect_correction(<<~RUBY) some_array.map do |something| my_method( something, ) end RUBY end it 'accepts a method call on multiple lines if there are comments on them' do expect_no_offenses(<<~RUBY) my_method(1, 2, "x") # X RUBY end it 'registers an offense for a method call with a double quoted split string in parentheses' do expect_offense(<<~RUBY) my_method("a" \\ ^^^^^^^^^^^^^^^ Redundant line break detected. "b") RUBY expect_correction(<<~RUBY) my_method("ab") RUBY end it 'registers an offense for a method call with a double quoted split string without parentheses' do expect_offense(<<~RUBY) puts "(\#{pl(i)}, " \\ ^^^^^^^^^^^^^^^^^^^^ Redundant line break detected. "\#{pl(f)})" RUBY expect_correction(<<~RUBY) puts "(\#{pl(i)}, \#{pl(f)})" RUBY end it 'registers an offense for a method call with a single quoted split string' do expect_offense(<<~RUBY) my_method('a'\\ ^^^^^^^^^^^^^^ Redundant line break detected. 'b') RUBY expect_correction(<<~RUBY) my_method('ab') RUBY end it 'registers an offense for a method call with a double and single quoted split string' do expect_offense(<<~RUBY) my_method("a" \\ ^^^^^^^^^^^^^^^ Redundant line break detected. 'b') my_method('a' \\ ^^^^^^^^^^^^^^^ Redundant line break detected. "b") RUBY expect_correction(<<~RUBY) my_method("a" + 'b') my_method('a' + "b") RUBY end it 'registers an offense for a method call with a split operation' do expect_offense(<<~RUBY) my_method(1 + ^^^^^^^^^^^^^ Redundant line break detected. 2 + 3) RUBY expect_correction(<<~RUBY) my_method(1 + 2 + 3) RUBY end it 'registers an offense for a method call as right hand side of an assignment' do expect_offense(<<~RUBY) a = ^^^ Redundant line break detected. m(1 + 2 + 3) b = m(4 + ^^^^^^^^^ Redundant line break detected. 5 + 6) long_variable_name = m(7 + ^^^^^ Redundant line break detected. 8 + 9) RUBY expect_correction(<<~RUBY) a = m(1 + 2 + 3) b = m(4 + 5 + 6) long_variable_name = m(7 + 8 + 9) RUBY end context 'method chains' do it 'properly corrects a method chain on multiple lines' do expect_offense(<<~RUBY) foo(' .x') ^^^^^^^^^^ Redundant line break detected. .bar .baz RUBY expect_correction(<<~RUBY) foo(' .x').bar.baz RUBY end it 'registers an offense and corrects with arguments on multiple lines' do expect_offense(<<~RUBY) foo(x, ^^^^^^ Redundant line break detected. y, z) .bar .baz RUBY expect_correction(<<~RUBY) foo(x, y, z).bar.baz RUBY end it 'registers an offense and corrects with a string argument on multiple lines' do expect_offense(<<~RUBY) foo('....' \\ ^^^^^^^^^^^^ Redundant line break detected. '....') .bar .baz RUBY expect_correction(<<~RUBY) foo('........').bar.baz RUBY end it 'does not register an offense with a heredoc argument' do expect_no_offenses(<<~RUBY) foo(<<~EOS) xyz EOS .bar .baz RUBY end it 'does not register an offense with a line broken string argument' do expect_no_offenses(<<~RUBY) foo(' xyz ') .bar .baz RUBY end it 'does not register an offense when the `%` form string `"%\n\n"` at the end of file' do expect_no_offenses("%\n\n") end it 'does not register an offense when assigning the `%` form string `"%\n\n"` to a variable at the end of file' do expect_no_offenses("x = %\n\n") end end end context 'for an expression that does not fit on a single line' do it 'accepts a method call on a multiple lines' do expect_no_offenses(<<~RUBY) my_method(11111, 22222, "abcxyz") my_method(111111 + 222222 + 333333) RUBY end it 'accepts a quoted symbol with a single newline' do expect_no_offenses(<<~RUBY) foo(:" ") RUBY end context 'with a longer max line length' do let(:max_line_length) { 82 } it 'accepts an assignment containing a method definition' do expect_no_offenses(<<~RUBY) VariableReference = Struct.new(:name) do def assignment? false end end RUBY end it 'accepts a method call followed by binary operations that are too long taken together' do expect_no_offenses(<<~RUBY) File.fnmatch?( pattern, path, File::FNM_PATHNAME | File::FNM_EXTGLOB ) && a && File.basename(path).start_with?('.') && !hidden_dir?(path) RUBY expect_no_offenses(<<~RUBY) File.fnmatch?( pattern, path, File::FNM_PATHNAME | File::FNM_EXTGLOB ) + a + File.basename(path).start_with?('.') + !hidden_dir?(path) RUBY end it 'accepts an assignment containing a heredoc' do expect_no_offenses(<<~RUBY) correct = lambda do expect_no_offenses(<<~EOT1) <<-EOT2 foo EOT2 EOT1 end RUBY end it 'accepts a complex method call on a multiple lines' do expect_no_offenses(<<~RUBY) node.each_node(:dstr) .select(&:heredoc?) .map { |n| n.loc.heredoc_body } .flat_map { |b| (b.line...b.last_line).to_a } RUBY end it 'accepts a complex method call on a multiple lines with numbered block', :ruby27 do expect_no_offenses(<<~RUBY) node.each_node(:dstr) .select(&:heredoc?) .map { _1.loc.heredoc_body } .flat_map { (_1.line..._1.last_line).to_a } RUBY end it 'accepts method call with a do keyword that would just surpass the max line length' do expect_no_offenses(<<~RUBY) context 'when the configuration includes ' \\ 'an unsafe cop that is 123456789012345678' do end RUBY end it 'registers an offense for a method call with a do keyword that is just under the max line length' do expect_offense(<<~RUBY) context 'when the configuration includes ' \\ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Redundant line break detected. 'an unsafe cop that is 123456789012345' do end RUBY expect_correction(<<~RUBY) context 'when the configuration includes an unsafe cop that is 123456789012345' do end RUBY end context 'for a block' do it 'accepts when it is difficult to convert to single line' do expect_no_offenses(<<~RUBY) RSpec.shared_context 'ruby 2.4', :ruby24 do let(:ruby_version) { 2.4 } end RUBY end end end end end context 'when InspectBlocks is true' do let(:inspect_blocks) { true } it_behaves_like 'common behavior' context 'for a block' do let(:max_line_length) { 82 } it 'registers an offense when the method call has parentheses' do expect_offense(<<~RUBY) RSpec.shared_context('ruby 2.4', :ruby24) do ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Redundant line break detected. let(:ruby_version) { 2.4 } end RUBY end it 'registers an offense when the method call has no arguments' do expect_offense(<<~RUBY) RSpec.shared_context do ^^^^^^^^^^^^^^^^^^^^^^^ Redundant line break detected. let(:ruby_version) { 2.4 } end RUBY end context 'when Layout/SingleLineBlockChain is enabled' do let(:single_line_block_chain_enabled) { true } it 'reports an offense for a multiline block without a chained method call' do expect_offense(<<~RUBY) f do ^^^^ Redundant line break detected. end RUBY end end context 'when Layout/SingleLineBlockChain is disabled' do let(:single_line_block_chain_enabled) { false } it 'reports an offense for a multiline block without a chained method call' do expect_offense(<<~RUBY) f do ^^^^ Redundant line break detected. end RUBY end it 'reports an offense for a method call chained onto a multiline block' do expect_offense(<<~RUBY) e.select do |i| ^^^^^^^^^^^^^^^ Redundant line break detected. i.cond? end.join RUBY expect_offense(<<~RUBY) a = e.select do |i| ^^^^^^^^^^^^^^^^^^^ Redundant line break detected. i.cond? end.join RUBY expect_offense(<<~RUBY) e.select do |i| ^^^^^^^^^^^^^^^ Redundant line break detected. i.cond? end.join + [] RUBY end end end end context 'when InspectBlocks is false' do let(:inspect_blocks) { false } it_behaves_like 'common behavior' context 'for a block' do let(:max_line_length) { 100 } it 'accepts when the method call has parentheses' do expect_no_offenses(<<~RUBY) a = RSpec.shared_context('ruby 2.4', :ruby24) do let(:ruby_version) { 2.4 } end RUBY end it 'accepts when the method call has parentheses with numbered block', :ruby27 do expect_no_offenses(<<~RUBY) a = Foo.do_something(arg) do _1 end RUBY end it 'accepts when the method call has no arguments' do expect_no_offenses(<<~RUBY) RSpec.shared_context do let(:ruby_version) { 2.4 } end RUBY end context 'when Layout/SingleLineBlockChain is enabled' do let(:single_line_block_chain_enabled) { true } it 'accepts a multiline block without a chained method call' do expect_no_offenses(<<~RUBY) f do end RUBY end it 'accepts a multiline numbered block without a chained method call', :ruby27 do expect_no_offenses(<<~RUBY) f do foo(_1) end RUBY end end context 'when Layout/SingleLineBlockChain is disabled' do let(:single_line_block_chain_enabled) { false } it 'accepts a multiline block without a chained method call' do expect_no_offenses(<<~RUBY) f do end RUBY end it 'accepts a multiline numbered block without a chained method call', :ruby27 do expect_no_offenses(<<~RUBY) f do foo(_1) end RUBY end it 'accepts a method call chained onto a multiline block' do expect_no_offenses(<<~RUBY) e.select do |i| i.cond? end.join a = e.select do |i| i.cond? end.join e.select do |i| i.cond? end.join + [] RUBY end it 'accepts a method call chained onto a multiline numbered block', :ruby27 do expect_no_offenses(<<~RUBY) e.select do _1.cond? end.join a = e.select do _1.cond? end.join e.select do _1.cond? end.join + [] RUBY end it 'accepts a method call chained onto a multiline `it` block', :ruby34 do expect_no_offenses(<<~RUBY) e.select do it.cond? end.join a = e.select do it.cond? end.join e.select do it.cond? end.join + [] RUBY end end end end context 'when `Layout/LineLength` is disabled' do let(:line_length_enabled) { false } it 'registers an offense for a method call on multiple lines' do expect_offense(<<~RUBY) my_method(1, ^^^^^^^^^^^^ Redundant line break detected. 2, "x") RUBY expect_correction(<<~RUBY) my_method(1, 2, "x") RUBY end end end
ruby
MIT
99fa0fdd0481910d7d052f4c2ed01ad36178f404
2026-01-04T15:37:41.211519Z
false
rubocop/rubocop
https://github.com/rubocop/rubocop/blob/99fa0fdd0481910d7d052f4c2ed01ad36178f404/spec/rubocop/cop/layout/multiline_method_call_brace_layout_spec.rb
spec/rubocop/cop/layout/multiline_method_call_brace_layout_spec.rb
# frozen_string_literal: true RSpec.describe RuboCop::Cop::Layout::MultilineMethodCallBraceLayout, :config do let(:enforced_style) { 'symmetrical' } let(:cop_config) { { 'EnforcedStyle' => enforced_style } } it 'ignores implicit calls' do expect_no_offenses(<<~RUBY) foo 1, 2 RUBY end it 'ignores single-line calls' do expect_no_offenses('foo(1,2)') end it 'ignores calls without arguments' do expect_no_offenses('puts') end it 'ignores calls with an empty brace' do expect_no_offenses('puts()') end it 'ignores calls with a multiline empty brace' do expect_no_offenses(<<~RUBY) puts( ) RUBY end it 'registers an offense when using method chain for heredoc argument in multiline literal brace layout' do expect_offense(<<~RUBY) foo(<<~EOS, arg text EOS ).do_something ^ Closing method call brace must be on the same line as the last argument when opening brace is on the same line as the first argument. RUBY expect_correction(<<~RUBY) foo(<<~EOS, arg).do_something text EOS RUBY end it 'registers an offense when using safe navigation method chain for heredoc argument in multiline literal brace layout' do expect_offense(<<~RUBY) foo(<<~EOS, arg text EOS )&.do_something ^ Closing method call brace must be on the same line as the last argument when opening brace is on the same line as the first argument. RUBY expect_correction(<<~RUBY) foo(<<~EOS, arg)&.do_something text EOS RUBY end it_behaves_like 'multiline literal brace layout' do let(:open) { 'foo(' } let(:close) { ')' } end it_behaves_like 'multiline literal brace layout trailing comma' do let(:open) { 'foo(' } let(:close) { ')' } let(:same_line_message) do 'Closing method call brace must be on the same line as the last ' \ 'argument when opening [...]' end let(:always_same_line_message) do 'Closing method call brace must be on the same line as the last argument.' end end context 'when EnforcedStyle is new_line' do let(:enforced_style) { 'new_line' } it 'still ignores single-line calls' do expect_no_offenses('puts("Hello world!")') end it 'ignores single-line calls with multi-line receiver' do expect_no_offenses(<<~RUBY) [ ].join(" ") RUBY end it 'ignores single-line calls with multi-line receiver with leading dot' do expect_no_offenses(<<~RUBY) [ ] .join(" ") RUBY end end context 'when comment present before closing brace' do it 'corrects closing brace without crashing' do expect_offense(<<~RUBY) super(bar(baz, ham # comment )) ^ Closing method call brace must be on the same line as the last argument when opening brace is on the same line as the first argument. RUBY expect_correction(<<~RUBY) super(bar(baz, ham)) # comment RUBY end end context 'with safe navigation' do it 'ignores single-line calls' do expect_no_offenses('foo&.bar(1,2)') end context 'with EnforcedStyle: symmetrical' do let(:enforced_style) { 'symmetrical' } it 'registers an offense and corrects' do expect_offense(<<~RUBY) foo&.bar( baz) ^ Closing method call brace must be on the line after the last argument when opening brace is on a separate line from the first argument. RUBY expect_correction(<<~RUBY) foo&.bar( baz ) RUBY end end context 'with EnforcedStyle: new_line' do let(:enforced_style) { 'new_line' } it 'registers an offense and corrects' do expect_offense(<<~RUBY) foo&.bar( baz) ^ Closing method call brace must be on the line after the last argument. RUBY expect_correction(<<~RUBY) foo&.bar( baz ) RUBY end end context 'with EnforcedStyle: same_line' do let(:enforced_style) { 'same_line' } it 'registers an offense and corrects' do expect_offense(<<~RUBY) foo&.bar( baz ) ^ Closing method call brace must be on the same line as the last argument. RUBY expect_correction(<<~RUBY) foo&.bar( baz) RUBY end end end end
ruby
MIT
99fa0fdd0481910d7d052f4c2ed01ad36178f404
2026-01-04T15:37:41.211519Z
false
rubocop/rubocop
https://github.com/rubocop/rubocop/blob/99fa0fdd0481910d7d052f4c2ed01ad36178f404/spec/rubocop/cop/layout/line_end_string_concatenation_indentation_spec.rb
spec/rubocop/cop/layout/line_end_string_concatenation_indentation_spec.rb
# frozen_string_literal: true RSpec.describe RuboCop::Cop::Layout::LineEndStringConcatenationIndentation, :config do let(:config) do merged = RuboCop::ConfigLoader .default_configuration['Layout/LineEndStringConcatenationIndentation'] .merge(cop_config) .merge('Enabled' => true) .merge('IndentationWidth' => cop_indent) RuboCop::Config .new('Layout/LineEndStringConcatenationIndentation' => merged, 'Layout/IndentationWidth' => { 'Width' => indentation_width }) end let(:indentation_width) { 2 } let(:cop_indent) { nil } # use indentation width from Layout/IndentationWidth shared_examples 'common' do it 'accepts single line string literal concatenation' do expect_no_offenses(<<~'RUBY') text = 'offense' puts 'This probably should not be '"an #{text}" RUBY end it 'accepts string literal with line break concatenated with other string' do expect_no_offenses(<<~'RUBY') text = 'offense' puts 'This probably should not be '"an #{text}" RUBY end it 'accepts a multiline string literal' do expect_no_offenses(<<~RUBY) puts %( foo bar ) RUBY end it 'accepts indented strings in implicit return statement of a block' do expect_no_offenses(<<~'RUBY') some_method do 'a' \ 'b' \ 'c' end RUBY end it 'accepts indented strings in implicit return statement of a method definition' do expect_no_offenses(<<~'RUBY') def some_method 'a' \ 'b' \ 'c' end RUBY end it 'registers an offense for aligned strings in an if/elsif/else statement' do expect_offense(<<~'RUBY') if cond1 'a' \ 'b' ^^^ Indent the first part of a string concatenated with backslash. elsif cond2 'c' \ 'd' ^^^ Indent the first part of a string concatenated with backslash. else 'e' \ 'f' ^^^ Indent the first part of a string concatenated with backslash. end RUBY expect_correction(<<~'RUBY') if cond1 'a' \ 'b' elsif cond2 'c' \ 'd' else 'e' \ 'f' end RUBY end it 'accepts indented strings in implicit return statement of a singleton method definition' do expect_no_offenses(<<~'RUBY') def self.some_method 'a' \ 'b' \ 'c' end RUBY end it 'accepts indented strings in implicit return statement of a method definition after other statement' do expect_no_offenses(<<~'RUBY') def some_method b = 'b' 'a' \ "#{b}" \ 'c' end RUBY end it 'accepts indented strings in ordinary statement' do expect_no_offenses(<<~'RUBY') 'a' \ 'b' \ 'c' RUBY end it 'accepts a heredoc string with interpolation' do expect_no_offenses(<<~'RUBY') warn <<~TEXT A #{b} TEXT RUBY end it 'accepts a heredoc string ...' do expect_no_offenses(<<~RUBY) let(:source) do <<~CODE func({ @abc => 0, @xyz => 1 }) func( { abc: 0 } ) func( {}, { xyz: 1 } ) CODE end RUBY end it 'accepts an empty heredoc string with interpolation' do expect_no_offenses(<<~RUBY) puts(<<~TEXT) TEXT RUBY end it 'accepts the `%` form string `"%\n\n"`' do expect_no_offenses("%\n\n") end end context 'when EnforcedStyle is aligned' do let(:cop_config) { { 'EnforcedStyle' => 'aligned' } } it_behaves_like 'common' it 'accepts aligned strings in method call' do expect_no_offenses(<<~'RUBY') puts 'a' \ 'b' RUBY end ['X =', '$x =', '@x =', 'x =', 'x +=', 'x ||='].each do |lhs_and_operator| context "for assignment with #{lhs_and_operator}" do let(:aligned_strings) do [%(#{lhs_and_operator} "a" \\), "#{' ' * lhs_and_operator.length} 'b'", ''].join("\n") end it 'accepts aligned strings' do expect_no_offenses(aligned_strings) end it 'registers an offense for indented strings' do expect_offense([%(#{lhs_and_operator} "a" \\), " 'b'", ' ^^^ Align parts of a string concatenated with backslash.', ''].join("\n")) expect_correction(aligned_strings) end end end it 'registers an offense for unaligned strings in hash literal values' do expect_offense(<<~'RUBY') MESSAGES = { KeyAlignment => 'Align the keys of a hash literal if ' \ 'they span more than one line.', ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Align parts of a string concatenated with backslash. SeparatorAlignment => 'Align the separators of a hash ' \ 'literal if they span more than one line.', ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Align parts of a string concatenated with backslash. TableAlignment => 'Align the keys and values of a hash ' \ 'literal if they span more than one line.' }.freeze ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Align parts of a string concatenated with backslash. RUBY expect_correction(<<~'RUBY') MESSAGES = { KeyAlignment => 'Align the keys of a hash literal if ' \ 'they span more than one line.', SeparatorAlignment => 'Align the separators of a hash ' \ 'literal if they span more than one line.', TableAlignment => 'Align the keys and values of a hash ' \ 'literal if they span more than one line.' }.freeze RUBY end it 'registers an offense for indented string' do expect_offense(<<~'RUBY') puts 'a' \ "b" \ ^^^ Align parts of a string concatenated with backslash. 'c' RUBY expect_correction(<<~'RUBY') puts 'a' \ "b" \ 'c' RUBY end it 'registers an offense for third part of a string if it is aligned only with the first' do expect_offense(<<~'RUBY') puts 'a' \ 'b' \ ^^^ Align parts of a string concatenated with backslash. 'c' ^^^ Align parts of a string concatenated with backslash. RUBY expect_correction(<<~'RUBY') puts 'a' \ 'b' \ 'c' RUBY end end context 'when EnforcedStyle is indented' do let(:cop_config) { { 'EnforcedStyle' => 'indented' } } it_behaves_like 'common' it 'accepts indented strings' do expect_no_offenses(<<~'RUBY') puts 'a' \ 'b' RUBY end ['X =', '$x =', '@x =', 'x =', 'x +=', 'x ||='].each do |lhs_and_operator| context "for assignment with #{lhs_and_operator}" do let(:indented_strings) do [%(#{lhs_and_operator} "a" \\), " 'b'", ''].join("\n") end it 'accepts indented strings' do expect_no_offenses(indented_strings) end it 'registers an offense for aligned strings' do margin = "#{' ' * lhs_and_operator.length} " # Including spaces around operator. expect_offense( [%(#{lhs_and_operator} "a" \\), "#{margin}'b'", "#{margin}^^^ Indent the first part of a string concatenated with backslash.", ''].join("\n") ) expect_correction(indented_strings) end end end it 'registers an offense for aligned strings in hash literal values' do expect_offense(<<~'RUBY') MESSAGES = { KeyAlignment => 'Align the keys of a hash literal if ' \ 'they span more than one line.', ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Indent the first part of a string concatenated with backslash. SeparatorAlignment => 'Align the separators of a hash ' \ 'literal if they span more than one line.', ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Indent the first part of a string concatenated with backslash. TableAlignment => 'Align the keys and values of a hash ' \ 'literal if they span more than one line.' }.freeze ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Indent the first part of a string concatenated with backslash. RUBY expect_correction(<<~'RUBY') MESSAGES = { KeyAlignment => 'Align the keys of a hash literal if ' \ 'they span more than one line.', SeparatorAlignment => 'Align the separators of a hash ' \ 'literal if they span more than one line.', TableAlignment => 'Align the keys and values of a hash ' \ 'literal if they span more than one line.' }.freeze RUBY end it 'registers an offense for aligned string' do expect_offense(<<~'RUBY') puts %Q(a) \ 'b' \ ^^^ Indent the first part of a string concatenated with backslash. 'c' RUBY expect_correction(<<~'RUBY') puts %Q(a) \ 'b' \ 'c' RUBY end it 'registers an offense for unaligned third part of string' do expect_offense(<<~'RUBY') puts 'a' \ "#{b}" \ "#{c}" ^^^^^^ Align parts of a string concatenated with backslash. RUBY expect_correction(<<~'RUBY') puts 'a' \ "#{b}" \ "#{c}" RUBY end context 'when IndentationWidth is 1' do let(:cop_indent) { 1 } it 'accepts indented strings' do expect_no_offenses(<<~'RUBY') puts 'a' \ 'b' RUBY end end end end
ruby
MIT
99fa0fdd0481910d7d052f4c2ed01ad36178f404
2026-01-04T15:37:41.211519Z
false
rubocop/rubocop
https://github.com/rubocop/rubocop/blob/99fa0fdd0481910d7d052f4c2ed01ad36178f404/spec/rubocop/cop/layout/empty_line_after_magic_comment_spec.rb
spec/rubocop/cop/layout/empty_line_after_magic_comment_spec.rb
# frozen_string_literal: true RSpec.describe RuboCop::Cop::Layout::EmptyLineAfterMagicComment, :config do it 'registers an offense for code that immediately follows comment' do expect_offense(<<~RUBY) # frozen_string_literal: true class Foo; end ^ Add an empty line after magic comments. RUBY expect_correction(<<~RUBY) # frozen_string_literal: true class Foo; end RUBY end it 'registers an offense when code that immediately follows `rbs_inline: enabled` comment' do expect_offense(<<~RUBY) # rbs_inline: enabled class Foo; end ^ Add an empty line after magic comments. RUBY expect_correction(<<~RUBY) # rbs_inline: enabled class Foo; end RUBY end it 'registers an offense when code that immediately follows `rbs_inline: disabled` comment' do expect_offense(<<~RUBY) # rbs_inline: disabled class Foo; end ^ Add an empty line after magic comments. RUBY expect_correction(<<~RUBY) # rbs_inline: disabled class Foo; end RUBY end it 'does not register an offense when code that immediately follows `rbs_inline: invalid_value` comment' do expect_no_offenses(<<~RUBY) # rbs_inline: invalid_value class Foo; end RUBY end it 'registers an offense when code that immediately follows typed comment' do expect_offense(<<~RUBY) # typed: true class Foo; end ^ Add an empty line after magic comments. RUBY expect_correction(<<~RUBY) # typed: true class Foo; end RUBY end it 'registers an offense for documentation immediately following comment' do expect_offense(<<~RUBY) # frozen_string_literal: true # Documentation for Foo ^ Add an empty line after magic comments. class Foo; end RUBY expect_correction(<<~RUBY) # frozen_string_literal: true # Documentation for Foo class Foo; end RUBY end it 'registers an offense when multiple magic comments without empty line' do expect_offense(<<~RUBY) # encoding: utf-8 # frozen_string_literal: true class Foo; end ^ Add an empty line after magic comments. RUBY expect_correction(<<~RUBY) # encoding: utf-8 # frozen_string_literal: true class Foo; end RUBY end it 'accepts magic comment followed by encoding' do expect_no_offenses(<<~RUBY) # frozen_string_literal: true # encoding: utf-8 class Foo; end RUBY end it 'accepts magic comment with shareable_constant_value' do expect_no_offenses(<<~RUBY) # frozen_string_literal: true # shareable_constant_value: literal class Foo; end RUBY expect_no_offenses(<<~RUBY) # shareable_constant_value: experimental_everything # frozen_string_literal: true class Foo; end RUBY end it 'accepts magic comment with `rbs_inline: enabled`' do expect_no_offenses(<<~RUBY) # frozen_string_literal: true # rbs_inline: enabled class Foo; end RUBY expect_no_offenses(<<~RUBY) # rbs_inline: enabled # frozen_string_literal: true class Foo; end RUBY end it 'accepts magic comment with `rbs_inline: disabled`' do expect_no_offenses(<<~RUBY) # frozen_string_literal: true # rbs_inline: disabled class Foo; end RUBY expect_no_offenses(<<~RUBY) # rbs_inline: disabled # frozen_string_literal: true class Foo; end RUBY end it 'accepts magic comment with typed' do expect_no_offenses(<<~RUBY) # frozen_string_literal: true # typed: true class Foo; end RUBY expect_no_offenses(<<~RUBY) # typed: true # frozen_string_literal: true class Foo; end RUBY end it 'registers an offense when frozen_string_literal used with shareable_constant_value without empty line' do expect_offense(<<~RUBY) # frozen_string_literal: true # shareable_constant_value: none class Foo; end ^ Add an empty line after magic comments. RUBY expect_correction(<<~RUBY) # frozen_string_literal: true # shareable_constant_value: none class Foo; end RUBY end it 'registers an offense when the file is comments only' do expect_offense(<<~RUBY) # frozen_string_literal: true # Hello! ^ Add an empty line after magic comments. RUBY expect_correction(<<~RUBY) # frozen_string_literal: true # Hello! RUBY end it 'accepts code that separates the comment from the code with a newline' do expect_no_offenses(<<~RUBY) # frozen_string_literal: true class Foo; end RUBY end it 'accepts an empty source file' do expect_no_offenses('') end it 'accepts a source file with only a magic comment' do expect_no_offenses('# frozen_string_literal: true') end end
ruby
MIT
99fa0fdd0481910d7d052f4c2ed01ad36178f404
2026-01-04T15:37:41.211519Z
false
rubocop/rubocop
https://github.com/rubocop/rubocop/blob/99fa0fdd0481910d7d052f4c2ed01ad36178f404/spec/rubocop/cop/layout/space_in_lambda_literal_spec.rb
spec/rubocop/cop/layout/space_in_lambda_literal_spec.rb
# frozen_string_literal: true RSpec.describe RuboCop::Cop::Layout::SpaceInLambdaLiteral, :config do context 'when configured to enforce spaces' do let(:cop_config) { { 'EnforcedStyle' => 'require_space' } } it 'registers an offense and corrects no space between -> and (' do expect_offense(<<~RUBY) a = ->(b, c) { b + c } ^^^^^^^^ Use a space between `->` and `(` in lambda literals. RUBY expect_correction(<<~RUBY) a = -> (b, c) { b + c } RUBY end it 'does not register an offense for a space between -> and (' do expect_no_offenses('a = -> (b, c) { b + c }') end it 'does not register an offense for multi-line lambdas' do expect_no_offenses(<<~RUBY) l = lambda do |a, b| tmp = a * 7 tmp * b / 50 end RUBY end it 'does not register an offense for no space between -> and {' do expect_no_offenses('a = ->{ b + c }') end it 'registers an offense and corrects no space in the inner nested lambda' do expect_offense(<<~RUBY) a = -> (b = ->(c) {}, d) { b + d } ^^^^^ Use a space between `->` and `(` in lambda literals. RUBY expect_correction(<<~RUBY) a = -> (b = -> (c) {}, d) { b + d } RUBY end it 'registers an offense and corrects no space in the outer nested lambda' do expect_offense(<<~RUBY) a = ->(b = -> (c) {}, d) { b + d } ^^^^^^^^^^^^^^^^^^^^ Use a space between `->` and `(` in lambda literals. RUBY expect_correction(<<~RUBY) a = -> (b = -> (c) {}, d) { b + d } RUBY end it 'registers an offense and corrects no space in both lambdas when nested' do expect_offense(<<~RUBY) a = ->(b = ->(c) {}, d) { b + d } ^^^^^ Use a space between `->` and `(` in lambda literals. ^^^^^^^^^^^^^^^^^^^ Use a space between `->` and `(` in lambda literals. RUBY expect_correction(<<~RUBY) a = -> (b = -> (c) {}, d) { b + d } RUBY end end context 'when configured to enforce no space' do let(:cop_config) { { 'EnforcedStyle' => 'require_no_space' } } it 'registers an offense and corrects a space between -> and (' do expect_offense(<<~RUBY) a = -> (b, c) { b + c } ^ Do not use spaces between `->` and `(` in lambda literals. RUBY expect_correction(<<~RUBY) a = ->(b, c) { b + c } RUBY end it 'does not register an offense for no space between -> and (' do expect_no_offenses('a = ->(b, c) { b + c }') end it 'does not register an offense for multi-line lambdas' do expect_no_offenses(<<~RUBY) l = lambda do |a, b| tmp = a * 7 tmp * b / 50 end RUBY end it 'does not register an offense for a space between -> and {' do expect_no_offenses('a = -> { b + c }') end it 'registers an offense and corrects spaces between -> and (' do expect_offense(<<~RUBY) a = -> (b, c) { b + c } ^^^ Do not use spaces between `->` and `(` in lambda literals. RUBY expect_correction(<<~RUBY) a = ->(b, c) { b + c } RUBY end it 'registers an offense and corrects a space in the inner nested lambda' do expect_offense(<<~RUBY) a = ->(b = -> (c) {}, d) { b + d } ^ Do not use spaces between `->` and `(` in lambda literals. RUBY expect_correction(<<~RUBY) a = ->(b = ->(c) {}, d) { b + d } RUBY end it 'registers an offense and corrects a space in the outer nested lambda' do expect_offense(<<~RUBY) a = -> (b = ->(c) {}, d) { b + d } ^ Do not use spaces between `->` and `(` in lambda literals. RUBY expect_correction(<<~RUBY) a = ->(b = ->(c) {}, d) { b + d } RUBY end it 'registers offenses and correct spaces in both lambdas when nested' do expect_offense(<<~RUBY) a = -> (b = -> (c) {}, d) { b + d } ^ Do not use spaces between `->` and `(` in lambda literals. ^ Do not use spaces between `->` and `(` in lambda literals. RUBY expect_correction(<<~RUBY) a = ->(b = ->(c) {}, d) { b + d } RUBY end end end
ruby
MIT
99fa0fdd0481910d7d052f4c2ed01ad36178f404
2026-01-04T15:37:41.211519Z
false
rubocop/rubocop
https://github.com/rubocop/rubocop/blob/99fa0fdd0481910d7d052f4c2ed01ad36178f404/spec/rubocop/cop/layout/array_alignment_spec.rb
spec/rubocop/cop/layout/array_alignment_spec.rb
# frozen_string_literal: true RSpec.describe RuboCop::Cop::Layout::ArrayAlignment, :config do let(:config) do RuboCop::Config.new('Layout/ArrayAlignment' => cop_config, 'Layout/IndentationWidth' => { 'Width' => indentation_width }) end let(:indentation_width) { 2 } context 'when aligned with first parameter' do let(:cop_config) { { 'EnforcedStyle' => 'with_first_element' } } it 'registers an offense and corrects misaligned array elements' do expect_offense(<<~RUBY) array = [a, b, ^ Align the elements of an array literal if they span more than one line. c, ^ Align the elements of an array literal if they span more than one line. d] ^ Align the elements of an array literal if they span more than one line. RUBY expect_correction(<<~RUBY) array = [a, b, c, d] RUBY end it 'accepts aligned array keys' do expect_no_offenses(<<~RUBY) array = [a, b, c, d] RUBY end it 'accepts single line array' do expect_no_offenses('array = [ a, b ]') end it 'accepts several elements per line' do expect_no_offenses(<<~RUBY) array = [ a, b, c, d ] RUBY end it 'accepts aligned array with fullwidth characters' do expect_no_offenses(<<~RUBY) puts 'Ruby', [ a, b ] RUBY end it 'autocorrects array within array with too much indentation' do expect_offense(<<~RUBY) [:l1, [:l2, ^^^^^ Align the elements of an array literal if they span more than one line. [:l3, ^^^^^ Align the elements of an array literal if they span more than one line. [:l4]]]] RUBY expect_correction(<<~RUBY, loop: false) [:l1, [:l2, [:l3, [:l4]]]] RUBY end it 'autocorrects array within array with too little indentation' do expect_offense(<<~RUBY) [:l1, [:l2, ^^^^^ Align the elements of an array literal if they span more than one line. [:l3, ^^^^^ Align the elements of an array literal if they span more than one line. [:l4]]]] RUBY expect_correction(<<~RUBY, loop: false) [:l1, [:l2, [:l3, [:l4]]]] RUBY end it 'does not indent heredoc strings when autocorrecting' do expect_offense(<<~RUBY) var = [ { :type => 'something', :sql => <<EOF Select something from atable EOF }, { :type => 'something', ^^^^^^^^^^^^^^^^^^^^^^^ Align the elements of an array literal if they span more than one line. :sql => <<EOF Select something from atable EOF } ] RUBY expect_correction(<<~RUBY) var = [ { :type => 'something', :sql => <<EOF Select something from atable EOF }, { :type => 'something', :sql => <<EOF Select something from atable EOF } ] RUBY end it 'accepts the first element being on a new row' do expect_no_offenses(<<~RUBY) array = [ a, b, c, d ] RUBY end it 'autocorrects misaligned array with the first element on a new row' do expect_offense(<<~RUBY) array = [ a, b, ^ Align the elements of an array literal if they span more than one line. c, d ^ Align the elements of an array literal if they span more than one line. ] RUBY expect_correction(<<~RUBY) array = [ a, b, c, d ] RUBY end it 'does not register an offense or try to correct parallel assignment' do expect_no_offenses(<<~RUBY) thing, foo = 1, 2 RUBY end end context 'when aligned with fixed indentation' do let(:cop_config) { { 'EnforcedStyle' => 'with_fixed_indentation' } } it 'registers an offense and corrects misaligned array elements' do expect_offense(<<~RUBY) array = [a, b, ^ Use one level of indentation for elements following the first line of a multi-line array. c, d] ^ Use one level of indentation for elements following the first line of a multi-line array. RUBY expect_correction(<<~RUBY) array = [a, b, c, d] RUBY end it 'accepts aligned array keys' do expect_no_offenses(<<~RUBY) array = [a, b, c, d] RUBY end it 'accepts when assigning aligned bracketed array elements' do expect_no_offenses(<<~RUBY) var = [ first, second ] RUBY end it 'accepts when assigning aligned unbracketed array elements' do expect_no_offenses(<<~RUBY) var = first, second RUBY end it 'registers an offense when assigning not aligned unbracketed array elements' do expect_offense(<<~RUBY) var = first, ^^^^^ Use one level of indentation for elements following the first line of a multi-line array. second ^^^^^^ Use one level of indentation for elements following the first line of a multi-line array. RUBY expect_correction(<<~RUBY) var = first, second RUBY end it 'accepts single line array' do expect_no_offenses('array = [ a, b ]') end it 'accepts several elements per line' do expect_no_offenses(<<~RUBY) array = [ a, b, c, d ] RUBY end it 'accepts aligned array with fullwidth characters' do expect_no_offenses(<<~RUBY) puts 'Ruby', [ a, b ] RUBY end it 'autocorrects array within array with too much indentation' do expect_offense(<<~RUBY) [:l1, [:l2, ^^^^^ Use one level of indentation for elements following the first line of a multi-line array. [:l3, ^^^^^ Use one level of indentation for elements following the first line of a multi-line array. [:l4]]]] RUBY expect_correction(<<~RUBY, loop: false) [:l1, [:l2, [:l3, [:l4]]]] RUBY end it 'autocorrects array within array with too little indentation' do expect_offense(<<~RUBY) [:l1, [:l2, ^^^^^ Use one level of indentation for elements following the first line of a multi-line array. [:l3, ^^^^^ Use one level of indentation for elements following the first line of a multi-line array. [:l4]]]] RUBY expect_correction(<<~RUBY, loop: false) [:l1, [:l2, [:l3, [:l4]]]] RUBY end it 'does not indent heredoc strings when autocorrecting' do expect_offense(<<~RUBY) var = [ { :type => 'something', :sql => <<EOF Select something from atable EOF }, { :type => 'something', ^^^^^^^^^^^^^^^^^^^^^^^ Use one level of indentation for elements following the first line of a multi-line array. :sql => <<EOF Select something from atable EOF } ] RUBY expect_correction(<<~RUBY) var = [ { :type => 'something', :sql => <<EOF Select something from atable EOF }, { :type => 'something', :sql => <<EOF Select something from atable EOF } ] RUBY end it 'accepts the first element being on a new row' do expect_no_offenses(<<~RUBY) array = [ a, b, c, d ] RUBY end it 'autocorrects misaligned array with the first element on a new row' do expect_offense(<<~RUBY) array = [ a, b, ^ Use one level of indentation for elements following the first line of a multi-line array. c, d ^ Use one level of indentation for elements following the first line of a multi-line array. ] RUBY expect_correction(<<~RUBY) array = [ a, b, c, d ] RUBY end it 'does not register an offense or try to correct parallel assignment' do expect_no_offenses(<<~RUBY) thing, foo = 1, 2 RUBY end end end
ruby
MIT
99fa0fdd0481910d7d052f4c2ed01ad36178f404
2026-01-04T15:37:41.211519Z
false
rubocop/rubocop
https://github.com/rubocop/rubocop/blob/99fa0fdd0481910d7d052f4c2ed01ad36178f404/spec/rubocop/cop/layout/block_end_newline_spec.rb
spec/rubocop/cop/layout/block_end_newline_spec.rb
# frozen_string_literal: true RSpec.describe RuboCop::Cop::Layout::BlockEndNewline, :config do it 'accepts a one-liner' do expect_no_offenses('test do foo end') end it 'accepts multiline blocks with newlines before the end' do expect_no_offenses(<<~RUBY) test do foo end RUBY end it 'does not register an offense when multiline blocks with newlines before the `; end`' do expect_no_offenses(<<~RUBY) test do foo ; end RUBY end it 'registers an offense and corrects when multiline block end is not on its own line' do expect_offense(<<~RUBY) test do foo end ^^^ Expression at 2, 7 should be on its own line. RUBY expect_correction(<<~RUBY) test do foo end RUBY end it 'registers an offense and corrects when multiline block `}` is not on its own line' do expect_offense(<<~RUBY) test { foo } ^ Expression at 2, 7 should be on its own line. RUBY expect_correction(<<~RUBY) test { foo } RUBY end it 'registers an offense and corrects when `}` of multiline block ' \ 'without processing is not on its own line' do expect_offense(<<~RUBY) test { |foo| } ^ Expression at 2, 9 should be on its own line. RUBY expect_correction(<<~RUBY) test { |foo| } RUBY end it 'registers an offense and corrects when multiline block `}` is not on its own line ' \ 'and using method chain' do expect_offense(<<~RUBY) test { foo }.bar.baz ^ Expression at 2, 7 should be on its own line. RUBY expect_correction(<<~RUBY) test { foo }.bar.baz RUBY end it 'registers an offense and corrects when multiline block `}` is not on its own line ' \ 'and it is used as multiple arguments' do expect_offense(<<~RUBY) foo(one { x }, two { ^ Expression at 2, 5 should be on its own line. y }) ^ Expression at 3, 5 should be on its own line. RUBY expect_correction(<<~RUBY) foo(one { x }, two { y }) RUBY end it 'registers an offense and corrects when multiline block `}` is not on its own line ' \ 'and using heredoc argument' do expect_offense(<<~RUBY) test { foo(<<~EOS) } ^ Expression at 2, 15 should be on its own line. Heredoc text. EOS RUBY expect_correction(<<~RUBY) test { foo(<<~EOS) Heredoc text. EOS } RUBY end it 'registers an offense and corrects when multiline block `}` is not on its own line ' \ 'and using multiple heredoc arguments' do expect_offense(<<~RUBY) test { foo(<<~FIRST, <<~SECOND) } ^ Expression at 2, 28 should be on its own line. Heredoc text. FIRST Heredoc text. SECOND RUBY expect_correction(<<~RUBY) test { foo(<<~FIRST, <<~SECOND) Heredoc text. FIRST Heredoc text. SECOND } RUBY end it 'registers an offense and corrects when multiline block `}` is not on its own line ' \ 'and using heredoc argument with method chain' do expect_offense(<<~RUBY) test { foo(<<~EOS).bar } ^ Expression at 2, 19 should be on its own line. Heredoc text. EOS RUBY expect_correction(<<~RUBY) test { foo(<<~EOS).bar Heredoc text. EOS } RUBY end it 'registers an offense and corrects when multiline block `}` is not on its own line ' \ 'and using multiple heredoc argument method chain' do expect_offense(<<~RUBY) test { foo(<<~FIRST).bar(<<~SECOND) } ^ Expression at 2, 32 should be on its own line. Heredoc text. FIRST Heredoc text. SECOND RUBY expect_correction(<<~RUBY) test { foo(<<~FIRST).bar(<<~SECOND) Heredoc text. FIRST Heredoc text. SECOND } RUBY end it 'registers an offense and corrects when a multiline block ends with a hash' do expect_offense(<<~RUBY) foo { { bar: :baz } } ^ Expression at 2, 17 should be on its own line. RUBY expect_correction(<<~RUBY) foo { { bar: :baz } } RUBY end it 'registers an offense and corrects when a multiline block ends with a method call with hash arguments' do expect_offense(<<~RUBY) foo { bar(baz: :quux) } ^ Expression at 2, 19 should be on its own line. RUBY expect_correction(<<~RUBY) foo { bar(baz: :quux) } RUBY end context 'Ruby 2.7', :ruby27 do it 'registers an offense and corrects when multiline block `}` is not on its own line ' \ 'and using method chain' do expect_offense(<<~RUBY) test { _1 }.bar.baz ^ Expression at 2, 6 should be on its own line. RUBY expect_correction(<<~RUBY) test { _1 }.bar.baz RUBY end it 'registers an offense and corrects when multiline block `}` is not on its own line ' \ 'and using heredoc argument' do expect_offense(<<~RUBY) test { _1.push(<<~EOS) } ^ Expression at 2, 19 should be on its own line. Heredoc text. EOS RUBY expect_correction(<<~RUBY) test { _1.push(<<~EOS) Heredoc text. EOS } RUBY end end context 'Ruby 3.4', :ruby34 do it 'registers an offense and corrects when multiline block `}` is not on its own line ' \ 'and using method chain' do expect_offense(<<~RUBY) test { it }.bar.baz ^ Expression at 2, 6 should be on its own line. RUBY expect_correction(<<~RUBY) test { it }.bar.baz RUBY end it 'registers an offense and corrects when multiline block `}` is not on its own line ' \ 'and using heredoc argument' do expect_offense(<<~RUBY) test { it.push(<<~EOS) } ^ Expression at 2, 19 should be on its own line. Heredoc text. EOS RUBY expect_correction(<<~RUBY) test { it.push(<<~EOS) Heredoc text. EOS } RUBY end end end
ruby
MIT
99fa0fdd0481910d7d052f4c2ed01ad36178f404
2026-01-04T15:37:41.211519Z
false
rubocop/rubocop
https://github.com/rubocop/rubocop/blob/99fa0fdd0481910d7d052f4c2ed01ad36178f404/spec/rubocop/cop/layout/space_around_keyword_spec.rb
spec/rubocop/cop/layout/space_around_keyword_spec.rb
# frozen_string_literal: true RSpec.describe RuboCop::Cop::Layout::SpaceAroundKeyword, :config do shared_examples 'missing before' do |highlight, expr, correct| it "registers an offense for missing space before keyword in `#{expr}`" do h_index = expr.index(highlight) expect_offense(<<~RUBY) #{expr} #{' ' * h_index}#{'^' * highlight.size} Space before keyword `#{highlight}` is missing. RUBY expect_correction("#{correct}\n") end end shared_examples 'missing after' do |highlight, expr, correct, options| it "registers an offense for missing space after keyword in `#{expr}` and autocorrects", *options do h_index = expr.index(highlight) expect_offense(<<~RUBY) #{expr} #{' ' * h_index}#{'^' * highlight.size} Space after keyword `#{highlight}` is missing. RUBY expect_correction("#{correct}\n") end end shared_examples 'accepts before' do |after, expr| it "accepts `#{after}` before keyword in `#{expr}`" do expect_no_offenses(expr) end end shared_examples 'accepts after' do |after, expr, options| it "accepts `#{after}` after keyword in `#{expr}`", *options do expect_no_offenses(expr) end end shared_examples 'accepts around' do |after, expr, options| it "accepts `#{after}` around keyword in `#{expr}`", *options do expect_no_offenses(expr) end end it_behaves_like 'missing after', 'BEGIN', 'BEGIN{}', 'BEGIN {}' it_behaves_like 'missing after', 'END', 'END{}', 'END {}' it_behaves_like 'missing before', 'and', '1and 2', '1 and 2' it_behaves_like 'missing after', 'and', '1 and(2)', '1 and (2)' it_behaves_like 'missing after', 'begin', 'begin"" end', 'begin "" end' it_behaves_like 'missing after', 'break', 'break""', 'break ""', [:ruby32, { unsupported_on: :prism }] it_behaves_like 'accepts after', '(', 'break(1)', [:ruby32, { unsupported_on: :prism }] it_behaves_like 'missing after', 'case', 'case"" when 1; end', 'case "" when 1; end' context '>= Ruby 2.7', :ruby27 do # rubocop:disable RSpec/RepeatedExampleGroupDescription it_behaves_like 'missing after', 'case', 'case""; in 1; end', 'case ""; in 1; end' end it_behaves_like 'missing before', 'do', 'a "b"do end', 'a "b" do end' it_behaves_like 'missing after', 'do', 'a do|x| end', 'a do |x| end' it_behaves_like 'missing before', 'do', 'while 1do end', 'while 1 do end' it_behaves_like 'missing after', 'do', 'while 1 do"x" end', 'while 1 do "x" end' it_behaves_like 'missing before', 'do', 'until 1do end', 'until 1 do end' it_behaves_like 'missing after', 'do', 'until 1 do"x" end', 'until 1 do "x" end' it_behaves_like 'missing before', 'do', 'for x in []do end', 'for x in [] do end' it_behaves_like 'missing after', 'do', 'for x in [] do"x" end', 'for x in [] do "x" end' it_behaves_like 'missing before', 'end', 'begin "a"end', 'begin "a" end' it_behaves_like 'missing before', 'end', 'if a; "b"end', 'if a; "b" end' it_behaves_like 'missing before', 'end', 'a do "a"end', 'a do "a" end' it_behaves_like 'missing before', 'end', 'while 1 do "x"end', 'while 1 do "x" end' it_behaves_like 'missing before', 'end', 'until 1 do "x"end', 'until 1 do "x" end' it_behaves_like 'missing before', 'end', 'for x in [] do "x"end', 'for x in [] do "x" end' it_behaves_like 'accepts after', '.', 'begin end.inspect' it_behaves_like 'missing before', 'else', 'if a; ""else end', 'if a; "" else end' it_behaves_like 'missing after', 'else', 'if a; else"" end', 'if a; else "" end' it_behaves_like 'missing before', 'else', 'begin rescue; ""else end', 'begin rescue; "" else end' it_behaves_like 'missing after', 'else', 'begin rescue; else"" end', 'begin rescue; else "" end' it_behaves_like 'missing before', 'else', 'case a; when b; ""else end', 'case a; when b; "" else end' it_behaves_like 'missing after', 'else', 'case a; when b; else"" end', 'case a; when b; else "" end' context '>= Ruby 2.7', :ruby27 do # rubocop:disable RSpec/RepeatedExampleGroupDescription it_behaves_like 'missing before', 'else', 'case a; in b; ""else end', 'case a; in b; "" else end' it_behaves_like 'missing after', 'else', 'case a; in b; else"" end', 'case a; in b; else "" end' it_behaves_like 'missing before', 'if', 'case a; in "pattern"if "condition"; else "" end', 'case a; in "pattern" if "condition"; else "" end' it_behaves_like 'missing after', 'if', 'case a; in "pattern" if"condition"; else "" end', 'case a; in "pattern" if "condition"; else "" end' it_behaves_like 'missing before', 'unless', 'case a; in "pattern"unless "condition"; else "" end', 'case a; in "pattern" unless "condition"; else "" end' it_behaves_like 'missing after', 'unless', 'case a; in "pattern" unless"condition"; else "" end', 'case a; in "pattern" unless "condition"; else "" end' end it_behaves_like 'missing before', 'elsif', 'if a; ""elsif b; end', 'if a; "" elsif b; end' it_behaves_like 'missing after', 'elsif', 'if a; elsif""; end', 'if a; elsif ""; end' it_behaves_like 'missing before', 'ensure', 'begin ""ensure end', 'begin "" ensure end' it_behaves_like 'missing after', 'ensure', 'begin ensure"" end', 'begin ensure "" end' it_behaves_like 'missing after', 'if', 'if""; end', 'if ""; end' it_behaves_like 'missing after', 'next', 'next""', 'next ""', [:ruby32, { unsupported_on: :prism }] it_behaves_like 'accepts after', '(', 'next(1)', [:ruby32, { unsupported_on: :prism }] it_behaves_like 'missing after', 'not', 'not""', 'not ""' it_behaves_like 'accepts after', '(', 'not(1)' it_behaves_like 'missing before', 'or', '1or 2', '1 or 2' it_behaves_like 'missing after', 'or', '1 or(2)', '1 or (2)' it_behaves_like 'missing before', 'rescue', '""rescue a', '"" rescue a' it_behaves_like 'missing after', 'rescue', 'a rescue""', 'a rescue ""' it_behaves_like 'accepts after', 'rescue', 'begin; rescue(Error); end', 'begin; rescue(Error); end' it_behaves_like 'missing after', 'return', 'return""', 'return ""' it_behaves_like 'missing after', 'return', 'return(1)', 'return (1)' it_behaves_like 'missing after', 'super', 'super""', 'super ""' it_behaves_like 'accepts after', '(', 'super(1)' it_behaves_like 'missing after', 'super', 'super{}', 'super {}' it_behaves_like 'accepts after', '(', 'defined?(1)' it_behaves_like 'missing after', 'defined?', 'defined?1', 'defined? 1' it_behaves_like 'missing before', 'then', 'if ""then a end', 'if "" then a end' it_behaves_like 'missing after', 'then', 'if a then"" end', 'if a then "" end' it_behaves_like 'missing after', 'unless', 'unless""; end', 'unless ""; end' it_behaves_like 'missing before', 'until', '1until ""', '1 until ""' it_behaves_like 'missing after', 'until', '1 until""', '1 until ""' it_behaves_like 'missing before', 'when', 'case ""when a; end', 'case "" when a; end' it_behaves_like 'missing after', 'when', 'case a when""; end', 'case a when ""; end' context '>= Ruby 2.7', :ruby27 do # rubocop:disable RSpec/RepeatedExampleGroupDescription # TODO: `case ""in a; end` is syntax error in Ruby 3.0.1. # This syntax is confirmed: https://bugs.ruby-lang.org/issues/17925 # The answer will determine whether to enable or discard the test in the future. # it_behaves_like 'missing before', 'in', 'case ""in a; end', 'case "" in a; end' it_behaves_like 'missing after', 'in', 'case a; in""; end', 'case a; in ""; end' it_behaves_like 'missing before', 'in', '""in a', '"" in a' it_behaves_like 'missing after', 'in', 'a in""', 'a in ""' end context '>= Ruby 3.0', :ruby30 do it_behaves_like 'accepts before', '=>', '""=> a' it_behaves_like 'accepts after', '=>', 'a =>""' end it_behaves_like 'missing before', 'while', '1while ""', '1 while ""' it_behaves_like 'missing after', 'while', '1 while""', '1 while ""' it_behaves_like 'missing after', 'yield', 'yield""', 'yield ""' it_behaves_like 'accepts after', '(', 'yield(1)' it_behaves_like 'accepts after', '+', '+begin end' it_behaves_like 'missing after', 'begin', 'begin+1 end', 'begin +1 end' # Common exceptions it_behaves_like 'accepts after', '\\', "test do\\\nend" it_behaves_like 'accepts after', '\n', "test do\nend" it_behaves_like 'accepts around', '()', '(next)', [:ruby32, { unsupported_on: :prism }] it_behaves_like 'accepts before', '!', '!yield' it_behaves_like 'accepts after', '.', 'yield.method' it_behaves_like 'accepts before', '!', '!yield.method' it_behaves_like 'accepts before', '!', '!super.method' it_behaves_like 'accepts after', '::', 'super::ModuleName' context '&.' do it_behaves_like 'accepts after', '&.', 'super&.foo' it_behaves_like 'accepts after', '&.', 'yield&.foo' end it_behaves_like 'accepts after', '[', 'super[1]' it_behaves_like 'accepts after', '[', 'yield[1]' # Layout/SpaceAroundBlockParameters it_behaves_like 'accepts before', '|', 'loop { |x|break }' # Layout/SpaceInsideRangeLiteral it_behaves_like 'accepts before', '..', '1..super.size' it_behaves_like 'accepts before', '...', '1...super.size' # Layout/SpaceAroundOperators it_behaves_like 'accepts before', '=', 'a=begin end' it_behaves_like 'accepts before', '==', 'a==begin end' it_behaves_like 'accepts before', '+', 'a+begin end' it_behaves_like 'accepts before', '+', 'a+begin; end.method' it_behaves_like 'accepts before', '-', 'a-begin end' it_behaves_like 'accepts before', '*', 'a*begin end' it_behaves_like 'accepts before', '**', 'a**begin end' it_behaves_like 'accepts before', '/', 'a/begin end' it_behaves_like 'accepts before', '<', 'a<begin end' it_behaves_like 'accepts before', '>', 'a>begin end' it_behaves_like 'accepts before', '&&', 'a&&begin end' it_behaves_like 'accepts before', '||', 'a||begin end' it_behaves_like 'accepts before', '=*', 'a=*begin end' # Layout/SpaceBeforeBlockBraces it_behaves_like 'accepts after', '{', 'loop{}' # Layout/SpaceBeforeComma, Layout/SpaceAfterComma it_behaves_like 'accepts around', ',', 'a 1,foo,1' # Layout/SpaceBeforeComment it_behaves_like 'accepts after', '#', 'next#comment', [:ruby32, { unsupported_on: :prism }] # Layout/SpaceBeforeSemicolon, Layout/SpaceAfterSemicolon it_behaves_like 'accepts around', ';', 'test do;end' # Layout/SpaceInsideArrayLiteralBrackets it_behaves_like 'accepts around', '[]', '[begin end]' # Layout/SpaceInsideBlockBraces it_behaves_like 'accepts around', '{}', 'loop {next}' # Layout/SpaceInsideHashLiteralBraces it_behaves_like 'accepts around', '{}', '{a: begin end}' # Layout/SpaceInsideReferenceBrackets it_behaves_like 'accepts around', '[]', 'a[begin end]' # Layout/SpaceInsideStringInterpolation it_behaves_like 'accepts around', '{}', '"#{begin end}"' end
ruby
MIT
99fa0fdd0481910d7d052f4c2ed01ad36178f404
2026-01-04T15:37:41.211519Z
false
rubocop/rubocop
https://github.com/rubocop/rubocop/blob/99fa0fdd0481910d7d052f4c2ed01ad36178f404/spec/rubocop/cop/layout/space_around_equals_in_parameter_default_spec.rb
spec/rubocop/cop/layout/space_around_equals_in_parameter_default_spec.rb
# frozen_string_literal: true RSpec.describe RuboCop::Cop::Layout::SpaceAroundEqualsInParameterDefault, :config do context 'when EnforcedStyle is space' do let(:cop_config) { { 'EnforcedStyle' => 'space' } } it 'registers an offense and corrects default value assignment without space' do expect_offense(<<~RUBY) def f(x, y=0, z= 1) ^ Surrounding space missing in default value assignment. ^^ Surrounding space missing in default value assignment. end RUBY expect_correction(<<~RUBY) def f(x, y = 0, z = 1) end RUBY end it 'registers an offense and corrects default value assignment where first is partially right ' \ 'without space' do expect_offense(<<~RUBY) def f(x, y= 0, z=1) ^^ Surrounding space missing in default value assignment. ^ Surrounding space missing in default value assignment. end RUBY expect_correction(<<~RUBY) def f(x, y = 0, z = 1) end RUBY end it 'registers an offense and corrects assigning empty string without space' do expect_offense(<<~RUBY) def f(x, y="") ^ Surrounding space missing in default value assignment. end RUBY expect_correction(<<~RUBY) def f(x, y = "") end RUBY end it 'registers an offense and corrects assignment of empty list without space' do expect_offense(<<~RUBY) def f(x, y=[]) ^ Surrounding space missing in default value assignment. end RUBY expect_correction(<<~RUBY) def f(x, y = []) end RUBY end it 'accepts default value assignment with space' do expect_no_offenses(<<~RUBY) def f(x, y = 0, z = {}) end RUBY end it 'accepts default value assignment with spaces and unary + operator' do expect_no_offenses(<<~RUBY) def f(x, y = +1, z = {}) end RUBY end it 'registers an offense and corrects missing space for arguments with unary operators' do expect_offense(<<~RUBY) def f(x=-1, y= 0, z =+1) ^^ Surrounding space missing in default value assignment. ^^ Surrounding space missing in default value assignment. ^ Surrounding space missing in default value assignment. end RUBY expect_correction(<<~RUBY) def f(x = -1, y = 0, z = +1) end RUBY end end context 'when EnforcedStyle is no_space' do let(:cop_config) { { 'EnforcedStyle' => 'no_space' } } it 'registers an offense and corrects default value assignment with space' do expect_offense(<<~RUBY) def f(x, y = 0, z =1, w= 2) ^^^ Surrounding space detected in default value assignment. ^^ Surrounding space detected in default value assignment. ^^ Surrounding space detected in default value assignment. end RUBY expect_correction(<<~RUBY) def f(x, y=0, z=1, w=2) end RUBY end it 'registers an offense and corrects assignment of empty string with space' do expect_offense(<<~RUBY) def f(x, y = "") ^^^ Surrounding space detected in default value assignment. end RUBY expect_correction(<<~RUBY) def f(x, y="") end RUBY end it 'registers an offense and corrects assignment of empty list with space' do expect_offense(<<~RUBY) def f(x, y = []) ^^^ Surrounding space detected in default value assignment. end RUBY expect_correction(<<~RUBY) def f(x, y=[]) end RUBY end it 'accepts default value assignment without space' do expect_no_offenses(<<~RUBY) def f(x, y=0, z={}) end RUBY end end end
ruby
MIT
99fa0fdd0481910d7d052f4c2ed01ad36178f404
2026-01-04T15:37:41.211519Z
false
rubocop/rubocop
https://github.com/rubocop/rubocop/blob/99fa0fdd0481910d7d052f4c2ed01ad36178f404/spec/rubocop/cop/layout/space_before_first_arg_spec.rb
spec/rubocop/cop/layout/space_before_first_arg_spec.rb
# frozen_string_literal: true RSpec.describe RuboCop::Cop::Layout::SpaceBeforeFirstArg, :config do let(:cop_config) { { 'AllowForAlignment' => true } } let(:message) { 'Put one space between the method name and the first argument.' } context 'for method calls without parentheses' do it 'registers an offense and corrects method call with two spaces before the first arg' do expect_offense(<<~RUBY) something x ^^ #{message} a.something y, z ^^ #{message} RUBY expect_correction(<<~RUBY) something x a.something y, z RUBY end context 'when using safe navigation operator' do it 'registers an offense and corrects method call with two spaces before the first arg' do expect_offense(<<~RUBY) a&.something y, z ^^ #{message} RUBY expect_correction(<<~RUBY) a&.something y, z RUBY end end it 'registers an offense for method call with no spaces before the first arg' do expect_offense(<<~RUBY) something'hello' ^{} #{message} a.something'hello world' ^{} #{message} RUBY expect_correction(<<~RUBY) something 'hello' a.something 'hello world' RUBY end context 'when a vertical argument positions are aligned' do it 'registers an offense' do expect_offense(<<~RUBY) obj = a_method(arg, arg2) obj.no_parenthesized'asdf' ^{} #{message} RUBY expect_correction(<<~RUBY) obj = a_method(arg, arg2) obj.no_parenthesized 'asdf' RUBY end end it 'accepts a method call with one space before the first arg' do expect_no_offenses(<<~RUBY) something x a.something y, z RUBY end it 'accepts + operator' do expect_no_offenses(<<~RUBY) something + x RUBY end it 'accepts setter call' do expect_no_offenses(<<~RUBY) something.x = y RUBY end it 'accepts multiple space containing line break' do expect_no_offenses(<<~RUBY) something \\ x RUBY end context 'when AllowForAlignment is true' do it 'accepts method calls with aligned first arguments' do expect_no_offenses(<<~RUBY) form.inline_input :full_name, as: :string form.disabled_input :password, as: :passwd form.masked_input :zip_code, as: :string form.masked_input :email_address, as: :email form.masked_input :phone_number, as: :tel RUBY end end context 'when AllowForAlignment is false' do let(:cop_config) { { 'AllowForAlignment' => false } } it 'registers an offense and corrects method calls with aligned first arguments' do expect_offense(<<~RUBY) form.inline_input :full_name, as: :string ^^^ Put one space between the method name and the first argument. form.disabled_input :password, as: :passwd form.masked_input :zip_code, as: :string ^^^ Put one space between the method name and the first argument. form.masked_input :email_address, as: :email ^^^ Put one space between the method name and the first argument. form.masked_input :phone_number, as: :tel ^^^ Put one space between the method name and the first argument. RUBY expect_correction(<<~RUBY) form.inline_input :full_name, as: :string form.disabled_input :password, as: :passwd form.masked_input :zip_code, as: :string form.masked_input :email_address, as: :email form.masked_input :phone_number, as: :tel RUBY end end end context 'for method calls with parentheses' do it 'accepts a method call without space' do expect_no_offenses(<<~RUBY) something(x) a.something(y, z) RUBY end it 'accepts a method call with space after the left parenthesis' do expect_no_offenses('something( x )') end end end
ruby
MIT
99fa0fdd0481910d7d052f4c2ed01ad36178f404
2026-01-04T15:37:41.211519Z
false