spec/ripper/extract_constants_spec.rb in grape-reload-0.0.3 vs spec/ripper/extract_constants_spec.rb in grape-reload-0.0.4

- old
+ new

@@ -58,14 +58,75 @@ end class WithoutModule def use_top_level TopLevel.new end + def self.method + SomeModule::ShouldntUse.call + end end CODE } + let!(:deeply_nested) { + <<CODE + module Test + module Subtest + class App2 < Grape::API + end + end + end +CODE + } + + # Sequel-related + let!(:class_reference_with_call) { + <<CODE + module Test + module Subtest + class App2 < Grape::API(:test) + end + end + end +CODE + } + + let!(:grape_desc_args) { + <<CODE + module Test + class App < Grape::API + group do + desc 'Blablabla', + entity: [Test::SomeAnotherEntity] + get :test do + SomeClass.usage + 'test2 response' + end + end + end + end +CODE + } + + let!(:class_level_call_with_args) { + <<CODE + module Test + class TestClass + UseModule::UseClass.call(arg) + end + end +CODE + } + + let!(:lambda_class_usage) { + <<CODE + some_method ->(arg) { + ModuleName::ClassName.call(arg) + } +CODE + } + it 'extract consts from code1 correctly' do consts = Ripper.extract_constants(code1) expect(consts[:declared].flatten).to include( '::TopClass', '::Test::Test1', @@ -79,15 +140,19 @@ expect(consts[:used].flatten).to include( '::Test3::AnotherClass', '::Test::NotExists::Test1', '::SomeExternalClass', - '::Superclass', - '::SomeClass1', - '::SomeClass2' + '::Superclass' ) + expect(consts[:used].flatten).not_to include( + '::SomeClass1', + '::SomeClass2' + ) + + end it 'extract consts from code2 correctly' do consts = Ripper.extract_constants(code2) expect(consts[:declared].flatten).to include( '::Test::App2', @@ -96,10 +161,39 @@ expect(consts[:used].flatten).to include( '::Test::Mount2', '::Test::Mount10', '::Test::SomeAnotherEntity', - '::SomeClass', - '::TopLevel' ) + + expect(consts[:used].flatten).not_to include( + '::SomeClass', + '::TopLevel' + ) + + end + + it 'extracts consts used in deeply nested modules up to root namespace' do + consts = Ripper.extract_constants(deeply_nested) + expect(consts[:used].flatten).to include('::Grape::API') + end + + it 'extracts const with call (sequel-related)' do + consts = Ripper.extract_constants(class_reference_with_call) + expect(consts[:used].flatten).to include('::Grape::API') + end + + it 'extracts consts from desc method args' do + consts = Ripper.extract_constants(grape_desc_args) + expect(consts[:used].flatten).to include('::Test::SomeAnotherEntity') + end + + it 'does not mess up class name when class level method called with argument' do + consts = Ripper.extract_constants(class_level_call_with_args) + expect(consts[:used].flatten).to include('::UseModule::UseClass') + end + + it 'does not include classes used in lambdas' do + consts = Ripper.extract_constants(lambda_class_usage) + expect(consts[:used].flatten).not_to include('::ModuleName::ClassName') end end \ No newline at end of file