1
require File.dirname(__FILE__) + "/../../spec_helper"
3
require 'jruby/compiler'
5
describe "A Ruby class generating a Java stub" do
7
node = JRuby.parse(script)
8
# we use __FILE__ so there's something for it to read
9
JRuby::Compiler::JavaGenerator.generate_java node, __FILE__
12
OBJECT_VOID_BAR_PATTERN =
13
/public *(.*) *Object bar\(\) {\s+.*IRubyObject ruby_result = RuntimeHelpers\.invoke\(.*, this, "bar"\);\s+return \(Object\)ruby_result\.toJava\(Object\.class\);/
14
OBJECT_OBJECT_BAR_PATTERN =
15
/public *(.*) *Object bar\(Object \w+\) {\s+IRubyObject \S+ = JavaUtil\.convertJavaToRuby\(__ruby__, \S+\);\s+IRubyObject ruby_result = RuntimeHelpers\.invoke\(.*, this, "bar", .*\);\s+return \(Object\)ruby_result\.toJava\(Object\.class\);/
16
BYTE_VOID_BAR_PATTERN =
17
/public *(.*) *byte bar_byte\(\) {\s+.*IRubyObject ruby_result = RuntimeHelpers\.invoke\(.*, this, "bar_byte"\);\s+return \(Byte\)ruby_result\.toJava\(byte\.class\);/
18
SHORT_VOID_BAR_PATTERN =
19
/public *(.*) *short bar_short\(\) {\s+.*IRubyObject ruby_result = RuntimeHelpers\.invoke\(.*, this, "bar_short"\);\s+return \(Short\)ruby_result\.toJava\(short\.class\);/
20
CHAR_VOID_BAR_PATTERN =
21
/public *(.*) *char bar_char\(\) {\s+.*IRubyObject ruby_result = RuntimeHelpers\.invoke\(.*, this, "bar_char"\);\s+return \(Character\)ruby_result\.toJava\(char\.class\);/
22
INT_VOID_BAR_PATTERN =
23
/public *(.*) *int bar_int\(\) {\s+.*IRubyObject ruby_result = RuntimeHelpers\.invoke\(.*, this, "bar_int"\);\s+return \(Integer\)ruby_result\.toJava\(int\.class\);/
24
LONG_VOID_BAR_PATTERN =
25
/public *(.*) *long bar_long\(\) {\s+.*IRubyObject ruby_result = RuntimeHelpers\.invoke\(.*, this, "bar_long"\);\s+return \(Long\)ruby_result\.toJava\(long\.class\);/
26
FLOAT_VOID_BAR_PATTERN =
27
/public *(.*) *float bar_float\(\) {\s+.*IRubyObject ruby_result = RuntimeHelpers\.invoke\(.*, this, "bar_float"\);\s+return \(Float\)ruby_result\.toJava\(float\.class\);/
28
DOUBLE_VOID_BAR_PATTERN =
29
/public *(.*) *double bar_double\(\) {\s+.*IRubyObject ruby_result = RuntimeHelpers\.invoke\(.*, this, "bar_double"\);\s+return \(Double\)ruby_result\.toJava\(double\.class\);/
30
BOOLEAN_VOID_BAR_PATTERN =
31
/public *(.*) *boolean bar_boolean\(\) {\s+.*IRubyObject ruby_result = RuntimeHelpers\.invoke\(.*, this, "bar_boolean"\);\s+return \(Boolean\)ruby_result\.toJava\(boolean\.class\);/
32
VOID_STRING_BAR_PATTERN =
33
/public *(.*) *void bar\(String \w+\) {\s+IRubyObject \S+ = JavaUtil\.convertJavaToRuby\(__ruby__, \S+\);\s+IRubyObject ruby_result = RuntimeHelpers\.invoke\(.*, this, "bar\S*", .*\);\s+return;/
34
VOID_INT_BAR_PATTERN =
35
/public *(.*) *void bar\(int \w+\) {\s+IRubyObject \S+ = JavaUtil\.convertJavaToRuby\(__ruby__, \S+\);\s+IRubyObject ruby_result = RuntimeHelpers\.invoke\(.*, this, "bar\S*", .*\);\s+return;/
37
describe "with a method" do
38
describe "with no java_signature" do
39
describe "and no arguments" do
40
it "generates an Object bar() method" do
41
cls = generate("class Foo; def bar; end; end").classes[0]
43
method = cls.methods[0]
44
method.should_not be nil
45
method.name.should == "bar"
46
method.constructor?.should == false
47
method.java_signature.to_s.should == "Object bar()"
48
method.args.length.should == 0
51
java.should match OBJECT_VOID_BAR_PATTERN
55
describe "and one argument" do
56
it "generates an Object(Object) method" do
57
cls = generate("class Foo; def bar(a); end; end").classes[0]
59
method = cls.methods[0]
60
method.should_not be nil
61
method.name.should == "bar"
62
method.constructor?.should == false
63
method.java_signature.to_s.should == "Object bar(Object a)"
64
method.args.length.should == 1
67
java.should match OBJECT_OBJECT_BAR_PATTERN
70
describe "defined on self (the Ruby class)" do
71
it "generates a static Object(Object) method" do
72
cls = generate("class Foo; def self.bar(a); end; end").classes[0]
74
method = cls.methods[0]
75
method.name.should == "bar"
76
method.static.should == true
79
java.should match /static/
85
describe "with a java_signature" do
86
describe "defined on self (the Ruby class)" do
87
it "generates a static Java method" do
88
cls = generate("class Foo; java_signature 'void bar(String)'; def self.bar(a); end; end").classes[0]
90
method = cls.methods[0]
91
method.name.should == "bar"
92
method.static.should == true
95
java.should match /static/
99
describe "and no arguments" do
100
describe "and a byte return type" do
101
it "generates a byte-returning method" do
104
java_signature 'byte bar_byte()'; def bar_byte; end
107
method = cls.methods[0]
108
method.should_not be nil
109
method.name.should == "bar_byte"
110
method.constructor?.should == false
111
method.java_signature.to_s.should == "byte bar_byte()"
112
method.args.length.should == 0
114
java.should match BYTE_VOID_BAR_PATTERN
118
describe "and a short return type" do
119
it "generates a short-returning method" do
122
java_signature 'short bar_short()'; def bar_short; end
125
method = cls.methods[0]
126
method.should_not be nil
127
method.name.should == "bar_short"
128
method.constructor?.should == false
129
method.java_signature.to_s.should == "short bar_short()"
130
method.args.length.should == 0
132
java.should match SHORT_VOID_BAR_PATTERN
136
describe "and a char return type" do
137
it "generates a char-returning method" do
140
java_signature 'char bar_char()'; def bar_char; end
143
method = cls.methods[0]
144
method.should_not be nil
145
method.name.should == "bar_char"
146
method.constructor?.should == false
147
method.java_signature.to_s.should == "char bar_char()"
148
method.args.length.should == 0
150
java.should match CHAR_VOID_BAR_PATTERN
154
describe "and an int return type" do
155
it "generates a int-returning method" do
158
java_signature 'int bar_int()'; def bar_int; end
161
method = cls.methods[0]
162
method.should_not be nil
163
method.name.should == "bar_int"
164
method.constructor?.should == false
165
method.java_signature.to_s.should == "int bar_int()"
166
method.args.length.should == 0
168
java.should match INT_VOID_BAR_PATTERN
172
describe "and a lond return type" do
173
it "generates a long-returning method" do
176
java_signature 'long bar_long()'; def bar_long; end
179
method = cls.methods[0]
180
method.should_not be nil
181
method.name.should == "bar_long"
182
method.constructor?.should == false
183
method.java_signature.to_s.should == "long bar_long()"
184
method.args.length.should == 0
186
java.should match LONG_VOID_BAR_PATTERN
190
describe "and a float return type" do
191
it "generates a float-returning method" do
194
java_signature 'float bar_float()'; def bar_float; end
197
method = cls.methods[0]
198
method.should_not be nil
199
method.name.should == "bar_float"
200
method.constructor?.should == false
201
method.java_signature.to_s.should == "float bar_float()"
202
method.args.length.should == 0
204
java.should match FLOAT_VOID_BAR_PATTERN
208
describe "and a double return type" do
209
it "generates a double-returning method" do
212
java_signature 'double bar_double()'; def bar_double; end
215
method = cls.methods[0]
216
method.should_not be nil
217
method.name.should == "bar_double"
218
method.constructor?.should == false
219
method.java_signature.to_s.should == "double bar_double()"
220
method.args.length.should == 0
222
java.should match DOUBLE_VOID_BAR_PATTERN
226
describe "and a boolean return type" do
227
it "generates a boolean-returning method" do
230
java_signature 'boolean bar_boolean()'; def bar_boolean; end
233
method = cls.methods[0]
234
method.should_not be nil
235
method.name.should == "bar_boolean"
236
method.constructor?.should == false
237
method.java_signature.to_s.should == "boolean bar_boolean()"
238
method.args.length.should == 0
240
java.should match BOOLEAN_VOID_BAR_PATTERN
245
describe "with a void return and String arg" do
246
it "generates a void method(String)" do
247
cls = generate("class Foo; java_signature 'void bar(String)'; def bar(a); end; end").classes[0]
249
method = cls.methods[0]
250
method.name.should == "bar"
251
method.constructor?.should == false
252
method.java_signature.should_not == nil
253
method.java_signature.to_s.should == "void bar(String)"
254
method.args.length.should == 1
255
method.args[0].should == 'a'
258
java.should match VOID_STRING_BAR_PATTERN
262
describe "with an Object(Object) and void(String) overload" do
263
it "generates Object(Object) and void(String) overloaded methods" do
266
java_signature 'void bar(String)'
269
java_signature 'void bar(int)'
273
cls.methods.length.should == 2
274
vs_method = cls.methods[0]
275
vs_method.name.should == "bar_str"
276
vs_method.constructor?.should == false
277
vs_method.java_signature.should_not == nil
278
vs_method.java_signature.to_s.should == "void bar(String)"
279
vs_method.args.length.should == 1
280
vs_method.args[0].should == 'a'
281
vs_method.to_s.should match VOID_STRING_BAR_PATTERN
283
oo_method = cls.methods[1]
284
oo_method.name.should == "bar_int"
285
oo_method.constructor?.should == false
286
oo_method.java_signature.should_not == nil
287
oo_method.java_signature.to_s.should == "void bar(int)"
288
oo_method.args.length.should == 1
289
oo_method.args[0].should == 'a'
290
oo_method.to_s.should match VOID_INT_BAR_PATTERN
294
describe "with Java modifiers" do
295
it "generates a method with those modifiers" do
296
cls = generate("class Foo; java_signature 'protected static abstract final strictfp native synchronized void bar()'; def self.bar; end; end").classes[0]
298
method = cls.methods[0]
299
method.name.should == "bar"
300
method.static.should == true
303
java.should match /protected/
304
java.should match /static/
305
java.should match /abstract/
306
java.should match /final/
307
java.should match /strictfp/
308
java.should match /native/
309
java.should match /synchronized/
310
java.should match /void/
313
describe "with multiple visibilities" do
314
it "uses the first visibility only" do
315
cls = generate("class Foo; java_signature 'private protected public void bar()'; def bar; end; end").classes[0]
317
method = cls.methods[0]
318
method.name.should == "bar"
319
method.static.should == false
322
java.should match /private/
323
java.should_not match /protected/
324
java.should_not match /public/