Skip to content

Commit 15e504e

Browse files
committed
Add blank line between class comments from different files in the some store
Create classes properly in RDoc::RI::Driver tests
1 parent 8c87409 commit 15e504e

File tree

5 files changed

+142
-50
lines changed

5 files changed

+142
-50
lines changed

lib/rdoc/context.rb

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -623,8 +623,10 @@ def add_to(array, thing)
623623

624624
##
625625
# Is there any content?
626-
# This means any of: comment, aliases, methods, attributes,
627-
# external aliases, require, constant.
626+
#
627+
# This means any of: comment, aliases, methods, attributes, external
628+
# aliases, require, constant.
629+
#
628630
# Includes are also checked unless <tt>includes == false</tt>.
629631

630632
def any_content(includes = true)

lib/rdoc/markup/document.rb

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -71,9 +71,7 @@ def accept visitor
7171
# Does this document have no parts?
7272

7373
def empty?
74-
@parts.empty? or
75-
(@parts.length == 1 and RDoc::Markup::Document === @parts.first and
76-
@parts.first.empty?)
74+
@parts.empty? or (@parts.length == 1 and merged? and @parts.first.empty?)
7775
end
7876

7977
##
@@ -85,6 +83,11 @@ def empty?
8583
# The information in +other+ is preferred over the receiver
8684

8785
def merge other
86+
if empty? then
87+
@parts = other.parts
88+
return self
89+
end
90+
8891
other.parts.each do |other_part|
8992
self.parts.delete_if do |self_part|
9093
self_part.file and self_part.file == other_part.file
@@ -96,6 +99,13 @@ def merge other
9699
self
97100
end
98101

102+
##
103+
# Does this Document contain other Documents?
104+
105+
def merged?
106+
RDoc::Markup::Document === @parts.first
107+
end
108+
99109
def pretty_print q # :nodoc:
100110
start = @file ? "[doc (#{@file}): " : '[doc: '
101111

lib/rdoc/ri/driver.rb

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -387,6 +387,8 @@ def add_class out, name, classes
387387
klass.superclass unless klass.module?
388388
end.compact.shift || 'Object'
389389

390+
superclass = superclass.full_name unless String === superclass
391+
390392
"#{name} < #{superclass}"
391393
end
392394

@@ -451,7 +453,7 @@ def add_includes out, includes
451453
# Adds a list of +methods+ to +out+ with a heading of +name+
452454

453455
def add_method_list out, methods, name
454-
return unless methods
456+
return if methods.empty?
455457

456458
out << RDoc::Markup::Heading.new(1, "#{name}:")
457459
out << RDoc::Markup::BlankLine.new
@@ -518,11 +520,13 @@ def class_document name, found, klasses, includes
518520

519521
found.each do |store, klass|
520522
comment = klass.comment
521-
class_methods = store.class_methods[klass.full_name]
522-
instance_methods = store.instance_methods[klass.full_name]
523-
attributes = store.attributes[klass.full_name]
523+
# TODO the store's cache should always return an empty Array
524+
class_methods = store.class_methods[klass.full_name] || []
525+
instance_methods = store.instance_methods[klass.full_name] || []
526+
attributes = store.attributes[klass.full_name] || []
524527

525-
if comment.empty? and !(instance_methods or class_methods) then
528+
if comment.empty? and
529+
instance_methods.empty? and class_methods.empty? then
526530
also_in << store
527531
next
528532
end
@@ -531,7 +535,17 @@ def class_document name, found, klasses, includes
531535

532536
unless comment.empty? then
533537
out << RDoc::Markup::Rule.new(1)
534-
out << comment
538+
539+
if comment.merged? then
540+
parts = comment.parts
541+
parts = parts.zip [RDoc::Markup::BlankLine.new] * parts.length
542+
parts.flatten!
543+
parts.pop
544+
545+
out.push(*parts)
546+
else
547+
out << comment
548+
end
535549
end
536550

537551
if class_methods or instance_methods or not klass.constants.empty? then
@@ -554,13 +568,12 @@ def class_document name, found, klasses, includes
554568
end)
555569

556570
out << list
571+
out << RDoc::Markup::BlankLine.new
557572
end
558573

559574
add_method_list out, class_methods, 'Class methods'
560575
add_method_list out, instance_methods, 'Instance methods'
561576
add_method_list out, attributes, 'Attributes'
562-
563-
out << RDoc::Markup::BlankLine.new
564577
end
565578

566579
add_also_in out, also_in

test/test_rdoc_markup_document.rb

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,24 @@ def test_merge_add
124124
assert_equal expected, result
125125
end
126126

