Skip to content

Commit

Permalink
The ClassModule includes list now merges based on source file. Issue r…
Browse files Browse the repository at this point in the history
  • Loading branch information
drbrain committed Jun 14, 2011
1 parent 7e1e189 commit b33e086
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 34 deletions.
42 changes: 20 additions & 22 deletions lib/rdoc/class_module.rb
Expand Up @@ -243,7 +243,7 @@ def marshal_dump # :nodoc:
[const.name, parse(const.comment), const.file.absolute_name]
end,
includes.map do |incl|
[incl.name, parse(incl.comment)]
[incl.name, parse(incl.comment), incl.file.absolute_name]
end,
method_types,
]
Expand Down Expand Up @@ -280,8 +280,9 @@ def marshal_load array # :nodoc:
const.record_location RDoc::TopLevel.new file
end

array[7].each do |name, comment|
add_include RDoc::Include.new(name, comment)
array[7].each do |name, comment, file|
incl = add_include RDoc::Include.new(name, comment)
incl.record_location RDoc::TopLevel.new file
end

array[8].each do |type, visibilities|
Expand Down Expand Up @@ -320,29 +321,15 @@ def merge class_module
@comment = document
end

other_attrs = class_module.attributes.group_by { |attr| attr.file }
my_attrs = attributes. group_by { |attr| attr.file }

other_attrs.each do |file, attrs|
next if my_attrs.include? file

attrs.each do |attr|
add_attribute attr
end
merge_collections attributes, class_module.attributes do |const|
add_attribute const
end

other_consts = class_module.constants.group_by { |const| const.file }
my_consts = constants. group_by { |const| const.file }

other_consts.each do |file, consts|
next if my_consts.include? file

consts.each do |const|
add_constant const
end
merge_collections constants, class_module.constants do |const|
add_constant const
end

class_module.each_include do |incl|
merge_collections includes, class_module.includes do |incl|
add_include incl
end

Expand All @@ -351,6 +338,17 @@ def merge class_module
end
end

def merge_collections mine, other, &block # :nodoc:
my_things = mine. group_by { |thing| thing.file }
other_things = other.group_by { |thing| thing.file }

other_things.each do |file, things|
next if my_things.include? file

things.each(&block)
end
end

##
# Does this object represent a module?

Expand Down
7 changes: 5 additions & 2 deletions lib/rdoc/context.rb
Expand Up @@ -492,8 +492,11 @@ def add_constant constant
##
# Adds included module +include+ which should be an RDoc::Include

def add_include(include)
add_to @includes, include unless @includes.map { |i| i.full_name }.include?( include.full_name )
def add_include include
add_to @includes, include unless
@includes.map { |i| i.full_name }.include? include.full_name

include
end

##
Expand Down
47 changes: 37 additions & 10 deletions test/test_rdoc_class_module.rb
Expand Up @@ -94,6 +94,7 @@ def test_marshal_dump
c1.record_location tl

i1 = RDoc::Include.new 'I1', ''
i1.record_location tl

cm.add_attribute a1
cm.add_attribute a2
Expand Down Expand Up @@ -124,6 +125,8 @@ def test_marshal_dump
assert_equal tl, loaded.attributes.first.file

assert_equal tl, loaded.constants.first.file

assert_equal tl, loaded.includes.first.file
end

def test_marshal_load_version_0
Expand Down Expand Up @@ -177,7 +180,6 @@ def test_merge
cm1 = RDoc::ClassModule.new 'Klass'
cm1.add_comment 'klass 1', tl

cm1.add_include RDoc::Include.new('I1', '')
cm1.add_method RDoc::AnyMethod.new(nil, 'm1')

cm2 = RDoc::ClassModule.new 'Klass'
Expand All @@ -186,7 +188,6 @@ def test_merge
@RM::Document.new(
@RM::Paragraph.new('klass 2')))

cm2.add_include RDoc::Include.new('I2', '')
cm2.add_method RDoc::AnyMethod.new(nil, 'm2')

cm1.merge cm2
Expand All @@ -200,14 +201,6 @@ def test_merge

assert_equal expected, cm1.comment

expected = [
RDoc::Include.new('I1', ''),
RDoc::Include.new('I2', ''),
]

expected.each do |i| i.parent = cm1 end
assert_equal expected, cm1.includes.sort

expected = [
RDoc::AnyMethod.new(nil, 'm1'),
RDoc::AnyMethod.new(nil, 'm2'),
Expand Down Expand Up @@ -288,6 +281,40 @@ def test_merge_constants
assert_equal expected, cm1.constants.sort
end

def test_merge_includes
tl1 = RDoc::TopLevel.new 'one.rb'
tl2 = RDoc::TopLevel.new 'two.rb'

cm1 = RDoc::ClassModule.new 'Klass'

incl = cm1.add_include RDoc::Include.new('I1', 'one')
incl.record_location tl1
incl = cm1.add_include RDoc::Include.new('I3', 'one')
incl.record_location tl1

cm2 = RDoc::ClassModule.new 'Klass'
cm2.instance_variable_set :@comment, @RM::Document.new

incl = cm2.add_include RDoc::Include.new('I2', 'two')
incl.record_location tl2
incl = cm2.add_include RDoc::Include.new('I3', 'two')
incl.record_location tl1
incl = cm2.add_include RDoc::Include.new('I4', 'two')
incl.record_location tl1

cm1.merge cm2

expected = [
RDoc::Include.new('I1', 'one'),
RDoc::Include.new('I2', 'two'),
RDoc::Include.new('I3', 'one'),
]

expected.each do |a| a.parent = cm1 end

assert_equal expected, cm1.includes.sort
end

# # TODO use merge with a version 0 ClassModule
# def test_merge_version_0
# tl = RDoc::TopLevel.new 'file.rb'
Expand Down
1 change: 1 addition & 0 deletions test/test_rdoc_ri_driver.rb
Expand Up @@ -912,6 +912,7 @@ def util_store
doc = @RM::Document.new @RM::Paragraph.new('Include thingy')

@cFooInc = RDoc::Include.new 'Inc', doc
@cFooInc.record_location @top_level
@cFoo.add_include @cFooInc

@cFoo_Bar = RDoc::NormalClass.new 'Bar'
Expand Down
2 changes: 2 additions & 0 deletions test/test_rdoc_ri_store.rb
Expand Up @@ -40,7 +40,9 @@ def setup
@nest_klass = @klass.add_class RDoc::NormalClass, 'SubClass'
@nest_meth = RDoc::AnyMethod.new nil, 'method'
@nest_meth.record_location @top_level

@nest_incl = RDoc::Include.new 'Incl', ''
@nest_incl.record_location @top_level

@nest_klass.add_method @nest_meth
@nest_klass.add_include @nest_incl
Expand Down

0 comments on commit b33e086

Please sign in to comment.