@@ -12,21 +12,20 @@ def test_add_comment
1212 comment_tl1 = RDoc ::Comment . new ( '# comment 1' , @top_level , :ruby )
1313 cm . add_comment comment_tl1 , tl1
1414
15- assert_equal [ [ comment_tl1 , tl1 ] ] , cm . comment_location
15+ assert_equal ( { tl1 => comment_tl1 } , cm . comment_location )
1616 assert_equal 'comment 1' , cm . comment . text
1717
1818 comment_tl2 = RDoc ::Comment . new ( '# comment 2' , @top_level , :ruby )
1919 cm . add_comment comment_tl2 , tl2
2020
21- assert_equal [ [ comment_tl1 , tl1 ] , [ comment_tl2 , tl2 ] ] , cm . comment_location
21+ assert_equal ( { tl1 => comment_tl1 , tl2 => comment_tl2 } , cm . comment_location )
2222 assert_equal "comment 1\n ---\n comment 2" , cm . comment
2323
2424 comment_tl3 = RDoc ::Comment . new ( '# * comment 3' , @top_level , :ruby )
2525 cm . add_comment comment_tl3 , tl3
2626
27- assert_equal [ [ comment_tl1 , tl1 ] ,
28- [ comment_tl2 , tl2 ] ,
29- [ comment_tl3 , tl3 ] ] , cm . comment_location
27+ assert_equal ( { tl1 => comment_tl1 , tl2 => comment_tl2 ,
28+ tl3 => comment_tl3 } , cm . comment_location )
3029 assert_equal "comment 1\n ---\n comment 2\n ---\n * comment 3" , cm . comment
3130 end
3231
@@ -47,8 +46,27 @@ def test_add_comment_duplicate
4746 cm . add_comment comment1 , tl1
4847 cm . add_comment comment2 , tl1
4948
50- assert_equal [ [ comment1 , tl1 ] ,
51- [ comment2 , tl1 ] ] , cm . comment_location
49+ # Hash replaces in-place for the same location
50+ assert_equal ( { tl1 => comment2 } , cm . comment_location )
51+ end
52+
53+ def test_add_comment_preserves_order_on_replace
54+ tl1 = @store . add_file 'one.rb'
55+ tl2 = @store . add_file 'two.rb'
56+
57+ cm = RDoc ::ClassModule . new 'Klass'
58+ cm . add_comment 'comment from one' , tl1
59+ cm . add_comment 'comment from two' , tl2
60+
61+ # Simulate keep_position clearing: set tl1's comment to empty
62+ cm . comment_location [ tl1 ] = RDoc ::Comment . new ( '' )
63+
64+ # Re-adding a comment for tl1 replaces in-place (hash preserves key order)
65+ cm . add_comment 'updated comment from one' , tl1
66+
67+ assert_equal 2 , cm . comment_location . size
68+ assert_equal [ tl1 , tl2 ] , cm . comment_location . keys
69+ assert_equal 'updated comment from one' , cm . comment_location [ tl1 ] . to_s
5270 end
5371
5472 def test_add_comment_stopdoc
@@ -156,7 +174,7 @@ def test_from_module_comment
156174
157175 klass = RDoc ::ClassModule . from_module RDoc ::NormalClass , klass
158176
159- assert_equal [ [ 'really a class' , tl ] ] , klass . comment_location
177+ assert_equal ( { tl => 'really a class' } , klass . comment_location )
160178 end
161179
162180 def test_marshal_dump
@@ -631,7 +649,7 @@ def test_search_snippet_after_marshal
631649 assert_match ( /class comment/ , snippet )
632650 end
633651
634- def test_comment_location_is_array_after_marshal
652+ def test_comment_location_is_hash_after_marshal
635653 @store . path = Dir . tmpdir
636654 tl = @store . add_file 'file.rb'
637655
@@ -642,10 +660,10 @@ def test_comment_location_is_array_after_marshal
642660 loaded = Marshal . load Marshal . dump cm
643661 loaded . store = @store
644662
645- assert_kind_of Array , loaded . comment_location
646- assert_equal 1 , loaded . comment_location . length
663+ assert_kind_of Hash , loaded . comment_location
664+ assert_equal 1 , loaded . comment_location . size
647665
648- comment , location = loaded . comment_location . first
666+ location , comment = loaded . comment_location . first
649667 assert_kind_of RDoc ::Markup ::Document , comment
650668 # After marshal, location is the filename string (from doc.file)
651669 assert_equal tl . relative_name , location
@@ -668,7 +686,7 @@ def test_merge
668686 assert c1 . current_section , 'original current_section'
669687 assert c2 . current_section , 'merged current_section'
670688
671- comment , location = c2 . comment_location . first
689+ location , comment = c2 . comment_location . first
672690 assert_kind_of RDoc ::Markup ::Document , comment
673691 assert_equal tl . relative_name , location
674692 end
@@ -793,7 +811,10 @@ def test_merge_comment
793811 inner2 = @RM ::Document . new @RM ::Paragraph . new 'klass 2'
794812 inner2 . file = 'two.rb'
795813
796- expected = @RM ::Document . new inner2 , inner1
814+ # With hash-based comment_location, one.rb comes first (its key was
815+ # inserted first), then two.rb. Document.merge preserves this order
816+ # since both self and other have the same files.
817+ expected = @RM ::Document . new inner1 , inner2
797818
798819 assert_equal expected , cm1 . comment . parse
799820 end
@@ -1217,17 +1238,15 @@ def test_parse_comment_location
12171238 cm . add_comment 'comment 1' , tl1
12181239 cm . add_comment 'comment 2' , tl2
12191240
1220- assert_kind_of Array , cm . comment_location
1221- assert_equal 2 , cm . comment_location . length
1222- assert_equal 'comment 1' , cm . comment_location [ 0 ] [ 0 ]
1223- assert_equal tl1 , cm . comment_location [ 0 ] [ 1 ]
1224- assert_equal 'comment 2' , cm . comment_location [ 1 ] [ 0 ]
1225- assert_equal tl2 , cm . comment_location [ 1 ] [ 1 ]
1241+ assert_kind_of Hash , cm . comment_location
1242+ assert_equal 2 , cm . comment_location . size
1243+ assert_equal 'comment 1' , cm . comment_location [ tl1 ]
1244+ assert_equal 'comment 2' , cm . comment_location [ tl2 ]
12261245
12271246 cm = Marshal . load Marshal . dump cm
12281247
1229- # After marshal, comment_location should still be an array
1230- assert_kind_of Array , cm . comment_location
1248+ # After marshal, comment_location should still be a hash
1249+ assert_kind_of Hash , cm . comment_location
12311250
12321251 # parse() produces a Document with parts for each comment
12331252 parsed = cm . parse ( cm . comment_location )
0 commit comments