Skip to content

Commit a6210f7

Browse files
committed
Compute file path with ActiveSupport Inflector if defined
1 parent 2f8ec8a commit a6210f7

File tree

3 files changed

+29
-0
lines changed

3 files changed

+29
-0
lines changed

lib/rubocop/cop/rspec/file_path.rb

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,13 @@ def expected_path(constant)
130130
end
131131

132132
def camel_to_snake_case(string)
133+
if defined?(ActiveSupport::Inflector)
134+
if File.exist?('./config/initializers/inflections.rb')
135+
require './config/initializers/inflections'
136+
end
137+
return ActiveSupport::Inflector.underscore(string)
138+
end
139+
133140
string
134141
.gsub(/([^A-Z])([A-Z]+)/, '\1_\2')
135142
.gsub(/([A-Z])([A-Z][^A-Z\d]+)/, '\1_\2')

rubocop-rspec.gemspec

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ Gem::Specification.new do |spec|
3939

4040
spec.add_runtime_dependency 'rubocop', '~> 1.19'
4141

42+
spec.add_development_dependency 'activesupport'
4243
spec.add_development_dependency 'rack'
4344
spec.add_development_dependency 'rake'
4445
spec.add_development_dependency 'rspec', '>= 3.4'

spec/rubocop/cop/rspec/file_path_spec.rb

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -246,6 +246,27 @@ class Foo
246246
end
247247
end
248248

249+
context 'when ActiveSupport::Inflector is defined' do
250+
before { require 'active_support/inflector' }
251+
252+
it 'registers an offense for a bad path when there is no custom acronym' do
253+
expect_offense(<<-RUBY, 'pvp_class_foo_spec.rb')
254+
describe PvPClass, 'foo' do; end
255+
^^^^^^^^^^^^^^^^^^^^^^^^ Spec path should end with `pv_p_class*foo*_spec.rb`.
256+
RUBY
257+
end
258+
259+
it 'does not register an offense when class name contains custom acronym' do
260+
ActiveSupport::Inflector.inflections do |inflect|
261+
inflect.acronym('PvP')
262+
end
263+
264+
expect_no_offenses(<<-RUBY, 'pvp_class_foo_spec.rb')
265+
describe PvPClass, 'foo' do; end
266+
RUBY
267+
end
268+
end
269+
249270
context 'when configured with IgnoreMethods' do
250271
let(:cop_config) { { 'IgnoreMethods' => true } }
251272

0 commit comments

Comments
 (0)