summaryrefslogtreecommitdiff
path: root/test/rdoc/test_rdoc_markup_to_html_snippet.rb
blob: f861db18499d53204760b4edbceb763637b952a0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
require 'rdoc/test_case'

class TestRDocMarkupToHtmlSnippet < RDoc::Markup::FormatterTestCase

  add_visitor_tests

  def setup
    super

    @to = RDoc::Markup::ToHtmlSnippet.new @options, 100, 100
    @ellipsis = @to.to_html '...'
  end

  def accept_blank_line
    assert_empty @to.res.join
  end

  def accept_block_quote
    assert_equal "\n<blockquote><p>quote\n</blockquote>\n", @to.res.join

    assert_equal 5, @to.characters
  end

  def accept_document
    assert_equal "<p>hello\n", @to.res.join
    assert_equal 5, @to.characters
  end

  def accept_heading
    assert_equal "<p>Hello\n", @to.res.join
    assert_equal 5, @to.characters
  end

  def accept_heading_1
    assert_equal "<p>Hello\n", @to.res.join
    assert_equal 5, @to.characters
  end

  def accept_heading_2
    assert_equal "<p>Hello\n", @to.res.join
    assert_equal 5, @to.characters
  end

  def accept_heading_3
    assert_equal "<p>Hello\n", @to.res.join
    assert_equal 5, @to.characters
  end

  def accept_heading_4
    assert_equal "<p>Hello\n", @to.res.join
    assert_equal 5, @to.characters
  end

  def accept_heading_b
    assert_equal "<p><strong>Hello</strong>\n",
                 @to.res.join
    assert_equal 5, @to.characters
  end

  def accept_heading_suppressed_crossref
    assert_equal "<p>Hello\n", @to.res.join
    assert_equal 5, @to.characters
  end

  def accept_list_end_bullet
    assert_equal [], @to.list
    assert_equal [], @to.in_list_entry

    assert_equal "\n", @to.res.join
    assert_equal 0, @to.characters
  end

  def accept_list_end_label
    assert_equal [], @to.list
    assert_equal [], @to.in_list_entry

    assert_equal "\n", @to.res.join
    assert_equal 0, @to.characters
  end

  def accept_list_end_lalpha
    assert_equal [], @to.list
    assert_equal [], @to.in_list_entry

    assert_equal "\n", @to.res.join
    assert_equal 0, @to.characters
  end

  def accept_list_end_number
    assert_equal [], @to.list
    assert_equal [], @to.in_list_entry

    assert_equal "\n", @to.res.join
    assert_equal 0, @to.characters
  end

  def accept_list_end_note
    assert_equal [], @to.list
    assert_equal [], @to.in_list_entry

    assert_equal "\n", @to.res.join
    assert_equal 0, @to.characters
  end

  def accept_list_end_ualpha
    assert_equal [], @to.list
    assert_equal [], @to.in_list_entry

    assert_equal "\n", @to.res.join
    assert_equal 0, @to.characters
  end

  def accept_list_item_end_bullet
    assert_equal [''], @to.in_list_entry
    assert_equal 0, @to.characters
  end

  def accept_list_item_end_label
    assert_equal [''], @to.in_list_entry
    assert_equal 4, @to.characters
  end

  def accept_list_item_end_lalpha
    assert_equal [''], @to.in_list_entry
    assert_equal 0, @to.characters
  end

  def accept_list_item_end_note
    assert_equal [''], @to.in_list_entry
    assert_equal 4, @to.characters
  end

  def accept_list_item_end_number
    assert_equal [''], @to.in_list_entry
    assert_equal 0, @to.characters
  end

  def accept_list_item_end_ualpha
    assert_equal [''], @to.in_list_entry
    assert_equal 0, @to.characters
  end

  def accept_list_item_start_bullet
    assert_equal "<p>", @to.res.join
    assert_equal 0, @to.characters
  end

  def accept_list_item_start_label
    assert_equal "<p>cat &mdash; ", @to.res.join
    assert_equal 4, @to.characters
  end

  def accept_list_item_start_lalpha
    assert_equal "<p>", @to.res.join
    assert_equal 0, @to.characters
  end

  def accept_list_item_start_note
    assert_equal "<p>cat &mdash; ",
                 @to.res.join
    assert_equal 4, @to.characters
  end

  def accept_list_item_start_note_2
    expected = <<-EXPECTED
<p><code>teletype</code> &mdash; teletype description

    EXPECTED

    assert_equal expected, @to.res.join
    assert_equal 29, @to.characters
  end

  def accept_list_item_start_note_multi_description
    expected = <<-EXPECTED
