~ubuntu-branches/ubuntu/quantal/ruby1.9.1/quantal

« back to all changes in this revision

Viewing changes to test/rdoc/test_rdoc_parser_c.rb

  • Committer: Bazaar Package Importer
  • Author(s): Lucas Nussbaum
  • Date: 2011-09-24 19:16:17 UTC
  • mfrom: (1.1.8 upstream) (13.1.7 experimental)
  • Revision ID: james.westby@ubuntu.com-20110924191617-o1qz4rcmqjot8zuy
Tags: 1.9.3~rc1-1
* New upstream release: 1.9.3 RC1.
  + Includes load.c fixes. Closes: #639959.
* Upload to unstable.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
require 'stringio'
2
2
require 'tempfile'
3
3
require 'rubygems'
4
 
require 'minitest/unit'
 
4
require 'minitest/autorun'
5
5
require 'rdoc/options'
6
6
require 'rdoc/parser/c'
7
7
 
 
8
=begin
 
9
  TODO: test call-seq parsing
 
10
 
 
11
/*
 
12
 *  call-seq:
 
13
 *     ARGF.readlines(sep=$/)     -> array
 
14
 *     ARGF.readlines(limit)      -> array
 
15
 *     ARGF.readlines(sep, limit) -> array
 
16
 *
 
17
 *     ARGF.to_a(sep=$/)     -> array
 
18
 *     ARGF.to_a(limit)      -> array
 
19
 *     ARGF.to_a(sep, limit) -> array
 
20
 *
 
21
 *  Reads +ARGF+'s current file in its entirety, returning an +Array+ of its
 
22
 *  lines, one line per element. Lines are assumed to be separated by _sep_.
 
23
 *
 
24
 *     lines = ARGF.readlines
 
25
 *     lines[0]                #=> "This is line one\n"
 
26
 */
 
27
 
 
28
assert call-seq did not stop at first empty line
 
29
 
 
30
/*
 
31
 * call-seq:
 
32
 *
 
33
 *  flt ** other  ->  float
 
34
 *
 
35
 * Raises <code>float</code> the <code>other</code> power.
 
36
 *
 
37
 *    2.0**3      #=> 8.0
 
38
 */
 
39
 
 
40
assert call-seq correct (bug: was empty)
 
41
 
 
42
/* call-seq: flt ** other  ->  float */
 
43
 
 
44
assert call-seq correct
 
45
 
 
46
=end
 
47
 
8
48
class RDoc::Parser::C
9
49
  attr_accessor :classes
10
50
 
30
70
    @tempfile.close
31
71
  end
32
72
 
 
73
  def test_class_can_parse
 
74
    c_parser = RDoc::Parser::C
 
75
 
 
76
    assert_equal c_parser, c_parser.can_parse('file.C')
 
77
    assert_equal c_parser, c_parser.can_parse('file.CC')
 
78
    assert_equal c_parser, c_parser.can_parse('file.H')
 
79
    assert_equal c_parser, c_parser.can_parse('file.HH')
 
80
    assert_equal c_parser, c_parser.can_parse('file.c')
 
81
    assert_equal c_parser, c_parser.can_parse('file.cc')
 
82
    assert_equal c_parser, c_parser.can_parse('file.cpp')
 
83
    assert_equal c_parser, c_parser.can_parse('file.cxx')
 
84
    assert_equal c_parser, c_parser.can_parse('file.h')
 
85
    assert_equal c_parser, c_parser.can_parse('file.hh')
 
86
    assert_equal c_parser, c_parser.can_parse('file.y')
 
87
  end
 
88
 
 
89
  def test_do_attr_rb_attr
 
90
    content = <<-EOF
 
91
void Init_Blah(void) {
 
92
  cBlah = rb_define_class("Blah", rb_cObject);
 
93
 
 
94
  /*
 
95
   * This is an accessor
 
96
   */
 
97
  rb_attr(cBlah, rb_intern("accessor"), 1, 1, Qfalse);
 
98
 
 
99
  /*
 
100
   * This is a reader
 
101
   */
 
102
  rb_attr(cBlah, rb_intern("reader"), 1, 0, Qfalse);
 
103
 
 
104
  /*
 
105
   * This is a writer
 
106
   */
 
107
  rb_attr(cBlah, rb_intern("writer"), 0, 1, Qfalse);
 
108
}
 
109
    EOF
 
110
 
 
111
    klass = util_get_class content, 'cBlah'
 
112
 
 
113
    attrs = klass.attributes
 
114
    assert_equal 3, attrs.length, attrs.inspect
 
115
 
 
116
    accessor = attrs.shift
 
117
    assert_equal 'accessor',            accessor.name
 
118
    assert_equal 'RW',                  accessor.rw
 
119
    assert_equal 'This is an accessor', accessor.comment
 
120
    assert_equal @top_level,            accessor.file
 
121
 
 
122
    reader = attrs.shift
 
123
    assert_equal 'reader',           reader.name
 
124
    assert_equal 'R',                reader.rw
 
125
    assert_equal 'This is a reader', reader.comment
 
126
 
 
127
    writer = attrs.shift
 
128
    assert_equal 'writer',           writer.name
 
129
    assert_equal 'W',                writer.rw
 
130
    assert_equal 'This is a writer', writer.comment
 
