forked from percy/percy-style
-
Notifications
You must be signed in to change notification settings - Fork 0
/
unless_with_multiple_conditions_spec.rb
61 lines (54 loc) · 1.95 KB
/
unless_with_multiple_conditions_spec.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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
require 'spec_helper'
require 'rubocop'
require './lib/unless_with_multiple_conditions'
describe RuboCop::Cop::Style::UnlessWithMultipleConditions do
# These helper methods are from https://github.com/bbatsov/rubocop/blob/4b26354497f32da8d771f35c66b2f47224f9674b/spec/support/cop_helper.rb#L10
def inspect_source(cop, source, file = nil)
if source.is_a?(Array) && source.size == 1
fail "Don't use an array for a single line of code: #{source}"
end
RuboCop::Formatter::DisabledConfigFormatter.config_to_allow_offenses = {}
processed_source = parse_source(source, file)
fail 'Error parsing example code' unless processed_source.valid_syntax?
_investigate(cop, processed_source)
end
def parse_source(source, file = nil)
source = source.join($RS) if source.is_a?(Array)
if file&.respond_to?(:write)
file.write(source)
file.rewind
file = file.path
end
RuboCop::ProcessedSource.new(source, 2.6, file)
end
def _investigate(cop, processed_source)
team = RuboCop::Cop::Team.new([cop], nil, raise_error: true)
report = team.investigate(processed_source)
@last_corrector = report.correctors.first || RuboCop::Cop::Corrector.new(processed_source)
report.offenses
end
it "isn't offended by single conditions, if statements with multiple conditions" do
[
['if(:foo)', 'end'],
['unless(:foo)', 'end'],
['if(:foo || :bar)', 'end'],
['if(:foo && :bar)', 'end'],
].each do |source|
cop = described_class.new
inspect_source(cop, source)
expect(cop.offenses.size).to eq(0)
end
end
it 'is offended by unless statements with multiple conditions' do
[
['unless :foo && :bar', 'end'],
['unless :foo and :bar', 'end'],
['unless :foo || :bar', 'end'],
['unless :foo or :bar', 'end'],
].each do |source|
cop = described_class.new
inspect_source(cop, source)
expect(cop.offenses.size).to eq(1)
end
end
end