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