<p>label &mdash; description one
<p>description two

    EXPECTED

    assert_equal expected, @to.res.join
    assert_equal 37, @to.characters
  end

  def accept_list_item_start_note_multi_label
    expected = <<-EXPECTED
<p>one, two &mdash; two headers

    EXPECTED

    assert_equal expected, @to.res.join
    assert_equal 18, @to.characters
  end

  def accept_list_item_start_number
    assert_equal "<p>", @to.res.join
    assert_equal 0, @to.characters
  end

  def accept_list_item_start_ualpha
    assert_equal "<p>", @to.res.join
    assert_equal 0, @to.characters
  end

  def accept_list_start_bullet
    assert_equal [:BULLET], @to.list
    assert_equal [''], @to.in_list_entry

    assert_equal '', @to.res.join
    assert_equal 0, @to.characters
  end

  def accept_list_start_label
    assert_equal [:LABEL], @to.list
    assert_equal [''], @to.in_list_entry

    assert_equal '', @to.res.join
    assert_equal 0, @to.characters
  end

  def accept_list_start_lalpha
    assert_equal [:LALPHA], @to.list
    assert_equal [''], @to.in_list_entry

    assert_equal '', @to.res.join
    assert_equal 0, @to.characters
  end

  def accept_list_start_note
    assert_equal [:NOTE], @to.list
    assert_equal [''], @to.in_list_entry

    assert_equal '', @to.res.join
    assert_equal 0, @to.characters
  end

  def accept_list_start_number
    assert_equal [:NUMBER], @to.list
    assert_equal [''], @to.in_list_entry

    assert_equal '', @to.res.join
    assert_equal 0, @to.characters
  end

  def accept_list_start_ualpha
    assert_equal [:UALPHA], @to.list
    assert_equal [''], @to.in_list_entry

    assert_equal '', @to.res.join
    assert_equal 0, @to.characters
  end

  def accept_paragraph
    assert_equal "<p>hi\n", @to.res.join

    assert_equal 2, @to.characters
  end

  def accept_paragraph_b
    assert_equal "<p>reg <strong>bold words</strong> reg\n", @to.res.join

    assert_equal 18, @to.characters
  end

  def accept_paragraph_br
    assert_equal "<p>one<br>two\n", @to.res.join

    assert_equal 6, @to.characters
  end

  def accept_paragraph_break
    assert_equal "<p>hello<br>\nworld\n", @to.res.join

    assert_equal 11, @to.characters
  end

  def accept_paragraph_i
    assert_equal "<p>reg <em>italic words</em> reg\n", @to.res.join

    assert_equal 20, @to.characters
  end

  def accept_paragraph_plus
    assert_equal "<p>reg <code>teletype</code> reg\n", @to.res.join

    assert_equal 16, @to.characters
  end

  def accept_paragraph_star
    assert_equal "<p>reg <strong>bold</strong> reg\n", @to.res.join

    assert_equal 12, @to.characters
  end

  def accept_paragraph_underscore
    assert_equal "<p>reg <em>italic</em> reg\n", @to.res.join

    assert_equal 14, @to.characters
  end

  def accept_raw
    assert_equal '', @to.res.join
    assert_equal 0, @to.characters
  end

  def accept_rule
    assert_empty @to.res
    assert_equal 0, @to.characters
  end

  def accept_verbatim
    assert_equal "\n<pre class=\"ruby\"><span class=\"ruby-identifier\">hi</span>\n  <span class=\"ruby-identifier\">world</span>\n</pre>\n", @to.res.join
    assert_equal 10, @to.characters
  end

  def end_accepting
    assert_equal 'hi', @to.res.join
  end

  def start_accepting
    assert_equal [], @to.res
    assert_equal [], @to.in_list_entry
    assert_equal [], @to.list
    assert_equal 0,  @to.characters
  end

  def list_nested
    expected = <<-EXPECTED
<p>l1
<p>l1.1

<p>l2

    EXPECTED

    assert_equal expected, @to.res.join
    assert_equal 8, @to.characters
  end

  def list_verbatim
    expected = <<-EXPECTED
<p>list stuff

<pre>* list
  with

  second

  1. indented
  2. numbered

  third

