1# frozen_string_literal: true 2require File.expand_path '../xref_test_case', __FILE__ 3 4class TestRDocClassModule < XrefTestCase 5 6 def test_add_comment 7 tl1 = @store.add_file 'one.rb' 8 tl2 = @store.add_file 'two.rb' 9 tl3 = @store.add_file 'three.rb' 10 11 cm = RDoc::ClassModule.new 'Klass' 12 cm.add_comment '# comment 1', tl1 13 14 assert_equal [['comment 1', tl1]], cm.comment_location 15 assert_equal 'comment 1', cm.comment 16 17 cm.add_comment '# comment 2', tl2 18 19 assert_equal [['comment 1', tl1], ['comment 2', tl2]], cm.comment_location 20 assert_equal "comment 1\n---\ncomment 2", cm.comment 21 22 cm.add_comment "# * comment 3", tl3 23 24 assert_equal [['comment 1', tl1], 25 ['comment 2', tl2], 26 ['* comment 3', tl3]], cm.comment_location 27 assert_equal "comment 1\n---\ncomment 2\n---\n* comment 3", cm.comment 28 end 29 30 def test_add_comment_comment 31 cm = RDoc::ClassModule.new 'Klass' 32 33 cm.add_comment comment('comment'), @top_level 34 35 assert_equal 'comment', cm.comment.text 36 end 37 38 def test_add_comment_duplicate 39 tl1 = @store.add_file 'one.rb' 40 41 cm = RDoc::ClassModule.new 'Klass' 42 cm.add_comment '# comment 1', tl1 43 cm.add_comment '# comment 2', tl1 44 45 assert_equal [['comment 1', tl1], 46 ['comment 2', tl1]], cm.comment_location 47 end 48 49 def test_add_comment_stopdoc 50 tl = @store.add_file 'file.rb' 51 52 cm = RDoc::ClassModule.new 'Klass' 53 cm.stop_doc 54 55 cm.add_comment '# comment 1', tl 56 57 assert_empty cm.comment 58 end 59 60 def test_ancestors 61 assert_equal [@parent, "Object"], @child.ancestors 62 end 63 64 def test_comment_equals 65 cm = RDoc::ClassModule.new 'Klass' 66 cm.comment = '# comment 1' 67 68 assert_equal 'comment 1', cm.comment 69 70 cm.comment = '# comment 2' 71 72 assert_equal "comment 1\n---\ncomment 2", cm.comment 73 74 cm.comment = "# * comment 3" 75 76 assert_equal "comment 1\n---\ncomment 2\n---\n* comment 3", cm.comment 77 end 78 79 def test_comment_equals_comment 80 cm = RDoc::ClassModule.new 'Klass' 81 82 cm.comment = comment 'comment' 83 84 assert_equal 'comment', cm.comment.text 85 end 86 87 def test_docuent_self_or_methods 88 assert @c1.document_self_or_methods 89 90 @c1.document_self = false 91 92 assert @c1.document_self_or_methods 93 94 @c1_plus.document_self = false 95 @c1_m.document_self = false 96 97 assert @c1.document_self_or_methods 98 99 @c1__m.document_self = false 100 101 refute @c1.document_self_or_methods 102 end 103 104 def test_documented_eh 105 cm = RDoc::ClassModule.new 'C' 106 107 refute cm.documented?, 'no comments, no markers' 108 109 cm.add_comment '', @top_level 110 111 refute cm.documented?, 'empty comment' 112 113 cm.add_comment 'hi', @top_level 114 115 assert cm.documented?, 'commented' 116 117 cm.comment_location.clear 118 119 refute cm.documented?, 'no comment' 120 121 cm.document_self = nil # notify :nodoc: 122 123 assert cm.documented?, ':nodoc:' 124 end 125 126 def test_each_ancestor 127 assert_equal [@parent], @child.each_ancestor.to_a 128 end 129 130 def test_each_ancestor_cycle 131 m_incl = RDoc::Include.new 'M', nil 132 133 m = @top_level.add_module RDoc::NormalModule, 'M' 134 m.add_include m_incl 135 136 assert_empty m.each_ancestor.to_a 137 end 138 139 # handle making a short module alias of yourself 140 141 def test_find_class_named 142 @c2.classes_hash['C2'] = @c2 143 144 assert_nil @c2.find_class_named('C1') 145 end 146 147 def test_from_module_comment 148 tl = @store.add_file 'file.rb' 149 klass = tl.add_class RDoc::NormalModule, 'Klass' 150 klass.add_comment 'really a class', tl 151 152 klass = RDoc::ClassModule.from_module RDoc::NormalClass, klass 153 154 assert_equal [['really a class', tl]], klass.comment_location 155 end 156 157 def test_marshal_dump 158 @store.path = Dir.tmpdir 159 tl = @store.add_file 'file.rb' 160 161 ns = tl.add_module RDoc::NormalModule, 'Namespace' 162 163 cm = ns.add_class RDoc::NormalClass, 'Klass', 'Super' 164 cm.document_self = true 165 cm.record_location tl 166 167 a1 = RDoc::Attr.new nil, 'a1', 'RW', '' 168 a1.record_location tl 169 a2 = RDoc::Attr.new nil, 'a2', 'RW', '', true 170 a2.record_location tl 171 172 m1 = RDoc::AnyMethod.new nil, 'm1' 173 m1.record_location tl 174 175 c1 = RDoc::Constant.new 'C1', nil, '' 176 c1.record_location tl 177 178 i1 = RDoc::Include.new 'I1', '' 179 i1.record_location tl 180 181 e1 = RDoc::Extend.new 'E1', '' 182 e1.record_location tl 183 184 section_comment = RDoc::Comment.new('section comment') 185 section_comment.location = tl 186 187 assert_equal 1, cm.sections.length, 'sanity, default section only' 188 s0 = cm.sections.first 189 s1 = cm.add_section 'section', section_comment 190 191 cm.add_attribute a1 192 cm.add_attribute a2 193 cm.add_method m1 194 cm.add_constant c1 195 cm.add_include i1 196 cm.add_extend e1 197 cm.add_comment 'this is a comment', tl 198 199 loaded = Marshal.load Marshal.dump cm 200 loaded.store = @store 201 202 assert_equal cm, loaded 203 204 inner = RDoc::Markup::Document.new( 205 RDoc::Markup::Paragraph.new('this is a comment')) 206 inner.file = tl 207 208 comment = RDoc::Markup::Document.new inner 209 210 assert_equal [a2, a1], loaded.attributes.sort 211 assert_equal comment, loaded.comment 212 assert_equal [c1], loaded.constants 213 assert_equal 'Namespace::Klass', loaded.full_name 214 assert_equal [i1], loaded.includes 215 assert_equal [e1], loaded.extends 216 assert_equal [m1], loaded.method_list 217 assert_equal 'Klass', loaded.name 218 assert_equal 'Super', loaded.superclass 219 assert_equal [tl], loaded.in_files 220 assert_equal 'Namespace', loaded.parent.name 221 222 expected = { nil => s0, 'section' => s1 } 223 assert_equal expected, loaded.sections_hash 224 225 assert_equal tl, loaded.attributes.first.file 226 227 assert_equal tl, loaded.constants.first.file 228 229 assert_equal tl, loaded.includes.first.file 230 231 assert_equal tl, loaded.extends.first.file 232 233 assert_equal tl, loaded.method_list.first.file 234 end 235 236 def test_marshal_dump_visibilty 237 @store.path = Dir.tmpdir 238 tl = @store.add_file 'file.rb' 239 240 ns = tl.add_module RDoc::NormalModule, 'Namespace' 241 242 cm = ns.add_class RDoc::NormalClass, 'Klass', 'Super' 243 cm.record_location tl 244 245 a1 = RDoc::Attr.new nil, 'a1', 'RW', '' 246 a1.record_location tl 247 a1.document_self = false 248 249 m1 = RDoc::AnyMethod.new nil, 'm1' 250 m1.record_location tl 251 m1.document_self = false 252 253 c1 = RDoc::Constant.new 'C1', nil, '' 254 c1.record_location tl 255 c1.document_self = false 256 257 i1 = RDoc::Include.new 'I1', '' 258 i1.record_location tl 259 i1.document_self = false 260 261 e1 = RDoc::Extend.new 'E1', '' 262 e1.record_location tl 263 e1.document_self = false 264 265 section_comment = RDoc::Comment.new('section comment') 266 section_comment.location = tl 267 268 assert_equal 1, cm.sections.length, 'sanity, default section only' 269 270 cm.add_attribute a1 271 cm.add_method m1 272 cm.add_constant c1 273 cm.add_include i1 274 cm.add_extend e1 275 cm.add_comment 'this is a comment', tl 276 277 loaded = Marshal.load Marshal.dump cm 278 loaded.store = @store 279 280 assert_equal cm, loaded 281 282 assert_empty loaded.attributes 283 assert_empty loaded.constants 284 assert_empty loaded.includes 285 assert_empty loaded.extends 286 assert_empty loaded.method_list 287 end 288 289 def test_marshal_load_version_0 290 tl = @store.add_file 'file.rb' 291 ns = tl.add_module RDoc::NormalModule, 'Namespace' 292 cm = ns.add_class RDoc::NormalClass, 'Klass', 'Super' 293 294 a = RDoc::Attr.new(nil, 'a1', 'RW', '') 295 m = RDoc::AnyMethod.new(nil, 'm1') 296 c = RDoc::Constant.new('C1', nil, '') 297 i = RDoc::Include.new('I1', '') 298 299 s0 = cm.sections.first 300 301 cm.add_attribute a 302 cm.add_method m 303 cm.add_constant c 304 cm.add_include i 305 cm.add_comment 'this is a comment', tl 306 307 loaded = Marshal.load "\x04\bU:\x16RDoc::NormalClass[\x0Ei\x00\"\nKlass" + 308 "\"\x15Namespace::KlassI\"\nSuper\x06:\x06EF" + 309 "o:\eRDoc::Markup::Document\x06:\v@parts[\x06" + 310 "o:\x1CRDoc::Markup::Paragraph\x06;\b[\x06I" + 311 "\"\x16this is a comment\x06;\x06F[\x06[\aI" + 312 "\"\aa1\x06;\x06FI\"\aRW\x06;\x06F[\x06[\aI" + 313 "\"\aC1\x06;\x06Fo;\a\x06;\b[\x00[\x06[\aI" + 314 "\"\aI1\x06;\x06Fo;\a\x06;\b[\x00[\a[\aI" + 315 "\"\nclass\x06;\x06F[\b[\a:\vpublic[\x00[\a" + 316 ":\x0Eprotected[\x00[\a:\fprivate[\x00[\aI" + 317 "\"\rinstance\x06;\x06F[\b[\a;\n[\x06I" + 318 "\"\am1\x06;\x06F[\a;\v[\x00[\a;\f[\x00" 319 320 loaded.store = @store 321 322 assert_equal cm, loaded 323 324 comment = RDoc::Markup::Document.new( 325 RDoc::Markup::Paragraph.new('this is a comment')) 326 327 assert_equal [a], loaded.attributes 328 assert_equal comment, loaded.comment 329 assert_equal [c], loaded.constants 330 assert_equal 'Namespace::Klass', loaded.full_name 331 assert_equal [i], loaded.includes 332 assert_equal [m], loaded.method_list 333 assert_equal 'Klass', loaded.name 334 assert_equal 'Super', loaded.superclass 335 assert_nil loaded.file 336 assert_empty loaded.in_files 337 assert_nil loaded.parent 338 assert loaded.current_section 339 340 expected = { nil => s0 } 341 assert_equal expected, loaded.sections_hash 342 343 assert loaded.display? 344 end 345 346 def test_marshal_load_version_1 347 tl = @store.add_file 'file.rb' 348 349 ns = tl.add_module RDoc::NormalModule, 'Namespace' 350 351 cm = ns.add_class RDoc::NormalClass, 'Klass', 'Super' 352 cm.record_location tl 353 354 a1 = RDoc::Attr.new nil, 'a1', 'RW', '' 355 a1.record_location tl 356 a2 = RDoc::Attr.new nil, 'a2', 'RW', '', true 357 a2.record_location tl 358 359 m1 = RDoc::AnyMethod.new nil, 'm1' 360 m1.record_location tl 361 362 c1 = RDoc::Constant.new 'C1', nil, '' 363 c1.record_location tl 364 365 i1 = RDoc::Include.new 'I1', '' 366 i1.record_location tl 367 368 s0 = cm.sections.first 369 370 cm.add_attribute a1 371 cm.add_attribute a2 372 cm.add_method m1 373 cm.add_constant c1 374 cm.add_include i1 375 cm.add_comment 'this is a comment', tl 376 377 loaded = Marshal.load "\x04\bU:\x16RDoc::NormalClass[\x0Ei\x06I\"\nKlass" + 378 "\x06:\x06EFI\"\x15Namespace::Klass\x06;\x06FI" + 379 "\"\nSuper\x06;\x06Fo:\eRDoc::Markup::Document\a" + 380 ":\v@parts[\x06o;\a\a;\b[\x06o" + 381 ":\x1CRDoc::Markup::Paragraph\x06;\b" + 382 "[\x06I\"\x16this is a comment\x06;\x06F" + 383 ":\n@fileI\"\ffile.rb\x06;\x06F;\n0[\a[\nI" + 384 "\"\aa2\x06;\x06FI\"\aRW\x06;\x06F:\vpublicT@\x11" + 385 "[\nI\"\aa1\x06;\x06FI\"\aRW\x06;\x06F;\vF@\x11" + 386 "[\x06[\bI\"\aC1\x06;\x06Fo;\a\a;\b[\x00;\n0@\x11" + 387 "[\x06[\bI\"\aI1\x06;\x06Fo;\a\a;\b[\x00;\n0@\x11" + 388 "[\a[\aI\"\nclass\x06;\x06F[\b[\a;\v[\x00" + 389 "[\a:\x0Eprotected[\x00[\a:\fprivate[\x00[\aI" + 390 "\"\rinstance\x06;\x06F[\b[\a;\v[\x06[\aI" + 391 "\"\am1\x06;\x06F@\x11[\a;\f[\x00[\a;\r[\x00" 392 393 loaded.store = @store 394 395 assert_equal cm, loaded 396 397 inner = RDoc::Markup::Document.new( 398 RDoc::Markup::Paragraph.new('this is a comment')) 399 inner.file = tl 400 401 comment = RDoc::Markup::Document.new inner 402 403 assert_equal [a2, a1], loaded.attributes.sort 404 assert_equal comment, loaded.comment 405 assert_equal [c1], loaded.constants 406 assert_equal 'Namespace::Klass', loaded.full_name 407 assert_equal [i1], loaded.includes 408 assert_empty loaded.extends 409 assert_equal [m1], loaded.method_list 410 assert_equal 'Klass', loaded.name 411 assert_equal 'Super', loaded.superclass 412 assert_empty loaded.in_files 413 assert_nil loaded.parent 414 assert loaded.current_section 415 416 assert_equal tl, loaded.attributes.first.file 417 assert_equal tl, loaded.constants.first.file 418 assert_equal tl, loaded.includes.first.file 419 assert_equal tl, loaded.method_list.first.file 420 421 expected = { nil => s0 } 422 assert_equal expected, loaded.sections_hash 423 end 424 425 def test_marshal_load_version_2 426 tl = @store.add_file 'file.rb' 427 428 ns = tl.add_module RDoc::NormalModule, 'Namespace' 429 430 cm = ns.add_class RDoc::NormalClass, 'Klass', 'Super' 431 cm.record_location tl 432 433 a1 = RDoc::Attr.new nil, 'a1', 'RW', '' 434 a1.record_location tl 435 a2 = RDoc::Attr.new nil, 'a2', 'RW', '', true 436 a2.record_location tl 437 438 m1 = RDoc::AnyMethod.new nil, 'm1' 439 m1.record_location tl 440 441 c1 = RDoc::Constant.new 'C1', nil, '' 442 c1.record_location tl 443 444 i1 = RDoc::Include.new 'I1', '' 445 i1.record_location tl 446 447 e1 = RDoc::Extend.new 'E1', '' 448 e1.record_location tl 449 450 s0 = cm.sections.first 451 452 cm.add_attribute a1 453 cm.add_attribute a2 454 cm.add_method m1 455 cm.add_constant c1 456 cm.add_include i1 457 cm.add_extend e1 458 cm.add_comment 'this is a comment', tl 459 460 loaded = Marshal.load "\x04\bU:\x16RDoc::NormalClass[\x0Fi\aI\"\nKlass" + 461 "\x06:\x06EFI\"\x15Namespace::Klass\x06;\x06FI" + 462 "\"\nSuper\x06;\x06Fo:\eRDoc::Markup::Document\a" + 463 ":\v@parts[\x06o;\a\a;\b[\x06o" + 464 ":\x1CRDoc::Markup::Paragraph\x06;\b" + 465 "[\x06I\"\x16this is a comment\x06;\x06F" + 466 ":\n@fileI\"\ffile.rb\x06;\x06F;\n0[\a[\nI" + 467 "\"\aa2\x06;\x06FI\"\aRW\x06;\x06F:\vpublicT@\x11" + 468 "[\nI\"\aa1\x06;\x06FI\"\aRW\x06;\x06F;\vF@\x11" + 469 "[\x06[\bI\"\aC1\x06;\x06Fo;\a\a;\b[\x00;\n0@\x11" + 470 "[\x06[\bI\"\aI1\x06;\x06Fo;\a\a;\b[\x00;\n0@\x11" + 471 "[\a[\aI\"\nclass\x06;\x06F[\b[\a;\v[\x00" + 472 "[\a:\x0Eprotected[\x00[\a:\fprivate[\x00[\aI" + 473 "\"\rinstance\x06;\x06F[\b[\a;\v[\x06[\aI" + 474 "\"\am1\x06;\x06F@\x11[\a;\f[\x00[\a;\r[\x00" + 475 "[\x06[\bI\"\aE1\x06;\x06Fo;\a\a;\b[\x00;\n0@\x11" 476 477 loaded.store = @store 478 479 assert_equal cm, loaded 480 481 inner = RDoc::Markup::Document.new( 482 RDoc::Markup::Paragraph.new('this is a comment')) 483 inner.file = tl 484 485 comment = RDoc::Markup::Document.new inner 486 487 assert_equal [a2, a1], loaded.attributes.sort 488 assert_equal comment, loaded.comment 489 assert_equal [c1], loaded.constants 490 assert_equal 'Namespace::Klass', loaded.full_name 491 assert_equal [i1], loaded.includes 492 assert_equal [e1], loaded.extends 493 assert_equal [m1], loaded.method_list 494 assert_equal 'Klass', loaded.name 495 assert_equal 'Super', loaded.superclass 496 assert_empty loaded.in_files 497 assert_nil loaded.parent 498 assert loaded.current_section 499 500 assert_equal tl, loaded.attributes. first.file 501 assert_equal tl, loaded.constants. first.file 502 assert_equal tl, loaded.includes. first.file 503 assert_equal tl, loaded.extends. first.file 504 assert_equal tl, loaded.method_list.first.file 505 506 expected = { nil => s0 } 507 assert_equal expected, loaded.sections_hash 508 end 509 510 def test_marshal_load_version_3 511 tl = @store.add_file 'file.rb' 512 513 ns = tl.add_module RDoc::NormalModule, 'Namespace' 514 515 cm = ns.add_class RDoc::NormalClass, 'Klass', 'Super' 516 cm.record_location tl 517 518 a1 = RDoc::Attr.new nil, 'a1', 'RW', '' 519 a1.record_location tl 520 a2 = RDoc::Attr.new nil, 'a2', 'RW', '', true 521 a2.record_location tl 522 523 m1 = RDoc::AnyMethod.new nil, 'm1' 524 m1.record_location tl 525 526 c1 = RDoc::Constant.new 'C1', nil, '' 527 c1.record_location tl 528 529 i1 = RDoc::Include.new 'I1', '' 530 i1.record_location tl 531 532 e1 = RDoc::Extend.new 'E1', '' 533 e1.record_location tl 534 535 section_comment = RDoc::Comment.new('section comment') 536 section_comment.location = tl 537 538 assert_equal 1, cm.sections.length, 'sanity, default section only' 539 s0 = cm.sections.first 540 s1 = cm.add_section 'section', section_comment 541 542 cm.add_attribute a1 543 cm.add_attribute a2 544 cm.add_method m1 545 cm.add_constant c1 546 cm.add_include i1 547 cm.add_extend e1 548 cm.add_comment 'this is a comment', tl 549 550 loaded = Marshal.load "\x04\bU:\x16RDoc::NormalClass[\x13i\bI\"\nKlass" + 551 "\x06:\x06ETI\"\x15Namespace::Klass\x06;\x06TI" + 552 "\"\nSuper\x06;\x06To:\eRDoc::Markup::Document\a" + 553 ":\v@parts[\x06o;\a\a;\b[\x06o" + 554 ":\x1CRDoc::Markup::Paragraph\x06;\b[\x06I" + 555 "\"\x16this is a comment\x06;\x06T:\n@fileI" + 556 "\"\ffile.rb\x06;\x06T;\n0[\a[\nI\"\aa2\x06;" + 557 "\x06TI\"\aRW\x06;\x06T:\vpublicT@\x11[\nI" + 558 "\"\aa1\x06;\x06TI\"\aRW\x06;\x06T;\vF@\x11" + 559 "[\x06U:\x13RDoc::Constant[\x0Fi\x00I\"\aC1\x06" + 560 ";\x06TI\"\x19Namespace::Klass::C1\x06;\x06T00o" + 561 ";\a\a;\b[\x00;\n0@\x11@\ac\x16RDoc::NormalClass0" + 562 "[\x06[\bI\"\aI1\x06;\x06To;\a\a;\b[\x00;\n0@\x11" + 563 "[\a[\aI\"\nclass\x06;\x06T[\b[\a;\v[\x00[\a" + 564 ":\x0Eprotected[\x00[\a:\fprivate[\x00[\aI" + 565 "\"\rinstance\x06;\x06T[\b[\a;\v[\x06[\aI" + 566 "\"\am1\x06;\x06T@\x11[\a;\r[\x00[\a;\x0E[\x00" + 567 "[\x06[\bI\"\aE1\x06;\x06To;\a\a;\b[\x00;\n0@\x11" + 568 "[\aU:\eRDoc::Context::Section[\bi\x000o;\a\a;\b" + 569 "[\x00;\n0U;\x0F[\bi\x00I\"\fsection\x06;\x06To" + 570 ";\a\a;\b[\x06o;\a\a;\b[\x06o;\t\x06;\b[\x06I" + 571 "\"\x14section comment\x06;\x06T;\n@\x11;\n0" + 572 "[\x06@\x11I\"\x0ENamespace\x06" + 573 ";\x06Tc\x17RDoc::NormalModule" 574 575 loaded.store = @store 576 577 assert_equal cm, loaded 578 579 inner = RDoc::Markup::Document.new( 580 RDoc::Markup::Paragraph.new('this is a comment')) 581 inner.file = tl 582 583 comment = RDoc::Markup::Document.new inner 584 585 assert_equal [a2, a1], loaded.attributes.sort 586 assert_equal comment, loaded.comment 587 assert_equal [c1], loaded.constants 588 assert_equal 'Namespace::Klass', loaded.full_name 589 assert_equal [i1], loaded.includes 590 assert_equal [e1], loaded.extends 591 assert_equal [m1], loaded.method_list 592 assert_equal 'Klass', loaded.name 593 assert_equal 'Super', loaded.superclass 594 assert_equal 'Namespace', loaded.parent.name 595 assert loaded.current_section 596 597 expected = { 598 nil => s0, 599 'section' => s1, 600 } 601 602 assert_equal expected, loaded.sections_hash 603 assert_equal [tl], loaded.in_files 604 605 assert_equal tl, loaded.attributes. first.file 606 assert_equal tl, loaded.constants. first.file 607 assert_equal tl, loaded.includes. first.file 608 assert_equal tl, loaded.extends. first.file 609 assert_equal tl, loaded.method_list.first.file 610 end 611 612 def test_merge 613 tl = @store.add_file 'one.rb' 614 p1 = tl.add_class RDoc::NormalClass, 'Parent' 615 c1 = p1.add_class RDoc::NormalClass, 'Klass' 616 617 c2 = RDoc::NormalClass.new 'Klass' 618 619 c2.merge c1 620 621 assert_equal 'Parent', c1.parent_name, 'original parent name' 622 assert_equal 'Parent', c2.parent_name, 'merged parent name' 623 624 assert c1.current_section, 'original current_section' 625 assert c2.current_section, 'merged current_section' 626 end 627 628 def test_merge_attributes 629 tl1 = @store.add_file 'one.rb' 630 tl2 = @store.add_file 'two.rb' 631 632 cm1 = RDoc::ClassModule.new 'Klass' 633 634 attr = cm1.add_attribute RDoc::Attr.new(nil, 'a1', 'RW', '') 635 attr.record_location tl1 636 attr = cm1.add_attribute RDoc::Attr.new(nil, 'a3', 'R', '') 637 attr.record_location tl1 638 attr = cm1.add_attribute RDoc::Attr.new(nil, 'a4', 'R', '') 639 attr.record_location tl1 640 641 cm2 = RDoc::ClassModule.new 'Klass' 642 # TODO allow merging when comment == '' 643 cm2.instance_variable_set :@comment, @RM::Document.new 644 645 attr = cm2.add_attribute RDoc::Attr.new(nil, 'a2', 'RW', '') 646 attr.record_location tl2 647 attr = cm2.add_attribute RDoc::Attr.new(nil, 'a3', 'W', '') 648 attr.record_location tl1 649 attr = cm2.add_attribute RDoc::Attr.new(nil, 'a4', 'W', '') 650 attr.record_location tl1 651 652 cm1.merge cm2 653 654 expected = [ 655 RDoc::Attr.new(nil, 'a2', 'RW', ''), 656 RDoc::Attr.new(nil, 'a3', 'W', ''), 657 RDoc::Attr.new(nil, 'a4', 'W', ''), 658 ] 659 660 expected.each do |a| a.parent = cm1 end 661 assert_equal expected, cm1.attributes.sort 662 end 663 664 def test_merge_attributes_version_0 665 tl1 = @store.add_file 'one.rb' 666 667 cm1 = RDoc::ClassModule.new 'Klass' 668 669 attr = cm1.add_attribute RDoc::Attr.new(nil, 'a1', 'RW', '') 670 attr.record_location tl1 671 attr = cm1.add_attribute RDoc::Attr.new(nil, 'a3', 'R', '') 672 attr.record_location tl1 673 attr = cm1.add_attribute RDoc::Attr.new(nil, 'a4', 'R', '') 674 attr.record_location tl1 675 676 cm2 = RDoc::ClassModule.new 'Klass' 677 # TODO allow merging when comment == '' 678 cm2.instance_variable_set :@comment, @RM::Document.new 679 680 attr = cm2.add_attribute RDoc::Attr.new(nil, 'a2', 'RW', '') 681 attr = cm2.add_attribute RDoc::Attr.new(nil, 'a3', 'W', '') 682 attr = cm2.add_attribute RDoc::Attr.new(nil, 'a4', 'W', '') 683 684 cm1.merge cm2 685 686 expected = [ 687 RDoc::Attr.new(nil, 'a1', 'RW', ''), 688 RDoc::Attr.new(nil, 'a2', 'RW', ''), 689 RDoc::Attr.new(nil, 'a3', 'RW', ''), 690 RDoc::Attr.new(nil, 'a4', 'RW', ''), 691 ] 692 693 expected.each do |a| a.parent = cm1 end 694 assert_equal expected, cm1.attributes.sort 695 end 696 697 def test_merge_collections_drop 698 tl = @store.add_file 'file' 699 700 cm1 = RDoc::ClassModule.new 'C' 701 cm1.record_location tl 702 703 const = cm1.add_constant RDoc::Constant.new('CONST', nil, nil) 704 const.record_location tl 705 706 cm2 = RDoc::ClassModule.new 'C' 707 cm2.record_location tl 708 709 added = [] 710 removed = [] 711 712 cm1.merge_collections cm1.constants, cm2.constants, cm2.in_files do |add, c| 713 if add then 714 added << c 715 else 716 removed << c 717 end 718 end 719 720 assert_empty added 721 assert_equal [const], removed 722 end 723 724 def test_merge_comment 725 tl1 = @store.add_file 'one.rb' 726 tl2 = @store.add_file 'two.rb' 727 728 cm1 = tl1.add_class RDoc::ClassModule, 'Klass' 729 cm1.add_comment 'klass 1', tl1 730 cm1.record_location tl1 731 732 cm2 = tl1.add_class RDoc::NormalClass, 'Klass' 733 cm2.add_comment 'klass 2', tl2 734 cm2.add_comment 'klass 3', tl1 735 cm2.record_location tl1 736 cm2.record_location tl2 737 738 cm2 = Marshal.load Marshal.dump cm2 739 cm2.store = @store 740 741 cm1.merge cm2 742 743 inner1 = @RM::Document.new @RM::Paragraph.new 'klass 3' 744 inner1.file = 'one.rb' 745 inner2 = @RM::Document.new @RM::Paragraph.new 'klass 2' 746 inner2.file = 'two.rb' 747 748 expected = @RM::Document.new inner2, inner1 749 750 assert_equal expected, cm1.comment 751 end 752 753 def test_merge_comment_version_0 754 tl = @store.add_file 'file.rb' 755 756 cm1 = RDoc::ClassModule.new 'Klass' 757 cm1.add_comment 'klass 1', tl 758 759 cm2 = RDoc::ClassModule.new 'Klass' 760 761 cm2.instance_variable_set(:@comment, 762 @RM::Document.new( 763 @RM::Paragraph.new('klass 2'))) 764 cm2.instance_variable_set :@comment_location, @RM::Document.new(cm2.comment) 765 766 cm1.merge cm2 767 768 inner = @RM::Document.new @RM::Paragraph.new 'klass 1' 769 inner.file = 'file.rb' 770 771 expected = @RM::Document.new \ 772 inner, 773 @RM::Document.new(@RM::Paragraph.new('klass 2')) 774 775 assert_equal expected, cm1.comment 776 end 777 778 def test_merge_constants 779 tl1 = @store.add_file 'one.rb' 780 tl2 = @store.add_file 'two.rb' 781 782 cm1 = tl1.add_class RDoc::ClassModule, 'Klass' 783 784 const = cm1.add_constant RDoc::Constant.new('C1', nil, 'one') 785 const.record_location tl1 786 const = cm1.add_constant RDoc::Constant.new('C3', nil, 'one') 787 const.record_location tl1 788 789 store = RDoc::Store.new 790 tl = store.add_file 'one.rb' 791 cm2 = tl.add_class RDoc::ClassModule, 'Klass' 792 cm2.instance_variable_set :@comment, @RM::Document.new 793 794 const = cm2.add_constant RDoc::Constant.new('C2', nil, 'two') 795 const.record_location tl2 796 const = cm2.add_constant RDoc::Constant.new('C3', nil, 'one') 797 const.record_location tl1 798 const = cm2.add_constant RDoc::Constant.new('C4', nil, 'one') 799 const.record_location tl1 800 801 cm1.merge cm2 802 803 expected = [ 804 RDoc::Constant.new('C2', nil, 'two'), 805 RDoc::Constant.new('C3', nil, 'one'), 806 RDoc::Constant.new('C4', nil, 'one'), 807 ] 808 809 expected.each do |a| a.parent = cm1 end 810 811 assert_equal expected, cm1.constants.sort 812 end 813 814 def test_merge_constants_version_0 815 tl1 = @store.add_file 'one.rb' 816 817 cm1 = tl1.add_class RDoc::ClassModule, 'Klass' 818 819 const = cm1.add_constant RDoc::Constant.new('C1', nil, 'one') 820 const.record_location tl1 821 const = cm1.add_constant RDoc::Constant.new('C3', nil, 'one') 822 const.record_location tl1 823 824 store = RDoc::Store.new 825 tl = store.add_file 'one.rb' 826 cm2 = tl.add_class RDoc::ClassModule, 'Klass' 827 cm2.instance_variable_set :@comment, @RM::Document.new 828 829 const = cm2.add_constant RDoc::Constant.new('C2', nil, 'two') 830 const = cm2.add_constant RDoc::Constant.new('C3', nil, 'two') 831 const = cm2.add_constant RDoc::Constant.new('C4', nil, 'two') 832 833 cm1.merge cm2 834 835 expected = [ 836 RDoc::Constant.new('C1', nil, 'one'), 837 RDoc::Constant.new('C2', nil, 'two'), 838 RDoc::Constant.new('C3', nil, 'one'), 839 RDoc::Constant.new('C4', nil, 'two'), 840 ] 841 842 expected.each do |a| a.parent = cm1 end 843 844 assert_equal expected, cm1.constants.sort 845 end 846 847 def test_merge_extends 848 tl1 = @store.add_file 'one.rb' 849 cm1 = tl1.add_class RDoc::ClassModule, 'Klass' 850 851 ext = cm1.add_extend RDoc::Extend.new('I1', 'one') 852 ext.record_location tl1 853 ext = cm1.add_extend RDoc::Extend.new('I3', 'one') 854 ext.record_location tl1 855 856 tl2 = @store.add_file 'two.rb' 857 tl2.store = RDoc::Store.new 858 859 cm2 = tl2.add_class RDoc::ClassModule, 'Klass' 860 cm2.instance_variable_set :@comment, @RM::Document.new 861 862 ext = cm2.add_extend RDoc::Extend.new('I2', 'two') 863 ext.record_location tl2 864 ext = cm2.add_extend RDoc::Extend.new('I3', 'one') 865 ext.record_location tl1 866 ext = cm2.add_extend RDoc::Extend.new('I4', 'one') 867 ext.record_location tl1 868 869 cm1.merge cm2 870 871 expected = [ 872 RDoc::Extend.new('I2', 'two'), 873 RDoc::Extend.new('I3', 'one'), 874 RDoc::Extend.new('I4', 'one'), 875 ] 876 877 expected.each do |a| a.parent = cm1 end 878 879 assert_equal expected, cm1.extends.sort 880 end 881 882 def test_merge_includes 883 tl1 = @store.add_file 'one.rb' 884 885 cm1 = tl1.add_class RDoc::ClassModule, 'Klass' 886 887 incl = cm1.add_include RDoc::Include.new('I1', 'one') 888 incl.record_location tl1 889 incl = cm1.add_include RDoc::Include.new('I3', 'one') 890 incl.record_location tl1 891 892 tl2 = @store.add_file 'two.rb' 893 tl2.store = RDoc::Store.new 894 895 cm2 = tl2.add_class RDoc::ClassModule, 'Klass' 896 cm2.instance_variable_set :@comment, @RM::Document.new 897 898 incl = cm2.add_include RDoc::Include.new('I2', 'two') 899 incl.record_location tl2 900 incl = cm2.add_include RDoc::Include.new('I3', 'one') 901 incl.record_location tl1 902 incl = cm2.add_include RDoc::Include.new('I4', 'one') 903 incl.record_location tl1 904 905 cm1.merge cm2 906 907 expected = [ 908 RDoc::Include.new('I2', 'two'), 909 RDoc::Include.new('I3', 'one'), 910 RDoc::Include.new('I4', 'one'), 911 ] 912 913 expected.each do |a| a.parent = cm1 end 914 915 assert_equal expected, cm1.includes.sort 916 end 917 918 def test_merge_includes_version_0 919 tl1 = @store.add_file 'one.rb' 920 921 cm1 = tl1.add_class RDoc::ClassModule, 'Klass' 922 923 incl = cm1.add_include RDoc::Include.new('I1', 'one') 924 incl.record_location tl1 925 incl = cm1.add_include RDoc::Include.new('I3', 'one') 926 incl.record_location tl1 927 928 tl2 = @store.add_file 'one.rb' 929 tl2.store = RDoc::Store.new 930 931 cm2 = tl2.add_class RDoc::ClassModule, 'Klass' 932 cm2.instance_variable_set :@comment, @RM::Document.new 933 934 incl = cm2.add_include RDoc::Include.new('I2', 'two') 935 incl = cm2.add_include RDoc::Include.new('I3', 'two') 936 incl = cm2.add_include RDoc::Include.new('I4', 'two') 937 938 cm1.merge cm2 939 940 expected = [ 941 RDoc::Include.new('I1', 'one'), 942 RDoc::Include.new('I2', 'two'), 943 RDoc::Include.new('I3', 'one'), 944 RDoc::Include.new('I4', 'two'), 945 ] 946 947 expected.each do |a| a.parent = cm1 end 948 949 assert_equal expected, cm1.includes.sort 950 end 951 952 def test_merge_methods 953 tl1 = @store.add_file 'one.rb' 954 tl2 = @store.add_file 'two.rb' 955 956 cm1 = tl1.add_class RDoc::NormalClass, 'Klass' 957 958 meth = cm1.add_method RDoc::AnyMethod.new(nil, 'm1') 959 meth.record_location tl1 960 meth = cm1.add_method RDoc::AnyMethod.new(nil, 'm3') 961 meth.record_location tl1 962 963 cm2 = RDoc::ClassModule.new 'Klass' 964 cm2.store = @store 965 cm2.instance_variable_set :@comment, @RM::Document.new 966 967 meth = cm2.add_method RDoc::AnyMethod.new(nil, 'm2') 968 meth.record_location tl2 969 meth = cm2.add_method RDoc::AnyMethod.new(nil, 'm3') 970 meth.record_location tl1 971 meth = cm2.add_method RDoc::AnyMethod.new(nil, 'm4') 972 meth.record_location tl1 973 974 cm1.merge cm2 975 976 expected = [ 977 RDoc::AnyMethod.new(nil, 'm2'), 978 RDoc::AnyMethod.new(nil, 'm3'), 979 RDoc::AnyMethod.new(nil, 'm4'), 980 ] 981 982 expected.each do |a| a.parent = cm1 end 983 984 assert_equal expected, cm1.method_list.sort 985 end 986 987 def test_merge_methods_version_0 988 tl1 = @store.add_file 'one.rb' 989 990 cm1 = tl1.add_class RDoc::NormalClass, 'Klass' 991 992 meth = cm1.add_method RDoc::AnyMethod.new(nil, 'm1') 993 meth.record_location tl1 994 meth = cm1.add_method RDoc::AnyMethod.new(nil, 'm3') 995 meth.record_location tl1 996 997 cm2 = RDoc::ClassModule.new 'Klass' 998 cm2.store = @store 999 cm2.instance_variable_set :@comment, @RM::Document.new 1000 1001 meth = cm2.add_method RDoc::AnyMethod.new(nil, 'm2') 1002 meth = cm2.add_method RDoc::AnyMethod.new(nil, 'm3') 1003 meth = cm2.add_method RDoc::AnyMethod.new(nil, 'm4') 1004 1005 cm1.merge cm2 1006 1007 expected = [ 1008 RDoc::AnyMethod.new(nil, 'm1'), 1009 RDoc::AnyMethod.new(nil, 'm2'), 1010 RDoc::AnyMethod.new(nil, 'm3'), 1011 RDoc::AnyMethod.new(nil, 'm4'), 1012 ] 1013 1014 expected.each do |a| a.parent = cm1 end 1015 1016 assert_equal expected, cm1.method_list.sort 1017 end 1018 1019 def test_merge_sections 1020 store1 = @store 1021 1022 tl1_1 = store1.add_file 'one.rb' 1023 1024 cm1 = tl1_1.add_class RDoc::ClassModule, 'Klass' 1025 cm1.record_location tl1_1 1026 1027 s1_0 = cm1.sections.first 1028 s1_1 = cm1.add_section 'section 1', comment('comment 1', tl1_1) 1029 cm1.add_section 'section 2', comment('comment 2 a', tl1_1) 1030 cm1.add_section 'section 4', comment('comment 4 a', tl1_1) 1031 1032 store2 = RDoc::Store.new 1033 tl2_1 = store2.add_file 'one.rb' 1034 tl2_2 = store2.add_file 'two.rb' 1035 1036 cm2 = tl2_1.add_class RDoc::ClassModule, 'Klass' 1037 cm2.record_location tl2_1 1038 cm2.record_location tl2_2 1039 1040 cm2.sections.first 1041 s2_2 = cm2.add_section 'section 2', comment('comment 2 b', tl2_1) 1042 s2_3 = cm2.add_section 'section 3', comment('comment 3', tl2_2) 1043 cm2.add_section 'section 4', comment('comment 4 b', tl2_2) 1044 1045 cm1.merge cm2 1046 1047 expected = [ 1048 s1_0, 1049 s1_1, 1050 s2_2, 1051 s2_3, 1052 RDoc::Context::Section.new(cm1, 'section 4', nil) 1053 ] 1054 1055 merged_sections = cm1.sections.sort_by do |s| 1056 s.title || '' 1057 end 1058 1059 assert_equal expected, merged_sections 1060 1061 assert_equal [comment('comment 2 b', tl2_1)], 1062 cm1.sections_hash['section 2'].comments 1063 1064 expected_s4_comments = [ 1065 comment('comment 4 a', tl2_1), 1066 comment('comment 4 b', tl2_2), 1067 ] 1068 1069 assert_equal expected_s4_comments, cm1.sections_hash['section 4'].comments 1070 end 1071 1072 def test_merge_sections_overlap 1073 store1 = @store 1074 1075 tl1_1 = store1.add_file 'one.rb' 1076 tl1_3 = store1.add_file 'three.rb' 1077 1078 cm1 = tl1_1.add_class RDoc::ClassModule, 'Klass' 1079 cm1.record_location tl1_1 1080 1081 cm1.add_section 'section', comment('comment 1 a', tl1_1) 1082 cm1.add_section 'section', comment('comment 3', tl1_3) 1083 1084 store2 = RDoc::Store.new 1085 tl2_1 = store2.add_file 'one.rb' 1086 tl2_2 = store2.add_file 'two.rb' 1087 tl2_3 = store2.add_file 'three.rb' 1088 1089 cm2 = tl2_1.add_class RDoc::ClassModule, 'Klass' 1090 cm2.record_location tl2_1 1091 cm2.record_location tl2_2 1092 1093 s2_0 = cm2.sections.first 1094 s2_1 = cm2.add_section 'section', comment('comment 1 b', tl1_1) 1095 cm2.add_section 'section', comment('comment 2', tl2_2) 1096 1097 cm1.merge_sections cm2 1098 1099 expected = [ 1100 s2_0, 1101 s2_1, 1102 ] 1103 1104 merged_sections = cm1.sections.sort_by do |s| 1105 s.title || '' 1106 end 1107 1108 assert_equal expected, merged_sections 1109 1110 expected = [ 1111 comment('comment 1 b', tl2_1), 1112 comment('comment 3', tl2_3), 1113 comment('comment 2', tl2_2), 1114 ] 1115 1116 comments = cm1.sections_hash['section'].comments 1117 1118 assert_equal expected, comments.sort_by { |c| c.file.name } 1119 end 1120 1121 def test_parse 1122 tl1 = @store.add_file 'one.rb' 1123 tl2 = @store.add_file 'two.rb' 1124 1125 cm = RDoc::ClassModule.new 'Klass' 1126 cm.add_comment 'comment 1', tl1 1127 cm.add_comment 'comment 2', tl2 1128 1129 doc1 = @RM::Document.new @RM::Paragraph.new 'comment 1' 1130 doc1.file = tl1 1131 doc2 = @RM::Document.new @RM::Paragraph.new 'comment 2' 1132 doc2.file = tl2 1133 1134 expected = @RM::Document.new doc1, doc2 1135 1136 assert_equal expected, cm.parse(cm.comment_location) 1137 end 1138 1139 def test_parse_comment 1140 tl1 = @store.add_file 'one.rb' 1141 1142 cm = RDoc::ClassModule.new 'Klass' 1143 cm.comment = comment 'comment 1', tl1 1144 1145 doc = @RM::Document.new @RM::Paragraph.new 'comment 1' 1146 doc.file = tl1 1147 1148 assert_equal doc, cm.parse(cm.comment) 1149 end 1150 1151 def test_parse_comment_format 1152 tl1 = @store.add_file 'one.rb' 1153 1154 cm = RDoc::ClassModule.new 'Klass' 1155 cm.comment = comment 'comment ((*1*))', tl1 1156 cm.comment.format = 'rd' 1157 1158 doc = @RM::Document.new @RM::Paragraph.new 'comment <em>1</em>' 1159 doc.file = tl1 1160 1161 assert_equal doc, cm.parse(cm.comment) 1162 end 1163 1164 def test_parse_comment_location 1165 tl1 = @store.add_file 'one.rb' 1166 tl2 = @store.add_file 'two.rb' 1167 1168 cm = tl1.add_class RDoc::NormalClass, 'Klass' 1169 cm.add_comment 'comment 1', tl1 1170 cm.add_comment 'comment 2', tl2 1171 1172 cm = Marshal.load Marshal.dump cm 1173 1174 doc1 = @RM::Document.new @RM::Paragraph.new 'comment 1' 1175 doc1.file = tl1 1176 doc2 = @RM::Document.new @RM::Paragraph.new 'comment 2' 1177 doc2.file = tl2 1178 1179 assert_same cm.comment_location, cm.parse(cm.comment_location) 1180 end 1181 1182 def test_remove_nodoc_children 1183 parent = @top_level.add_class RDoc::ClassModule, 'A' 1184 parent.modules_hash.replace 'B' => true, 'C' => true 1185 @store.modules_hash.replace 'A::B' => true 1186 1187 parent.classes_hash.replace 'D' => true, 'E' => true 1188 @store.classes_hash.replace 'A::D' => true 1189 1190 parent.remove_nodoc_children 1191 1192 assert_equal %w[B], parent.modules_hash.keys 1193 assert_equal %w[D], parent.classes_hash.keys 1194 end 1195 1196 def test_search_record 1197 @c2_c3.add_comment 'This is a comment.', @xref_data 1198 1199 expected = [ 1200 'C3', 1201 'C2::C3', 1202 'C2::C3', 1203 '', 1204 'C2/C3.html', 1205 '', 1206 "<p>This is a comment.\n" 1207 ] 1208 1209 assert_equal expected, @c2_c3.search_record 1210 end 1211 1212 def test_search_record_merged 1213 @c2_c3.add_comment 'comment A', @store.add_file('a.rb') 1214 @c2_c3.add_comment 'comment B', @store.add_file('b.rb') 1215 1216 expected = [ 1217 'C3', 1218 'C2::C3', 1219 'C2::C3', 1220 '', 1221 'C2/C3.html', 1222 '', 1223 "<p>comment A\n<p>comment B\n" 1224 ] 1225 1226 assert_equal expected, @c2_c3.search_record 1227 end 1228 1229 def test_store_equals 1230 # version 2 1231 loaded = Marshal.load "\x04\bU:\x16RDoc::NormalClass[\x0Fi\aI\"\nKlass" + 1232 "\x06:\x06EFI\"\x15Namespace::Klass\x06;\x06FI" + 1233 "\"\nSuper\x06;\x06Fo:\eRDoc::Markup::Document\a" + 1234 ":\v@parts[\x06o;\a\a;\b[\x06o" + 1235 ":\x1CRDoc::Markup::Paragraph\x06;\b" + 1236 "[\x06I\"\x16this is a comment\x06;\x06F" + 1237 ":\n@fileI\"\ffile.rb\x06;\x06F;\n0[\a[\nI" + 1238 "\"\aa2\x06;\x06FI\"\aRW\x06;\x06F:\vpublicT@\x11" + 1239 "[\nI\"\aa1\x06;\x06FI\"\aRW\x06;\x06F;\vF@\x11" + 1240 "[\x06[\bI\"\aC1\x06;\x06Fo;\a\a;\b[\x00;\n0@\x11" + 1241 "[\x06[\bI\"\aI1\x06;\x06Fo;\a\a;\b[\x00;\n0@\x11" + 1242 "[\a[\aI\"\nclass\x06;\x06F[\b[\a;\v[\x00" + 1243 "[\a:\x0Eprotected[\x00[\a:\fprivate[\x00[\aI" + 1244 "\"\rinstance\x06;\x06F[\b[\a;\v[\x06[\aI" + 1245 "\"\am1\x06;\x06F@\x11[\a;\f[\x00[\a;\r[\x00" + 1246 "[\x06[\bI\"\aE1\x06;\x06Fo;\a\a;\b[\x00;\n0@\x11" 1247 1248 loaded.store = @store 1249 1250 assert_same @store, loaded.store 1251 1252 a = loaded.attributes.first 1253 assert_same @store, a.store 1254 assert_same @store, a.file.store 1255 1256 c = loaded.constants.first 1257 assert_same @store, c.store 1258 assert_same @store, c.file.store 1259 1260 i = loaded.includes.first 1261 assert_same @store, i.store 1262 assert_same @store, i.file.store 1263 1264 e = loaded.extends.first 1265 assert_same @store, e.store 1266 assert_same @store, e.file.store 1267 1268 m = loaded.method_list.first 1269 assert_same @store, m.store 1270 assert_same @store, m.file.store 1271 end 1272 1273 def test_superclass 1274 assert_equal @c3_h1, @c3_h2.superclass 1275 end 1276 1277 def test_update_aliases_class 1278 n1 = @xref_data.add_module RDoc::NormalClass, 'N1' 1279 n1_k2 = n1.add_module RDoc::NormalClass, 'N2' 1280 1281 a1 = RDoc::Constant.new 'A1', '', '' 1282 n1.add_module_alias n1_k2, n1_k2.name, a1, @xref_data 1283 1284 n1_a1_c = n1.constants.find { |c| c.name == 'A1' } 1285 refute_nil n1_a1_c 1286 assert_equal n1_k2, n1_a1_c.is_alias_for, 'sanity check' 1287 1288 n1.update_aliases 1289 1290 n1_a1_k = @xref_data.find_class_or_module 'N1::A1' 1291 refute_nil n1_a1_k 1292 assert_equal n1_k2, n1_a1_k.is_alias_for 1293 refute_equal n1_k2, n1_a1_k 1294 1295 assert_equal 1, n1_k2.aliases.length 1296 assert_equal n1_a1_k, n1_k2.aliases.first 1297 1298 assert_equal 'N1::N2', n1_k2.full_name 1299 assert_equal 'N1::A1', n1_a1_k.full_name 1300 end 1301 1302 def test_update_aliases_module 1303 n1 = @xref_data.add_module RDoc::NormalModule, 'N1' 1304 n1_n2 = n1.add_module RDoc::NormalModule, 'N2' 1305 1306 a1 = RDoc::Constant.new 'A1', '', '' 1307 n1.add_module_alias n1_n2, n1_n2.name, a1, @xref_data 1308 1309 n1_a1_c = n1.constants.find { |c| c.name == 'A1' } 1310 refute_nil n1_a1_c 1311 assert_equal n1_n2, n1_a1_c.is_alias_for, 'sanity check' 1312 1313 n1.update_aliases 1314 1315 n1_a1_m = @xref_data.find_class_or_module 'N1::A1' 1316 refute_nil n1_a1_m 1317 assert_equal n1_n2, n1_a1_m.is_alias_for 1318 refute_equal n1_n2, n1_a1_m 1319 1320 assert_equal 1, n1_n2.aliases.length 1321 assert_equal n1_a1_m, n1_n2.aliases.first 1322 1323 assert_equal 'N1::N2', n1_n2.full_name 1324 assert_equal 'N1::A1', n1_a1_m.full_name 1325 end 1326 1327 def test_update_aliases_reparent 1328 l1 = @xref_data.add_module RDoc::NormalModule, 'L1' 1329 l1_l2 = l1.add_module RDoc::NormalModule, 'L2' 1330 o1 = @xref_data.add_module RDoc::NormalModule, 'O1' 1331 1332 a1 = RDoc::Constant.new 'A1', '', '' 1333 o1.add_module_alias l1_l2, l1_l2.name, a1, @xref_data 1334 1335 o1_a1_c = o1.constants.find { |c| c.name == 'A1' } 1336 refute_nil o1_a1_c 1337 assert_equal l1_l2, o1_a1_c.is_alias_for 1338 refute_equal l1_l2, o1_a1_c 1339 1340 o1.update_aliases 1341 1342 o1_a1_m = @xref_data.find_class_or_module 'O1::A1' 1343 refute_nil o1_a1_m 1344 assert_equal l1_l2, o1_a1_m.is_alias_for 1345 1346 assert_equal 1, l1_l2.aliases.length 1347 assert_equal o1_a1_m, l1_l2.aliases[0] 1348 1349 assert_equal 'L1::L2', l1_l2.full_name 1350 assert_equal 'O1::A1', o1_a1_m.full_name 1351 end 1352 1353 def test_update_aliases_reparent_root 1354 store = RDoc::Store.new 1355 1356 top_level = store.add_file 'file.rb' 1357 1358 klass = top_level.add_class RDoc::NormalClass, 'Klass' 1359 object = top_level.add_class RDoc::NormalClass, 'Object' 1360 1361 const = RDoc::Constant.new 'A', nil, '' 1362 const.record_location top_level 1363 const.is_alias_for = klass 1364 1365 a = RDoc::Constant.new 'A', '', '' 1366 top_level.add_module_alias klass, klass.name, a, top_level 1367 1368 object.add_constant const 1369 1370 object.update_aliases 1371 1372 assert_equal %w[A Klass Object], store.classes_hash.keys.sort 1373 1374 assert_equal 'A', store.classes_hash['A'].full_name 1375 assert_equal 'Klass', store.classes_hash['Klass'].full_name 1376 end 1377 1378 def test_update_includes 1379 a = RDoc::Include.new 'M1', nil 1380 b = RDoc::Include.new 'M2', nil 1381 c = RDoc::Include.new 'C', nil 1382 1383 @c1.add_include a 1384 @c1.add_include b 1385 @c1.add_include c 1386 @c1.ancestors # cache included modules 1387 1388 @m1_m2.document_self = nil 1389 assert @m1_m2.remove_from_documentation? 1390 1391 assert @store.modules_hash.key? @m1_m2.full_name 1392 refute @store.modules_hash[@m1_m2.full_name].nil? 1393 1394 @store.remove_nodoc @store.modules_hash 1395 refute @store.modules_hash.key? @m1_m2.full_name 1396 1397 @c1.update_includes 1398 1399 assert_equal [a, c], @c1.includes 1400 end 1401 1402 def test_update_includes_trim 1403 a = RDoc::Include.new 'D::M', nil 1404 b = RDoc::Include.new 'D::M', nil 1405 1406 @c1.add_include a 1407 @c1.add_include b 1408 @c1.ancestors # cache included modules 1409 1410 @c1.update_includes 1411 1412 assert_equal [a], @c1.includes 1413 end 1414 1415 def test_update_includes_with_colons 1416 a = RDoc::Include.new 'M1', nil 1417 b = RDoc::Include.new 'M1::M2', nil 1418 c = RDoc::Include.new 'C', nil 1419 1420 @c1.add_include a 1421 @c1.add_include b 1422 @c1.add_include c 1423 @c1.ancestors # cache included modules 1424 1425 @m1_m2.document_self = nil 1426 assert @m1_m2.remove_from_documentation? 1427 1428 assert @store.modules_hash.key? @m1_m2.full_name 1429 refute @store.modules_hash[@m1_m2.full_name].nil? 1430 @store.remove_nodoc @store.modules_hash 1431 refute @store.modules_hash.key? @m1_m2.full_name 1432 1433 @c1.update_includes 1434 1435 assert_equal [a, c], @c1.includes 1436 end 1437 1438 def test_update_extends 1439 a = RDoc::Extend.new 'M1', nil 1440 b = RDoc::Extend.new 'M2', nil 1441 c = RDoc::Extend.new 'C', nil 1442 1443 @c1.add_extend a 1444 @c1.add_extend b 1445 @c1.add_extend c 1446 @c1.each_extend do |extend| extend.module end # cache extended modules 1447 1448 @m1_m2.document_self = nil 1449 assert @m1_m2.remove_from_documentation? 1450 1451 assert @store.modules_hash.key? @m1_m2.full_name 1452 refute @store.modules_hash[@m1_m2.full_name].nil? 1453 @store.remove_nodoc @store.modules_hash 1454 refute @store.modules_hash.key? @m1_m2.full_name 1455 1456 @c1.update_extends 1457 1458 assert_equal [a, b, c], @c1.extends 1459 end 1460 1461 def test_update_extends_trim 1462 a = RDoc::Extend.new 'D::M', nil 1463 b = RDoc::Extend.new 'D::M', nil 1464 1465 @c1.add_extend a 1466 @c1.add_extend b 1467 @c1.each_extend do |extend| extend.module end # cache extended modules 1468 1469 @c1.update_extends 1470 1471 assert_equal [a], @c1.extends 1472 end 1473 1474 def test_update_extends_with_colons 1475 a = RDoc::Extend.new 'M1', nil 1476 b = RDoc::Extend.new 'M1::M2', nil 1477 c = RDoc::Extend.new 'C', nil 1478 1479 @c1.add_extend a 1480 @c1.add_extend b 1481 @c1.add_extend c 1482 @c1.each_extend do |extend| extend.module end # cache extended modules 1483 1484 @m1_m2.document_self = nil 1485 assert @m1_m2.remove_from_documentation? 1486 1487 assert @store.modules_hash.key? @m1_m2.full_name 1488 refute @store.modules_hash[@m1_m2.full_name].nil? 1489 1490 @store.remove_nodoc @store.modules_hash 1491 refute @store.modules_hash.key? @m1_m2.full_name 1492 1493 @c1.update_extends 1494 1495 assert_equal [a, c], @c1.extends 1496 end 1497 1498end 1499 1500