Update to RDoc r56
[rbx.git] / test / rdoc / test_rdoc_parser_c.rb
bloba52018f37259a1cb86d5c4906702678c5e03cb93
1 require 'stringio'
2 require 'tempfile'
3 require 'test/unit'
4 require 'rdoc/options'
5 require 'rdoc/parser/c'
7 class RDoc::Parser::C
8   attr_accessor :classes
10   public :do_classes, :do_constants
11 end
13 class TestRdocParserC < Test::Unit::TestCase
15   def setup
16     @tempfile = Tempfile.new self.class.name
17     filename = @tempfile.path
19     @top_level = RDoc::TopLevel.new filename
20     @fn = filename
21     @options = RDoc::Options.new Hash.new
22     @stats = RDoc::Stats.new
24     @progress = StringIO.new
25   end
27   def teardown
28     @tempfile.close
29   end
31   def test_do_classes_boot_class
32     content = <<-EOF
33 /* Document-class: Foo
34  * this is the Foo boot class
35  */
36 VALUE cFoo = boot_defclass("Foo", 0);
37     EOF
39     klass = util_get_class content, 'cFoo'
40     assert_equal "   this is the Foo boot class\n   ", klass.comment
41   end
43   def test_do_classes_class
44     content = <<-EOF
45 /* Document-class: Foo
46  * this is the Foo class
47  */
48 VALUE cFoo = rb_define_class("Foo", rb_cObject);
49     EOF
51     klass = util_get_class content, 'cFoo'
52     assert_equal "   this is the Foo class\n   ", klass.comment
53   end
55   def test_do_classes_class_under
56     content = <<-EOF
57 /* Document-class: Kernel::Foo
58  * this is the Foo class under Kernel
59  */
60 VALUE cFoo = rb_define_class_under(rb_mKernel, "Foo", rb_cObject);
61     EOF
63     klass = util_get_class content, 'cFoo'
64     assert_equal "   this is the Foo class under Kernel\n   ", klass.comment
65   end
67   def test_do_classes_module
68     content = <<-EOF
69 /* Document-module: Foo
70  * this is the Foo module
71  */
72 VALUE mFoo = rb_define_module("Foo");
73     EOF
75     klass = util_get_class content, 'mFoo'
76     assert_equal "   this is the Foo module\n   ", klass.comment
77   end
79   def test_do_classes_module_under
80     content = <<-EOF
81 /* Document-module: Kernel::Foo
82  * this is the Foo module under Kernel
83  */
84 VALUE mFoo = rb_define_module_under(rb_mKernel, "Foo");
85     EOF
87     klass = util_get_class content, 'mFoo'
88     assert_equal "   this is the Foo module under Kernel\n   ", klass.comment
89   end
91   def test_do_constants
92     content = <<-EOF
93 #include <ruby.h>
95 void Init_foo(){
96    VALUE cFoo = rb_define_class("Foo", rb_cObject);
98    /* 300: The highest possible score in bowling */
99    rb_define_const(cFoo, "PERFECT", INT2FIX(300));
101    /* Huzzah!: What you cheer when you roll a perfect game */
102    rb_define_const(cFoo, "CHEER", rb_str_new2("Huzzah!"));
104    /* TEST\:TEST: Checking to see if escaped semicolon works */
105    rb_define_const(cFoo, "TEST", rb_str_new2("TEST:TEST"));
107    /* \\: The file separator on MS Windows */
108    rb_define_const(cFoo, "MSEPARATOR", rb_str_new2("\\"));
110    /* /: The file separator on Unix */
111    rb_define_const(cFoo, "SEPARATOR", rb_str_new2("/"));
113    /* C:\\Program Files\\Stuff: A directory on MS Windows */
114    rb_define_const(cFoo, "STUFF", rb_str_new2("C:\\Program Files\\Stuff"));
116    /* Default definition */
117    rb_define_const(cFoo, "NOSEMI", INT2FIX(99));
119    rb_define_const(cFoo, "NOCOMMENT", rb_str_new2("No comment"));
121    /*
122     * Multiline comment goes here because this comment spans multiple lines.
123     * Multiline comment goes here because this comment spans multiple lines.
124     */
125    rb_define_const(cFoo, "MULTILINE", INT2FIX(1));
127    /*
128     * 1: Multiline comment goes here because this comment spans multiple lines.
129     * Multiline comment goes here because this comment spans multiple lines.
130     */
131    rb_define_const(cFoo, "MULTILINE_VALUE", INT2FIX(1));
133    /* Multiline comment goes here because this comment spans multiple lines.
134     * Multiline comment goes here because this comment spans multiple lines.
135     */
136    rb_define_const(cFoo, "MULTILINE_NOT_EMPTY", INT2FIX(1));
139     EOF
141     parser = util_parser content
143     parser.do_classes
144     parser.do_constants
146     klass = parser.classes['cFoo']
147     assert klass
149     constants = klass.constants
150     assert !klass.constants.empty?
152     constants = constants.map { |c| [c.name, c.value, c.comment] }
154     assert_equal ['PERFECT', '300',
155                   "\n      The highest possible score in bowling   \n   "],
156                  constants.shift
157     assert_equal ['CHEER', 'Huzzah!',
158                   "\n      What you cheer when you roll a perfect game   \n   "],
159                  constants.shift
160     assert_equal ['TEST', 'TEST:TEST',
161                   "\n      Checking to see if escaped semicolon works   \n   "],
162                  constants.shift
163     assert_equal ['MSEPARATOR', '\\',
164                   "\n      The file separator on MS Windows   \n   "],
165                  constants.shift
166     assert_equal ['SEPARATOR', '/',
167                   "\n      The file separator on Unix   \n   "],
168                  constants.shift
169     assert_equal ['STUFF', 'C:\\Program Files\\Stuff',
170                   "\n      A directory on MS Windows   \n   "],
171                  constants.shift
172     assert_equal ['NOSEMI', 'INT2FIX(99)',
173                   "\n      Default definition   \n   "],
174                  constants.shift
175     assert_equal ['NOCOMMENT', 'rb_str_new2("No comment")', nil],
176                  constants.shift
178     comment = <<-EOF.chomp
180      
181       Multiline comment goes here because this comment spans multiple lines.
182       Multiline comment goes here because this comment spans multiple lines.
183       
184    
185     EOF
186     assert_equal ['MULTILINE', 'INT2FIX(1)', comment], constants.shift
187     assert_equal ['MULTILINE_VALUE', '1', comment], constants.shift
189     comment = <<-EOF.chomp
191       Multiline comment goes here because this comment spans multiple lines.
192       Multiline comment goes here because this comment spans multiple lines.
193       
194    
195     EOF
196     assert_equal ['MULTILINE_NOT_EMPTY', 'INT2FIX(1)', comment], constants.shift
198     assert constants.empty?, constants.inspect
199   end
201   def test_find_class_comment_init
202     content = <<-EOF
204  * a comment for class Foo
205  */
206 void
207 Init_Foo(void) {
208   VALUE foo = rb_define_class("Foo", rb_cObject);
210     EOF
212     klass = util_get_class content, 'foo'
214     assert_equal "  \n   a comment for class Foo\n   \n", klass.comment
215   end
217   def test_find_class_comment_define_class
218     content = <<-EOF
220  * a comment for class Foo
221  */
222 VALUE foo = rb_define_class("Foo", rb_cObject);
223     EOF
225     klass = util_get_class content, 'foo'
227     assert_equal "  \n   a comment for class Foo\n   ", klass.comment
228   end
230   def test_find_class_comment_define_class_Init_Foo
231     content = <<-EOF
233  * a comment for class Foo on Init
234  */
235 void
236 Init_Foo(void) {
237     /*
238      * a comment for class Foo on rb_define_class
239      */
240     VALUE foo = rb_define_class("Foo", rb_cObject);
242     EOF
244     klass = util_get_class content, 'foo'
246     assert_equal "  \n   a comment for class Foo on Init\n   \n", klass.comment
247   end
249   def util_get_class(content, name)
250     parser = util_parser content
251     parser.do_classes
252     parser.classes[name]
253   end
255   def util_parser(content)
256     parser = RDoc::Parser::C.new @top_level, @fn, content, @options, @stats
257     parser.progress = @progress
258     parser
259   end