* second</pre>

    EXPECTED

    assert_equal expected, @to.end_accepting
    assert_equal 81, @to.characters
  end

  def test_accept_heading_7
    @to.start_accepting

    @to.accept_heading @RM::Heading.new(7, 'Hello')

    assert_equal "<p>Hello\n", @to.res.join
    assert_equal 5, @to.characters
  end

  def test_accept_heading_aref_class
    @to.code_object = RDoc::NormalClass.new 'Foo'
    @to.start_accepting

    @to.accept_heading @RM::Heading.new(1, 'Hello')

    assert_equal "<p>Hello\n",
                 @to.res.join
    assert_equal 5, @to.characters
  end

  def test_accept_heading_aref_method
    @to.code_object = RDoc::AnyMethod.new nil, 'foo'
    @to.start_accepting

    @to.accept_heading @RM::Heading.new(1, 'Hello')

    assert_equal "<p>Hello\n",
                 @to.res.join
    assert_equal 5, @to.characters
  end

  def test_accept_verbatim_ruby
    options = RDoc::Options.new
    rdoc = RDoc::RDoc.new
    rdoc.options = options
    RDoc::RDoc.current = rdoc

    verb = @RM::Verbatim.new("class C\n", "end\n")

    @to.start_accepting
    @to.accept_verbatim verb

    expected = <<-EXPECTED

<pre class="ruby"><span class="ruby-keyword">class</span> <span class="ruby-constant">C</span>
<span class="ruby-keyword">end</span>
</pre>
    EXPECTED

    assert_equal expected, @to.res.join
    assert_equal 11, @to.characters
  end

  def test_accept_verbatim_ruby_error
    options = RDoc::Options.new
    rdoc = RDoc::RDoc.new
    rdoc.options = options
    RDoc::RDoc.current = rdoc

    verb = @RM::Verbatim.new("a % 09 # => blah\n")

    @to.start_accepting
    @to.accept_verbatim verb

    inner = CGI.escapeHTML "a % 09 # => blah"

    expected = <<-EXPECTED

<pre>#{inner}</pre>
    EXPECTED

    assert_equal expected, @to.res.join
    assert_equal 16, @to.characters
  end

  def test_add_paragraph
    @to = RDoc::Markup::ToHtmlSnippet.new @options, 0, 3
    assert_throws :done do
      @to.add_paragraph
      @to.add_paragraph
      @to.add_paragraph
    end

    assert_equal 3, @to.paragraphs
  end

  def test_convert_limit
    rdoc = <<-RDOC
= Hello

This is some text, it *will* be cut off after 100 characters and an ellipsis
must follow

So there you have it
    RDOC

    expected = <<-EXPECTED
<p>Hello
<p>This is some text, it <strong>will</strong> be cut off after 100 characters
and an ellipsis must follow
<p>So there you #{@ellipsis}
    EXPECTED

    actual = @to.convert rdoc

    assert_equal expected, actual
    assert_equal 111, @to.characters, 'snippet character length'
  end

  def test_convert_limit_2
    rdoc = <<-RDOC
Outputs formatted RI data for the class or method +name+.

Returns true if +name+ was found, false if it was not an alternative could
be guessed, raises an error if +name+ couldn't be guessed.
    RDOC

    expected = <<-EXPECTED
<p>Outputs formatted RI data for the class or method <code>name</code>.
<p>Returns true if <code>name</code> was found, false if it was #{@ellipsis}
    EXPECTED

    actual = @to.convert rdoc

    assert_equal expected, actual
    assert_equal 159, @to.characters, 'snippet character length'
  end

  def test_convert_limit_paragraphs
    @to = RDoc::Markup::ToHtmlSnippet.new @options, 100, 3

    rdoc = <<-RDOC
= \RDoc - Ruby Documentation System

