-
-
Notifications
You must be signed in to change notification settings - Fork 3k
/
alignment_examples.rb
38 lines (32 loc) · 1.37 KB
/
alignment_examples.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# frozen_string_literal: true
# `cop` and `source` must be declared with #let.
shared_examples_for 'misaligned' do |annotated_source, used_style|
config_to_allow_offenses = if used_style
{ 'EnforcedStyleAlignWith' => used_style.to_s }
else
{ 'Enabled' => false }
end
annotated_source.split("\n\n").each do |chunk|
chunk << "\n" unless chunk.end_with?("\n")
source = chunk.lines.grep_v(/^ *\^/).join
name = source.gsub(/\n(?=[a-z ])/, ' <newline> ').gsub(/\s+/, ' ')
it "registers an offense for mismatched #{name} and autocorrects" do
expect_offense(chunk)
expect(cop.config_to_allow_offenses).to eq(config_to_allow_offenses)
raise if chunk !~ /\^\^\^ `end` at (\d), \d is not aligned with `.*` at \d, (\d)./
line_index = Integer(Regexp.last_match(1)) - 1
correct_indentation = ' ' * Integer(Regexp.last_match(2))
expect_correction(
source.lines[0...line_index].join +
"#{correct_indentation}#{source.lines[line_index].strip}\n"
)
end
end
end
shared_examples_for 'aligned' do |alignment_base, arg, end_kw, name|
name ||= alignment_base
name = name.gsub(/\n/, ' <newline>')
it "accepts matching #{name} ... end" do
expect_no_offenses("#{alignment_base} #{arg}\n#{end_kw}")
end
end