1require 'rdoc/test_case'
2
3class TestRDocRdBlockParser < RDoc::TestCase
4
5  def setup
6    super
7
8    @block_parser = RDoc::RD::BlockParser.new
9  end
10
11  def mu_pp(obj)
12    s = ""
13    s = PP.pp obj, s
14    s = s.force_encoding(Encoding.default_external) if defined? Encoding
15    s.chomp
16  end
17
18  def test_add_footnote
19    index = @block_parser.add_footnote 'context'
20
21    assert_equal 1, index
22
23    expected = [
24      para('{^1}[rdoc-label:footmark-1:foottext-1]', ' ', 'context'),
25      blank_line,
26    ]
27
28    assert_equal expected, @block_parser.footnotes
29
30    index = @block_parser.add_footnote 'other'
31
32    assert_equal 2, index
33  end
34
35  def test_parse_desclist
36    list = <<-LIST
37:one
38  desc one
39:two
40  desc two
41    LIST
42
43    expected =
44      doc(
45        list(:NOTE,
46          item("one", para("desc one")),
47          item("two", para("desc two"))))
48
49    assert_equal expected, parse(list)
50  end
51
52  def test_parse_enumlist
53    list = <<-LIST
54(1) one
55(1) two
56    LIST
57
58    expected =
59      doc(
60        list(:NUMBER,
61          item(nil, para("one")),
62          item(nil, para("two"))))
63
64    assert_equal expected, parse(list)
65  end
66
67  def test_parse_enumlist_paragraphs
68    list = <<-LIST
69(1) one
70
71    two
72    LIST
73
74    expected =
75      doc(
76        list(:NUMBER,
77          item(nil,
78            para("one"),
79            para("two"))))
80
81    assert_equal expected, parse(list)
82  end
83
84  def test_parse_enumlist_multiline
85    list = <<-LIST
86(1) one
87    two
88    LIST
89
90    contents = "one\n     two" # 1.8 vs 1.9
91
92    expected =
93      doc(
94        list(:NUMBER,
95          item(nil, para(*contents))))
96
97    assert_equal expected, parse(list)
98  end
99
100  def test_parse_enumlist_verbatim
101    list = <<-LIST
102(1) item
103      verbatim
104    LIST
105
106    expected =
107      doc(
108        list(:NUMBER,
109          item(nil,
110            para("item"),
111            verb("verbatim\n"))))
112
113    assert_equal expected, parse(list)
114  end
115
116  def test_parse_enumlist_verbatim_continue
117    list = <<-LIST
118(1) one
119      verbatim
120    two
121    LIST
122
123    expected =
124      doc(
125        list(:NUMBER,
126          item(nil,
127            para("one"),
128            verb("verbatim\n"),
129            para("two"))))
130
131    assert_equal expected, parse(list)
132  end
133
134  def test_parse_footnote
135    expected =
136      doc(
137        para("{*1}[rdoc-label:foottext-1:footmark-1]"),
138        rule(1),
139        para("{^1}[rdoc-label:footmark-1:foottext-1]", " ", "text"),
140        blank_line)
141
142    assert_equal expected, parse("((-text-))")
143  end
144
145  def test_parse_include
146    @block_parser.include_path = [Dir.tmpdir]
147
148    expected = doc(@RM::Include.new("parse_include", [Dir.tmpdir]))
149
150    assert_equal expected, parse("<<< parse_include")
151  end
152
153  def test_parse_include_subtree
154    @block_parser.include_path = [Dir.tmpdir]
155
156    expected =
157      doc(
158        blank_line,
159        para("include <em>worked</em>"),
160        blank_line,
161        blank_line)
162
163    Tempfile.open %w[parse_include .rd] do |io|
164      io.puts "=begin\ninclude ((*worked*))\n=end"
165      io.flush
166
167      str = <<-STR
168<<< #{File.basename io.path}
169      STR
170
171      assert_equal expected, parse(str)
172    end
173  end
174
175  def test_parse_heading
176    assert_equal doc(head(1, "H")), parse("= H")
177    assert_equal doc(head(2, "H")), parse("== H")
178    assert_equal doc(head(3, "H")), parse("=== H")
179    assert_equal doc(head(4, "H")), parse("==== H")
180    assert_equal doc(head(5, "H")), parse("+ H")
181    assert_equal doc(head(6, "H")), parse("++ H")
182  end
183
184  def test_parse_itemlist
185    list = <<-LIST
186* one
187* two
188    LIST
189
190    expected =
191      doc(
192        list(:BULLET,
193          item(nil, para("one")),
194          item(nil, para("two"))))
195
196    assert_equal expected, parse(list)
197  end
198
199  def test_parse_itemlist_multiline
200    list = <<-LIST
201* one
202  two
203    LIST
204
205    contents = "one\n   two" # 1.8 vs 1.9
206
207    expected =
208      doc(
209        list(:BULLET,
210          item(nil, para(*contents))))
211
212    assert_equal expected, parse(list)
213  end
214
215  def test_parse_itemlist_nest
216    list = <<-LIST
217* one
218  * inner
219* two
220    LIST
221
222    expected =
223      doc(
224        list(:BULLET,
225          item(nil,
226            para("one"),
227            list(:BULLET,
228              item(nil, para("inner")))),
229          item(nil,
230            para("two"))))
231
232    assert_equal expected, parse(list)
233  end
234
235  def test_parse_itemlist_paragraphs
236    list = <<-LIST
237* one
238
239  two
240    LIST
241
242    expected =
243      doc(
244        list(:BULLET,
245          item(nil,
246            para("one"),
247            para("two"))))
248
249    assert_equal expected, parse(list)
250  end
251
252  def test_parse_itemlist_verbatim
253    list = <<-LIST
254* item
255    verbatim
256    LIST
257
258    expected =
259      doc(
260        list(:BULLET,
261          item(nil,
262            para("item"),
263            verb("verbatim\n"))))
264
265    assert_equal expected, parse(list)
266  end
267
268  def test_parse_itemlist_verbatim_continue
269    list = <<-LIST
270* one
271    verbatim
272  two
273    LIST
274
275    expected =
276      doc(
277        list(:BULLET,
278          item(nil,
279            para("one"),
280            verb("verbatim\n"),
281            para("two"))))
282
283    assert_equal expected, parse(list)
284  end
285
286  def test_parse_lists
287    list = <<-LIST
288(1) one
289(1) two
290* three
291* four
292(1) five
293(1) six
294    LIST
295
296    expected =
297      doc(
298        list(:NUMBER,
299          item(nil, para("one")),
300          item(nil, para("two"))),
301        list(:BULLET,
302          item(nil, para("three")),
303          item(nil, para("four"))),
304        list(:NUMBER,
305          item(nil, para("five")),
306          item(nil, para("six"))))
307
308    assert_equal expected, parse(list)
309  end
310
311  def test_parse_lists_nest
312    list = <<-LIST
313(1) one
314(1) two
315      * three
316      * four
317(1) five
318(1) six
319    LIST
320
321    expected =
322      doc(
323        list(:NUMBER,
324          item(nil, para("one")),
325          item(nil,
326            para("two"),
327            list(:BULLET,
328              item(nil, para("three")),
329              item(nil, para("four")))),
330          item(nil, para("five")),
331          item(nil, para("six"))))
332
333    assert_equal expected, parse(list)
334  end
335
336  def test_parse_lists_nest_verbatim
337    list = <<-LIST
338(1) one
339(1) two
340      * three
341      * four
342     verbatim
343(1) five
344(1) six
345    LIST
346
347    expected =
348      doc(
349        list(:NUMBER,
350          item(nil, para("one")),
351          item(nil,
352            para("two"),
353            list(:BULLET,
354              item(nil, para("three")),
355              item(nil, para("four"))),
356            verb("verbatim\n")),
357          item(nil, para("five")),
358          item(nil, para("six"))))
359
360    assert_equal expected, parse(list)
361  end
362
363  def test_parse_lists_nest_verbatim2
364    list = <<-LIST
365(1) one
366(1) two
367      * three
368      * four
369      verbatim
370(1) five
371(1) six
372    LIST
373
374    expected =
375      doc(
376        list(:NUMBER,
377          item(nil, para("one")),
378          item(nil,
379            para("two"),
380            list(:BULLET,
381              item(nil, para("three")),
382              item(nil, para("four"))),
383            verb("verbatim\n")),
384          item(nil, para("five")),
385          item(nil, para("six"))))
386
387    assert_equal expected, parse(list)
388  end
389
390  def test_parse_methodlist
391    list = <<-LIST
392--- Array#each {|i| ... }
393      yield block for each item.
394--- Array#index(val)
395      return index of first item which equals with val. if it hasn't
396      same item, return nil.
397    LIST
398
399    expected =
400      doc(
401        list(:LABEL,
402          item(
403            "<tt>Array#each {|i| ... }</tt>",
404            para("yield block for each item.")),
405          item(
406            "<tt>Array#index(val)</tt>",
407            para("return index of first item which equals with val. if it hasn't same item, return nil."))))
408
409    assert_equal expected, parse(list)
410  end
411
412  def test_parse_methodlist_empty
413    list = <<-LIST
414--- A#b
415
416    LIST
417
418    expected =
419      doc(
420        list(:LABEL,
421          item("<tt>A#b</tt>")))
422
423    assert_equal expected, parse(list)
424  end
425
426  def test_parse_methodlist_paragraph
427    list = <<-LIST
428--- A#b
429
430    one
431    LIST
432
433    expected =
434      doc(
435        list(:LABEL,
436          item(
437            "<tt>A#b</tt>",
438            para("one"))))
439
440    assert_equal expected, parse(list)
441  end
442
443  def test_parse_methodlist_paragraph2
444    list = <<-LIST.chomp
445--- A#b
446
447    one
448two
449    LIST
450
451    expected =
452      doc(
453        list(:LABEL,
454          item(
455            "<tt>A#b</tt>",
456            para("one"))),
457        para("two"))
458
459    assert_equal expected, parse(list)
460  end
461
462  def test_parse_methodlist_paragraph_verbatim
463    list = <<-LIST.chomp
464--- A#b
465
466    text
467      verbatim
468    LIST
469
470    expected =
471      doc(
472        list(:LABEL,
473          item(
474            "<tt>A#b</tt>",
475            para("text"),
476            verb("verbatim\n"))))
477
478    assert_equal expected, parse(list)
479  end
480
481  def test_parse_verbatim
482    assert_equal doc(verb("verbatim\n")), parse("  verbatim")
483  end
484
485  def test_parse_verbatim_blankline
486    expected = doc(verb("one\n", "\n", "two\n"))
487
488    verbatim = <<-VERBATIM
489  one
490
491  two
492    VERBATIM
493
494    assert_equal expected, parse(verbatim)
495  end
496
497  def test_parse_verbatim_indent
498    expected = doc(verb("one\n", " two\n"))
499
500    verbatim = <<-VERBATIM
501  one
502   two
503    VERBATIM
504
505    assert_equal expected, parse(verbatim)
506  end
507
508  def test_parse_verbatim_multi
509    expected = doc(verb("one\n", "two\n"))
510
511    verbatim = <<-VERBATIM
512  one
513  two
514    VERBATIM
515
516    assert_equal expected, parse(verbatim)
517  end
518
519  def test_parse_textblock
520    assert_equal doc(para("text")), parse("text")
521  end
522
523  def test_parse_textblock_multi
524    expected = doc(para("one two"))
525
526    assert_equal expected, parse("one\ntwo")
527  end
528
529  def parse text
530    text = ["=begin", text, "=end"].join "\n"
531
532    doc = @block_parser.parse text.lines.to_a
533
534    assert_equal blank_line, doc.parts.shift, "=begin blankline"
535    assert_equal blank_line, doc.parts.pop, "=end blankline"
536
537    doc
538  end
539
540end
541
542