Skip to content

Commit 688fd10

Browse files
Copilotkwerle
andcommitted
Add comprehensive tests for constant, method, and variable fallback
Co-authored-by: kwerle <23320+kwerle@users.noreply.github.com>
1 parent c923aeb commit 688fd10

1 file changed

Lines changed: 59 additions & 18 deletions

File tree

spec/lib/ruby_language_server/project_manager_spec.rb

Lines changed: 59 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -506,36 +506,77 @@ def my_method
506506
end
507507

508508
it 'finds methods defined elsewhere when not in local scope' do
509-
# Define a class with a method in one file
510-
external_file = <<~CODE_FILE
511-
class MyExternalClass
512-
def my_external_method
509+
# Define a helper module with utility methods
510+
helper_file = <<~CODE_FILE
511+
module Helpers
512+
def self.format_text(text)
513+
text.upcase
513514
end
514515
end
515516
CODE_FILE
516517

517-
# Reference the method from another scope
518-
reference_file = <<~CODE_FILE
519-
module DifferentModule
520-
class DifferentClass
521-
def some_method
522-
# Call method that's not in local scope
518+
# Use the method from a different module
519+
usage_file = <<~CODE_FILE
520+
module Application
521+
class TextService
522+
def process
523+
format_text
523524
end
524525
end
525526
end
526527
CODE_FILE
527528

528-
project_manager.update_document_content('external_uri', external_file)
529-
project_manager.tags_for_uri('external_uri')
529+
project_manager.update_document_content('helper_uri', helper_file)
530+
project_manager.tags_for_uri('helper_uri')
530531

531-
project_manager.update_document_content('reference_uri', reference_file)
532-
project_manager.tags_for_uri('reference_uri')
532+
project_manager.update_document_content('usage_uri', usage_file)
533+
project_manager.tags_for_uri('usage_uri')
533534

534-
# Note: Testing method fallback would require context parsing
535-
# For now, just verify constant fallback doesn't break method lookup
535+
# Position on "format_text" method call (line 3, character 8)
536536
position = OpenStruct.new(line: 3, character: 8)
537-
results = project_manager.possible_definitions('reference_uri', position)
538-
assert_instance_of Array, results
537+
results = project_manager.possible_definitions('usage_uri', position)
538+
539+
# Should find the method definition via global fallback
540+
# Note: This finds the class method Helpers.format_text
541+
assert_equal 1, results.length
542+
assert_equal 'helper_uri', results.first[:uri]
543+
assert_equal 1, results.first[:range][:start][:line]
544+
end
545+
546+
it 'finds constants/variables defined elsewhere when not in local scope' do
547+
# Define constants in a class
548+
constants_file = <<~CODE_FILE
549+
class AppConfig
550+
DATABASE_URL = 'postgres://localhost/db'
551+
MAX_RETRIES = 3
552+
end
553+
CODE_FILE
554+
555+
# Reference from another module
556+
usage_file = <<~CODE_FILE
557+
module Application
558+
class Config
559+
def load_config
560+
DATABASE_URL
561+
end
562+
end
563+
end
564+
CODE_FILE
565+
566+
project_manager.update_document_content('constants_uri', constants_file)
567+
project_manager.tags_for_uri('constants_uri')
568+
569+
project_manager.update_document_content('usage_uri', usage_file)
570+
project_manager.tags_for_uri('usage_uri')
571+
572+
# Position on "DATABASE_URL" (line 3, character 8)
573+
position = OpenStruct.new(line: 3, character: 8)
574+
results = project_manager.possible_definitions('usage_uri', position)
575+
576+
# Should find the constant via global fallback
577+
assert_equal 1, results.length
578+
assert_equal 'constants_uri', results.first[:uri]
579+
assert_equal 1, results.first[:range][:start][:line]
539580
end
540581

541582
it 'returns empty array when nothing exists anywhere' do

0 commit comments

Comments
 (0)