1require 'rdoc/test_case' 2 3class TestRDocMarkupToHtmlSnippet < RDoc::Markup::FormatterTestCase 4 5 add_visitor_tests 6 7 def setup 8 super 9 10 @to = RDoc::Markup::ToHtmlSnippet.new @options, 100, 100 11 @ellipsis = @to.to_html '...' 12 end 13 14 def accept_blank_line 15 assert_empty @to.res.join 16 end 17 18 def accept_block_quote 19 assert_equal "\n<blockquote><p>quote\n</blockquote>\n", @to.res.join 20 21 assert_equal 5, @to.characters 22 end 23 24 def accept_document 25 assert_equal "<p>hello\n", @to.res.join 26 assert_equal 5, @to.characters 27 end 28 29 def accept_heading 30 assert_equal "<p>Hello\n", @to.res.join 31 assert_equal 5, @to.characters 32 end 33 34 def accept_heading_1 35 assert_equal "<p>Hello\n", @to.res.join 36 assert_equal 5, @to.characters 37 end 38 39 def accept_heading_2 40 assert_equal "<p>Hello\n", @to.res.join 41 assert_equal 5, @to.characters 42 end 43 44 def accept_heading_3 45 assert_equal "<p>Hello\n", @to.res.join 46 assert_equal 5, @to.characters 47 end 48 49 def accept_heading_4 50 assert_equal "<p>Hello\n", @to.res.join 51 assert_equal 5, @to.characters 52 end 53 54 def accept_heading_b 55 assert_equal "<p><strong>Hello</strong>\n", 56 @to.res.join 57 assert_equal 5, @to.characters 58 end 59 60 def accept_heading_suppressed_crossref 61 assert_equal "<p>Hello\n", @to.res.join 62 assert_equal 5, @to.characters 63 end 64 65 def accept_list_end_bullet 66 assert_equal [], @to.list 67 assert_equal [], @to.in_list_entry 68 69 assert_equal "\n", @to.res.join 70 assert_equal 0, @to.characters 71 end 72 73 def accept_list_end_label 74 assert_equal [], @to.list 75 assert_equal [], @to.in_list_entry 76 77 assert_equal "\n", @to.res.join 78 assert_equal 0, @to.characters 79 end 80 81 def accept_list_end_lalpha 82 assert_equal [], @to.list 83 assert_equal [], @to.in_list_entry 84 85 assert_equal "\n", @to.res.join 86 assert_equal 0, @to.characters 87 end 88 89 def accept_list_end_number 90 assert_equal [], @to.list 91 assert_equal [], @to.in_list_entry 92 93 assert_equal "\n", @to.res.join 94 assert_equal 0, @to.characters 95 end 96 97 def accept_list_end_note 98 assert_equal [], @to.list 99 assert_equal [], @to.in_list_entry 100 101 assert_equal "\n", @to.res.join 102 assert_equal 0, @to.characters 103 end 104 105 def accept_list_end_ualpha 106 assert_equal [], @to.list 107 assert_equal [], @to.in_list_entry 108 109 assert_equal "\n", @to.res.join 110 assert_equal 0, @to.characters 111 end 112 113 def accept_list_item_end_bullet 114 assert_equal [''], @to.in_list_entry 115 assert_equal 0, @to.characters 116 end 117 118 def accept_list_item_end_label 119 assert_equal [''], @to.in_list_entry 120 assert_equal 4, @to.characters 121 end 122 123 def accept_list_item_end_lalpha 124 assert_equal [''], @to.in_list_entry 125 assert_equal 0, @to.characters 126 end 127 128 def accept_list_item_end_note 129 assert_equal [''], @to.in_list_entry 130 assert_equal 4, @to.characters 131 end 132 133 def accept_list_item_end_number 134 assert_equal [''], @to.in_list_entry 135 assert_equal 0, @to.characters 136 end 137 138 def accept_list_item_end_ualpha 139 assert_equal [''], @to.in_list_entry 140 assert_equal 0, @to.characters 141 end 142 143 def accept_list_item_start_bullet 144 assert_equal "<p>", @to.res.join 145 assert_equal 0, @to.characters 146 end 147 148 def accept_list_item_start_label 149 assert_equal "<p>cat — ", @to.res.join 150 assert_equal 4, @to.characters 151 end 152 153 def accept_list_item_start_lalpha 154 assert_equal "<p>", @to.res.join 155 assert_equal 0, @to.characters 156 end 157 158 def accept_list_item_start_note 159 assert_equal "<p>cat — ", 160 @to.res.join 161 assert_equal 4, @to.characters 162 end 163 164 def accept_list_item_start_note_2 165 expected = <<-EXPECTED 166<p><code>teletype</code> — teletype description 167 168 EXPECTED 169 170 assert_equal expected, @to.res.join 171 assert_equal 29, @to.characters 172 end 173 174 def accept_list_item_start_note_multi_description 175 expected = <<-EXPECTED 176<p>label — description one 177<p>description two 178 179 EXPECTED 180 181 assert_equal expected, @to.res.join 182 assert_equal 37, @to.characters 183 end 184 185 def accept_list_item_start_note_multi_label 186 expected = <<-EXPECTED 187<p>one, two — two headers 188 189 EXPECTED 190 191 assert_equal expected, @to.res.join 192 assert_equal 18, @to.characters 193 end 194 195 def accept_list_item_start_number 196 assert_equal "<p>", @to.res.join 197 assert_equal 0, @to.characters 198 end 199 200 def accept_list_item_start_ualpha 201 assert_equal "<p>", @to.res.join 202 assert_equal 0, @to.characters 203 end 204 205 def accept_list_start_bullet 206 assert_equal [:BULLET], @to.list 207 assert_equal [''], @to.in_list_entry 208 209 assert_equal '', @to.res.join 210 assert_equal 0, @to.characters 211 end 212 213 def accept_list_start_label 214 assert_equal [:LABEL], @to.list 215 assert_equal [''], @to.in_list_entry 216 217 assert_equal '', @to.res.join 218 assert_equal 0, @to.characters 219 end 220 221 def accept_list_start_lalpha 222 assert_equal [:LALPHA], @to.list 223 assert_equal [''], @to.in_list_entry 224 225 assert_equal '', @to.res.join 226 assert_equal 0, @to.characters 227 end 228 229 def accept_list_start_note 230 assert_equal [:NOTE], @to.list 231 assert_equal [''], @to.in_list_entry 232 233 assert_equal '', @to.res.join 234 assert_equal 0, @to.characters 235 end 236 237 def accept_list_start_number 238 assert_equal [:NUMBER], @to.list 239 assert_equal [''], @to.in_list_entry 240 241 assert_equal '', @to.res.join 242 assert_equal 0, @to.characters 243 end 244 245 def accept_list_start_ualpha 246 assert_equal [:UALPHA], @to.list 247 assert_equal [''], @to.in_list_entry 248 249 assert_equal '', @to.res.join 250 assert_equal 0, @to.characters 251 end 252 253 def accept_paragraph 254 assert_equal "<p>hi\n", @to.res.join 255 256 assert_equal 2, @to.characters 257 end 258 259 def accept_paragraph_b 260 assert_equal "<p>reg <strong>bold words</strong> reg\n", @to.res.join 261 262 assert_equal 18, @to.characters 263 end 264 265 def accept_paragraph_br 266 assert_equal "<p>one<br>two\n", @to.res.join 267 268 assert_equal 6, @to.characters 269 end 270 271 def accept_paragraph_break 272 assert_equal "<p>hello<br>\nworld\n", @to.res.join 273 274 assert_equal 11, @to.characters 275 end 276 277 def accept_paragraph_i 278 assert_equal "<p>reg <em>italic words</em> reg\n", @to.res.join 279 280 assert_equal 20, @to.characters 281 end 282 283 def accept_paragraph_plus 284 assert_equal "<p>reg <code>teletype</code> reg\n", @to.res.join 285 286 assert_equal 16, @to.characters 287 end 288 289 def accept_paragraph_star 290 assert_equal "<p>reg <strong>bold</strong> reg\n", @to.res.join 291 292 assert_equal 12, @to.characters 293 end 294 295 def accept_paragraph_underscore 296 assert_equal "<p>reg <em>italic</em> reg\n", @to.res.join 297 298 assert_equal 14, @to.characters 299 end 300 301 def accept_raw 302 assert_equal '', @to.res.join 303 assert_equal 0, @to.characters 304 end 305 306 def accept_rule 307 assert_empty @to.res 308 assert_equal 0, @to.characters 309 end 310 311 def accept_verbatim 312 assert_equal "\n<pre>hi\n world</pre>\n", @to.res.join 313 assert_equal 10, @to.characters 314 end 315 316 def end_accepting 317 assert_equal 'hi', @to.res.join 318 end 319 320 def start_accepting 321 assert_equal [], @to.res 322 assert_equal [], @to.in_list_entry 323 assert_equal [], @to.list 324 assert_equal 0, @to.characters 325 end 326 327 def list_nested 328 expected = <<-EXPECTED 329<p>l1 330<p>l1.1 331 332<p>l2 333 334 EXPECTED 335 336 assert_equal expected, @to.res.join 337 assert_equal 8, @to.characters 338 end 339 340 def list_verbatim 341 expected = <<-EXPECTED 342<p>list stuff 343 344<pre>* list 345 with 346 347 second 348 349 1. indented 350 2. numbered 351 352 third 353 354* second</pre> 355 356 EXPECTED 357 358 assert_equal expected, @to.end_accepting 359 assert_equal 81, @to.characters 360 end 361 362 def test_accept_heading_7 363 @to.start_accepting 364 365 @to.accept_heading @RM::Heading.new(7, 'Hello') 366 367 assert_equal "<p>Hello\n", @to.res.join 368 assert_equal 5, @to.characters 369 end 370 371 def test_accept_heading_aref_class 372 @to.code_object = RDoc::NormalClass.new 'Foo' 373 @to.start_accepting 374 375 @to.accept_heading @RM::Heading.new(1, 'Hello') 376 377 assert_equal "<p>Hello\n", 378 @to.res.join 379 assert_equal 5, @to.characters 380 end 381 382 def test_accept_heading_aref_method 383 @to.code_object = RDoc::AnyMethod.new nil, 'foo' 384 @to.start_accepting 385 386 @to.accept_heading @RM::Heading.new(1, 'Hello') 387 388 assert_equal "<p>Hello\n", 389 @to.res.join 390 assert_equal 5, @to.characters 391 end 392 393 def test_accept_verbatim_ruby 394 options = RDoc::Options.new 395 rdoc = RDoc::RDoc.new 396 rdoc.options = options 397 RDoc::RDoc.current = rdoc 398 399 verb = @RM::Verbatim.new("class C\n", "end\n") 400 401 @to.start_accepting 402 @to.accept_verbatim verb 403 404 expected = <<-EXPECTED 405 406<pre class="ruby"><span class="ruby-keyword">class</span> <span class="ruby-constant">C</span> 407<span class="ruby-keyword">end</span> 408</pre> 409 EXPECTED 410 411 assert_equal expected, @to.res.join 412 assert_equal 11, @to.characters 413 end 414 415 def test_accept_verbatim_ruby_error 416 options = RDoc::Options.new 417 rdoc = RDoc::RDoc.new 418 rdoc.options = options 419 RDoc::RDoc.current = rdoc 420 421 verb = @RM::Verbatim.new("a % 09 # => blah\n") 422 423 @to.start_accepting 424 @to.accept_verbatim verb 425 426 inner = CGI.escapeHTML "a % 09 # => blah" 427 428 expected = <<-EXPECTED 429 430<pre>#{inner} 431</pre> 432 EXPECTED 433 434 assert_equal expected, @to.res.join 435 assert_equal 16, @to.characters 436 end 437 438 def test_add_paragraph 439 @to = RDoc::Markup::ToHtmlSnippet.new @options, 0, 3 440 assert_throws :done do 441 @to.add_paragraph 442 @to.add_paragraph 443 @to.add_paragraph 444 end 445 446 assert_equal 3, @to.paragraphs 447 end 448 449 def test_convert_limit 450 rdoc = <<-RDOC 451= Hello 452 453This is some text, it *will* be cut off after 100 characters and an ellipsis 454must follow 455 456So there you have it 457 RDOC 458 459 expected = <<-EXPECTED 460<p>Hello 461<p>This is some text, it <strong>will</strong> be cut off after 100 characters 462and an ellipsis must follow 463<p>So there you #{@ellipsis} 464 EXPECTED 465 466 actual = @to.convert rdoc 467 468 assert_equal expected, actual 469 assert_equal 111, @to.characters, 'snippet character length' 470 end 471 472 def test_convert_limit_2 473 rdoc = <<-RDOC 474Outputs formatted RI data for the class or method +name+. 475 476Returns true if +name+ was found, false if it was not an alternative could 477be guessed, raises an error if +name+ couldn't be guessed. 478 RDOC 479 480 expected = <<-EXPECTED 481<p>Outputs formatted RI data for the class or method <code>name</code>. 482<p>Returns true if <code>name</code> was found, false if it was #{@ellipsis} 483 EXPECTED 484 485 actual = @to.convert rdoc 486 487 assert_equal expected, actual 488 assert_equal 159, @to.characters, 'snippet character length' 489 end 490 491 def test_convert_limit_paragraphs 492 @to = RDoc::Markup::ToHtmlSnippet.new @options, 100, 3 493 494 rdoc = <<-RDOC 495= \RDoc - Ruby Documentation System 496 497* {RDoc Project Page}[https://github.com/rdoc/rdoc/] 498* {RDoc Documentation}[http://docs.seattlerb.org/rdoc] 499* {RDoc Bug Tracker}[https://github.com/rdoc/rdoc/issues] 500 501== DESCRIPTION: 502 503RDoc produces HTML and command-line documentation for Ruby projects. RDoc 504includes the +rdoc+ and +ri+ tools for generating and displaying online 505documentation. 506 507See RDoc for a description of RDoc's markup and basic use. 508 RDOC 509 510 expected = <<-EXPECTED 511<p>RDoc - Ruby Documentation System 512<p>RDoc Project Page 513<p>RDoc Documentation 514 EXPECTED 515 516 actual = @to.convert rdoc 517 518 assert_equal expected, actual 519 assert_equal 67, @to.characters 520 end 521 522 def test_convert_limit_in_tag 523 @to = RDoc::Markup::ToHtmlSnippet.new @options, 4 524 rdoc = "* ab *c* d\n" 525 526 expected = "<p>ab <strong>c</strong> #{@ellipsis}\n\n" 527 528 actual = @to.convert rdoc 529 530 assert_equal 4, @to.characters 531 assert_equal expected, actual 532 end 533 534 def test_convert_limit_verbatim 535 rdoc = <<-RDOC 536= Hello There 537 538This is some text, it *will* be cut off after 100 characters 539 540 This one is cut off in this verbatim section 541 RDOC 542 543 expected = <<-EXPECTED 544<p>Hello There 545<p>This is some text, it <strong>will</strong> be cut off after 100 characters 546 547<pre>This one is cut off in this verbatim ...</pre> 548 EXPECTED 549 550 actual = @to.convert rdoc 551 552 assert_equal expected, actual 553 assert_equal 113, @to.characters 554 end 555 556 def test_convert_limit_verbatim_2 557 rdoc = <<-RDOC 558Extracts the class, selector and method name parts from +name+ like 559Foo::Bar#baz. 560 561NOTE: Given Foo::Bar, Bar is considered a class even though it may be a 562 method 563 RDOC 564 565 expected = <<-EXPECTED 566<p>Extracts the class, selector and method name parts from <code>name</code> 567like Foo::Bar#baz. 568<p>NOTE: Given Foo::Bar, #{@ellipsis} 569 EXPECTED 570 571 actual = @to.convert rdoc 572 573 assert_equal expected, actual 574 assert_equal 101, @to.characters 575 end 576 577 def test_convert_limit_verbatim_multiline 578 rdoc = <<-RDOC 579Look for directives in a normal comment block: 580 581 # :stopdoc: 582 # Don't display comment from this point forward 583 584This routine modifies its +comment+ parameter. 585 RDOC 586 587 inner = CGI.escapeHTML "# Don't display comment from this point forward" 588 expected = <<-EXPECTED 589<p>Look for directives in a normal comment block: 590 591<pre># :stopdoc: 592#{inner}</pre> 593 EXPECTED 594 595 actual = @to.convert rdoc 596 597 assert_equal expected, actual 598 assert_equal 105, @to.characters 599 end 600 601 def test_convert_limit_over 602 @to = RDoc::Markup::ToHtmlSnippet.new @options, 4 603 rdoc = "* text\n" * 2 604 605 expected = "<p>text\n" 606 expected.chomp! 607 expected << " #{@ellipsis}\n" 608 609 actual = @to.convert rdoc 610 611 assert_equal 4, @to.characters 612 assert_equal expected, actual 613 end 614 615 def test_convert_string 616 assert_equal '<>', @to.convert_string('<>') 617 end 618 619 def test_convert_RDOCLINK_label_label 620 result = @to.convert 'rdoc-label:label-One' 621 622 assert_equal "<p>One\n", result 623 assert_equal 3, @to.characters 624 end 625 626 def test_convert_RDOCLINK_label_foottext 627 result = @to.convert 'rdoc-label:foottext-1' 628 629 assert_equal "<p>*1\n", result 630 assert_equal 2, @to.characters 631 end 632 633 def test_convert_RDOCLINK_label_footmark 634 result = @to.convert 'rdoc-label:footmark-1' 635 636 assert_equal "<p>^1\n", result 637 assert_equal 2, @to.characters 638 end 639 640 def test_convert_RDOCLINK_ref 641 result = @to.convert 'rdoc-ref:C' 642 643 assert_equal "<p>C\n", result 644 assert_equal 1, @to.characters 645 end 646 647 def test_convert_TIDYLINK_rdoc_label 648 result = @to.convert '{foo}[rdoc-label:foottext-1]' 649 650 assert_equal "<p>foo\n", result 651 assert_equal 3, @to.characters 652 end 653 654 def test_handle_special_HYPERLINK_link 655 special = RDoc::Markup::Special.new 0, 'link:README.txt' 656 657 link = @to.handle_special_HYPERLINK special 658 659 assert_equal 'README.txt', link 660 end 661 662 def test_list_verbatim_2 663 str = "* one\n verb1\n verb2\n* two\n" 664 665 expected = <<-EXPECTED 666<p>one 667 668<pre>verb1 669verb2</pre> 670<p>two 671 672 EXPECTED 673 674 assert_equal expected, @m.convert(str, @to) 675 assert_equal 17, @to.characters 676 end 677 678 def test_on_tags 679 on = RDoc::Markup::AttrChanger.new 2, 0 680 681 @to.on_tags [], on 682 683 assert_equal 2, @to.mask 684 end 685 686 def test_off_tags 687 on = RDoc::Markup::AttrChanger.new 2, 0 688 off = RDoc::Markup::AttrChanger.new 0, 2 689 690 @to.on_tags [], on 691 @to.off_tags [], off 692 693 assert_equal 0, @to.mask 694 end 695 696 def test_to_html 697 assert_equal "<p><code>--</code>\n", util_format("<tt>--</tt>") 698 assert_equal 2, @to.characters 699 end 700 701 def util_format text 702 paragraph = RDoc::Markup::Paragraph.new text 703 704 @to.start_accepting 705 @to.accept_paragraph paragraph 706 @to.end_accepting 707 end 708 709end 710 711