127+
def test_merge_empty
128+
original = @RM::Document.new
129+
root = @RM::Document.new original
130+
131+
replace = @RM::Document.new @RM::Paragraph.new 'replace'
132+
replace.file = 'file.rb'
133+
134+
other = @RM::Document.new replace
135+
136+
result = root.merge other
137+
138+
inner = @RM::Document.new @RM::Paragraph.new 'replace'
139+
inner.file = 'file.rb'
140+
expected = @RM::Document.new inner
141+
142+
assert_equal expected, result
143+
end
144+
127145
def test_push
128146
@d.push @RM::BlankLine.new, @RM::BlankLine.new
129147

test/test_rdoc_ri_driver.rb

Lines changed: 86 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
require 'fileutils'
66
require 'stringio'
77
require 'rdoc/ri/driver'
8+
require 'rdoc/rdoc'
89

910
class TestRDocRIDriver < MiniTest::Unit::TestCase
1011

@@ -223,7 +224,7 @@ def test_add_method_list_interative
223224
def test_add_method_list_none
224225
out = @RM::Document.new
225226

226-
@driver.add_method_list out, nil, 'Class'
227+
@driver.add_method_list out, [], 'Class'
227228

228229
assert_equal @RM::Document.new, out
229230
end
@@ -249,6 +250,46 @@ def test_classes
249250
assert_equal expected, @driver.classes
250251
end
251252

