@@ -281,4 +281,85 @@ class Foo
281
281
RUBY
282
282
end
283
283
end
284
+
285
+ context 'when ActiveSupport inflections are available' do
286
+ before do
287
+ allow ( described_class ) . to receive ( :require ) . with ( 'active_support/inflector' ) . and_return ( true )
288
+ stub_const ( 'ActiveSupport::Inflector' , double ( 'ActiveSupport::Inflector' ) )
289
+ allow ( ActiveSupport ::Inflector ) . to receive ( :underscore ) . with ( 'TestClass' ) . and_return ( 'test_class' )
290
+ end
291
+
292
+ around do |example |
293
+ described_class . reset_activesupport_cache!
294
+ example . run
295
+ described_class . reset_activesupport_cache!
296
+ end
297
+
298
+ it 'uses ActiveSupport inflections for custom acronyms' do
299
+ allow ( ActiveSupport ::Inflector ) . to receive ( :underscore ) . with ( 'PvPClass' ) . and_return ( 'pvp_class' )
300
+
301
+ expect_no_offenses ( <<~RUBY , 'pvp_class_spec.rb' )
302
+ describe PvPClass do; end
303
+ RUBY
304
+ end
305
+
306
+ it 'registers an offense when ActiveSupport inflections suggest different path' do
307
+ allow ( ActiveSupport ::Inflector ) . to receive ( :underscore ) . with ( 'PvPClass' ) . and_return ( 'pvp_class' )
308
+
309
+ expect_offense ( <<~RUBY , 'pv_p_class_spec.rb' )
310
+ describe PvPClass do; end
311
+ ^^^^^^^^^^^^^^^^^ Spec path should end with `pvp_class*_spec.rb`.
312
+ RUBY
313
+ end
314
+
315
+ it 'does not register complex acronyms with method names' do
316
+ allow ( ActiveSupport ::Inflector ) . to receive ( :underscore ) . with ( 'PvPClass' ) . and_return ( 'pvp_class' )
317
+
318
+ expect_no_offenses ( <<~RUBY , 'pvp_class_foo_spec.rb' )
319
+ describe PvPClass, 'foo' do; end
320
+ RUBY
321
+ end
322
+
323
+ it 'does not register nested namespaces with custom acronyms' do
324
+ allow ( ActiveSupport ::Inflector ) . to receive ( :underscore ) . with ( 'API' ) . and_return ( 'api' )
325
+ allow ( ActiveSupport ::Inflector ) . to receive ( :underscore ) . with ( 'HTTPClient' ) . and_return ( 'http_client' )
326
+
327
+ expect_no_offenses ( <<~RUBY , 'api/http_client_spec.rb' )
328
+ describe API::HTTPClient do; end
329
+ RUBY
330
+ end
331
+ end
332
+
333
+ context 'when ActiveSupport inflections are not available' do
334
+ before do
335
+ allow ( described_class ) . to receive ( :require ) . with ( 'active_support/inflector' ) . and_raise ( LoadError )
336
+ end
337
+
338
+ it 'falls back to default inflection behavior' do
339
+ expect_no_offenses ( <<~RUBY , 'pv_p_class_spec.rb' )
340
+ describe PvPClass do; end
341
+ RUBY
342
+ end
343
+
344
+ it 'registers offense when default inflection does not match' do
345
+ expect_offense ( <<~RUBY , 'pvp_class_spec.rb' )
346
+ describe PvPClass do; end
347
+ ^^^^^^^^^^^^^^^^^ Spec path should end with `pv_p_class*_spec.rb`.
348
+ RUBY
349
+ end
350
+ end
351
+
352
+ context 'when ActiveSupport loading raises an error' do
353
+ before do
354
+ allow ( described_class ) . to receive ( :require ) . with ( 'active_support/inflector' ) . and_raise (
355
+ StandardError , 'Something went wrong'
356
+ )
357
+ end
358
+
359
+ it 'gracefully falls back to default behavior' do
360
+ expect_no_offenses ( <<~RUBY , 'pv_p_class_spec.rb' )
361
+ describe PvPClass do; end
362
+ RUBY
363
+ end
364
+ end
284
365
end
0 commit comments