/
nested_file_dirname_spec.rb
47 lines (40 loc) · 1.36 KB
/
nested_file_dirname_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
# frozen_string_literal: true
RSpec.describe RuboCop::Cop::Style::NestedFileDirname, :config do
context 'Ruby >= 3.1', :ruby31 do
it 'registers and corrects an offense when using `File.dirname(path)` nested two times' do
expect_offense(<<~RUBY)
File.dirname(File.dirname(path))
^^^^^^^^^^^^^^^^^^^^^^^^^^^ Use `dirname(path, 2)` instead.
RUBY
expect_correction(<<~RUBY)
File.dirname(path, 2)
RUBY
end
it 'registers and corrects an offense when using `File.dirname(path)` nested three times' do
expect_offense(<<~RUBY)
File.dirname(File.dirname(File.dirname(path)))
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Use `dirname(path, 3)` instead.
RUBY
expect_correction(<<~RUBY)
File.dirname(path, 3)
RUBY
end
it 'does not register an offense when using non nested `File.dirname(path)`' do
expect_no_offenses(<<~RUBY)
File.dirname(path)
RUBY
end
it 'does not register an offense when using `File.dirname(path, 2)`' do
expect_no_offenses(<<~RUBY)
File.dirname(path, 2)
RUBY
end
end
context 'Ruby <= 3.0', :ruby30 do
it 'does not register an offense when using `File.dirname(path)` nested two times' do
expect_no_offenses(<<~RUBY)
File.dirname(File.dirname(path))
RUBY
end
end
end