131
  end
 
132
 
 
133
  def test_do_attr_rb_define_attr
 
134
    content = <<-EOF
 
135
void Init_Blah(void) {
 
136
  cBlah = rb_define_class("Blah", rb_cObject);
 
137
 
 
138
  /*
 
139
   * This is an accessor
 
140
   */
 
141
  rb_define_attr(cBlah, "accessor", 1, 1);
 
142
}
 
143
    EOF
 
144
 
 
145
    klass = util_get_class content, 'cBlah'
 
146
 
 
147
    attrs = klass.attributes
 
148
    assert_equal 1, attrs.length, attrs.inspect
 
149
 
 
150
    accessor = attrs.shift
 
151
    assert_equal 'accessor',            accessor.name
 
152
    assert_equal 'RW',                  accessor.rw
 
153
    assert_equal 'This is an accessor', accessor.comment
 
154
    assert_equal @top_level,            accessor.file
 
155
  end
 
156
 
 
157
  def test_do_aliases
 
158
    content = <<-EOF
 
159
/*
 
160
 * This should show up as an alias with documentation
 
161
 */
 
162
VALUE blah(VALUE klass, VALUE year) {
 
163
}
 
164
 
 
165
void Init_Blah(void) {
 
166
  cDate = rb_define_class("Date", rb_cObject);
 
167
 
 
168
  rb_define_method(cDate, "blah", blah, 1);
 
169
 
 
170
  rb_define_alias(cDate, "bleh", "blah");
 
171
}
 
172
    EOF
 
173
 
 
174
    klass = util_get_class content, 'cDate'
 
175
 
 
176
    methods = klass.method_list
 
177
    assert_equal 2,      methods.length
 
178
    assert_equal 'bleh', methods.last.name
 
179
    assert_equal 'blah', methods.last.is_alias_for.name
 
180
 
 
181
    assert_equal @top_level, methods.last.is_alias_for.file
 
182
    assert_equal @top_level, methods.last.file
 
183
  end
 
184
 
 
185
  def test_do_aliases_singleton
 
186
    content = <<-EOF
 
187
/*
 
188
 * This should show up as a method with documentation
 
189
 */
 
190
VALUE blah(VALUE klass, VALUE year) {
 
191
}
 
192
 
 
193
void Init_Blah(void) {
 
194
  cDate = rb_define_class("Date", rb_cObject);
 
195
  sDate = rb_singleton_class(cDate);
 
196
 
 
197
  rb_define_method(sDate, "blah", blah, 1);
 
198
 
 
199
  /*
 
200
   * This should show up as an alias
 
201
   */
 
202
  rb_define_alias(sDate, "bleh", "blah");
 
203
}
 
204
    EOF
 
205
 
 
206
    klass = util_get_class content, 'cDate'
 
207
 
 
208
    methods = klass.method_list
 
209
 
 
210
    assert_equal 2,      methods.length
 
211
    assert_equal 'bleh', methods.last.name
 
212
    assert               methods.last.singleton
 
213
    assert_equal 'blah', methods.last.is_alias_for.name
 
214
    assert_equal 'This should show up as an alias', methods.last.comment
 
215
  end
 
216
 
33
217
  def test_do_classes_boot_class
34
218
    content = <<-EOF
35
219
/* Document-class: Foo
56
240
    assert_equal nil, klass.superclass
57
241
  end
58
242
 
 
243
  def test_do_aliases_missing_class
 
244
    content = <<-EOF
 
245
void Init_Blah(void) {
 
246
  rb_define_alias(cDate, "b", "a");
 
247
}
 
248
    EOF
 
249
 
 
250
    _, err = capture_io do
 
251
      refute util_get_class(content, 'cDate')
 
252
    end
 
253
 
 
254
    assert_equal "Enclosing class/module \"cDate\" for alias b a not known\n",
 
255
                 err
 
256
  end
 
257
 
59
258
  def test_do_classes_class
60
259
    content = <<-EOF
61
260
/* Document-class: Foo
77
276
    EOF
78
277
 
79
278
    klass = util_get_class content, 'cFoo'
 
279
    assert_equal 'Kernel::Foo', klass.full_name
80
280
    assert_equal "this is the Foo class under Kernel", klass.comment
81
281
  end
82
282
 
 
283
  def test_do_classes_class_under_rb_path2class
 
284
    content = <<-EOF
 
285
/* Document-class: Kernel::Foo
 
286
 * this is Kernel::Foo < A::B
 
287
 */
 
288
VALUE cFoo = rb_define_class_under(rb_mKernel, "Foo", rb_path2class("A::B"));
 
289
    EOF
 
290
 
 
291
    klass = util_get_class content, 'cFoo'
 
292
 
 
293
    assert_equal 'Kernel::Foo', klass.full_name
 
294
    assert_equal 'A::B', klass.superclass
 
295
    assert_equal 'this is Kernel::Foo < A::B', klass.comment
 
296
  end
 
297
 
 
298
  def test_do_classes_singleton
 
299
    content = <<-EOF
 
300
VALUE cFoo = rb_define_class("Foo", rb_cObject);
 
301
VALUE cFooS = rb_singleton_class(cFoo);
 
302
    EOF
 
303
 
 
304
    util_get_class content, 'cFooS'
 
