forked from rubocop/rubocop
-
Notifications
You must be signed in to change notification settings - Fork 0
/
single_line_comparison_spec.rb
103 lines (85 loc) · 3.01 KB
/
single_line_comparison_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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# 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 '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
end