1# coding: utf-8 2 3require 'rdoc/test_case' 4 5class TestRDocText < RDoc::TestCase 6 7 include RDoc::Text 8 9 def setup 10 super 11 12 @options = RDoc::Options.new 13 14 @top_level = @store.add_file 'file.rb' 15 end 16 17 def mu_pp obj 18 s = '' 19 s = PP.pp obj, s 20 s = s.force_encoding Encoding.default_external if defined? Encoding 21 s.chomp 22 end 23 24 def test_self_encode_fallback 25 skip "Encoding not implemented" unless Object.const_defined? :Encoding 26 27 assert_equal '…', 28 RDoc::Text::encode_fallback('…', Encoding::UTF_8, '...') 29 assert_equal '...', 30 RDoc::Text::encode_fallback('…', Encoding::US_ASCII, '...') 31 end 32 33 def test_expand_tabs 34 assert_equal("hello\n dave", 35 expand_tabs("hello\n dave"), 'spaces') 36 37 assert_equal("hello\n dave", 38 expand_tabs("hello\n\tdave"), 'tab') 39 40 assert_equal("hello\n dave", 41 expand_tabs("hello\n \tdave"), '1 space tab') 42 43 assert_equal("hello\n dave", 44 expand_tabs("hello\n \tdave"), '2 space tab') 45 46 assert_equal("hello\n dave", 47 expand_tabs("hello\n \tdave"), '3 space tab') 48 49 assert_equal("hello\n dave", 50 expand_tabs("hello\n \tdave"), '4 space tab') 51 52 assert_equal("hello\n dave", 53 expand_tabs("hello\n \tdave"), '5 space tab') 54 55 assert_equal("hello\n dave", 56 expand_tabs("hello\n \tdave"), '6 space tab') 57 58 assert_equal("hello\n dave", 59 expand_tabs("hello\n \tdave"), '7 space tab') 60 61 assert_equal("hello\n dave", 62 expand_tabs("hello\n \tdave"), '8 space tab') 63 64 assert_equal('. .', 65 expand_tabs(".\t\t."), 'dot tab tab dot') 66 end 67 68 def test_expand_tabs_encoding 69 skip "Encoding not implemented" unless Object.const_defined? :Encoding 70 71 inn = "hello\ns\tdave" 72 inn.force_encoding Encoding::BINARY 73 74 out = expand_tabs inn 75 76 assert_equal "hello\ns dave", out 77 assert_equal Encoding::BINARY, out.encoding 78 end 79 80 def test_flush_left 81 text = <<-TEXT 82 83 we don't worry too much. 84 85 The comments associated with 86 TEXT 87 88 expected = <<-EXPECTED 89 90we don't worry too much. 91 92The comments associated with 93 EXPECTED 94 95 assert_equal expected, flush_left(text) 96 end 97 98 def test_flush_left_encoding 99 skip "Encoding not implemented" unless Object.const_defined? :Encoding 100 101 text = <<-TEXT 102 103 we don't worry too much. 104 105 The comments associated with 106 TEXT 107 108 text.force_encoding Encoding::US_ASCII 109 110 expected = <<-EXPECTED 111 112we don't worry too much. 113 114The comments associated with 115 EXPECTED 116 117 result = flush_left text 118 119 assert_equal expected, result 120 assert_equal Encoding::US_ASCII, result.encoding 121 end 122 123 def test_markup_string 124 out = markup('hi').gsub("\n", '') 125 126 assert_equal '<p>hi</p>', out 127 end 128 129 def test_markup_comment 130 out = markup(comment('hi')).gsub("\n", '') 131 132 assert_equal '<p>hi</p>', out 133 end 134 135 def test_normalize_comment_hash 136 text = <<-TEXT 137## 138# we don't worry too much. 139# 140# The comments associated with 141 TEXT 142 143 expected = <<-EXPECTED.rstrip 144we don't worry too much. 145 146The comments associated with 147 EXPECTED 148 149 assert_equal expected, normalize_comment(text) 150 end 151 152 def test_normalize_comment_stars_single_space 153 text = <<-TEXT 154/* 155 * we don't worry too much. 156 * 157 * The comments associated with 158 */ 159 TEXT 160 161 expected = <<-EXPECTED.rstrip 162we don't worry too much. 163 164The comments associated with 165 EXPECTED 166 167 assert_equal expected, normalize_comment(text) 168 end 169 170 def test_normalize_comment_stars_single_double_space 171 text = <<-TEXT 172/* 173 * we don't worry too much. 174 * 175 * The comments associated with 176 */ 177 TEXT 178 179 expected = <<-EXPECTED.rstrip 180we don't worry too much. 181 182The comments associated with 183 EXPECTED 184 185 assert_equal expected, normalize_comment(text) 186 end 187 188 def test_parse 189 assert_kind_of RDoc::Markup::Document, parse('hi') 190 end 191 192 def test_parse_comment 193 expected = RDoc::Markup::Document.new 194 expected.file = @top_level 195 196 c = comment '' 197 parsed = parse c 198 199 assert_equal expected, parsed 200 assert_same parsed, parse(c) 201 end 202 203 def test_parse_document 204 assert_equal RDoc::Markup::Document.new, parse(RDoc::Markup::Document.new) 205 end 206 207 def test_parse_empty 208 assert_equal RDoc::Markup::Document.new, parse('') 209 end 210 211 def test_parse_empty_newline 212 assert_equal RDoc::Markup::Document.new, parse("#\n") 213 end 214 215 def test_parse_format_markdown 216 expected = 217 @RM::Document.new( 218 @RM::Paragraph.new('it _works_')) 219 220 parsed = parse 'it *works*', 'markdown' 221 222 assert_equal expected, parsed 223 end 224 225 def test_parse_format_rd 226 expected = 227 @RM::Document.new( 228 @RM::Paragraph.new('it <em>works</em>')) 229 230 parsed = parse 'it ((*works*))', 'rd' 231 232 assert_equal expected, parsed 233 end 234 235 def test_parse_format_tomdoc 236 code = verb('1 + 1') 237 code.format = :ruby 238 239 expected = 240 doc( 241 para('It does a thing'), 242 blank_line, 243 head(3, 'Examples'), 244 blank_line, 245 code) 246 247 text = <<-TOMDOC 248It does a thing 249 250Examples 251 252 1 + 1 253 TOMDOC 254 255 parsed = parse text, 'tomdoc' 256 257 assert_equal expected, parsed 258 end 259 260 def test_parse_newline 261 assert_equal RDoc::Markup::Document.new, parse("\n") 262 end 263 264# def test_snippet 265# text = <<-TEXT 266#This is one-hundred characters or more of text in a single paragraph. This 267#paragraph will be cut off some point after the one-hundredth character. 268# TEXT 269# 270# expected = text.gsub(/\r?\n/, ' ').sub(/ some point.*/, '') 271# 272# assert_equal expected, snippet(text) 273# end 274# 275# def test_snippet_comment 276# c = comment 'This is a comment' 277# 278# assert_equal 'This is a comment', snippet(c) 279# end 280# 281# def test_snippet_no_space 282# text = <<-TEXT.strip 283#This is one-hundred characters or more of text in a single paragraph. This 284#paragraph will not be cut 285# TEXT 286# 287# expected = <<-EXPECTED.strip.gsub(/\r?\n/, ' ') 288#This is one-hundred characters or more of text in a single paragraph. This 289#paragraph will not be cut 290# EXPECTED 291# 292# assert_equal expected, snippet(text) 293# end 294# 295# def test_snippet_short 296# text = 'This is a comment' 297# 298# assert_equal text.dup, snippet(text) 299# end 300 301 def test_strip_hashes 302 text = <<-TEXT 303## 304# we don't worry too much. 305# 306# The comments associated with 307 TEXT 308 309 expected = <<-EXPECTED 310 311 we don't worry too much. 312 313 The comments associated with 314 EXPECTED 315 316 assert_equal expected, strip_hashes(text) 317 end 318 319 def test_strip_hashes_encoding 320 skip "Encoding not implemented" unless Object.const_defined? :Encoding 321 322 text = <<-TEXT 323## 324# we don't worry too much. 325# 326# The comments associated with 327 TEXT 328 329 text.force_encoding Encoding::CP852 330 331 expected = <<-EXPECTED 332 333 we don't worry too much. 334 335 The comments associated with 336 EXPECTED 337 338 stripped = strip_hashes text 339 340 assert_equal expected, stripped 341 assert_equal Encoding::CP852, stripped.encoding 342 end 343 344 def test_strip_newlines 345 assert_equal ' ', strip_newlines("\n \n") 346 347 assert_equal 'hi', strip_newlines("\n\nhi") 348 349 assert_equal 'hi', strip_newlines( "hi\n\n") 350 351 assert_equal 'hi', strip_newlines("\n\nhi\n\n") 352 end 353 354 def test_strip_newlines_encoding 355 skip "Encoding not implemented" unless Object.const_defined? :Encoding 356 357 assert_equal Encoding::UTF_8, ''.encoding, 'Encoding sanity check' 358 359 text = " \n" 360 text.force_encoding Encoding::US_ASCII 361 362 stripped = strip_newlines text 363 364 assert_equal ' ', stripped 365 366 assert_equal Encoding::US_ASCII, stripped.encoding 367 end 368 369 def test_strip_stars 370 text = <<-TEXT 371/* 372 * * we don't worry too much. 373 * 374 * The comments associated with 375 */ 376 TEXT 377 378 expected = <<-EXPECTED 379 380 * we don't worry too much. 381 382 The comments associated with 383 EXPECTED 384 385 assert_equal expected, strip_stars(text) 386 end 387 388 def test_strip_stars_document_method 389 text = <<-TEXT 390/* 391 * Document-method: Zlib::GzipFile#mtime= 392 * 393 * A comment 394 */ 395 TEXT 396 397 expected = <<-EXPECTED 398 399 A comment 400 EXPECTED 401 402 assert_equal expected, strip_stars(text) 403 end 404 405 def test_strip_stars_encoding 406 skip "Encoding not implemented" unless Object.const_defined? :Encoding 407 408 text = <<-TEXT 409/* 410 * * we don't worry too much. 411 * 412 * The comments associated with 413 */ 414 TEXT 415 416 text.force_encoding Encoding::CP852 417 418 expected = <<-EXPECTED 419 420 * we don't worry too much. 421 422 The comments associated with 423 EXPECTED 424 425 result = strip_stars text 426 427 assert_equal expected, result 428 assert_equal Encoding::CP852, result.encoding 429 end 430 431 def test_strip_stars_encoding2 432 skip "Encoding not implemented" unless Object.const_defined? :Encoding 433 434 text = <<-TEXT 435/* 436 * * we don't worry too much. 437 * 438 * The comments associated with 439 */ 440 TEXT 441 442 text.force_encoding Encoding::BINARY 443 444 expected = <<-EXPECTED 445 446 * we don't worry too much. 447 448 The comments associated with 449 EXPECTED 450 451 result = strip_stars text 452 453 assert_equal expected, result 454 assert_equal Encoding::BINARY, result.encoding 455 end 456 457 def test_strip_stars_no_stars 458 text = <<-TEXT 459* we don't worry too much. 460 461The comments associated with 462 463 TEXT 464 465 expected = <<-EXPECTED 466* we don't worry too much. 467 468The comments associated with 469 470 EXPECTED 471 472 assert_equal expected, strip_stars(text) 473 end 474 475 def test_to_html_apostrophe 476 assert_equal '���a', to_html("'a") 477 assert_equal 'a���', to_html("a'") 478 479 assert_equal '���a��� ���', to_html("'a' '") 480 end 481 482 def test_to_html_backslash 483 assert_equal 'S', to_html('\\S') 484 end 485 486 def test_to_html_br 487 assert_equal '<br>', to_html('<br>') 488 end 489 490 def test_to_html_copyright 491 assert_equal '��', to_html('(c)') 492 end 493 494 def test_to_html_dash 495 assert_equal '-', to_html('-') 496 assert_equal '���', to_html('--') 497 assert_equal '���', to_html('---') 498 assert_equal '���-', to_html('----') 499 end 500 501 def test_to_html_double_backtick 502 assert_equal '���a', to_html('``a') 503 assert_equal '���a���', to_html('``a``') 504 end 505 506 def test_to_html_double_quote 507 assert_equal '���a', to_html('"a') 508 assert_equal '���a���', to_html('"a"') 509 end 510 511 def test_to_html_double_quote_quot 512 assert_equal '���a', to_html('"a') 513 assert_equal '���a���', to_html('"a"') 514 end 515 516 def test_to_html_double_tick 517 assert_equal '���a', to_html("''a") 518 assert_equal '���a���', to_html("''a''") 519 end 520 521 def test_to_html_ellipsis 522 assert_equal '..', to_html('..') 523 assert_equal '���', to_html('...') 524 assert_equal '.���', to_html('....') 525 end 526 527 def test_to_html_encoding 528 skip "Encoding not implemented" unless Object.const_defined? :Encoding 529 530 s = '...(c)'.encode Encoding::Shift_JIS 531 532 html = to_html s 533 534 assert_equal Encoding::Shift_JIS, html.encoding 535 536 expected = '���(c)'.encode Encoding::Shift_JIS 537 538 assert_equal expected, html 539 end 540 541 def test_to_html_html_tag 542 assert_equal '<a href="http://example">hi���s</a>', 543 to_html('<a href="http://example">hi\'s</a>') 544 end 545 546 def test_to_html_registered_trademark 547 assert_equal '��', to_html('(r)') 548 end 549 550 def test_to_html_tt_tag 551 assert_equal '<tt>hi\'s</tt>', to_html('<tt>hi\'s</tt>') 552 assert_equal '<tt>hi\\\'s</tt>', to_html('<tt>hi\\\\\'s</tt>') 553 end 554 555 def test_to_html_tt_tag_mismatch 556 _, err = verbose_capture_io do 557 assert_equal '<tt>hi', to_html('<tt>hi') 558 end 559 560 assert_equal "mismatched <tt> tag\n", err 561 end 562 563 def formatter() 564 RDoc::Markup::ToHtml.new @options 565 end 566 567end 568 569