305
 
 
306
    assert_equal 'Foo', @parser.singleton_classes['cFooS']
 
307
  end
 
308
 
83
309
  def test_do_classes_module
84
310
    content = <<-EOF
85
311
/* Document-module: Foo
165
391
    constants = klass.constants
166
392
    assert !klass.constants.empty?
167
393
 
 
394
    assert_equal @top_level, constants.first.file
 
395
 
168
396
    constants = constants.map { |c| [c.name, c.value, c.comment] }
169
397
 
170
398
    assert_equal ['PERFECT', '300', 'The highest possible score in bowling   '],
201
429
    assert constants.empty?, constants.inspect
202
430
  end
203
431
 
 
432
  def test_do_constants_curses
 
433
    content = <<-EOF
 
434
void Init_curses(){
 
435
  mCurses = rb_define_module("Curses");
 
436
 
 
437
  /*
 
438
   * Document-const: Curses::COLOR_BLACK
 
439
   *
 
440
   * Value of the color black
 
441
   */
 
442
  rb_curses_define_const(COLOR_BLACK);
 
443
}
 
444
    EOF
 
445
 
 
446
    @parser = util_parser content
 
447
 
 
448
    @parser.do_classes
 
449
    @parser.do_constants
 
450
 
 
451
    klass = @parser.classes['mCurses']
 
452
 
 
453
    constants = klass.constants
 
454
    refute_empty klass.constants
 
455
 
 
456
    assert_equal 'COLOR_BLACK', constants.first.name
 
457
    assert_equal 'UINT2NUM(COLOR_BLACK)', constants.first.value
 
458
    assert_equal 'Value of the color black', constants.first.comment
 
459
  end
 
460
 
 
461
  def test_do_includes
 
462
    content = <<-EOF
 
463
Init_foo() {
 
464
   VALUE cFoo = rb_define_class("Foo", rb_cObject);
 
465
   VALUE mInc = rb_define_module("Inc");
 
466
 
 
467
   rb_include_module(cFoo, mInc);
 
468
}
 
469
    EOF
 
470
 
 
471
    klass = util_get_class content, 'cFoo'
 
472
 
 
473
    incl = klass.includes.first
 
474
    assert_equal 'Inc',      incl.name
 
475
    assert_equal '',         incl.comment
 
476
    assert_equal @top_level, incl.file
 
477
  end
 
478
 
 
479
  # HACK parsing warning instead of setting up in file
 
480
  def test_do_methods_in_c
 
481
    content = <<-EOF
 
482
VALUE blah(VALUE klass, VALUE year) {
 
483
}
 
484
 
 
485
void Init_Blah(void) {
 
486
  cDate = rb_define_class("Date", rb_cObject);
 
487
 
 
488
  rb_define_method(cDate, "blah", blah, 1); /* in blah.c */
 
489
}
 
490
    EOF
 
491
 
 
492
    klass = nil
 
493
 
 
494
    _, err = capture_io do
 
495
      klass = util_get_class content, 'cDate'
 
496
    end
 
497
 
 
498
    assert_match ' blah.c ', err
 
499
  end
 
500
 
 
501
  # HACK parsing warning instead of setting up in file
 
502
  def test_do_methods_in_cpp
 
503
    content = <<-EOF
 
504
VALUE blah(VALUE klass, VALUE year) {
 
505
}
 
506
 
 
507
void Init_Blah(void) {
 
508
  cDate = rb_define_class("Date", rb_cObject);
 
509
 
 
510
  rb_define_method(cDate, "blah", blah, 1); /* in blah.cpp */
 
511
}
 
512
    EOF
 
513
 
 
514
    klass = nil
 
515
 
 
516
    _, err = capture_io do
 
517
      klass = util_get_class content, 'cDate'
 
518
    end
 
519
 
 
520
    assert_match ' blah.cpp ', err
 
521
  end
 
522
 
 
523
  # HACK parsing warning instead of setting up in file
 
524
  def test_do_methods_in_y
 
525
    content = <<-EOF
 
526
VALUE blah(VALUE klass, VALUE year) {
 
527
}
 
528
 
 
529
void Init_Blah(void) {
 
530
  cDate = rb_define_class("Date", rb_cObject);
 
531
 
 
532
  rb_define_method(cDate, "blah", blah, 1); /* in blah.y */
 
533
}
 
534
    EOF
 
535
 
 
536
    klass = nil
 
537
 
 
538
    _, err = capture_io do
 
539
      klass = util_get_class content, 'cDate'
 
540
    end
 
541
 
 
542
    assert_match ' blah.y ', err
 
543
  end
 
544
 
 
545
  def test_do_methods_singleton_class
 
546
    content = <<-EOF
 
547
VALUE blah(VALUE klass, VALUE year) {
 
548
}
 
549
 
 
550
void Init_Blah(void) {
 
551
  cDate = rb_define_class("Date", rb_cObject);
 
552
  sDate = rb_singleton_class(cDate);
 
553
 
 
554
  rb_define_method(sDate, "blah", blah, 1);
 
555
}
 
556
    EOF
 
557
 
 
558
    klass = util_get_class content, 'cDate'
 
559
 
 
560
    methods = klass.method_list
 
561
    assert_equal 1,      methods.length
 
