Skip to content

Commit

Permalink
Drop unnecessary file_name parameter from Parser.for method. (#1135)
Browse files Browse the repository at this point in the history
* Unify top_level creation in tests

* Remove unnecessary file_name param from Parser.for

It should be always the same as the top_level's absolute_name, so there's
no point of taking it as a separate parameter.
  • Loading branch information
st0012 authored Jul 31, 2024
1 parent 011de3f commit 97c497d
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 40 deletions.
3 changes: 2 additions & 1 deletion lib/rdoc/parser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,8 @@ def self.check_modeline file_name
# Finds and instantiates the correct parser for the given +file_name+ and
# +content+.

def self.for top_level, file_name, content, options, stats
def self.for top_level, content, options, stats
file_name = top_level.absolute_name
return if binary? file_name

parser = use_markup content
Expand Down
2 changes: 1 addition & 1 deletion lib/rdoc/rdoc.rb
Original file line number Diff line number Diff line change
Expand Up @@ -356,7 +356,7 @@ def parse_file filename

top_level = @store.add_file filename, relative_name: relative_path.to_s

parser = RDoc::Parser.for top_level, filename, content, @options, @stats
parser = RDoc::Parser.for top_level, content, @options, @stats

return unless parser

Expand Down
73 changes: 35 additions & 38 deletions test/rdoc/test_rdoc_parser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,12 @@

require_relative 'helper'

class TestRDocParser < RDoc::TestCase

class RDocParserTest < RDoc::TestCase
def setup
super

@RP = RDoc::Parser
@binary_dat = File.expand_path '../binary.dat', __FILE__

@fn = 'file.rb'
@top_level = RDoc::TopLevel.new @fn
@binary_dat_fixture_path = File.expand_path '../binary.dat', __FILE__
@options = RDoc::Options.new
end

Expand Down Expand Up @@ -71,7 +67,7 @@ def test_class_can_parse

assert_equal @RP::Simple, @RP.can_parse(readme_file_name)

assert_equal @RP::Simple, @RP.can_parse(@binary_dat)
assert_equal @RP::Simple, @RP.can_parse(@binary_dat_fixture_path)

jtest_file_name = File.expand_path '../test.ja.txt', __FILE__
assert_equal @RP::Simple, @RP.can_parse(jtest_file_name)
Expand All @@ -90,16 +86,12 @@ def test_class_can_parse
end

def test_class_for_executable
temp_dir do
content = "#!/usr/bin/env ruby -w\n"
File.open 'app', 'w' do |io| io.write content end
app = @store.add_file 'app'

parser = @RP.for app, 'app', content, @options, :stats
with_top_level("app", "#!/usr/bin/env ruby -w\n") do |top_level, content|
parser = @RP.for top_level, content, @options, :stats

assert_kind_of RDoc::Parser::Ruby, parser

assert_equal 'app', parser.file_name
assert_equal top_level.absolute_name, parser.file_name
end
end

Expand All @@ -111,7 +103,7 @@ def test_class_for_forbidden
File.chmod 0000, io.path
forbidden = @store.add_file io.path

parser = @RP.for forbidden, 'forbidden', '', @options, :stats
parser = @RP.for forbidden, '', @options, :stats

assert_nil parser
ensure
Expand All @@ -123,13 +115,8 @@ def test_class_for_forbidden
end

def test_class_for_modeline
temp_dir do
content = "# -*- rdoc -*-\n= NEWS\n"

File.open 'NEWS', 'w' do |io| io.write content end
app = @store.add_file 'NEWS'

parser = @RP.for app, 'NEWS', content, @options, :stats
with_top_level("NEWS", "# -*- rdoc -*-\n= NEWS\n") do |top_level, content|
parser = @RP.for top_level, content, @options, :stats

assert_kind_of RDoc::Parser::Simple, parser

Expand Down Expand Up @@ -226,25 +213,18 @@ def test_check_modeline_no_modeline
end

def test_class_for_binary
rp = @RP.dup

class << rp
alias old_can_parse can_parse
dat_fixture = File.read(@binary_dat_fixture_path)
with_top_level("binary.dat", dat_fixture) do |top_level, content|
assert_nil @RP.for(top_level, content, @options, nil)
end

def rp.can_parse(*args) nil end

assert_nil @RP.for(nil, @binary_dat, nil, nil, nil)
end

def test_class_for_markup
content = <<-CONTENT
# coding: utf-8 markup: rd
CONTENT
with_top_level("file.rb", "# coding: utf-8 markup: rd") do |top_level, content|
parser = @RP.for top_level, content, @options, nil

parser = @RP.for @top_level, __FILE__, content, @options, nil

assert_kind_of @RP::RD, parser
assert_kind_of @RP::RD, parser
end
end

def test_class_use_markup
Expand Down Expand Up @@ -329,9 +309,26 @@ def test_class_use_markup_unknown
end

def test_initialize
@RP.new @top_level, @fn, '', @options, nil
with_top_level("file.rb", "") do |top_level, content|
@RP.new top_level, top_level.absolute_name, content, @options, nil

assert_equal @RP, top_level.parser
end
end

assert_equal @RP, @top_level.parser
private

def with_top_level(filename, content, &block)
absoluate_filename = File.join Dir.tmpdir, filename
File.open absoluate_filename, 'w' do |io|
io.write content
end

top_level = RDoc::TopLevel.new absoluate_filename

yield(top_level, content)
ensure
File.unlink absoluate_filename
end

end

0 comments on commit 97c497d

Please sign in to comment.