File: constructor_spec.rb

package info (click to toggle)
jruby 9.3.9.0%2Bds-8
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 80,856 kB
  • sloc: ruby: 517,823; java: 260,094; xml: 31,930; ansic: 5,777; yacc: 4,973; sh: 1,163; makefile: 105; jsp: 48; tcl: 40; exp: 11
file content (145 lines) | stat: -rw-r--r-- 5,560 bytes parent folder | download | duplicates (3)
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
require_relative '../spec_helper'

describe "A Ruby class generating a Java stub" do
  def generate(script)
    node = JRuby.parse(script)
    # we use __FILE__ so there's something for it to read
    JRuby::Compiler::JavaGenerator.generate_java node, __FILE__
  end

  EMPTY_INITIALIZE_PATTERN =
    /public\s+Foo\(\) {\s+this\(__ruby__, __metaclass__\);\s+Helpers.invoke\(.*, this, "initialize"\);/
  OBJECT_INITIALIZE_PATTERN =
    /public\s+Foo\(Object \w+\) {\s+this\(__ruby__, __metaclass__\);\s+IRubyObject \w+ = JavaUtil.convertJavaToRuby\(__ruby__, \w+\);\s+Helpers.invoke\(.*, this, "initialize", .*\);/
  STRING_INITIALIZE_PATTERN =
    /public\s+Foo\(String \w+\) {\s+this\(__ruby__, __metaclass__\);\s+IRubyObject \w+ = JavaUtil.convertJavaToRuby\(__ruby__, \w+\);\s+Helpers.invoke\(.*, this, "initialize", .*\);/

  describe "with no initialize method" do
    it "generates a default constructor" do
      cls = generate("class Foo; end").classes[0]
      expect( cls.has_constructor? ).to be false

      expect( cls.to_s ).to match EMPTY_INITIALIZE_PATTERN
    end
  end

  describe "with an initialize method" do
    describe "and a constructor java_signature on a another method" do
      it "generates a default constructor" do
        cls = generate("class Foo; def initialize(a); end; java_signature 'Foo()'; def default_cnstr(); end; end").classes[0]
        expect( cls.has_constructor? ).to be true

        expect( init = cls.methods[0] ).to_not be nil
        expect( init.name ).to eql "initialize"
        expect( init.constructor? ).to be true
        expect( init.java_signature.to_s ).to eql "Object initialize(Object a)"
        expect( init.args.length ).to eql 1

        java = init.to_s
        expect( java ).to match OBJECT_INITIALIZE_PATTERN

        expect( def_cnstr = cls.methods[1] ).to_not be nil
        expect( def_cnstr.constructor? ).to be true
        expect( def_cnstr.java_signature.to_s ).to eql "Foo()"
        expect( def_cnstr.args.length ).to eql 0

        java = def_cnstr.to_s
        expect( java ).to match EMPTY_INITIALIZE_PATTERN
      end
    end

    describe "with no arguments" do
      it "generates a default constructor" do
        cls = generate("class Foo; def initialize; end; end").classes[0]
        expect( cls.has_constructor? ).to be true

        expect( init = cls.methods[0] ).to_not be nil
        expect( init.name ).to eql 'initialize'
        expect( init.constructor? ).to be true
        expect( init.java_signature.to_s ).to eql 'Object initialize()'
        expect( init.args.length ).to eql 0

        java = init.to_s
        expect( java ).to match EMPTY_INITIALIZE_PATTERN
      end
    end

    describe "with one argument and no java_signature" do
      it "generates an (Object) constructor" do
        cls = generate("class Foo; def initialize(a); end; end").classes[0]
        expect( cls.has_constructor? ).to be true

        init = cls.methods[0]
        expect( init.name ).to eql 'initialize'
        expect( init.constructor? ).to be true
        expect( init.java_signature.to_s ).to eql 'Object initialize(Object a)'
        expect( init.args ).to eql ['a']

        java = init.to_s
        expect( java ).to match OBJECT_INITIALIZE_PATTERN
      end
    end

    describe "with one argument and a java_signature" do
      it "generates a type-appropriate constructor" do
        cls = generate("class Foo; java_signature 'Foo(String)'; def initialize(a); end; end").classes[0]
        expect( cls.has_constructor? ).to be true

        init = cls.methods[0]
        expect( init.name ).to eql 'initialize'
        expect( init.constructor? ).to be true
        expect( init.java_signature ).to_not be nil
        expect( init.java_signature.to_s ).to eql "Foo(String)"
        expect( init.args ).to eql ['a']

        java = init.to_s
        expect( java ).to match STRING_INITIALIZE_PATTERN
      end
    end

    describe "with throws clause" do
      it "generates a throws clause" do
        cls = generate("class Foo; java_signature 'Foo() throws FooBarException'; def initialize(); end; end").classes[0]

        method = cls.methods[0]
        expect( method.java_signature.to_s ).to eql 'Foo() throws FooBarException'
      end

      it 'generates a throws clause for more than one exception' do
        cls = generate("class Foo; java_signature 'Foo() throws FooBarException,QuxBazException'; def initialize(); end; end").classes[0]

        method = cls.methods[0]
        expect( method.java_signature.to_s ).to eql 'Foo() throws FooBarException, QuxBazException'
      end
    end

  end

  describe "with an initialize() Java method override" do
    it "generates correct method despite initialize name" do
      # java.beans.beancontext.BeanContextSupport
      cls = generate("class BeanContext < RubyObject; def initialize(); end;" +
                     " java_signature 'void initialize()'; def init(); end; end").classes[0]

      init = cls.methods[0]

      expect( init.name ).to eql 'initialize'
      expect( init.constructor? ).to be true
      expect( init.args.length ).to eql 0

      init = cls.methods[1]

      expect( init.name ).to eql 'init'
      expect( init.constructor? ).to be false
      expect( init.args.length ).to eql 0

      java = init.to_s
      expect( java.index('public void initialize()') ).to_not be nil

      puts cls.to_s if $VERBOSE

      javac_status = javac_compile_contents(cls.to_s, 'BeanContext.java')
      raise 'javac failed (see above output)' unless javac_status
    end
  end
end