253+
def test_class_document
254+
util_store
255+
256+
tl1 = RDoc::TopLevel.new 'one.rb'
257+
tl2 = RDoc::TopLevel.new 'two.rb'
258+
259+
@cFoo.add_comment 'one', tl1
260+
@cFoo.add_comment 'two', tl2
261+
@store.save_class @cFoo
262+
263+
found = [
264+
[@store, @store.load_class(@cFoo.full_name)]
265+
]
266+
267+
out = @driver.class_document @cFoo.full_name, found, [], []
268+
269+
expected = @RM::Document.new
270+
@driver.add_class expected, 'Foo', []
271+
@driver.add_includes expected, []
272+
@driver.add_from expected, @store
273+
expected << @RM::Rule.new(1)
274+
275+
doc = @RM::Document.new(@RM::Paragraph.new('one'))
276+
doc.file = 'one.rb'
277+
expected.push doc
278+
expected << @RM::BlankLine.new
279+
doc = @RM::Document.new(@RM::Paragraph.new('two'))
280+
doc.file = 'two.rb'
281+
expected.push doc
282+
283+
expected << @RM::Rule.new(1)
284+
expected << @RM::Heading.new(1, 'Instance methods:')
285+
expected << @RM::BlankLine.new
286+
expected << @RM::Verbatim.new('inherit')
287+
expected << @RM::Verbatim.new('override')
288+
expected << @RM::BlankLine.new
289+
290+
assert_equal expected, out
291+
end
292+
252293
def test_complete
253294
store = RDoc::RI::Store.new @home_ri
254295
store.cache[:ancestors] = {
@@ -633,8 +674,24 @@ def test_list_known_classes_name
633674
def test_list_methods_matching
634675
util_store
635676

636-
assert_equal %w[Foo::Bar#attr Foo::Bar#blah Foo::Bar#bother Foo::Bar::new],
637-
@driver.list_methods_matching('Foo::Bar.')
677+
assert_equal %w[
678+
Foo::Bar#attr
679+
Foo::Bar#blah
680+
Foo::Bar#bother
681+
Foo::Bar::new
682+
],
683+
@driver.list_methods_matching('Foo::Bar.').sort
684+
end
685+
686+
def test_list_methods_matching_inherit
687+
util_multi_store
688+
689+
assert_equal %w[
690+
Bar#baz
691+
Bar#inherit
692+
Bar#override
693+
],
694+
@driver.list_methods_matching('Bar.').sort
638695
end
639696

640697
def test_list_methods_matching_regexp
@@ -900,29 +957,28 @@ def util_ancestors_store
900957

901958
def util_multi_store
902959
util_store
960+
903961
@store1 = @store
904962

963+
@top_level = RDoc::TopLevel.new 'file.rb'
964+
905965
@home_ri2 = "#{@home_ri}2"
906966
@store2 = RDoc::RI::Store.new @home_ri2
907967

908968
# as if seen in a namespace like class Ambiguous::Other
909-
@mAmbiguous = RDoc::NormalModule.new 'Ambiguous'
969+
@mAmbiguous = @top_level.add_module RDoc::NormalModule, 'Ambiguous'
910970

911-
@cFoo = RDoc::NormalClass.new 'Foo'
971+
@cFoo = @top_level.add_class RDoc::NormalClass, 'Foo'
912972

913-
@cBar = RDoc::NormalClass.new 'Bar'
914-
@cBar.superclass = 'Foo'
915-
@cFoo_Baz = RDoc::NormalClass.new 'Baz'
916-
@cFoo_Baz.parent = @cFoo
973+
@cBar = @top_level.add_class RDoc::NormalClass, 'Bar', 'Foo'
974+
@cFoo_Baz = @cFoo.add_class RDoc::NormalClass, 'Baz'
917975

918-
@baz = RDoc::AnyMethod.new nil, 'baz'
976+
@baz = @cBar.add_method RDoc::AnyMethod.new(nil, 'baz')
919977
@baz.record_location @top_level
920-
@cBar.add_method @baz
921978

922-
@override = RDoc::AnyMethod.new nil, 'override'
979+
@override = @cBar.add_method RDoc::AnyMethod.new(nil, 'override')
923980
@override.comment = 'must be displayed'
924981
@override.record_location @top_level
925-
@cBar.add_method @override
926982

927983
@store2.save_class @mAmbiguous
928984
@store2.save_class @cBar
@@ -934,60 +990,51 @@ def util_multi_store
934990
@store2.save_cache
935991

936992
@driver.stores = [@store1, @store2]
993+
994+
RDoc::RDoc.reset
937995
end
938996

939997
def util_store
940998
@store = RDoc::RI::Store.new @home_ri
941999

9421000
@top_level = RDoc::TopLevel.new 'file.rb'
9431001

944-
@cFoo = RDoc::NormalClass.new 'Foo'
945-
@mInc = RDoc::NormalModule.new 'Inc'
946-
@cAmbiguous = RDoc::NormalClass.new 'Ambiguous'
1002+
@cFoo = @top_level.add_class RDoc::NormalClass, 'Foo'
1003+
@mInc = @top_level.add_module RDoc::NormalModule, 'Inc'
1004+
@cAmbiguous = @top_level.add_class RDoc::NormalClass, 'Ambiguous'
9471005

9481006
doc = @RM::Document.new @RM::Paragraph.new('Include thingy')
949-
950-
@cFooInc = RDoc::Include.new 'Inc', doc
1007+
@cFooInc = @cFoo.add_include RDoc::Include.new('Inc', doc)
9511008
@cFooInc.record_location @top_level
952-
@cFoo.add_include @cFooInc
9531009

954-
@cFoo_Bar = RDoc::NormalClass.new 'Bar'
955-
@cFoo_Bar.parent = @cFoo
1010+
@cFoo_Bar = @cFoo.add_class RDoc::NormalClass, 'Bar'
9561011

957-
@blah = RDoc::AnyMethod.new nil, 'blah'
1012+
@blah = @cFoo_Bar.add_method RDoc::AnyMethod.new(nil, 'blah')
9581013
@blah.call_seq = "blah(5) => 5\nblah(6) => 6\n"
9591014
@blah.record_location @top_level
9601015

961-
@bother = RDoc::AnyMethod.new nil, 'bother'
1016+
@bother = @cFoo_Bar.add_method RDoc::AnyMethod.new(nil, 'bother')
9621017
@bother.block_params = "stuff"
9631018
@bother.params = "(things)"
9641019
@bother.record_location @top_level
9651020

966-
@new = RDoc::AnyMethod.new nil, 'new'
1021+
@new = @cFoo_Bar.add_method RDoc::AnyMethod.new nil, 'new'
9671022
@new.record_location @top_level
9681023
@new.singleton = true
9691024

970-
@cFoo_Bar.add_method @blah
971-
@cFoo_Bar.add_method @bother
972-
@cFoo_Bar.add_method @new
973-
974-
@attr = RDoc::Attr.new nil, 'attr', 'RW', ''
1025+
@attr = @cFoo_Bar.add_attribute RDoc::Attr.new nil, 'attr', 'RW', ''
9751026
@attr.record_location @top_level
9761027

977-
@cFoo_Bar.add_attribute @attr
1028+
@cFoo_Baz = @cFoo.add_class RDoc::NormalClass, 'Baz'
1029+
@cFoo_Baz.record_location @top_level
9781030

979-
@cFoo_Baz = RDoc::NormalClass.new 'Baz'
980-
@cFoo_Baz.parent = @cFoo
981-
982-
@inherit = RDoc::AnyMethod.new nil, 'inherit'
1031+
@inherit = @cFoo.add_method RDoc::AnyMethod.new(nil, 'inherit')
9831032
@inherit.record_location @top_level
984-
@cFoo.add_method @inherit
9851033

9861034
# overriden by Bar in multi_store
987-
@overriden = RDoc::AnyMethod.new nil, 'override'
1035+
@overriden = @cFoo.add_method RDoc::AnyMethod.new(nil, 'override')
9881036
@overriden.comment = 'must not be displayed'
9891037
@overriden.record_location @top_level
990-
@cFoo.add_method @overriden
9911038

9921039
@store.save_class @cFoo
9931040
@store.save_class @cFoo_Bar
@@ -1006,6 +1053,8 @@ def util_store
10061053
@store.save_cache
10071054

10081055
@driver.stores = [@store]
1056+
1057+
RDoc::RDoc.reset
10091058
end
10101059

10111060
end

0 commit comments

Comments
 (0)