1# frozen_string_literal: true
2require 'minitest_helper'
3
4class TestRDocTomDoc < RDoc::TestCase
5
6  def setup
7    super
8
9    @top_level = @store.add_file 'file.rb'
10
11    @TD = RDoc::TomDoc
12    @td = @TD.new
13  end
14
15  def test_class_add_post_processor
16    RDoc::TomDoc.add_post_processor
17
18    pp = RDoc::Markup::PreProcess.new __FILE__, []
19
20    text = "# Public: Do some stuff\n"
21
22    comment = RDoc::Comment.new text, nil
23    comment.format = 'tomdoc'
24
25    parent = RDoc::Context.new
26
27    pp.handle comment, parent
28
29    method = parent.add_method RDoc::AnyMethod.new(nil, 'm')
30
31    assert_equal 'Public', method.section.title
32    assert_equal "# Do some stuff\n", comment.text
33  end
34
35  def test_class_signature
36    c = comment <<-COMMENT
37Signature
38
39  method_<here>(args)
40
41here - something
42    COMMENT
43    c.format = 'tomdoc'
44
45    signature = @TD.signature c
46
47    assert_equal "method_<here>(args)\n", signature
48  end
49
50  def test_class_signature_no_space
51    c = comment <<-COMMENT
52Signature
53  method_<here>(args)
54
55here - something
56    COMMENT
57    c.format = 'tomdoc'
58
59    signature = @TD.signature c
60
61    assert_equal "method_<here>(args)\n", signature
62
63    expected =
64      doc(
65        head(3, 'Signature'),
66        list(:NOTE,
67          item(%w[here],
68            para('something'))))
69    expected.file = @top_level
70
71    assert_equal expected, c.parse
72  end
73
74  def test_class_signature_none
75    c = comment ''
76    c.format = 'tomdoc'
77
78    assert_nil @TD.signature c
79  end
80
81  def test_class_rdoc
82    c = comment <<-COMMENT
83=== Signature
84
85  method_<here>(args)
86
87here - something
88    COMMENT
89    c.format = 'rdoc'
90
91    signature = @TD.signature c
92
93    assert_nil signature
94  end
95
96  def test_class_signature_two_space
97    c = comment <<-COMMENT
98Signature
99
100
101  method_<here>(args)
102
103here - something
104    COMMENT
105    c.format = 'tomdoc'
106
107    signature = @TD.signature c
108
109    assert_equal "method_<here>(args)\n", signature
110
111    expected =
112      doc(
113        head(3, 'Signature'),
114        list(:NOTE,
115          item(%w[here],
116            para('something'))))
117    expected.file = @top_level
118
119    assert_equal expected, c.parse
120  end
121
122  def test_parse_paragraph
123    text = "Public: Do some stuff\n"
124
125    expected =
126      @RM::Document.new(
127        @RM::Paragraph.new('Do some stuff'))
128
129    assert_equal expected, @TD.parse(text)
130  end
131
132  def test_parse_multiline_paragraph
133    text = "Public: Do some stuff\n"
134    text += "On a new line\n"
135
136    expected =
137      doc(
138        para('Do some stuff', ' ', 'On a new line'))
139
140    assert_equal expected, @TD.parse(text)
141  end
142
143  def test_parse_arguments
144    text = <<-TEXT
145Create new Arg object.
146
147name        - name of argument
148description - arguments description
149    TEXT
150
151    expected =
152      doc(
153        para('Create new Arg object.'),
154        blank_line,
155        list(:NOTE,
156          item(%w[name],
157            para('name of argument')),
158          item(%w[description],
159            para('arguments description'))))
160
161    assert_equal expected, @TD.parse(text)
162  end
163
164  def test_parse_arguments_array
165    text = <<-TEXT
166Create new Arg object.
167
168names[] - names of arguments
169    TEXT
170
171    expected =
172      doc(
173        para('Create new Arg object.'),
174        blank_line,
175        list(:NOTE,
176          item(%w[names[]],
177            para('names of arguments'))))
178
179    assert_equal expected, @TD.parse(text)
180  end
181
182  def test_parse_arguments_multiline
183    text = <<-TEXT
184Do some stuff
185
186foo - A comment goes here
187  and is more than one line
188    TEXT
189
190    expected =
191      doc(
192        para('Do some stuff'),
193        blank_line,
194        list(:NOTE,
195          item(%w[foo],
196            para('A comment goes here', ' ', 'and is more than one line'))))
197
198    assert_equal expected, @TD.parse(text)
199  end
200
201  def test_parse_arguments_nested
202    text = <<-TEXT
203Do some stuff
204
205foo - A comment goes here
206      :bar - bar documentation
207    TEXT
208
209    expected =
210      doc(
211        para('Do some stuff'),
212        blank_line,
213        list(:NOTE,
214          item(%w[foo],
215            para('A comment goes here'),
216            list(:NOTE,
217              item(%w[:bar],
218                para('bar documentation'))))))
219
220    assert_equal expected, @TD.parse(text)
221  end
222
223  def test_parse_examples
224    text = <<-TEXT
225Do some stuff
226
227Examples
228
229  1 + 1
230    TEXT
231
232    code = verb("1 + 1\n")
233    code.format = :ruby
234
235    expected =
236      doc(
237        para('Do some stuff'),
238        blank_line,
239        head(3, 'Examples'),
240        blank_line,
241        code)
242
243    document = @TD.parse(text)
244    assert_equal expected, document
245    assert document.parts.last.ruby?
246  end
247
248  def test_parse_examples_signature
249    text = <<-TEXT
250Do some stuff
251
252Examples
253
254  1 + 1
255
256Signature
257
258  foo(args)
259    TEXT
260
261    code1 = verb("1 + 1\n")
262    code1.format = :ruby
263
264    code2 = verb("foo(args)\n")
265
266    expected =
267      doc(
268        para('Do some stuff'),
269        blank_line,
270        head(3, 'Examples'),
271        blank_line,
272        code1,
273        head(3, 'Signature'),
274        blank_line,
275        code2)
276
277    document = @TD.parse text
278
279    assert_equal expected, document
280  end
281
282  def test_parse_returns
283    text = <<-TEXT
284Do some stuff
285
286Returns a thing
287
288Returns another thing
289    TEXT
290
291    expected =
292      doc(
293        para('Do some stuff'),
294        blank_line,
295        head(3, 'Returns'),
296        blank_line,
297        para('Returns a thing'),
298        blank_line,
299        para('Returns another thing'))
300
301    assert_equal expected, @TD.parse(text)
302  end
303
304  def test_parse_returns_with_raises
305    text = <<-TEXT
306Do some stuff
307
308Returns a thing
309Raises ArgumentError when stuff
310Raises StandardError when stuff
311    TEXT
312    expected =
313      doc(
314        para('Do some stuff'),
315        blank_line,
316        head(3, 'Returns'),
317        blank_line,
318        para('Returns a thing'),
319        para('Raises ArgumentError when stuff'),
320        para('Raises StandardError when stuff'))
321
322    assert_equal expected, @TD.parse(text)
323  end
324
325  def test_parse_raises_without_returns
326    text = <<-TEXT
327Do some stuff
328
329Raises ArgumentError when stuff
330    TEXT
331    expected =
332      doc(
333        para('Do some stuff'),
334        blank_line,
335        head(3, 'Returns'),
336        blank_line,
337        para('Raises ArgumentError when stuff'))
338
339    assert_equal expected, @TD.parse(text)
340  end
341
342  def test_parse_returns_multiline
343    text = <<-TEXT
344Do some stuff
345
346Returns a thing
347  that is multiline
348    TEXT
349
350    expected =
351      doc(
352        para('Do some stuff'),
353        blank_line,
354        head(3, 'Returns'),
355        blank_line,
356        para('Returns a thing', ' ', 'that is multiline'))
357
358    assert_equal expected, @TD.parse(text)
359  end
360
361  def test_parse_returns_multiline_and_raises
362    text = <<-TEXT
363Do some stuff
364
365Returns a thing
366  that is multiline
367Raises ArgumentError
368    TEXT
369
370    expected =
371      doc(
372        para('Do some stuff'),
373        blank_line,
374        head(3, 'Returns'),
375        blank_line,
376        para('Returns a thing', ' ', 'that is multiline'),
377        para('Raises ArgumentError'))
378
379    assert_equal expected, @TD.parse(text)
380  end
381
382  def test_parse_signature
383    text = <<-TEXT
384Do some stuff
385
386Signature
387
388  some_method(args)
389    TEXT
390
391    expected =
392      @RM::Document.new(
393        @RM::Paragraph.new('Do some stuff'),
394        @RM::BlankLine.new,
395        @RM::Heading.new(3, 'Signature'),
396        @RM::BlankLine.new,
397        @RM::Verbatim.new("some_method(args)\n"))
398
399    assert_equal expected, @TD.parse(text)
400  end
401
402  def test_tokenize_paragraph
403    @td.tokenize "Public: Do some stuff\n"
404
405    expected = [
406      [:TEXT,    "Do some stuff",  0, 0],
407      [:NEWLINE, "\n",            13, 0],
408    ]
409
410    assert_equal expected, @td.tokens
411  end
412
413  def test_tokenize_multiline_paragraph
414    text = "Public: Do some stuff\n"
415    text += "On a new line\n"
416
417    @td.tokenize text
418
419    expected = [
420      [:TEXT,     "Do some stuff",   0, 0],
421      [:NEWLINE,  "\n",             13, 0],
422      [:TEXT,     "On a new line",   0, 1],
423      [:NEWLINE,  "\n",             13, 1]
424    ]
425
426    assert_equal expected, @td.tokens
427  end
428
429  def test_tokenize_arguments
430    @td.tokenize <<-TEXT
431Create new Arg object.
432
433name        - name of argument
434description - arguments description
435    TEXT
436
437    expected = [
438      [:TEXT,    "Create new Arg object.",  0, 0],
439      [:NEWLINE, "\n",                     22, 0],
440      [:NEWLINE, "\n",                      0, 1],
441      [:NOTE,    "name",                    0, 2],
442      [:TEXT,    "name of argument",       14, 2],
443      [:NEWLINE, "\n",                     30, 2],
444      [:NOTE,    "description",             0, 3],
445      [:TEXT,    "arguments description",  14, 3],
446      [:NEWLINE, "\n",                     35, 3],
447    ]
448
449    assert_equal expected, @td.tokens
450  end
451
452  def test_tokenize_arguments_array
453    @td.tokenize <<-TEXT
454Create new Arg object.
455
456names[stuff] - names of arguments
457    TEXT
458
459    expected = [
460      [:TEXT,    "Create new Arg object.",  0, 0],
461      [:NEWLINE, "\n",                     22, 0],
462      [:NEWLINE, "\n",                      0, 1],
463      [:NOTE,    "names[stuff]",            0, 2],
464      [:TEXT,    "names of arguments",     15, 2],
465      [:NEWLINE, "\n",                     33, 2],
466    ]
467
468    assert_equal expected, @td.tokens
469  end
470
471  def test_tokenize_arguments_multiline
472    @td.tokenize <<-TEXT
473Do some stuff
474
475foo - A comment goes here
476  and is more than one line
477    TEXT
478
479    expected = [
480      [:TEXT,    "Do some stuff",              0, 0],
481      [:NEWLINE, "\n",                        13, 0],
482      [:NEWLINE, "\n",                         0, 1],
483      [:NOTE,    "foo",                        0, 2],
484      [:TEXT,    "A comment goes here",        6, 2],
485      [:NEWLINE, "\n",                        25, 2],
486      [:TEXT,    "and is more than one line",  2, 3],
487      [:NEWLINE, "\n",                        27, 3],
488    ]
489
490    assert_equal expected, @td.tokens
491  end
492
493  def test_tokenize_arguments_nested
494    @td.tokenize <<-TEXT
495Do some stuff
496
497foo - A comment goes here
498      :bar - bar documentation
499    TEXT
500
501    expected = [
502      [:TEXT,    "Do some stuff",              0, 0],
503      [:NEWLINE, "\n",                        13, 0],
504      [:NEWLINE, "\n",                         0, 1],
505      [:NOTE,    "foo",                        0, 2],
506      [:TEXT,    "A comment goes here",        6, 2],
507      [:NEWLINE, "\n",                        25, 2],
508      [:NOTE,    ":bar",                       6, 3],
509      [:TEXT,    "bar documentation",         13, 3],
510      [:NEWLINE, "\n",                        30, 3],
511    ]
512
513    assert_equal expected, @td.tokens
514  end
515
516  def test_tokenize_examples
517    @td.tokenize <<-TEXT
518Do some stuff
519
520Examples
521
522  1 + 1
523    TEXT
524
525    expected = [
526      [:TEXT,    "Do some stuff",  0, 0],
527      [:NEWLINE, "\n",            13, 0],
528      [:NEWLINE, "\n",             0, 1],
529      [:HEADER,  3,                0, 2],
530      [:TEXT,    "Examples",       0, 2],
531      [:NEWLINE, "\n",             8, 2],
532      [:NEWLINE, "\n",             0, 3],
533      [:TEXT,    "1 + 1",          2, 4],
534      [:NEWLINE, "\n",             7, 4],
535    ]
536
537    assert_equal expected, @td.tokens
538  end
539
540  def test_tokenize_returns
541    @td.tokenize <<-TEXT
542Do some stuff
543
544Returns a thing
545    TEXT
546
547    expected = [
548      [:TEXT,    "Do some stuff",    0, 0],
549      [:NEWLINE, "\n",              13, 0],
550      [:NEWLINE, "\n",               0, 1],
551      [:TEXT,    "Returns a thing",  0, 2],
552      [:NEWLINE, "\n",              15, 2],
553    ]
554
555    assert_equal expected, @td.tokens
556  end
557
558  def test_tokenize_returns_multiline
559    @td.tokenize <<-TEXT
560Do some stuff
561
562Returns a thing
563  that is multiline
564    TEXT
565
566    expected = [
567      [:TEXT,    "Do some stuff",      0, 0],
568      [:NEWLINE, "\n",                13, 0],
569      [:NEWLINE, "\n",                 0, 1],
570      [:TEXT,    "Returns a thing",    0, 2],
571      [:NEWLINE, "\n",                15, 2],
572      [:TEXT,    "that is multiline",  2, 3],
573      [:NEWLINE, "\n",                19, 3],
574    ]
575
576    assert_equal expected, @td.tokens
577  end
578
579end
580