Skip to content

Commit

Permalink
Compute file path with ActiveSupport Inflector if defined
Browse files Browse the repository at this point in the history
  • Loading branch information
jeromedalbert committed Apr 26, 2022
1 parent 2f8ec8a commit 1dd8f53
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 0 deletions.
7 changes: 7 additions & 0 deletions lib/rubocop/cop/rspec/file_path.rb
Expand Up @@ -130,6 +130,13 @@ def expected_path(constant)
end

def camel_to_snake_case(string)
if defined?(ActiveSupport::Inflector)
if File.exist?('./config/initializers/inflections.rb')
require './config/initializers/inflections'
end
return ActiveSupport::Inflector.underscore(string)
end

string
.gsub(/([^A-Z])([A-Z]+)/, '\1_\2')
.gsub(/([A-Z])([A-Z][^A-Z\d]+)/, '\1_\2')
Expand Down
1 change: 1 addition & 0 deletions rubocop-rspec.gemspec
Expand Up @@ -39,6 +39,7 @@ Gem::Specification.new do |spec|

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

spec.add_development_dependency 'activesupport'
spec.add_development_dependency 'rack'
spec.add_development_dependency 'rake'
spec.add_development_dependency 'rspec', '>= 3.4'
Expand Down
21 changes: 21 additions & 0 deletions spec/rubocop/cop/rspec/file_path_spec.rb
Expand Up @@ -246,6 +246,27 @@ class Foo
end
end

context 'when ActiveSupport is defined' do
before { require 'active_support/inflector' }

it 'registers an offense for a bad path when there is no custom acronym' do
expect_offense(<<-RUBY, 'pvp_class_foo_spec.rb')
describe PvPClass, 'foo' do; end
^^^^^^^^^^^^^^^^^^^^^^^^ Spec path should end with `pv_p_class*foo*_spec.rb`.
RUBY
end

it 'does not register an offense when class name contains custom acronym' do
ActiveSupport::Inflector.inflections do |inflect|
inflect.acronym('PvP')
end

expect_no_offenses(<<-RUBY, 'pvp_class_foo_spec.rb')
describe PvPClass, 'foo' do; end
RUBY
end
end

context 'when configured with IgnoreMethods' do
let(:cop_config) { { 'IgnoreMethods' => true } }

Expand Down

0 comments on commit 1dd8f53

Please sign in to comment.