* {RDoc Project Page}[https://github.com/rdoc/rdoc/]
* {RDoc Documentation}[http://docs.seattlerb.org/rdoc]
* {RDoc Bug Tracker}[https://github.com/rdoc/rdoc/issues]

== DESCRIPTION:

RDoc produces HTML and command-line documentation for Ruby projects.  RDoc
includes the +rdoc+ and +ri+ tools for generating and displaying online
documentation.

See RDoc for a description of RDoc's markup and basic use.
    RDOC

    expected = <<-EXPECTED
<p>RDoc - Ruby Documentation System
<p>RDoc Project Page
<p>RDoc Documentation
    EXPECTED

    actual = @to.convert rdoc

    assert_equal expected, actual
    assert_equal 67, @to.characters
  end

  def test_convert_limit_in_tag
    @to = RDoc::Markup::ToHtmlSnippet.new @options, 4
    rdoc = "* ab *c* d\n"

    expected = "<p>ab <strong>c</strong> #{@ellipsis}\n\n"

    actual = @to.convert rdoc

    assert_equal 4, @to.characters
    assert_equal expected, actual
  end

  def test_convert_limit_verbatim
    rdoc = <<-RDOC
= Hello There

This is some text, it *will* be cut off after 100 characters

  This one is cut off in this verbatim section
    RDOC

    expected = <<-EXPECTED
<p>Hello There
<p>This is some text, it <strong>will</strong> be cut off after 100 characters

<pre>This one is cut off in this verbatim ...</pre>
    EXPECTED

    actual = @to.convert rdoc

    assert_equal expected, actual
    assert_equal 113, @to.characters
  end

  def test_convert_limit_verbatim_2
    rdoc = <<-RDOC
Extracts the class, selector and method name parts from +name+ like
Foo::Bar#baz.

NOTE: Given Foo::Bar, Bar is considered a class even though it may be a
      method
    RDOC

    expected = <<-EXPECTED
<p>Extracts the class, selector and method name parts from <code>name</code>
like Foo::Bar#baz.
<p>NOTE: Given Foo::Bar, #{@ellipsis}
    EXPECTED

    actual = @to.convert rdoc

    assert_equal expected, actual
    assert_equal 101, @to.characters
  end

  def test_convert_limit_verbatim_multiline
    rdoc = <<-RDOC
Look for directives in a normal comment block:

  # :stopdoc:
  # Don't display comment from this point forward

This routine modifies its +comment+ parameter.
    RDOC

    inner = CGI.escapeHTML "# Don't display comment from this point forward"
    expected = <<-EXPECTED
<p>Look for directives in a normal comment block:

<pre class=\"ruby\"><span class=\"ruby-comment\"># :stopdoc:</span>
<span class=\"ruby-comment\">#{inner}</span>
</pre>
    EXPECTED

    actual = @to.convert rdoc

    assert_equal expected, actual
    assert_equal 105, @to.characters
  end

  def test_convert_limit_over
    @to = RDoc::Markup::ToHtmlSnippet.new @options, 4
    rdoc = "* text\n" * 2

    expected = "<p>text\n"
    expected.chomp!
    expected << " #{@ellipsis}\n"

    actual = @to.convert rdoc

    assert_equal 4, @to.characters
    assert_equal expected, actual
  end

  def test_convert_string
    assert_equal '&lt;&gt;', @to.convert_string('<>')
  end

  def test_convert_RDOCLINK_label_label
    result = @to.convert 'rdoc-label:label-One'

    assert_equal "<p>One\n", result
    assert_equal 3, @to.characters
  end

  def test_convert_RDOCLINK_label_foottext
    result = @to.convert 'rdoc-label:foottext-1'

    assert_equal "<p>1\n", result
    assert_equal 1, @to.characters
  end

  def test_convert_RDOCLINK_label_footmark
    result = @to.convert 'rdoc-label:footmark-1'

    assert_equal "<p>1\n", result
    assert_equal 1, @to.characters
  end

  def test_convert_RDOCLINK_ref
    result = @to.convert 'rdoc-ref:C'

    assert_equal "<p>C\n", result
    assert_equal 1, @to.characters
  end

  def test_convert_TIDYLINK_rdoc_label
    result = @to.convert '{foo}[rdoc-label:foottext-1]'

    assert_equal "<p>foo\n", result
    assert_equal 3, @to.characters
  end

  def test_handle_special_HYPERLINK_link
    special = RDoc::Markup::Special.new 0, 'link:README.txt'

    link = @to.handle_special_HYPERLINK special

    assert_equal 'README.txt', link
  end

  def test_list_verbatim_2
    str = "* one\n    verb1\n    verb2\n* two\n"

    expected = <<-EXPECTED
<p>one

<pre class=\"ruby\"><span class=\"ruby-identifier\">verb1</span>
<span class=\"ruby-identifier\">verb2</span>
</pre>
<p>two

    EXPECTED

    assert_equal expected, @m.convert(str, @to)
    assert_equal 17, @to.characters
  end

  def test_on_tags
    on = RDoc::Markup::AttrChanger.new 2, 0

    @to.on_tags [], on

    assert_equal 2, @to.mask
  end

  def test_off_tags
    on  = RDoc::Markup::AttrChanger.new 2, 0
    off = RDoc::Markup::AttrChanger.new 0, 2

    @to.on_tags  [], on
    @to.off_tags [], off

    assert_equal 0, @to.mask
  end

  def test_to_html
    assert_equal "<p><code>--</code>\n", util_format("<tt>--</tt>")
    assert_equal 2, @to.characters
  end

  def util_format text
    paragraph = RDoc::Markup::Paragraph.new text

    @to.start_accepting
    @to.accept_paragraph paragraph
    @to.end_accepting
  end

end