summaryrefslogtreecommitdiff
path: root/jni/ruby/test/rdoc/test_rdoc_stats.rb
blob: cd37c29fce9bda86907778734093d700e50fe22d (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
712
713
714
715
716
717
718
719
720
721
722
require 'rdoc/test_case'

class TestRDocStats < RDoc::TestCase

  def setup
    super

    @s = RDoc::Stats.new @store, 0

    @tl = @store.add_file 'file.rb'
    @tl.parser = RDoc::Parser::Ruby
  end

  def test_doc_stats
    c = RDoc::CodeObject.new

    assert_equal [1, 1], @s.doc_stats([c])
  end

  def test_doc_stats_documented
    c = RDoc::CodeObject.new
    c.comment = comment 'x'

    assert_equal [1, 0], @s.doc_stats([c])
  end

  def test_doc_stats_display_eh
    c = RDoc::CodeObject.new
    c.ignore

    assert_equal [0, 0], @s.doc_stats([c])
  end

  def test_report_attr
    c = @tl.add_class RDoc::NormalClass, 'C'
    c.record_location @tl
    c.add_comment 'C', @tl

    a = RDoc::Attr.new nil, 'a', 'RW', nil
    a.record_location @tl
    c.add_attribute a

    @store.complete :public

    report = @s.report

    expected =
      doc(
        para('The following items are not documented:'),
        blank_line,
        verb(
          "class C # is documented\n",
          "\n",
          "  attr_accessor :a # in file file.rb\n",
          "\n",
          "end\n"),
        blank_line)

    assert_equal expected, report
  end

  def test_report_attr_documented
    c = @tl.add_class RDoc::NormalClass, 'C'
    c.record_location @tl
    c.add_comment 'C', @tl

    a = RDoc::Attr.new nil, 'a', 'RW', 'a'
    a.record_location @tl
    c.add_attribute a

    @store.complete :public

    report = @s.report

    assert_equal @s.great_job, report
  end

  def test_report_attr_line
    c = @tl.add_class RDoc::NormalClass, 'C'
    c.record_location @tl
    c.add_comment 'C', @tl

    a = RDoc::Attr.new nil, 'a', 'RW', nil
    a.record_location @tl
    a.line = 3
    c.add_attribute a

    @store.complete :public

    assert_match '# in file file.rb:3', @s.report.accept(to_rdoc)
  end

  def test_report_constant
    m = @tl.add_module RDoc::NormalModule, 'M'
    m.record_location @tl
    m.add_comment 'M', @tl

    c = RDoc::Constant.new 'C', nil, nil
    c.record_location @tl
    m.add_constant c

    @store.complete :public

    report = @s.report

    expected =
      doc(
        para('The following items are not documented:'),
        blank_line,
        verb(
          "module M # is documented\n",
          "\n",
          "  # in file file.rb\n",
          "  C = nil\n",
          "\n",
          "end\n"),
        blank_line)

    assert_equal expected, report
  end

  def test_report_constant_alias
    mod = @tl.add_module RDoc::NormalModule, 'M'

    c = @tl.add_class RDoc::NormalClass, 'C'
    mod.add_constant c

    ca = RDoc::Constant.new 'CA', nil, nil
    ca.is_alias_for = c

    @tl.add_constant ca

    @store.complete :public

    report = @s.report

    # TODO change this to refute match, aliases should be ignored as they are
    # programmer convenience constructs
    assert_match 'class Object', report.accept(to_rdoc)
  end

  def test_report_constant_documented
    m = @tl.add_module RDoc::NormalModule, 'M'
    m.record_location @tl
    m.add_comment 'M', @tl

    c = RDoc::Constant.new 'C', nil, 'C'
    c.record_location @tl
    m.add_constant c

    @store.complete :public

    report = @s.report

    assert_equal @s.great_job, report
  end

  def test_report_constant_line
    m = @tl.add_module RDoc::NormalModule, 'M'
    m.record_location @tl
    m.add_comment 'M', @tl

    c = RDoc::Constant.new 'C', nil, nil
    c.record_location @tl
    c.line = 5
    m.add_constant c

    @store.complete :public

    assert_match '# in file file.rb:5', @s.report.accept(to_rdoc)
  end

  def test_report_class
    c = @tl.add_class RDoc::NormalClass, 'C'
    c.record_location @tl

    m = RDoc::AnyMethod.new nil, 'm'
    m.record_location @tl
    c.add_method m
    m.comment = 'm'

    @store.complete :public

    report = @s.report

    expected =
      doc(
        para('The following items are not documented:'),
        blank_line,
        para('In files:'),
        list(:BULLET, *[
          item(nil, para('file.rb'))]),
        blank_line,
        verb("class C\n", "end\n"),
        blank_line)

    assert_equal expected, report
  end

  def test_report_skip_object
    c = @tl.add_class RDoc::NormalClass, 'Object'
    c.record_location @tl

    m = RDoc::AnyMethod.new nil, 'm'
    m.record_location @tl
    c.add_method m
    m.comment = 'm'

    @store.complete :public

    refute_match %r%^class Object$%, @s.report.accept(to_rdoc)
  end

  def test_report_class_documented
    c = @tl.add_class RDoc::NormalClass, 'C'
    c.record_location @tl
    c.add_comment 'C', @tl

    m = RDoc::AnyMethod.new nil, 'm'
    m.record_location @tl
    c.add_method m
    m.comment = 'm'

    @store.complete :public

    report = @s.report

    assert_equal @s.great_job, report
  end

  def test_report_class_documented_level_1
    c1 = @tl.add_class RDoc::NormalClass, 'C1'
    c1.record_location @tl
    c1.add_comment 'C1', @tl

    m1 = RDoc::AnyMethod.new nil, 'm1'
    m1.record_location @tl
    c1.add_method m1
    m1.comment = 'm1'

    c2 = @tl.add_class RDoc::NormalClass, 'C2'
    c2.record_location @tl

    m2 = RDoc::AnyMethod.new nil, 'm2'
    m2.record_location @tl
    c2.add_method m2
    m2.comment = 'm2'

    @store.complete :public

    @s.coverage_level = 1

    report = @s.report

    expected =
      doc(
        para('The following items are not documented:'),
        blank_line,
        para('In files:'),
        list(:BULLET, *[
          item(nil, para('file.rb'))]),
        blank_line,
        verb("class C2\n", "end\n"),
        blank_line)

    assert_equal expected, report
  end

  def test_report_class_empty
    @tl.add_class RDoc::NormalClass, 'C'

    @store.complete :public

    report = @s.report

    expected =
      doc(
        para('The following items are not documented:'),
        blank_line,
        para('class C is referenced but empty.'),
        para("It probably came from another project.  I'm sorry I'm holding it against you."),
        blank_line)

    assert_equal expected, report
  end

  def test_report_class_empty_2
    c1 = @tl.add_class RDoc::NormalClass, 'C1'
    c1.record_location @tl

    c2 = @tl.add_class RDoc::NormalClass, 'C2'
    c2.record_location @tl
    c2.add_comment 'C2', @tl

    @store.complete :public

    @s.coverage_level = 1
    report = @s.report

    expected =
      doc(
        para('The following items are not documented:'),
        blank_line,
        para('In files:'),
        list(:BULLET, *[
          item(nil, para('file.rb'))]),
        blank_line,
        verb("class C1\n", "end\n"),
        blank_line)

    assert_equal expected, report
  end

  def test_report_class_method_documented
    c = @tl.add_class RDoc::NormalClass, 'C'
    c.record_location @tl

    m = RDoc::AnyMethod.new nil, 'm'
    m.record_location @tl
    c.add_method m
    m.comment = 'm'

    @store.complete :public

    report = @s.report

    expected =
      doc(
        para('The following items are not documented:'),
        blank_line,
        para('In files:'),
        list(:BULLET, *[
          item(nil, para('file.rb'))]),
        blank_line,
        verb("class C\n", "end\n"),
        blank_line)

    assert_equal expected, report
  end

  def test_report_class_module_ignore
    c = @tl.add_class RDoc::NormalClass, 'C'
    c.ignore

    @store.complete :public

    report = @s.report_class_module c

    assert_nil report
  end

  def test_report_empty
    @store.complete :public

    report = @s.report

    assert_equal @s.great_job, report
  end

  def test_report_method
    c = @tl.add_class RDoc::NormalClass, 'C'
    c.record_location @tl
    c.add_comment 'C', @tl

    m1 = RDoc::AnyMethod.new nil, 'm1'
    m1.record_location @tl
    c.add_method m1

    m2 = RDoc::AnyMethod.new nil, 'm2'
    m2.record_location @tl
    c.add_method m2
    m2.comment = 'm2'

    @store.complete :public

    report = @s.report

    expected =
      doc(
        para('The following items are not documented:'),
        blank_line,
        verb(*[
          "class C # is documented\n",
          "\n",
          "  # in file file.rb\n",
          "  def m1; end\n",
          "\n",
          "end\n"]),
        blank_line)

    assert_equal expected, report
  end

  def test_report_method_class
    c = @tl.add_class RDoc::NormalClass, 'C'
    c.record_location @tl
    c.add_comment 'C', @tl

    m1 = RDoc::AnyMethod.new nil, 'm1'
    m1.record_location @tl
    m1.singleton = true
    c.add_method m1

    m2 = RDoc::AnyMethod.new nil, 'm2'
    m2.record_location @tl
    m2.singleton = true
    c.add_method m2
    m2.comment = 'm2'

    @store.complete :public

    report = @s.report

    expected =
      doc(
        para('The following items are not documented:'),
        blank_line,
        verb(*[
          "class C # is documented\n",
          "\n",
          "  # in file file.rb\n",
          "  def self.m1; end\n",
          "\n",
          "end\n"]),
        blank_line)

    assert_equal expected, report
  end

  def test_report_method_documented
    c = @tl.add_class RDoc::NormalClass, 'C'
    c.record_location @tl
    c.add_comment 'C', @tl

    m = RDoc::AnyMethod.new nil, 'm'
    m.record_location @tl
    c.add_method m
    m.comment = 'm'

    @store.complete :public

    report = @s.report

    assert_equal @s.great_job, report
  end

  def test_report_method_line
    c = @tl.add_class RDoc::NormalClass, 'C'
    c.record_location @tl
    c.add_comment 'C', @tl

    m1 = RDoc::AnyMethod.new nil, 'm1'
    m1.record_location @tl
    m1.line = 4
    c.add_method m1

    @store.complete :public

    assert_match '# in file file.rb:4', @s.report.accept(to_rdoc)
  end

  def test_report_method_parameters
    c = @tl.add_class RDoc::NormalClass, 'C'
    c.record_location @tl
    c.add_comment 'C', @tl

    m1 = RDoc::AnyMethod.new nil, 'm1'
    m1.record_location @tl
    m1.params = '(p1, p2)'
    m1.comment = 'Stuff with +p1+'
    c.add_method m1

    m2 = RDoc::AnyMethod.new nil, 'm2'
    m2.record_location @tl
    c.add_method m2
    m2.comment = 'm2'

    @store.complete :public

    @s.coverage_level = 1
    report = @s.report

    expected =
      doc(
        para('The following items are not documented:'),
        blank_line,
        verb(*[
          "class C # is documented\n",
          "\n",
          "  # in file file.rb\n",
          "  # +p2+ is not documented\n",
          "  def m1(p1, p2); end\n",
          "\n",
          "end\n"]),
        blank_line)

    assert_equal expected, report
  end

  def test_report_method_parameters_documented
    @tl.parser = RDoc::Parser::Ruby
    c = @tl.add_class RDoc::NormalClass, 'C'
    c.record_location @tl
    c.add_comment 'C', @tl

    m = RDoc::AnyMethod.new nil, 'm'
    m.record_location @tl
    m.params = '(p1)'
    m.comment = 'Stuff with +p1+'
    c.add_method m

    @store.complete :public

    @s.coverage_level = 1
    report = @s.report

    assert_equal @s.great_job, report
  end

  def test_report_method_parameters_yield
    c = @tl.add_class RDoc::NormalClass, 'C'
    c.record_location @tl
    c.add_comment 'C', @tl

    m = RDoc::AnyMethod.new nil, 'm'
    m.record_location @tl
    m.call_seq = <<-SEQ
m(a) { |c| ... }
m(a, b) { |c, d| ... }
    SEQ
    m.comment = 'Stuff with +a+, yields +c+ for you to do stuff with'
    c.add_method m

    @store.complete :public

    @s.coverage_level = 1
    report = @s.report

    expected =
      doc(
        para('The following items are not documented:'),
        blank_line,
        verb(
          "class C # is documented\n",
          "\n",
          "  # in file file.rb\n",
          "  # +b+, +d+ is not documented\n",
          "  def m; end\n",
          "\n",
          "end\n"),
        blank_line)

    assert_equal expected, report
  end

  def test_summary
    c = @tl.add_class RDoc::NormalClass, 'C'
    c.record_location @tl

    m = @tl.add_module RDoc::NormalModule, 'M'
    m.record_location @tl

    a = RDoc::Attr.new nil, 'a', 'RW', nil
    a.record_location @tl
    c.add_attribute a

    c_c = RDoc::Constant.new 'C', nil, nil
    c_c.record_location @tl
    c.add_constant c_c

    m = RDoc::AnyMethod.new nil, 'm'
    m.record_location @tl
    c.add_method m

    @store.complete :public

    summary = @s.summary.accept to_rdoc
    summary.sub!(/  Elapsed:.*/m, '')

    expected = <<-EXPECTED
  Files:      0

  Classes:    1 (1 undocumented)
  Modules:    1 (1 undocumented)
  Constants:  1 (1 undocumented)
  Attributes: 1 (1 undocumented)
  Methods:    1 (1 undocumented)

  Total:      5 (5 undocumented)
    0.00% documented

    EXPECTED

    assert_equal summary, expected
  end

  def test_summary_level_false
    c = @tl.add_class RDoc::NormalClass, 'C'
    c.record_location @tl

    @store.complete :public

    @s.coverage_level = false

    summary = @s.summary.accept to_rdoc
    summary.sub!(/  Elapsed:.*/m, '')

    expected = <<-EXPECTED
  Files:      0

  Classes:    1 (1 undocumented)
  Modules:    0 (0 undocumented)
  Constants:  0 (0 undocumented)
  Attributes: 0 (0 undocumented)
  Methods:    0 (0 undocumented)

  Total:      1 (1 undocumented)
    0.00% documented

    EXPECTED

    assert_equal summary, expected
  end

  def test_summary_level_1
    c = @tl.add_class RDoc::NormalClass, 'C'
    c.record_location @tl
    c.add_comment 'C', @tl

    m = RDoc::AnyMethod.new nil, 'm'
    m.record_location @tl
    m.params = '(p1, p2)'
    m.comment = 'Stuff with +p1+'
    c.add_method m

    @store.complete :public

    @s.coverage_level = 1
    @s.report

    summary = @s.summary.accept to_rdoc
    summary.sub!(/  Elapsed:.*/m, '')

    expected = <<-EXPECTED
  Files:      0

  Classes:    1 (0 undocumented)
  Modules:    0 (0 undocumented)
  Constants:  0 (0 undocumented)
  Attributes: 0 (0 undocumented)
  Methods:    1 (0 undocumented)
  Parameters: 2 (1 undocumented)

  Total:      4 (1 undocumented)
   75.00% documented

    EXPECTED

    assert_equal summary, expected
  end

  def to_rdoc
    RDoc::Markup::ToRdoc.new
  end

  def test_undoc_params
    method = RDoc::AnyMethod.new [], 'm'
    method.params = '(a)'
    method.comment = comment 'comment'

    total, undoc = @s.undoc_params method

    assert_equal 1,     total
    assert_equal %w[a], undoc
  end

  def test_undoc_params_block
    method = RDoc::AnyMethod.new [], 'm'
    method.params = '(&a)'
    method.comment = comment '+a+'

    total, undoc = @s.undoc_params method

    assert_equal 1, total
    assert_empty    undoc
  end

  def test_undoc_params_documented
    method = RDoc::AnyMethod.new [], 'm'
    method.params = '(a)'
    method.comment = comment '+a+'

    total, undoc = @s.undoc_params method

    assert_equal 1, total
    assert_empty    undoc
  end

  def test_undoc_params_keywords
    method = RDoc::AnyMethod.new [], 'm'
    method.params = '(**a)'
    method.comment = comment '+a+'

    total, undoc = @s.undoc_params method

    assert_equal 1, total
    assert_empty    undoc
  end

  def test_undoc_params_splat
    method = RDoc::AnyMethod.new [], 'm'
    method.params = '(*a)'
    method.comment = comment '+a+'

    total, undoc = @s.undoc_params method

    assert_equal 1, total
    assert_empty    undoc
  end

end