1require 'minitest/unit' 2 3## 4# Test case for creating new RDoc::Markup formatters. See 5# test/test_rdoc_markup_to_*.rb for examples. 6# 7# This test case adds a variety of tests to your subclass when 8# #add_visitor_tests is called. Most tests set up a scenario then call a 9# method you will provide to perform the assertion on the output. 10# 11# Your subclass must instantiate a visitor and assign it to <tt>@to</tt>. 12# 13# For example, test_accept_blank_line sets up a RDoc::Markup::BlockLine then 14# calls accept_blank_line on your visitor. You are responsible for asserting 15# that the output is correct. 16# 17# Example: 18# 19# class TestRDocMarkupToNewFormat < RDoc::Markup::FormatterTestCase 20# 21# add_visitor_tests 22# 23# def setup 24# super 25# 26# @to = RDoc::Markup::ToNewFormat.new 27# end 28# 29# def accept_blank_line 30# assert_equal :junk, @to.res.join 31# end 32# 33# # ... 34# 35# end 36 37class RDoc::Markup::FormatterTestCase < RDoc::TestCase 38 39 ## 40 # Call #setup when inheriting from this test case. 41 # 42 # Provides the following instance variables: 43 # 44 # +@m+:: RDoc::Markup.new 45 # +@RM+:: RDoc::Markup # to reduce typing 46 # +@bullet_list+:: @RM::List.new :BULLET, # ... 47 # +@label_list+:: @RM::List.new :LABEL, # ... 48 # +@lalpha_list+:: @RM::List.new :LALPHA, # ... 49 # +@note_list+:: @RM::List.new :NOTE, # ... 50 # +@number_list+:: @RM::List.new :NUMBER, # ... 51 # +@ualpha_list+:: @RM::List.new :UALPHA, # ... 52 53 def setup 54 super 55 56 @options = RDoc::Options.new 57 58 @m = @RM.new 59 60 @bullet_list = @RM::List.new(:BULLET, 61 @RM::ListItem.new(nil, @RM::Paragraph.new('l1')), 62 @RM::ListItem.new(nil, @RM::Paragraph.new('l2'))) 63 64 @label_list = @RM::List.new(:LABEL, 65 @RM::ListItem.new('cat', @RM::Paragraph.new('cats are cool')), 66 @RM::ListItem.new('dog', @RM::Paragraph.new('dogs are cool too'))) 67 68 @lalpha_list = @RM::List.new(:LALPHA, 69 @RM::ListItem.new(nil, @RM::Paragraph.new('l1')), 70 @RM::ListItem.new(nil, @RM::Paragraph.new('l2'))) 71 72 @note_list = @RM::List.new(:NOTE, 73 @RM::ListItem.new('cat', @RM::Paragraph.new('cats are cool')), 74 @RM::ListItem.new('dog', @RM::Paragraph.new('dogs are cool too'))) 75 76 @number_list = @RM::List.new(:NUMBER, 77 @RM::ListItem.new(nil, @RM::Paragraph.new('l1')), 78 @RM::ListItem.new(nil, @RM::Paragraph.new('l2'))) 79 80 @ualpha_list = @RM::List.new(:UALPHA, 81 @RM::ListItem.new(nil, @RM::Paragraph.new('l1')), 82 @RM::ListItem.new(nil, @RM::Paragraph.new('l2'))) 83 end 84 85 ## 86 # Call to add the visitor tests to your test case 87 88 def self.add_visitor_tests 89 class_eval do 90 91 ## 92 # Calls start_accepting which needs to verify startup state 93 94 def test_start_accepting 95 @to.start_accepting 96 97 start_accepting 98 end 99 100 ## 101 # Calls end_accepting on your test case which needs to call 102 # <tt>@to.end_accepting</tt> and verify document generation 103 104 def test_end_accepting 105 @to.start_accepting 106 @to.res << 'hi' 107 108 end_accepting 109 end 110 111 ## 112 # Calls accept_blank_line 113 114 def test_accept_blank_line 115 @to.start_accepting 116 117 @to.accept_blank_line @RM::BlankLine.new 118 119 accept_blank_line 120 end 121 122 ## 123 # Calls accept_block_quote 124 125 def test_accept_block_quote 126 @to.start_accepting 127 128 @to.accept_block_quote block para 'quote' 129 130 accept_block_quote 131 end 132 ## 133 # Test case that calls <tt>@to.accept_document</tt> 134 135 def test_accept_document 136 @to.start_accepting 137 @to.accept_document @RM::Document.new @RM::Paragraph.new 'hello' 138 139 accept_document 140 end 141 142 ## 143 # Calls accept_heading with a level 5 RDoc::Markup::Heading 144 145 def test_accept_heading 146 @to.start_accepting 147 148 @to.accept_heading @RM::Heading.new(5, 'Hello') 149 150 accept_heading 151 end 152 153 ## 154 # Calls accept_heading_1 with a level 1 RDoc::Markup::Heading 155 156 def test_accept_heading_1 157 @to.start_accepting 158 159 @to.accept_heading @RM::Heading.new(1, 'Hello') 160 161 accept_heading_1 162 end 163 164 ## 165 # Calls accept_heading_2 with a level 2 RDoc::Markup::Heading 166 167 def test_accept_heading_2 168 @to.start_accepting 169 170 @to.accept_heading @RM::Heading.new(2, 'Hello') 171 172 accept_heading_2 173 end 174 175 ## 176 # Calls accept_heading_3 with a level 3 RDoc::Markup::Heading 177 178 def test_accept_heading_3 179 # HACK this doesn't belong here 180 skip "No String#chars, upgrade your ruby" unless ''.respond_to? :chars 181 182 @to.start_accepting 183 184 @to.accept_heading @RM::Heading.new(3, 'Hello') 185 186 accept_heading_3 187 end 188 189 ## 190 # Calls accept_heading_4 with a level 4 RDoc::Markup::Heading 191 192 def test_accept_heading_4 193 @to.start_accepting 194 195 @to.accept_heading @RM::Heading.new(4, 'Hello') 196 197 accept_heading_4 198 end 199 200 ## 201 # Calls accept_heading_b with a bold level 1 RDoc::Markup::Heading 202 203 def test_accept_heading_b 204 @to.start_accepting 205 206 @to.accept_heading @RM::Heading.new(1, '*Hello*') 207 208 accept_heading_b 209 end 210 211 ## 212 # Calls accept_heading_suppressed_crossref with a level 1 213 # RDoc::Markup::Heading containing a suppressed crossref 214 215 def test_accept_heading_suppressed_crossref # HACK to_html_crossref test 216 @to.start_accepting 217 218 @to.accept_heading @RM::Heading.new(1, '\\Hello') 219 220 accept_heading_suppressed_crossref 221 end 222 223 ## 224 # Calls accept_paragraph 225 226 def test_accept_paragraph 227 @to.start_accepting 228 229 @to.accept_paragraph @RM::Paragraph.new('hi') 230 231 accept_paragraph 232 end 233 234 ## 235 # Calls accept_paragraph_b with a RDoc::Markup::Paragraph containing 236 # bold words 237 238 def test_accept_paragraph_b 239 @to.start_accepting 240 241 @to.accept_paragraph @RM::Paragraph.new('reg <b>bold words</b> reg') 242 243 accept_paragraph_b 244 end 245 246 ## 247 # Calls accept_paragraph_br with a RDoc::Markup::Paragraph containing 248 # a \<br> 249 250 def test_accept_paragraph_br 251 @to.start_accepting 252 253 @to.accept_paragraph para 'one<br>two' 254 255 accept_paragraph_br 256 end 257 258 ## 259 # Calls accept_paragraph with a Paragraph containing a hard break 260 261 def test_accept_paragraph_break 262 @to.start_accepting 263 264 @to.accept_paragraph para('hello', hard_break, 'world') 265 266 accept_paragraph_break 267 end 268 269 ## 270 # Calls accept_paragraph_i with a RDoc::Markup::Paragraph containing 271 # emphasized words 272 273 def test_accept_paragraph_i 274 @to.start_accepting 275 276 @to.accept_paragraph @RM::Paragraph.new('reg <em>italic words</em> reg') 277 278 accept_paragraph_i 279 end 280 281 ## 282 # Calls accept_paragraph_plus with a RDoc::Markup::Paragraph containing 283 # teletype words 284 285 def test_accept_paragraph_plus 286 @to.start_accepting 287 288 @to.accept_paragraph @RM::Paragraph.new('reg +teletype+ reg') 289 290 accept_paragraph_plus 291 end 292 293 ## 294 # Calls accept_paragraph_star with a RDoc::Markup::Paragraph containing 295 # bold words 296 297 def test_accept_paragraph_star 298 @to.start_accepting 299 300 @to.accept_paragraph @RM::Paragraph.new('reg *bold* reg') 301 302 accept_paragraph_star 303 end 304 305 ## 306 # Calls accept_paragraph_underscore with a RDoc::Markup::Paragraph 307 # containing emphasized words 308 309 def test_accept_paragraph_underscore 310 @to.start_accepting 311 312 @to.accept_paragraph @RM::Paragraph.new('reg _italic_ reg') 313 314 accept_paragraph_underscore 315 end 316 317 ## 318 # Calls accept_verbatim with a RDoc::Markup::Verbatim 319 320 def test_accept_verbatim 321 @to.start_accepting 322 323 @to.accept_verbatim @RM::Verbatim.new("hi\n", " world\n") 324 325 accept_verbatim 326 end 327 328 ## 329 # Calls accept_raw with a RDoc::Markup::Raw 330 331 def test_accept_raw 332 @to.start_accepting 333 334 @to.accept_raw @RM::Raw.new("<table>", 335 "<tr><th>Name<th>Count", 336 "<tr><td>a<td>1", 337 "<tr><td>b<td>2", 338 "</table>") 339 340 accept_raw 341 end 342 343 ## 344 # Calls accept_rule with a RDoc::Markup::Rule 345 346 def test_accept_rule 347 @to.start_accepting 348 349 @to.accept_rule @RM::Rule.new(4) 350 351 accept_rule 352 end 353 354 ## 355 # Calls accept_list_item_start_bullet 356 357 def test_accept_list_item_start_bullet 358 @to.start_accepting 359 360 @to.accept_list_start @bullet_list 361 362 @to.accept_list_item_start @bullet_list.items.first 363 364 accept_list_item_start_bullet 365 end 366 367 ## 368 # Calls accept_list_item_start_label 369 370 def test_accept_list_item_start_label 371 @to.start_accepting 372 373 @to.accept_list_start @label_list 374 375 @to.accept_list_item_start @label_list.items.first 376 377 accept_list_item_start_label 378 end 379 380 ## 381 # Calls accept_list_item_start_lalpha 382 383 def test_accept_list_item_start_lalpha 384 @to.start_accepting 385 386 @to.accept_list_start @lalpha_list 387 388 @to.accept_list_item_start @lalpha_list.items.first 389 390 accept_list_item_start_lalpha 391 end 392 393 ## 394 # Calls accept_list_item_start_note 395 396 def test_accept_list_item_start_note 397 @to.start_accepting 398 399 @to.accept_list_start @note_list 400 401 @to.accept_list_item_start @note_list.items.first 402 403 accept_list_item_start_note 404 end 405 406 ## 407 # Calls accept_list_item_start_note_2 408 409 def test_accept_list_item_start_note_2 410 list = list(:NOTE, 411 item('<tt>teletype</tt>', 412 para('teletype description'))) 413 414 @to.start_accepting 415 416 list.accept @to 417 418 @to.end_accepting 419 420 accept_list_item_start_note_2 421 end 422 423 ## 424 # Calls accept_list_item_start_note_multi_description 425 426 def test_accept_list_item_start_note_multi_description 427 list = list(:NOTE, 428 item(%w[label], 429 para('description one')), 430 item(nil, para('description two'))) 431 432 @to.start_accepting 433 434 list.accept @to 435 436 @to.end_accepting 437 438 accept_list_item_start_note_multi_description 439 end 440 441 ## 442 # Calls accept_list_item_start_note_multi_label 443 444 def test_accept_list_item_start_note_multi_label 445 list = list(:NOTE, 446 item(%w[one two], 447 para('two headers'))) 448 449 @to.start_accepting 450 451 list.accept @to 452 453 @to.end_accepting 454 455 accept_list_item_start_note_multi_label 456 end 457 458 ## 459 # Calls accept_list_item_start_number 460 461 def test_accept_list_item_start_number 462 @to.start_accepting 463 464 @to.accept_list_start @number_list 465 466 @to.accept_list_item_start @number_list.items.first 467 468 accept_list_item_start_number 469 end 470 471 ## 472 # Calls accept_list_item_start_ualpha 473 474 def test_accept_list_item_start_ualpha 475 @to.start_accepting 476 477 @to.accept_list_start @ualpha_list 478 479 @to.accept_list_item_start @ualpha_list.items.first 480 481 accept_list_item_start_ualpha 482 end 483 484 ## 485 # Calls accept_list_item_end_bullet 486 487 def test_accept_list_item_end_bullet 488 @to.start_accepting 489 490 @to.accept_list_start @bullet_list 491 492 @to.accept_list_item_start @bullet_list.items.first 493 494 @to.accept_list_item_end @bullet_list.items.first 495 496 accept_list_item_end_bullet 497 end 498 499 ## 500 # Calls accept_list_item_end_label 501 502 def test_accept_list_item_end_label 503 @to.start_accepting 504 505 @to.accept_list_start @label_list 506 507 @to.accept_list_item_start @label_list.items.first 508 509 @to.accept_list_item_end @label_list.items.first 510 511 accept_list_item_end_label 512 end 513 514 ## 515 # Calls accept_list_item_end_lalpha 516 517 def test_accept_list_item_end_lalpha 518 @to.start_accepting 519 520 @to.accept_list_start @lalpha_list 521 522 @to.accept_list_item_start @lalpha_list.items.first 523 524 @to.accept_list_item_end @lalpha_list.items.first 525 526 accept_list_item_end_lalpha 527 end 528 529 ## 530 # Calls accept_list_item_end_note 531 532 def test_accept_list_item_end_note 533 @to.start_accepting 534 535 @to.accept_list_start @note_list 536 537 @to.accept_list_item_start @note_list.items.first 538 539 @to.accept_list_item_end @note_list.items.first 540 541 accept_list_item_end_note 542 end 543 544 ## 545 # Calls accept_list_item_end_number 546 547 def test_accept_list_item_end_number 548 @to.start_accepting 549 550 @to.accept_list_start @number_list 551 552 @to.accept_list_item_start @number_list.items.first 553 554 @to.accept_list_item_end @number_list.items.first 555 556 accept_list_item_end_number 557 end 558 559 ## 560 # Calls accept_list_item_end_ualpha 561 562 def test_accept_list_item_end_ualpha 563 @to.start_accepting 564 565 @to.accept_list_start @ualpha_list 566 567 @to.accept_list_item_start @ualpha_list.items.first 568 569 @to.accept_list_item_end @ualpha_list.items.first 570 571 accept_list_item_end_ualpha 572 end 573 574 ## 575 # Calls accept_list_start_bullet 576 577 def test_accept_list_start_bullet 578 @to.start_accepting 579 580 @to.accept_list_start @bullet_list 581 582 accept_list_start_bullet 583 end 584 585 ## 586 # Calls accept_list_start_label 587 588 def test_accept_list_start_label 589 @to.start_accepting 590 591 @to.accept_list_start @label_list 592 593 accept_list_start_label 594 end 595 596 ## 597 # Calls accept_list_start_lalpha 598 599 def test_accept_list_start_lalpha 600 @to.start_accepting 601 602 @to.accept_list_start @lalpha_list 603 604 accept_list_start_lalpha 605 end 606 607 ## 608 # Calls accept_list_start_note 609 610 def test_accept_list_start_note 611 @to.start_accepting 612 613 @to.accept_list_start @note_list 614 615 accept_list_start_note 616 end 617 618 ## 619 # Calls accept_list_start_number 620 621 def test_accept_list_start_number 622 @to.start_accepting 623 624 @to.accept_list_start @number_list 625 626 accept_list_start_number 627 end 628 629 ## 630 # Calls accept_list_start_ualpha 631 632 def test_accept_list_start_ualpha 633 @to.start_accepting 634 635 @to.accept_list_start @ualpha_list 636 637 accept_list_start_ualpha 638 end 639 640 ## 641 # Calls accept_list_end_bullet 642 643 def test_accept_list_end_bullet 644 @to.start_accepting 645 646 @to.accept_list_start @bullet_list 647 648 @to.accept_list_end @bullet_list 649 650 accept_list_end_bullet 651 end 652 653 ## 654 # Calls accept_list_end_label 655 656 def test_accept_list_end_label 657 @to.start_accepting 658 659 @to.accept_list_start @label_list 660 661 @to.accept_list_end @label_list 662 663 accept_list_end_label 664 end 665 666 ## 667 # Calls accept_list_end_lalpha 668 669 def test_accept_list_end_lalpha 670 @to.start_accepting 671 672 @to.accept_list_start @lalpha_list 673 674 @to.accept_list_end @lalpha_list 675 676 accept_list_end_lalpha 677 end 678 679 ## 680 # Calls accept_list_end_number 681 682 def test_accept_list_end_number 683 @to.start_accepting 684 685 @to.accept_list_start @number_list 686 687 @to.accept_list_end @number_list 688 689 accept_list_end_number 690 end 691 692 ## 693 # Calls accept_list_end_note 694 695 def test_accept_list_end_note 696 @to.start_accepting 697 698 @to.accept_list_start @note_list 699 700 @to.accept_list_end @note_list 701 702 accept_list_end_note 703 end 704 705 ## 706 # Calls accept_list_end_ualpha 707 708 def test_accept_list_end_ualpha 709 @to.start_accepting 710 711 @to.accept_list_start @ualpha_list 712 713 @to.accept_list_end @ualpha_list 714 715 accept_list_end_ualpha 716 end 717 718 ## 719 # Calls list_nested with a two-level list 720 721 def test_list_nested 722 doc = @RM::Document.new( 723 @RM::List.new(:BULLET, 724 @RM::ListItem.new(nil, 725 @RM::Paragraph.new('l1'), 726 @RM::List.new(:BULLET, 727 @RM::ListItem.new(nil, 728 @RM::Paragraph.new('l1.1')))), 729 @RM::ListItem.new(nil, 730 @RM::Paragraph.new('l2')))) 731 732 doc.accept @to 733 734 list_nested 735 end 736 737 ## 738 # Calls list_verbatim with a list containing a verbatim block 739 740 def test_list_verbatim # HACK overblown 741 doc = 742 doc( 743 list(:BULLET, 744 item(nil, 745 para('list stuff'), 746 blank_line, 747 verb("* list\n", 748 " with\n", 749 "\n", 750 " second\n", 751 "\n", 752 " 1. indented\n", 753 " 2. numbered\n", 754 "\n", 755 " third\n", 756 "\n", 757 "* second\n")))) 758 759 doc.accept @to 760 761 list_verbatim 762 end 763 end 764 end 765 766end 767 768