562
    assert_equal 'blah', methods.first.name
 
563
    assert               methods.first.singleton
 
564
  end
 
565
 
 
566
  def test_find_alias_comment
 
567
    parser = util_parser ''
 
568
 
 
569
    comment = parser.find_alias_comment 'C', '[]', 'index'
 
570
 
 
571
    assert_equal '', comment
 
572
 
 
573
    parser = util_parser <<-C
 
574
/*
 
575
 * comment
 
576
 */
 
577
 
 
578
rb_define_alias(C, "[]", "index");
 
579
    C
 
580
 
 
581
    comment = parser.find_alias_comment 'C', '[]', 'index'
 
582
 
 
583
    assert_equal "/*\n * comment\n */\n\n", comment
 
584
  end
 
585
 
204
586
  def test_find_class_comment_include
205
587
    @options.rdoc_include << File.dirname(__FILE__)
206
588
 
308
690
    assert_equal '', klass.comment
309
691
  end
310
692
 
 
693
  def test_find_const_comment_rb_define
 
694
    content = <<-EOF
 
695
/*
 
696
 * A comment
 
697
 */
 
698
rb_define_const(cFoo, "CONST", value);
 
699
    EOF
 
700
 
 
701
    parser = util_parser content
 
702
 
 
703
    comment = parser.find_const_comment 'const', 'CONST'
 
704
 
 
705
    assert_equal "/*\n * A comment\n */\n", comment
 
706
  end
 
707
 
 
708
  def test_find_const_comment_document_const
 
709
    content = <<-EOF
 
710
/*
 
711
 * Document-const: CONST
 
712
 *
 
713
 * A comment
 
714
 */
 
715
    EOF
 
716
 
 
717
    parser = util_parser content
 
718
 
 
719
    comment = parser.find_const_comment nil, 'CONST'
 
720
 
 
721
    assert_equal " *\n * A comment\n */", comment
 
722
  end
 
723
 
 
724
  def test_find_const_comment_document_const_full_name
 
725
    content = <<-EOF
 
726
/*
 
727
 * Document-const: Foo::CONST
 
728
 *
 
729
 * A comment
 
730
 */
 
731
    EOF
 
732
 
 
733
    parser = util_parser content
 
734
 
 
735
    comment = parser.find_const_comment nil, 'CONST', 'Foo'
 
736
 
 
737
    assert_equal " *\n * A comment\n */", comment
 
738
  end
 
739
 
311
740
  def test_find_body
312
741
    content = <<-EOF
313
742
/*
335
764
 
336
765
    code = other_function.token_stream.first.text
337
766
 
338
 
    assert_equal "VALUE\nother_function() ", code
 
767
    assert_equal "VALUE\nother_function() {\n}", code
 
768
  end
 
769
 
 
770
  def test_find_body_2
 
771
    content = <<-CONTENT
 
772
/* Copyright (C) 2010  Sven Herzberg
 
773
 *
 
774
 * This file is free software; the author(s) gives unlimited
 
775
 * permission to copy and/or distribute it, with or without
 
776
 * modifications, as long as this notice is preserved.
 
777
 */
 
778
 
 
779
#include <ruby.h>
 
780
 
 
781
static VALUE
 
782
wrap_initialize (VALUE  self)
 
783
{
 
784
  return self;
 
785
}
 
786
 
 
787
/* */
 
788
static VALUE
 
789
wrap_shift (VALUE self,
 
790
            VALUE arg)
 
791
{
 
792
  return self;
 
793
}
 
794
 
 
795
void
 
796
init_gi_repository (void)
 
797
{
 
798
  VALUE mTest = rb_define_module ("Test");
 
799
  VALUE cTest = rb_define_class_under (mTest, "Test", rb_cObject);
 
800
 
 
801
  rb_define_method (cTest, "initialize", wrap_initialize, 0);
 
802
  rb_define_method (cTest, "shift", wrap_shift, 0);
 
803
}
 
804
    CONTENT
 
805
 
 
806
    klass = util_get_class content, 'cTest'
 
807
    assert_equal 2, klass.method_list.length
339
808
  end
340
809
 
341
810
  def test_find_body_define
342
811
    content = <<-EOF
 
812
#define something something_else
 
813
 
 
814
#define other_function rb_other_function
 
815
 
 
816
/*
 
817
 * a comment for rb_other_function
 
818
 */
 
819
VALUE
 
820
rb_other_function() {
 
821
}
 
822
 
 
823
void
 
824
Init_Foo(void) {
 
825
    VALUE foo = rb_define_class("Foo", rb_cObject);
 
826
 
 
827
    rb_define_method(foo, "my_method", other_function, 0);
 
828
}
 
829
    EOF
 
830
 
 
831
    klass = util_get_class content, 'foo'
 
832
    other_function = klass.method_list.first
 
833
 
 
834
    assert_equal 'my_method', other_function.name
 
835
    assert_equal 'a comment for rb_other_function', other_function.comment
 
836
    assert_equal '()', other_function.params
 
837
    assert_equal 118, other_function.offset
 
838
    assert_equal 8, other_function.line
 
839
 
 
840
    code = other_function.token_stream.first.text
 
841
 
 
842
    assert_equal "VALUE\nrb_other_function() {\n}", code
 
