/
method_handler_spec.rb
327 lines (299 loc) · 11.1 KB
/
method_handler_spec.rb
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
# frozen_string_literal: true
require File.dirname(__FILE__) + "/spec_helper"
RSpec.describe YARD::Handlers::C::MethodHandler do
it "registers methods" do
parse_init <<-eof
mFoo = rb_define_module("Foo");
rb_define_method(mFoo, "bar", bar, 0);
eof
expect(Registry.at('Foo#bar')).not_to be nil
expect(Registry.at('Foo#bar').visibility).to eq :public
end
it "registers private methods" do
parse_init <<-eof
mFoo = rb_define_module("Foo");
rb_define_private_method(mFoo, "bar", bar, 0);
eof
expect(Registry.at('Foo#bar')).not_to be nil
expect(Registry.at('Foo#bar').visibility).to eq :private
end
it "registers singleton methods" do
parse_init <<-eof
mFoo = rb_define_module("Foo");
rb_define_singleton_method(mFoo, "bar", bar, 0);
eof
expect(Registry.at('Foo.bar')).not_to be nil
expect(Registry.at('Foo.bar').visibility).to eq :public
end
it "registers module functions" do
parse <<-eof
/* DOCSTRING
* @return [String] foo!
*/
static VALUE bar(VALUE self) { x(); y(); z(); }
void Init_Foo() {
mFoo = rb_define_module("Foo");
rb_define_module_function(mFoo, "bar", bar, 0);
}
eof
bar_c = Registry.at('Foo.bar')
bar_i = Registry.at('Foo#bar')
expect(bar_c).to be_module_function
expect(bar_c.visibility).to eq :public
expect(bar_c.docstring).to eq "DOCSTRING"
expect(bar_c.tag(:return).object).to eq bar_c
expect(bar_c.source).to eq "static VALUE bar(VALUE self) { x(); y(); z(); }"
expect(bar_i).not_to be_module_function
expect(bar_i.visibility).to eq :private
expect(bar_i.docstring).to eq "DOCSTRING"
expect(bar_i.tag(:return).object).to eq bar_i
expect(bar_i.source).to eq bar_c.source
end
it "registers global functions into Kernel" do
parse_init 'rb_define_global_function("bar", bar, 0);'
expect(Registry.at('Kernel#bar')).not_to be nil
end
it "looks for symbol containing method source" do
parse <<-eof
static VALUE foo(VALUE self) { x(); y(); z(); }
VALUE bar() { a(); b(); c(); }
void Init_Foo() {
mFoo = rb_define_module("Foo");
rb_define_method(mFoo, "foo", foo, 0);
rb_define_method(mFoo, "bar", bar, 0);
}
eof
foo = Registry.at('Foo#foo')
bar = Registry.at('Foo#bar')
expect(foo.source).to eq "static VALUE foo(VALUE self) { x(); y(); z(); }"
expect(foo.file).to eq '(stdin)'
expect(foo.line).to eq 1
expect(bar.source).to eq "VALUE bar() { a(); b(); c(); }"
expect(bar.file).to eq '(stdin)'
expect(bar.line).to eq 2
end
it "finds docstrings attached to method symbols" do
parse <<-eof
/* DOCSTRING */
static VALUE foo(VALUE self) { x(); y(); z(); }
void Init_Foo() {
mFoo = rb_define_module("Foo");
rb_define_method(mFoo, "foo", foo, 0);
}
eof
foo = Registry.at('Foo#foo')
expect(foo.docstring).to eq 'DOCSTRING'
end
it "uses declaration comments as docstring if there are no others" do
parse <<-eof
static VALUE foo(VALUE self) { x(); y(); z(); }
void Init_Foo() {
mFoo = rb_define_module("Foo");
/* DOCSTRING */
rb_define_method(mFoo, "foo", foo, 0);
// DOCSTRING!
rb_define_method(mFoo, "bar", bar, 0);
}
eof
foo = Registry.at('Foo#foo')
expect(foo.docstring).to eq 'DOCSTRING'
bar = Registry.at('Foo#bar')
expect(bar.docstring).to eq 'DOCSTRING!'
end
it "looks for symbols in other file" do
other = <<-eof
/* DOCSTRING! */
static VALUE foo() { x(); }
eof
expect(File).to receive(:read).with('other.c').and_return(other)
parse <<-eof
void Init_Foo() {
mFoo = rb_define_module("Foo");
rb_define_method(mFoo, "foo", foo, 0); // in other.c
}
eof
foo = Registry.at('Foo#foo')
expect(foo.docstring).to eq 'DOCSTRING!'
expect(foo.file).to eq 'other.c'
expect(foo.line).to eq 2
expect(foo.source).to eq 'static VALUE foo() { x(); }'
end
it "allows extra file to include /'s and other filename characters" do
expect(File).to receive(:read).at_least(1).times.with('ext/a-file.c').and_return(<<-eof)
/* FOO */
VALUE foo(VALUE x) { int value = x; }
/* BAR */
VALUE bar(VALUE x) { int value = x; }
eof
parse_init <<-eof
rb_define_method(rb_cFoo, "foo", foo, 1); /* in ext/a-file.c */
rb_define_global_function("bar", bar, 1); /* in ext/a-file.c */
eof
expect(Registry.at('Foo#foo').docstring).to eq 'FOO'
expect(Registry.at('Kernel#bar').docstring).to eq 'BAR'
end
it "warns if other file can't be found" do
expect(log).to receive(:warn).with(/Missing source file `other.c' when parsing Foo#foo/)
parse <<-eof
void Init_Foo() {
mFoo = rb_define_module("Foo");
rb_define_method(mFoo, "foo", foo, 0); // in other.c
}
eof
end
it "looks at override comments for docstring" do
parse <<-eof
/* Document-method: Foo::foo
* Document-method: new
* Document-method: Foo::Bar#baz
* Foo bar!
*/
// init comments
void Init_Foo() {
mFoo = rb_define_module("Foo");
rb_define_method(mFoo, "foo", foo, 0);
rb_define_method(mFoo, "initialize", foo, 0);
mBar = rb_define_module_under(mFoo, "Bar");
rb_define_method(mBar, "baz", foo, 0);
}
eof
expect(Registry.at('Foo#foo').docstring).to eq 'Foo bar!'
expect(Registry.at('Foo#initialize').docstring).to eq 'Foo bar!'
expect(Registry.at('Foo::Bar#baz').docstring).to eq 'Foo bar!'
end
it "looks at overrides in other files" do
other = <<-eof
/* Document-method: Foo::foo
* Document-method: new
* Document-method: Foo::Bar#baz
* Foo bar!
*/
eof
expect(File).to receive(:read).with('foo/bar/other.c').and_return(other)
src = <<-eof
void Init_Foo() {
mFoo = rb_define_module("Foo");
rb_define_method(mFoo, "foo", foo, 0); // in foo/bar/other.c
rb_define_method(mFoo, "initialize", foo, 0); // in foo/bar/other.c
mBar = rb_define_module_under(mFoo, "Bar"); // in foo/bar/other.c
rb_define_method(mBar, "baz", foo, 0); // in foo/bar/other.c
}
eof
parse(src, 'foo/bar/baz/init.c')
expect(Registry.at('Foo#foo').docstring).to eq 'Foo bar!'
expect(Registry.at('Foo#initialize').docstring).to eq 'Foo bar!'
expect(Registry.at('Foo::Bar#baz').docstring).to eq 'Foo bar!'
end
it "adds return tag on methods ending in '?'" do
parse <<-eof
/* DOCSTRING */
static VALUE foo(VALUE self) { x(); y(); z(); }
void Init_Foo() {
mFoo = rb_define_module("Foo");
rb_define_method(mFoo, "foo?", foo, 0);
}
eof
foo = Registry.at('Foo#foo?')
expect(foo.docstring).to eq 'DOCSTRING'
expect(foo.tag(:return).types).to eq ['Boolean']
expect(foo.tags(:return).size).to eq 1
end
it "does not add return tag if return tags exist" do
parse <<-eof
// @return [String] foo
static VALUE foo(VALUE self) { x(); y(); z(); }
void Init_Foo() {
mFoo = rb_define_module("Foo");
rb_define_method(mFoo, "foo?", foo, 0);
}
eof
foo = Registry.at('Foo#foo?')
expect(foo.tag(:return).types).to eq ['String']
expect(foo.tags(:return).size).to eq 1
end
it "handles casted method names" do
parse_init <<-eof
mFoo = rb_define_module("Foo");
rb_define_method(mFoo, "bar", (METHOD)bar, 0);
rb_define_global_function("baz", (METHOD)baz, 0);
eof
expect(Registry.at('Foo#bar')).not_to be nil
expect(Registry.at('Kernel#baz')).not_to be nil
end
it "extracts at regular method parameters from C function signatures" do
parse <<-eof
static VALUE noargs_func(VALUE self) { return Qnil; }
static VALUE twoargs_func(VALUE self, VALUE a, VALUE b) { return a; }
void Init_Foo() {
mFoo = rb_define_module("Foo");
rb_define_method(mFoo, "noargs", noargs_func, 0);
rb_define_method(mFoo, "twoargs", twoargs_func, 2);
}
eof
expect(Registry.at('Foo#noargs').parameters).to be_empty
expect(Registry.at('Foo#twoargs').parameters).to eq [['a', nil], ['b', nil]]
end
it "extracts at varargs method parameters from C function signatures" do
parse <<-eof
static VALUE varargs_func(int argc, VALUE *argv, VALUE self) { return self; }
/* let's see if parser is robust in the face of strange spacing */
static VALUE varargs_func2( int argc , VALUE
* argv ,VALUE self )
{return self;}
void Init_Foo() {
mFoo = rb_define_module("Foo");
rb_define_method(mFoo, "varargs", varargs_func, -1);
rb_define_method( mFoo ,"varargs2",varargs_func2 ,-1);
}
eof
expect(Registry.at('Foo#varargs').parameters).to eq [['*args', nil]]
expect(Registry.at('Foo#varargs2').parameters).to eq [['*args', nil]]
end
it "is not too strict or too loose about matching override comments to methods" do
parse <<-eof
/* Document-method: Foo::foo
* Document-method: new
* Document-method: Foo::Bar#baz
* Foo bar!
*/
void Init_Foo() {
mFoo = rb_define_module("Foo");
mBar = rb_define_module_under(mFoo, "Bar");
rb_define_method(mFoo, "foo", foo, 0);
rb_define_singleton_method(mFoo, "foo", foo, 0);
rb_define_method(mBar, "foo", foo, 0);
rb_define_singleton_method(mBar, "foo", foo, 0);
rb_define_method(mFoo, "initialize", foo, 0);
rb_define_method(mBar, "initialize", foo, 0);
rb_define_method(mFoo, "baz", foo, 0);
rb_define_singleton_method(mFoo, "baz", foo, 0);
rb_define_method(mBar, "baz", foo, 0);
rb_define_singleton_method(mBar, "baz", foo, 0);
}
eof
expect(Registry.at('Foo#foo').docstring).to eq 'Foo bar!'
expect(Registry.at('Foo.foo').docstring).to eq 'Foo bar!'
expect(Registry.at('Foo::Bar#foo').docstring).to be_empty
expect(Registry.at('Foo::Bar.foo').docstring).to be_empty
expect(Registry.at('Foo#initialize').docstring).to eq 'Foo bar!'
expect(Registry.at('Foo::Bar#initialize').docstring).to eq 'Foo bar!'
expect(Registry.at('Foo#baz').docstring).to be_empty
expect(Registry.at('Foo.baz').docstring).to be_empty
expect(Registry.at('Foo::Bar#baz').docstring).to eq 'Foo bar!'
expect(Registry.at('Foo::Bar.baz').docstring).to be_empty
end
it "recognizes core Ruby classes and modules provided by ruby.h" do
parse_init <<-eof
rb_define_method(rb_cFixnum, "popcount", fix_popcount, 0);
rb_define_private_method(rb_mKernel, "pp", obj_pp, 0);
rb_define_method(rb_mEnumerable, "to_hash", enum_to_hash, 0);
eof
expect(Registry.at('Fixnum').type).to eq :class
expect(Registry.at('Fixnum#popcount').type).to eq :method
expect(Registry.at('Object').type).to eq :class
# Methods defined on Kernel are treated as if they were defined on Object
expect(Registry.at('Object#pp').type).to eq :method
expect(Registry.at('Enumerable').type).to eq :module
expect(Registry.at('Enumerable#to_hash').type).to eq :method
end
end