~ubuntu-branches/ubuntu/raring/jruby/raring

« back to all changes in this revision

Viewing changes to spec/java_integration/jrubyc/java/method_spec.rb

  • Committer: Bazaar Package Importer
  • Author(s): Sebastien Delafond
  • Date: 2010-05-12 15:56:25 UTC
  • mfrom: (1.2.1 upstream) (4.1.1 maverick)
  • Revision ID: james.westby@ubuntu.com-20100512155625-sgdfaj1hn8k84090
Tags: 1.5.0~rc3-1
New upstream release (Closes: #581360).

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
require File.dirname(__FILE__) + "/../../spec_helper"
 
2
require 'jruby'
 
3
require 'jruby/compiler'
 
4
 
 
5
describe "A Ruby class generating a Java stub" do
 
6
  def generate(script)
 
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__
 
10
  end
 
11
  
 
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;/
 
36
 
 
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]
 
42
 
 
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
 
49
 
 
50
          java = method.to_s
 
51
          java.should match OBJECT_VOID_BAR_PATTERN
 
52
        end
 
53
      end
 
54
 
 
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]
 
58
 
 
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
 
65
 
 
66
          java = method.to_s
 
67
          java.should match OBJECT_OBJECT_BAR_PATTERN
 
68
        end
 
69
 
 
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]
 
73
 
 
74
            method = cls.methods[0]
 
75
            method.name.should == "bar"
 
76
            method.static.should == true
 
77
 
 
78
            java = method.to_s
 
79
            java.should match /static/
 
80
          end
 
81
        end
 
82
      end
 
83
    end
 
84
 
 
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]
 
89
 
 
90
          method = cls.methods[0]
 
91
          method.name.should == "bar"
 
92
          method.static.should == true
 
93
 
 
94
          java = method.to_s
 
95
          java.should match /static/
 
96
        end
 
97
      end
 
98
      
 
99
      describe "and no arguments" do
 
100
        describe "and a byte return type" do
 
101
          it "generates a byte-returning method" do
 
102
            cls = generate("
 
103
      class Foo
 
104
        java_signature 'byte bar_byte()'; def bar_byte; end
 
105
      end").classes[0]
 
106
 
 
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
 
113
            java = method.to_s
 
114
            java.should match BYTE_VOID_BAR_PATTERN
 
115
          end
 
116
        end
 
117
 
 
118
        describe "and a short return type" do
 
119
          it "generates a short-returning method" do
 
120
            cls = generate("
 
121
      class Foo
 
122
        java_signature 'short bar_short()'; def bar_short; end
 
123
      end").classes[0]
 
124
 
 
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
 
131
            java = method.to_s
 
132
            java.should match SHORT_VOID_BAR_PATTERN
 
133
          end
 
134
        end
 
135
 
 
136
        describe "and a char return type" do
 
137
          it "generates a char-returning method" do
 
138
            cls = generate("
 
139
      class Foo
 
140
        java_signature 'char bar_char()'; def bar_char; end
 
141
      end").classes[0]
 
142
 
 
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
 
149
            java = method.to_s
 
150
            java.should match CHAR_VOID_BAR_PATTERN
 
151
          end
 
152
        end
 
153
 
 
154
        describe "and an int return type" do
 
155
          it "generates a int-returning method" do
 
156
            cls = generate("
 
157
      class Foo
 
158
        java_signature 'int bar_int()'; def bar_int; end
 
159
      end").classes[0]
 
160
 
 
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
 
167
            java = method.to_s
 
168
            java.should match INT_VOID_BAR_PATTERN
 
169
          end
 
170
        end
 
171
 
 
172
        describe "and a lond return type" do
 
173
          it "generates a long-returning method" do
 
174
            cls = generate("
 
175
      class Foo
 
176
        java_signature 'long bar_long()'; def bar_long; end
 
177
      end").classes[0]
 
178
 
 
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
 
185
            java = method.to_s
 
186
            java.should match LONG_VOID_BAR_PATTERN
 
187
          end
 
188
        end
 
189
 
 
190
        describe "and a float return type" do
 
191
          it "generates a float-returning method" do
 
192
            cls = generate("
 
193
      class Foo
 
194
        java_signature 'float bar_float()'; def bar_float; end
 
195
      end").classes[0]
 
196
 
 
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
 
203
            java = method.to_s
 
204
            java.should match FLOAT_VOID_BAR_PATTERN
 
205
          end
 
206
        end
 
207
 
 
208
        describe "and a double return type" do
 
209
          it "generates a double-returning method" do
 
210
            cls = generate("
 
211
      class Foo
 
212
        java_signature 'double bar_double()'; def bar_double; end
 
213
      end").classes[0]
 
214
 
 
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
 
221
            java = method.to_s
 
222
            java.should match DOUBLE_VOID_BAR_PATTERN
 
223
          end
 
224
        end
 
225
 
 
226
        describe "and a boolean return type" do
 
227
          it "generates a boolean-returning method" do
 
228
            cls = generate("
 
229
      class Foo
 
230
        java_signature 'boolean bar_boolean()'; def bar_boolean; end
 
231
      end").classes[0]
 
232
 
 
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
 
239
            java = method.to_s
 
240
            java.should match BOOLEAN_VOID_BAR_PATTERN
 
241
          end
 
242
        end
 
243
      end
 
244
 
 
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]
 
248
 
 
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'
 
256
 
 
257
          java = method.to_s
 
258
          java.should match VOID_STRING_BAR_PATTERN
 
259
        end
 
260
      end
 
261
 
 
262
      describe "with an Object(Object) and void(String) overload" do
 
263
        it "generates Object(Object) and void(String) overloaded methods" do
 
264
          cls = generate("
 
265
            class Foo
 
266
              java_signature 'void bar(String)'
 
267
              def bar_str(a); end
 
268
 
 
269
              java_signature 'void bar(int)'
 
270
              def bar_int(a); end
 
271
            end").classes[0]
 
272
 
 
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
 
282
 
 
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
 
291
        end
 
292
      end
 
293
 
 
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]
 
297
 
 
298
          method = cls.methods[0]
 
299
          method.name.should == "bar"
 
300
          method.static.should == true
 
301
 
 
302
          java = method.to_s
 
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/
 
311
        end
 
312
 
 
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]
 
316
 
 
317
            method = cls.methods[0]
 
318
            method.name.should == "bar"
 
319
            method.static.should == false
 
320
 
 
321
            java = method.to_s
 
322
            java.should match /private/
 
323
            java.should_not match /protected/
 
324
            java.should_not match /public/
 
325
          end
 
326
        end
 
327
      end
 
328
    end
 
329
  end
 
330
end