843
  end
 
844
 
 
845
  def test_find_body_define_comment
 
846
    content = <<-EOF
343
847
/*
344
848
 * a comment for other_function
345
849
 */
362
866
    other_function = klass.method_list.first
363
867
 
364
868
    assert_equal 'my_method', other_function.name
365
 
    assert_equal "a comment for other_function",
366
 
                 other_function.comment
 
869
    assert_equal 'a comment for other_function', other_function.comment
367
870
    assert_equal '()', other_function.params
 
871
    assert_equal 39, other_function.offset
 
872
    assert_equal 4, other_function.line
368
873
 
369
874
    code = other_function.token_stream.first.text
370
875
 
403
908
 
404
909
    baz = methods.last
405
910
    assert_equal 'Foo#baz', baz.full_name
406
 
    assert_equal "a comment for bar", bar.comment
 
911
    assert_equal "a comment for bar", baz.comment
 
912
  end
 
913
 
 
914
  def test_find_body_document_method_equals
 
915
    content = <<-EOF
 
916
/*
 
917
 * Document-method: Zlib::GzipFile#mtime=
 
918
 *
 
919
 * A comment
 
920
 */
 
921
static VALUE
 
922
rb_gzfile_set_mtime(VALUE obj, VALUE mtime)
 
923
{
 
924
 
 
925
void
 
926
Init_zlib() {
 
927
    mZlib = rb_define_module("Zlib");
 
928
    cGzipFile = rb_define_class_under(mZlib, "GzipFile", rb_cObject);
 
929
    cGzipWriter = rb_define_class_under(mZlib, "GzipWriter", cGzipFile);
 
930
    rb_define_method(cGzipWriter, "mtime=", rb_gzfile_set_mtime, 1);
 
931
}
 
932
    EOF
 
933
 
 
934
    klass = util_get_class content, 'cGzipWriter'
 
935
    assert_equal 1, klass.method_list.length
 
936
 
 
937
    methods = klass.method_list.sort
 
938
 
 
939
    bar = methods.first
 
940
    assert_equal 'Zlib::GzipWriter#mtime=', bar.full_name
 
941
    assert_equal 'A comment', bar.comment
 
942
  end
 
943
 
 
944
  def test_find_body_document_method_same
 
945
    content = <<-EOF
 
946
VALUE
 
947
s_bar() {
 
948
}
 
949
 
 
950
VALUE
 
951
bar() {
 
952
}
 
953
 
 
954
/*
 
955
 * Document-method: Foo::bar
 
956
 *
 
957
 * a comment for Foo::bar
 
958
 */
 
959
 
 
960
/*
 
961
 * Document-method: Foo#bar
 
962
 *
 
963
 * a comment for Foo#bar
 
964
 */
 
965
 
 
966
void
 
967
Init_Foo(void) {
 
968
    VALUE foo = rb_define_class("Foo", rb_cObject);
 
969
 
 
970
    rb_define_singleton_method(foo, "bar", s_bar, 0);
 
971
    rb_define_method(foo, "bar", bar, 0);
 
972
}
 
973
    EOF
 
974
 
 
975
    klass = util_get_class content, 'foo'
 
976
    assert_equal 2, klass.method_list.length
 
977
 
 
978
    methods = klass.method_list.sort
 
979
 
 
980
    s_bar = methods.first
 
981
    assert_equal 'Foo::bar', s_bar.full_name
 
982
    assert_equal "a comment for Foo::bar", s_bar.comment
 
983
 
 
984
    bar = methods.last
 
985
    assert_equal 'Foo#bar', bar.full_name
 
986
    assert_equal "a comment for Foo#bar", bar.comment
 
987
  end
 
988
 
 
989
  def test_find_modifiers_call_seq
 
990
    comment = <<-COMMENT
 
991
/* call-seq:
 
992
 *   commercial() -> Date <br />
 
993
 *   commercial(cwyear, cweek=41, cwday=5, sg=nil) -> Date [ruby 1.8] <br />
 
994
 *   commercial(cwyear, cweek=1, cwday=1, sg=nil) -> Date [ruby 1.9]
 
995
 *
 
996
 * If no arguments are given:
 
997
 * * ruby 1.8: returns a +Date+ for 1582-10-15 (the Day of Calendar Reform in
 
998
 *   Italy)
 
999
 * * ruby 1.9: returns a +Date+ for julian day 0
 
1000
 *
 
1001
 * Otherwise, returns a +Date+ for the commercial week year, commercial week,
 
1002
 * and commercial week day given. Ignores the 4th argument.
 
1003
 */
 
1004
 
 
1005
    COMMENT
 
1006
 
 
1007
    parser = util_parser ''
 
1008
    method_obj = RDoc::AnyMethod.new nil, 'blah'
 
1009
 
 
1010
    parser.find_modifiers comment, method_obj
 
1011
 
 
1012
    expected = <<-CALL_SEQ.chomp
 
1013
commercial() -> Date <br />
 
1014
commercial(cwyear, cweek=41, cwday=5, sg=nil) -> Date [ruby 1.8] <br />
 
1015
commercial(cwyear, cweek=1, cwday=1, sg=nil) -> Date [ruby 1.9]
 
1016
 
 
1017
    CALL_SEQ
 
1018
 
 
1019
    assert_equal expected, method_obj.call_seq
 
1020
  end
 
1021
 
 
1022
  def test_find_modifiers_call_seq_no_blank
 
1023
    comment = <<-COMMENT
 
1024
/* call-seq:
 
1025
 *   commercial() -> Date <br />
 
1026
 *   commercial(cwyear, cweek=41, cwday=5, sg=nil) -> Date [ruby 1.8] <br />
 
1027
 *   commercial(cwyear, cweek=1, cwday=1, sg=nil) -> Date [ruby 1.9]
 
1028
 */
 
1029
 
 
1030
    COMMENT
 
1031
 
 
1032
    parser = util_parser ''
 
1033
    method_obj = RDoc::AnyMethod.new nil, 'blah'
 
1034
 
 
1035
    parser.find_modifiers comment, method_obj
 
1036
 
 
1037
    expected = <<-CALL_SEQ.chomp
 
1038
commercial() -> Date <br />
 
1039
commercial(cwyear, cweek=41, cwday=5, sg=nil) -> Date [ruby 1.8] <br />
 
1040
commercial(cwyear, cweek=1, cwday=1, sg=nil) -> Date [ruby 1.9]
 
1041
 
 
1042
    CALL_SEQ
 
1043
 
 
1044
    assert_equal expected, method_obj.call_seq
 
1045
  end
 
1046
 
 
1047
  def test_find_modifiers_nodoc
 
1048
    comment = <<-COMMENT
 
1049
/* :nodoc:
 
1050
 *
 
1051
 * Blah
 
1052
 */
 
1053
 
 
1054
    COMMENT
 
1055
 
 
1056
    parser = util_parser ''
 
1057
    method_obj = RDoc::AnyMethod.new nil, 'blah'
 
1058
 
 
1059
    parser.find_modifiers comment, method_obj
 
1060
 
 
1061
    assert_equal nil, method_obj.document_self
 
1062
  end
 
1063
 
 
1064
  def test_find_modifiers_yields
 
1065
    comment = <<-COMMENT
 
1066
/* :yields: a, b
 
1067
 *
 
1068
 * Blah
 
1069
 */
 
1070
 
 
1071
    COMMENT
 
1072
 
 
1073
    parser = util_parser ''
 
1074
    method_obj = RDoc::AnyMethod.new nil, 'blah'
 
1075
 
 
1076
    parser.find_modifiers comment, method_obj
 
1077
 
 
1078
    assert_equal 'a, b', method_obj.block_params
 
1079
 
 
1080
    expected = <<-EXPECTED
 
1081
/*
 
1082
 *
 
1083
 * Blah
 
1084
 */
 
1085
 
 
1086
    EXPECTED
 
1087
 
 
1088
    assert_equal expected, comment
 
1089
  end
 
1090
 
 
1091
  def test_handle_method_args_minus_1
 
1092
    parser = util_parser "Document-method: Object#m\n blah */"
 
1093
 
 
1094
    parser.content = <<-BODY
 
1095
VALUE
 
1096
rb_other(VALUE obj) {
 
1097
  rb_funcall(obj, rb_intern("other"), 0);
 
1098
  return rb_str_new2("blah, blah, blah");
 
1099
}
 
1100
 
 
1101
VALUE
 
1102
rb_m(int argc, VALUE *argv, VALUE obj) {
 
1103
  VALUE o1, o2;
 
1104
  rb_scan_args(argc, argv, "1", &o1, &o2);
 
1105
}
 
1106
    BODY
 
1107
 
 
1108
    parser.handle_method 'method', 'rb_cObject', 'm', 'rb_m', -1
 
1109
 
 
1110
    m = @top_level.find_module_named('Object').method_list.first
 
1111
 
 
1112
    assert_equal 'm', m.name
 
1113
    assert_equal @top_level, m.file
 
1114
    assert_equal 115, m.offset
 
1115
    assert_equal 7, m.line
 
1116
 
 
1117
    assert_equal '(p1)', m.params
 
1118
  end
 
1119
 
 
1120
  def test_handle_method_args_0
 
1121
    parser = util_parser "Document-method: BasicObject#==\n blah */"
 
1122
 
 
1123
    parser.handle_method 'method', 'rb_cBasicObject', '==', 'rb_obj_equal', 0
 
1124
 
 
1125
    bo = @top_level.find_module_named 'BasicObject'
 
1126
 
 
1127
    assert_equal 1, bo.method_list.length
 
1128
 
 
1129
    equals2 = bo.method_list.first
 
1130
 
 
1131
    assert_equal '()', equals2.params
 
1132
  end
 
1133
 
 
1134
  def test_handle_method_args_1
 
1135
    parser = util_parser "Document-method: BasicObject#==\n blah */"
 
1136
 
 
1137
    parser.handle_method 'method', 'rb_cBasicObject', '==', 'rb_obj_equal', 1
 
1138
 
 
1139
    bo = @top_level.find_module_named 'BasicObject'
 
1140
 
 
1141
    assert_equal 1, bo.method_list.length
 
1142
 
 
1143
    equals2 = bo.method_list.first
 
1144
 
 
1145
    assert_equal '(p1)', equals2.params
 
1146
  end
 
1147
 
 
1148
  def test_handle_method_args_2
 
1149
    parser = util_parser "Document-method: BasicObject#==\n blah */"
 
1150
 
 
1151
    parser.handle_method 'method', 'rb_cBasicObject', '==', 'rb_obj_equal', 2
 
1152
 
 
1153
    bo = @top_level.find_module_named 'BasicObject'
 
1154
 
 
1155
    assert_equal 1, bo.method_list.length
 
1156
 
 
1157
    equals2 = bo.method_list.first
 
1158
 
 
1159
    assert_equal '(p1, p2)', equals2.params
 
1160
  end
 
1161
 
 
1162
  # test_handle_args_minus_1 handled by test_handle_method
 
1163
 
 
1164
  def test_handle_method_args_minus_2
 
1165
    parser = util_parser "Document-method: BasicObject#==\n blah */"
 
1166
 
 
1167
    parser.handle_method 'method', 'rb_cBasicObject', '==', 'rb_obj_equal', -2
 
1168
 
 
1169
    bo = @top_level.find_module_named 'BasicObject'
 
1170
 
 
1171
    assert_equal 1, bo.method_list.length
 
1172
 
 
1173
    equals2 = bo.method_list.first
 
1174
 
 
1175
    assert_equal '(*args)', equals2.params
 
1176
  end
 
1177
 
 
1178
  def test_handle_method_initialize
 
1179
    parser = util_parser "Document-method: BasicObject::new\n blah */"
 
1180
 
 
1181
    parser.handle_method('private_method', 'rb_cBasicObject',
 
1182
                         'initialize', 'rb_obj_dummy', -1)
 
1183
 
 
1184
    bo = @top_level.find_module_named 'BasicObject'
 
1185
 
 
1186
    assert_equal 1, bo.method_list.length
 
1187
 
 
1188
    new = bo.method_list.first
 
1189
 
 
1190
    assert_equal 'new',   new.name
 
1191
    assert_equal :public, new.visibility
 
1192
  end
 
1193
 
 
1194
  def test_handle_singleton
 
1195
    parser = util_parser <<-SINGLE
 
1196
void Init_Blah(void) {
 
1197
  cDate = rb_define_class("Date", rb_cObject);
 
1198
  sDate = rb_singleton_class(cDate);
 
1199
}
 
1200
    SINGLE
 
1201
 
 
1202
    parser.scan
 
1203
 
 
1204
    assert_equal 'Date', parser.known_classes['sDate']
 
1205
    assert_equal 'Date', parser.singleton_classes['sDate']
407
1206
  end
408
1207
 
409
1208
  def test_look_for_directives_in
442
1241
    read_method = klass.method_list.first
443
1242
    assert_equal "read", read_method.name
444
1243
    assert_equal "Method Comment!   ", read_method.comment
 
1244
    assert_equal "rb_io_s_read", read_method.c_function
 
1245
    assert read_method.singleton
 
1246
  end
 
1247
 
 
1248
  def test_define_method_with_prototype
 
1249
    content = <<-EOF
 
1250
static VALUE rb_io_s_read(int, VALUE*, VALUE);
 
1251
 
 
1252
/* Method Comment! */
 
1253
static VALUE
 
1254
rb_io_s_read(argc, argv, io)
 
1255
    int argc;
 
1256
    VALUE *argv;
 
1257
    VALUE io;
 
1258
{
 
1259
}
 
1260
 
 
1261
void
 
1262
Init_IO(void) {
 
1263
    /*
 
1264
     * a comment for class Foo on rb_define_class
 
1265
     */
 
1266
    VALUE rb_cIO = rb_define_class("IO", rb_cObject);
 
1267
    rb_define_singleton_method(rb_cIO, "read", rb_io_s_read, -1);
 
1268
}
 
1269
    EOF
 
1270
 
 
1271
    klass = util_get_class content, 'rb_cIO'
 
1272
    read_method = klass.method_list.first
 
1273
    assert_equal "read", read_method.name
 
1274
    assert_equal "Method Comment!   ", read_method.comment
 
1275
    assert_equal "rb_io_s_read", read_method.c_function
 
1276
    assert read_method.singleton
445
1277
  end
446
1278
 
447
1279
  def test_define_method_private
472
1304
    assert_equal "Method Comment!   ", read_method.comment
473
1305
  end
474
1306
 
 
1307
  def test_define_method_private_singleton
 
1308
    content = <<-EOF
 
1309
/*Method Comment! */
 
1310
static VALUE
 
1311
rb_io_s_read(argc, argv, io)
 
1312
    int argc;
 
1313
    VALUE *argv;
 
1314
    VALUE io;
 
1315
{
 
1316
}
 
1317
 
 
1318
void
 
1319
Init_IO(void) {
 
1320
    /*
 
1321
     * a comment for class Foo on rb_define_class
 
1322
     */
 
1323
    VALUE rb_cIO = rb_define_class("IO", rb_cObject);
 
1324
    VALUE rb_cIO_s = rb_singleton_class(rb_cIO);
 
1325
    rb_define_private_method(rb_cIO_s, "read", rb_io_s_read, -1);
 
1326
}
 
1327
    EOF
 
1328
 
 
1329
    klass = util_get_class content, 'rb_cIO'
 
1330
    read_method = klass.method_list.first
 
1331
    assert_equal "read", read_method.name
 
1332
    assert_equal "Method Comment!   ", read_method.comment
 
1333
    assert_equal :private, read_method.visibility
 
1334
    assert read_method.singleton
 
1335
  end
 
1336
 
 
1337
  def test_define_method_singleton
 
1338
    content = <<-EOF
 
1339
/*Method Comment! */
 
1340
static VALUE
 
1341
rb_io_s_read(argc, argv, io)
 
1342
    int argc;
 
1343
    VALUE *argv;
 
1344
    VALUE io;
 
1345
{
 
1346
}
 
1347
 
 
1348
void
 
1349
Init_IO(void) {
 
1350
    /*
 
1351
     * a comment for class Foo on rb_define_class
 
1352
     */
 
1353
    VALUE rb_cIO = rb_define_class("IO", rb_cObject);
 
1354
    VALUE rb_cIO_s = rb_singleton_class(rb_cIO);
 
1355
    rb_define_method(rb_cIO_s, "read", rb_io_s_read, -1);
 
1356
}
 
1357
    EOF
 
1358
 
 
1359
    klass = util_get_class content, 'rb_cIO'
 
1360
    read_method = klass.method_list.first
 
1361
    assert_equal "read", read_method.name
 
1362
    assert_equal "Method Comment!   ", read_method.comment
 
1363
    assert read_method.singleton
 
1364
  end
 
1365
 
 
1366
  def test_rb_scan_args
 
1367
    parser = util_parser ''
 
1368
 
 
1369
    assert_equal '(p1)',
 
1370
                 parser.rb_scan_args('rb_scan_args(a, b, "1",)')
 
1371
    assert_equal '(p1, p2)',
 
1372
                 parser.rb_scan_args('rb_scan_args(a, b, "2",)')
 
1373
 
 
1374
    assert_equal '(p1 = v1)',
 
1375
                 parser.rb_scan_args('rb_scan_args(a, b, "01",)')
 
1376
    assert_equal '(p1 = v1, p2 = v2)',
 
1377
                 parser.rb_scan_args('rb_scan_args(a, b, "02",)')
 
1378
 
 
1379
    assert_equal '(p1, p2 = v2)',
 
1380
                 parser.rb_scan_args('rb_scan_args(a, b, "11",)')
 
1381
 
 
1382
    assert_equal '(p1, *args)',
 
1383
                 parser.rb_scan_args('rb_scan_args(a, b, "1*",)')
 
1384
    assert_equal '(p1, p2 = {})',
 
1385
                 parser.rb_scan_args('rb_scan_args(a, b, "1:",)')
 
1386
    assert_equal '(p1, &block)',
 
1387
                 parser.rb_scan_args('rb_scan_args(a, b, "1&",)')
 
1388
 
 
1389
    assert_equal '(p1, p2)',
 
1390
                 parser.rb_scan_args('rb_scan_args(a, b, "101",)')
 
1391
 
 
1392
    assert_equal '(p1, p2 = v2, p3)',
 
1393
                 parser.rb_scan_args('rb_scan_args(a, b, "111",)')
 
1394
 
 
1395
    assert_equal '(p1, *args, p3)',
 
1396
                 parser.rb_scan_args('rb_scan_args(a, b, "1*1",)')
 
1397
 
 
1398
    assert_equal '(p1, p2 = v2, *args)',
 
1399
                 parser.rb_scan_args('rb_scan_args(a, b, "11*",)')
 
1400
    assert_equal '(p1, p2 = v2, p3 = {})',
 
1401
                 parser.rb_scan_args('rb_scan_args(a, b, "11:",)')
 
1402
    assert_equal '(p1, p2 = v2, &block)',
 
1403
                 parser.rb_scan_args('rb_scan_args(a, b, "11&",)')
 
1404
 
 
1405
    assert_equal '(p1, p2 = v2, *args, p4, p5 = {}, &block)',
 
1406
                 parser.rb_scan_args('rb_scan_args(a, b, "11*1:&",)')
 
1407
 
 
1408
    # The following aren't valid according to spec but are according to the
 
1409
    # implementation.
 
1410
    assert_equal '(*args)',
 
1411
                 parser.rb_scan_args('rb_scan_args(a, b, "*",)')
 
1412
    assert_equal '(p1 = {})',
 
1413
                 parser.rb_scan_args('rb_scan_args(a, b, ":",)')
 
1414
    assert_equal '(&block)',
 
1415
                 parser.rb_scan_args('rb_scan_args(a, b, "&",)')
 
1416
 
 
1417
    assert_equal '(*args, p2 = {})',
 
1418
                 parser.rb_scan_args('rb_scan_args(a, b, "*:",)')
 
1419
    assert_equal '(p1 = {}, &block)',
 
1420
                 parser.rb_scan_args('rb_scan_args(a, b, ":&",)')
 
1421
    assert_equal '(*args, p2 = {}, &block)',
 
1422
                 parser.rb_scan_args('rb_scan_args(a, b, "*:&",)')
 
1423
  end
 
1424
 
475
1425
  def util_get_class(content, name)
476
1426
    @parser = util_parser content
477
1427
    @parser.scan
484
1434
 
485
1435
end
486
1436
 
487
 
MiniTest::Unit.autorun