Skip to content

Commit 97c497d

Browse files
authored
Drop unnecessary file_name parameter from Parser.for method. (#1135)
* 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.
1 parent 011de3f commit 97c497d

File tree

3 files changed

+38
-40
lines changed

3 files changed

+38
-40
lines changed

lib/rdoc/parser.rb

+2-1
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,8 @@ def self.check_modeline file_name
166166
# Finds and instantiates the correct parser for the given +file_name+ and
167167
# +content+.
168168

169-
def self.for top_level, file_name, content, options, stats
169+
def self.for top_level, content, options, stats
170+
file_name = top_level.absolute_name
170171
return if binary? file_name
171172

172173
parser = use_markup content

lib/rdoc/rdoc.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -356,7 +356,7 @@ def parse_file filename
356356

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

359-
parser = RDoc::Parser.for top_level, filename, content, @options, @stats
359+
parser = RDoc::Parser.for top_level, content, @options, @stats
360360

361361
return unless parser
362362

test/rdoc/test_rdoc_parser.rb

+35-38
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,12 @@
33

44
require_relative 'helper'
55

6-
class TestRDocParser < RDoc::TestCase
7-
6+
class RDocParserTest < RDoc::TestCase
87
def setup
98
super
109

1110
@RP = RDoc::Parser
12-
@binary_dat = File.expand_path '../binary.dat', __FILE__
13-
14-
@fn = 'file.rb'
15-
@top_level = RDoc::TopLevel.new @fn
11+
@binary_dat_fixture_path = File.expand_path '../binary.dat', __FILE__
1612
@options = RDoc::Options.new
1713
end
1814

@@ -71,7 +67,7 @@ def test_class_can_parse
7167

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

74-
assert_equal @RP::Simple, @RP.can_parse(@binary_dat)
70+
assert_equal @RP::Simple, @RP.can_parse(@binary_dat_fixture_path)
7571

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

9288
def test_class_for_executable
93-
temp_dir do
94-
content = "#!/usr/bin/env ruby -w\n"
95-
File.open 'app', 'w' do |io| io.write content end
96-
app = @store.add_file 'app'
97-
98-
parser = @RP.for app, 'app', content, @options, :stats
89+
with_top_level("app", "#!/usr/bin/env ruby -w\n") do |top_level, content|
90+
parser = @RP.for top_level, content, @options, :stats
9991

10092
assert_kind_of RDoc::Parser::Ruby, parser
10193

102-
assert_equal 'app', parser.file_name
94+
assert_equal top_level.absolute_name, parser.file_name
10395
end
10496
end
10597

@@ -111,7 +103,7 @@ def test_class_for_forbidden
111103
File.chmod 0000, io.path
112104
forbidden = @store.add_file io.path
113105

114-
parser = @RP.for forbidden, 'forbidden', '', @options, :stats
106+
parser = @RP.for forbidden, '', @options, :stats
115107

116108
assert_nil parser
117109
ensure
@@ -123,13 +115,8 @@ def test_class_for_forbidden
123115
end
124116

125117
def test_class_for_modeline
126-
temp_dir do
127-
content = "# -*- rdoc -*-\n= NEWS\n"
128-
129-
File.open 'NEWS', 'w' do |io| io.write content end
130-
app = @store.add_file 'NEWS'
131-
132-
parser = @RP.for app, 'NEWS', content, @options, :stats
118+
with_top_level("NEWS", "# -*- rdoc -*-\n= NEWS\n") do |top_level, content|
119+
parser = @RP.for top_level, content, @options, :stats
133120

134121
assert_kind_of RDoc::Parser::Simple, parser
135122

@@ -226,25 +213,18 @@ def test_check_modeline_no_modeline
226213
end
227214

228215
def test_class_for_binary
229-
rp = @RP.dup
230-
231-
class << rp
232-
alias old_can_parse can_parse
216+
dat_fixture = File.read(@binary_dat_fixture_path)
217+
with_top_level("binary.dat", dat_fixture) do |top_level, content|
218+
assert_nil @RP.for(top_level, content, @options, nil)
233219
end
234-
235-
def rp.can_parse(*args) nil end
236-
237-
assert_nil @RP.for(nil, @binary_dat, nil, nil, nil)
238220
end
239221

240222
def test_class_for_markup
241-
content = <<-CONTENT
242-
# coding: utf-8 markup: rd
243-
CONTENT
223+
with_top_level("file.rb", "# coding: utf-8 markup: rd") do |top_level, content|
224+
parser = @RP.for top_level, content, @options, nil
244225

245-
parser = @RP.for @top_level, __FILE__, content, @options, nil
246-
247-
assert_kind_of @RP::RD, parser
226+
assert_kind_of @RP::RD, parser
227+
end
248228
end
249229

250230
def test_class_use_markup
@@ -329,9 +309,26 @@ def test_class_use_markup_unknown
329309
end
330310

331311
def test_initialize
332-
@RP.new @top_level, @fn, '', @options, nil
312+
with_top_level("file.rb", "") do |top_level, content|
313+
@RP.new top_level, top_level.absolute_name, content, @options, nil
314+
315+
assert_equal @RP, top_level.parser
316+
end
317+
end
333318

334-
assert_equal @RP, @top_level.parser
319+
private
320+
321+
def with_top_level(filename, content, &block)
322+
absoluate_filename = File.join Dir.tmpdir, filename
323+
File.open absoluate_filename, 'w' do |io|
324+
io.write content
325+
end
326+
327+
top_level = RDoc::TopLevel.new absoluate_filename
328+
329+
yield(top_level, content)
330+
ensure
331+
File.unlink absoluate_filename
335332
end
336333

337334
end

0 commit comments

Comments
 (0)