Skip to content

Commit

Permalink
test: make test runner compatible yarp fixtures
Browse files Browse the repository at this point in the history
  • Loading branch information
snutij committed Jun 28, 2023
1 parent 6cefa75 commit 2d78109
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 5 deletions.
2 changes: 1 addition & 1 deletion Rakefile
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ require "ruby_lsp/check_docs"
Rake::TestTask.new(:test) do |t|
t.libs << "test"
t.libs << "lib"
t.test_files = FileList["test/**/*_test.rb"]
t.test_files = FileList["test/**/*_test.rb"].exclude("test/fixtures/**/*.rb")
end

RDoc::Task.new do |rdoc|
Expand Down
22 changes: 18 additions & 4 deletions test/expectations/expectations_test_runner.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@

class ExpectationsTestRunner < Minitest::Test
TEST_EXP_DIR = "test/expectations"
TEST_FIXTURES_DIR = "test/fixtures"
TEST_FIXTURES_GLOB = File.join(TEST_FIXTURES_DIR, "**", "*.rb")
TEST_RUBY_LSP_FIXTURES_GLOB = File.join("test/fixtures", "*.rb")
TEST_RAILS_FIXTURES_GLOB = File.join("test/fixtures/rails_app", "**", "*.rb")
TEST_YARP_FIXTURES_GLOB = File.join("test/fixtures/yarp/test/fixtures", "**", "*.txt")

class << self
def expectations_tests(handler_class, expectation_suffix)
Expand Down Expand Up @@ -49,7 +50,7 @@ def default_args
include ExpectationsRunnerMethods
RB

Dir.glob(TEST_FIXTURES_GLOB).each do |path|
Dir.glob([TEST_RUBY_LSP_FIXTURES_GLOB]).each do |path|
test_name = File.basename(path, ".rb")

expectations_dir = File.join(TEST_EXP_DIR, expectation_suffix)
Expand Down Expand Up @@ -91,10 +92,23 @@ def test_#{expectation_suffix}_#{uniq_name_from_path(path)}__does_not_raise
RB
end
end

Dir.glob([TEST_RAILS_FIXTURES_GLOB, TEST_YARP_FIXTURES_GLOB]).each do |path|
class_eval(<<~RB, __FILE__, __LINE__ + 1)
def test_#{expectation_suffix}_#{uniq_name_from_path(path)}__does_not_raise
@_path = "#{path}"
source = File.read(@_path)
run_expectations(source)
end
RB
end
end

# Ensure that the test name include path context to avoid duplicate
# from test/fixtures/yarp/test/fixtures/unparser/corpus/semantic/and.txt
# to test_expectations__test_fixtures_yarp_test_fixtures_unparser_corpus_semantic_and
def uniq_name_from_path(path)
path.split("/").last(2).join("_").delete_suffix('.rb')
path.gsub(/#{File.extname(path)}$/, '').split("/").join("_")
end

def ruby_requirement_magic_comment_version(fixture_path)
Expand Down
2 changes: 2 additions & 0 deletions test/requests/hover_expectations_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
class HoverExpectationsTest < ExpectationsTestRunner
expectations_tests RubyLsp::Requests::Hover, "hover"

TEST_FIXTURES_DIR = "test/fixtures"

def assert_expectations(source, expected)
source = substitute(source)
actual = T.cast(run_expectations(source), T.nilable(LanguageServer::Protocol::Interface::Hover))
Expand Down

0 comments on commit 2d78109

Please sign in to comment.