/
io_readlines_spec.rb
62 lines (52 loc) · 2.12 KB
/
io_readlines_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
# frozen_string_literal: true
RSpec.describe RuboCop::Cop::Performance::IoReadlines do
subject(:cop) { described_class.new }
it 'registers an offense when using `File.readlines` followed by Enumerable method' do
expect_offense(<<~RUBY)
File.readlines('testfile').map { |l| l.size }
^^^^^^^^^^^^^^^^^^^^^^^^^ Use `each_line.map` instead of `readlines.map`.
RUBY
end
it 'registers an offense when using `IO.readlines` followed by Enumerable method' do
expect_offense(<<~RUBY)
IO.readlines('testfile').map { |l| l.size }
^^^^^^^^^^^^^^^^^^^^^^^^^ Use `each_line.map` instead of `readlines.map`.
RUBY
end
it 'registers an offense when using `IO.readlines` followed by `#each` method' do
# Note: `each_line` in message, not `each_line.each`
expect_offense(<<~RUBY)
IO.readlines('testfile').each { |l| puts l }
^^^^^^^^^^^^^^^^^^^^^^^^^^ Use `each_line` instead of `readlines.each`.
RUBY
end
it 'does not register an offense when using `.readlines` and not followed by Enumerable method' do
expect_no_offenses(<<~RUBY)
File.readlines('testfile').not_enumerable_method
RUBY
end
it 'registers an offense and corrects when using `#readlines` on an instance followed by Enumerable method' do
expect_offense(<<~RUBY)
file.readlines(10).map { |l| l.size }
^^^^^^^^^^^^^^^^^ Use `each_line.map` instead of `readlines.map`.
RUBY
expect_correction(<<~RUBY)
file.each_line(10).map { |l| l.size }
RUBY
end
it 'registers an offense and corrects when using `#readlines` on an instance followed by `#each` method' do
# Note: `each_line` in message, not `each_line.each`
expect_offense(<<~RUBY)
file.readlines(10).each { |l| puts l }
^^^^^^^^^^^^^^^^^^ Use `each_line` instead of `readlines.each`.
RUBY
expect_correction(<<~RUBY)
file.each_line(10) { |l| puts l }
RUBY
end
it 'does not register an offense when using `#readlines` on an instance and not followed by Enumerable method' do
expect_no_offenses(<<~RUBY)
file.readlines.not_enumerable_method
RUBY
end
end