1# coding: us-ascii
2
3require 'rdoc/test_case'
4
5class TestRDocComment < RDoc::TestCase
6
7  def setup
8    super
9
10    @top_level = @store.add_file 'file.rb'
11    @comment = RDoc::Comment.new
12    @comment.location = @top_level
13    @comment.text = 'this is a comment'
14  end
15
16  def test_empty_eh
17    refute_empty @comment
18
19    @comment = ''
20
21    assert_empty @comment
22  end
23
24  def test_equals2
25    assert_equal @comment, @comment.dup
26
27    c2 = @comment.dup
28    c2.text = nil
29
30    refute_equal @comment, c2
31
32    c3 = @comment.dup
33    c3.location = nil
34
35    refute_equal @comment, c3
36  end
37
38  def test_extract_call_seq
39    m = RDoc::AnyMethod.new nil, 'm'
40
41    comment = RDoc::Comment.new <<-COMMENT, @top_level
42call-seq:
43  bla => true or false
44
45moar comment
46    COMMENT
47
48    comment.extract_call_seq m
49
50    assert_equal "bla => true or false\n", m.call_seq
51  end
52
53  def test_extract_call_seq_blank
54    m = RDoc::AnyMethod.new nil, 'm'
55
56    comment = RDoc::Comment.new <<-COMMENT, @top_level
57call-seq:
58  bla => true or false
59
60    COMMENT
61
62    comment.extract_call_seq m
63
64    assert_equal "bla => true or false\n", m.call_seq
65  end
66
67  def test_extract_call_seq_commented
68    m = RDoc::AnyMethod.new nil, 'm'
69
70    comment = RDoc::Comment.new <<-COMMENT, @top_level
71# call-seq:
72#   bla => true or false
73#
74# moar comment
75    COMMENT
76
77    comment.extract_call_seq m
78
79    assert_equal nil, m.call_seq
80  end
81
82  def test_extract_call_seq_no_blank
83    m = RDoc::AnyMethod.new nil, 'm'
84
85    comment = RDoc::Comment.new <<-COMMENT, @top_level
86call-seq:
87  bla => true or false
88    COMMENT
89
90    comment.extract_call_seq m
91
92    assert_equal "bla => true or false\n", m.call_seq
93  end
94
95  def test_extract_call_seq_undent
96    m = RDoc::AnyMethod.new nil, 'm'
97
98    comment = RDoc::Comment.new <<-COMMENT, @top_level
99call-seq:
100  bla => true or false
101moar comment
102    COMMENT
103
104    comment.extract_call_seq m
105
106    assert_equal "bla => true or false\nmoar comment\n", m.call_seq
107  end
108
109  def test_extract_call_seq_c
110    comment = RDoc::Comment.new <<-COMMENT
111call-seq:
112  commercial() -> Date <br />
113  commercial(cwyear, cweek=41, cwday=5, sg=nil) -> Date [ruby 1.8] <br />
114  commercial(cwyear, cweek=1, cwday=1, sg=nil) -> Date [ruby 1.9]
115
116If no arguments are given:
117* ruby 1.8: returns a +Date+ for 1582-10-15 (the Day of Calendar Reform in
118  Italy)
119* ruby 1.9: returns a +Date+ for julian day 0
120
121Otherwise, returns a +Date+ for the commercial week year, commercial week,
122and commercial week day given. Ignores the 4th argument.
123    COMMENT
124
125    method_obj = RDoc::AnyMethod.new nil, 'blah'
126
127    comment.extract_call_seq method_obj
128
129    expected = <<-CALL_SEQ.chomp
130commercial() -> Date <br />
131commercial(cwyear, cweek=41, cwday=5, sg=nil) -> Date [ruby 1.8] <br />
132commercial(cwyear, cweek=1, cwday=1, sg=nil) -> Date [ruby 1.9]
133
134    CALL_SEQ
135
136    assert_equal expected, method_obj.call_seq
137  end
138
139  def test_extract_call_seq_c_no_blank
140    comment = RDoc::Comment.new <<-COMMENT
141call-seq:
142  commercial() -> Date <br />
143  commercial(cwyear, cweek=41, cwday=5, sg=nil) -> Date [ruby 1.8] <br />
144  commercial(cwyear, cweek=1, cwday=1, sg=nil) -> Date [ruby 1.9]
145    COMMENT
146
147    method_obj = RDoc::AnyMethod.new nil, 'blah'
148
149    comment.extract_call_seq method_obj
150
151    expected = <<-CALL_SEQ.chomp
152commercial() -> Date <br />
153commercial(cwyear, cweek=41, cwday=5, sg=nil) -> Date [ruby 1.8] <br />
154commercial(cwyear, cweek=1, cwday=1, sg=nil) -> Date [ruby 1.9]
155
156    CALL_SEQ
157
158    assert_equal expected, method_obj.call_seq
159  end
160
161  def test_extract_call_seq_c_separator
162    comment = RDoc::Comment.new <<-'COMMENT'
163call-seq:
164   ARGF.readlines(sep=$/)     -> array
165   ARGF.readlines(limit)      -> array
166   ARGF.readlines(sep, limit) -> array
167
168   ARGF.to_a(sep=$/)     -> array
169   ARGF.to_a(limit)      -> array
170   ARGF.to_a(sep, limit) -> array
171
172Reads +ARGF+'s current file in its entirety, returning an +Array+ of its
173lines, one line per element. Lines are assumed to be separated by _sep_.
174
175   lines = ARGF.readlines
176   lines[0]                #=> "This is line one\n"
177
178    COMMENT
179
180    method_obj = RDoc::AnyMethod.new nil, 'blah'
181
182    comment.extract_call_seq method_obj
183
184    expected = <<-CALL_SEQ
185ARGF.readlines(sep=$/)     -> array
186ARGF.readlines(limit)      -> array
187ARGF.readlines(sep, limit) -> array
188ARGF.to_a(sep=$/)     -> array
189ARGF.to_a(limit)      -> array
190ARGF.to_a(sep, limit) -> array
191    CALL_SEQ
192
193    assert_equal expected, method_obj.call_seq
194
195    expected = <<-'COMMENT'
196
197Reads +ARGF+'s current file in its entirety, returning an +Array+ of its
198lines, one line per element. Lines are assumed to be separated by _sep_.
199
200   lines = ARGF.readlines
201   lines[0]                #=> "This is line one\n"
202
203    COMMENT
204
205    assert_equal expected, comment.text
206  end
207
208  def test_force_encoding
209    skip "Encoding not implemented" unless Object.const_defined? :Encoding
210
211    @comment.force_encoding Encoding::UTF_8
212
213    assert_equal Encoding::UTF_8, @comment.text.encoding
214  end
215
216  def test_format
217    assert_equal 'rdoc', @comment.format
218  end
219
220  def test_format_equals
221    c = comment 'content'
222    document = c.parse
223
224    c.format = RDoc::RD
225
226    assert_equal RDoc::RD, c.format
227    refute_same document, c.parse
228  end
229
230  def test_initialize_copy
231    copy = @comment.dup
232
233    refute_same @comment.text, copy.text
234    assert_same @comment.location, copy.location
235  end
236
237  def test_location
238    assert_equal @top_level, @comment.location
239  end
240
241  def test_normalize
242    @comment.text = <<-TEXT
243  # comment
244    TEXT
245
246    assert_same @comment, @comment.normalize
247
248    assert_equal 'comment', @comment.text
249  end
250
251  def test_normalize_twice
252    @comment.text = <<-TEXT
253  # comment
254    TEXT
255
256    @comment.normalize
257
258    text = @comment.text
259
260    @comment.normalize
261
262    assert_same text, @comment.text, 'normalize not cached'
263  end
264
265  def test_normalize_document
266    @comment.text = nil
267    @comment.document = @RM::Document.new
268
269    assert_same @comment, @comment.normalize
270
271    assert_nil @comment.text
272  end
273
274  def test_normalize_eh
275    refute @comment.normalized?
276
277    @comment.normalize
278
279    assert @comment.normalized?
280  end
281
282  def test_text
283    assert_equal 'this is a comment', @comment.text
284  end
285
286  def test_text_equals
287    @comment.text = 'other'
288
289    assert_equal 'other', @comment.text
290    refute @comment.normalized?
291  end
292
293  def test_text_equals_no_text
294    c = RDoc::Comment.new nil, @top_level
295    c.document = @RM::Document.new
296
297    e = assert_raises RDoc::Error do
298      c.text = 'other'
299    end
300
301    assert_equal 'replacing document-only comment is not allowed', e.message
302  end
303
304  def test_text_equals_parsed
305    document = @comment.parse
306
307    @comment.text = 'other'
308
309    refute_equal document, @comment.parse
310  end
311
312  def test_tomdoc_eh
313    refute @comment.tomdoc?
314
315    @comment.format = 'tomdoc'
316
317    assert @comment.tomdoc?
318  end
319
320  def test_parse
321    parsed = @comment.parse
322
323    expected = @RM::Document.new(
324      @RM::Paragraph.new('this is a comment'))
325
326    expected.file = @top_level
327
328    assert_equal expected, parsed
329    assert_same  parsed, @comment.parse
330  end
331
332  def test_parse_rd
333    c = comment 'it ((*works*))'
334    c.format = 'rd'
335
336    expected =
337      @RM::Document.new(
338        @RM::Paragraph.new('it <em>works</em>'))
339    expected.file = @top_level
340
341    assert_equal expected, c.parse
342  end
343
344  def test_remove_private_encoding
345    skip "Encoding not implemented" unless Object.const_defined? :Encoding
346
347    comment = RDoc::Comment.new <<-EOS, @top_level
348# This is text
349#--
350# this is private
351    EOS
352
353    comment.force_encoding Encoding::IBM437
354
355    comment.remove_private
356
357    assert_equal Encoding::IBM437, comment.text.encoding
358  end
359
360  def test_remove_private_hash
361    @comment.text = <<-TEXT
362#--
363# private
364#++
365# public
366    TEXT
367
368    @comment.remove_private
369
370    assert_equal "# public\n", @comment.text
371  end
372
373  def test_remove_private_hash_trail
374    comment = RDoc::Comment.new <<-EOS, @top_level
375# This is text
376#--
377# this is private
378    EOS
379
380    expected = RDoc::Comment.new <<-EOS, @top_level
381# This is text
382    EOS
383
384    comment.remove_private
385
386    assert_equal expected, comment
387  end
388
389  def test_remove_private_long
390    comment = RDoc::Comment.new <<-EOS, @top_level
391#-----
392#++
393# this is text
394#-----
395    EOS
396
397    expected = RDoc::Comment.new <<-EOS, @top_level
398# this is text
399    EOS
400
401    comment.remove_private
402
403    assert_equal expected, comment
404  end
405
406  def test_remove_private_rule
407    comment = RDoc::Comment.new <<-EOS, @top_level
408# This is text with a rule:
409# ---
410# this is also text
411    EOS
412
413    expected = comment.dup
414
415    comment.remove_private
416
417    assert_equal expected, comment
418  end
419
420  def test_remove_private_star
421    @comment.text = <<-TEXT
422/*
423 *--
424 * private
425 *++
426 * public
427 */
428    TEXT
429
430    @comment.remove_private
431
432    assert_equal "/*\n * public\n */\n", @comment.text
433  end
434
435  def test_remove_private_star2
436    @comment.text = <<-TEXT
437/*--
438 * private
439 *++
440 * public
441 */
442    TEXT
443
444    @comment.remove_private
445
446    assert_equal "/*--\n * private\n *++\n * public\n */\n", @comment.text
447  end
448
449  def test_remove_private_toggle
450    comment = RDoc::Comment.new <<-EOS, @top_level
451# This is text
452#--
453# this is private
454#++
455# This is text again.
456    EOS
457
458    expected = RDoc::Comment.new <<-EOS, @top_level
459# This is text
460# This is text again.
461    EOS
462
463    comment.remove_private
464
465    assert_equal expected, comment
466  end
467
468  def test_remove_private_toggle_encoding
469    skip "Encoding not implemented" unless Object.const_defined? :Encoding
470
471    comment = RDoc::Comment.new <<-EOS, @top_level
472# This is text
473#--
474# this is private
475#++
476# This is text again.
477    EOS
478
479    comment.force_encoding Encoding::IBM437
480
481    comment.remove_private
482
483    assert_equal Encoding::IBM437, comment.text.encoding
484  end
485
486  def test_remove_private_toggle_encoding_ruby_bug?
487    skip "Encoding not implemented" unless Object.const_defined? :Encoding
488
489    comment = RDoc::Comment.new <<-EOS, @top_level
490#--
491# this is private
492#++
493# This is text again.
494    EOS
495
496    comment.force_encoding Encoding::IBM437
497
498    comment.remove_private
499
500    assert_equal Encoding::IBM437, comment.text.encoding
501  end
502
503end
504
505