dienashorner 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,352 @@
1
+ require File.expand_path('../spec_helper', File.dirname(__FILE__))
2
+
3
+ shared_examples_for Nashorn::Ruby::Scriptable, :shared => true do
4
+
5
+ it "puts, gets and has a read/write attr" do
6
+ @wrapper.unwrap.instance_eval do
7
+ def foo; @foo; end
8
+ def foo=(foo); @foo = foo; end
9
+ end
10
+
11
+ @wrapper.put('foo', 42)
12
+ @wrapper.has('foo').should == true
13
+ @wrapper.get('foo').should == 42
14
+ @wrapper.unwrap.instance_variable_get(:'@foo').should == 42
15
+ end
16
+
17
+ it "puts, gets and has a write only attr" do
18
+ @wrapper.unwrap.instance_eval do
19
+ def foo=(foo); @foo = foo; end
20
+ end
21
+
22
+ @wrapper.put('foo', 42)
23
+ @wrapper.has('foo').should == true
24
+ @wrapper.get('foo').should be(nil)
25
+ @wrapper.unwrap.instance_variable_get(:'@foo').should == 42
26
+ end
27
+
28
+ it "puts, gets and has gets delegated if it acts like a Hash" do
29
+ @wrapper.unwrap.instance_eval do
30
+ def [](name); (@hash ||= {})[name]; end
31
+ def []=(name, value); (@hash ||= {})[name] = value; end
32
+ end
33
+
34
+ @wrapper.put('foo', 42)
35
+ @wrapper.has('foo').should == true
36
+ @wrapper.get('foo').should == 42
37
+ @wrapper.unwrap.instance_variable_get(:'@hash')['foo'].should == 42
38
+ end
39
+
40
+ it "puts, gets and has non-existing property" do
41
+ @wrapper.put('foo', 42)
42
+ @wrapper.has('foo').should == false
43
+ @wrapper.get('foo').should be nil # (Rhino::JS::Scriptable::NOT_FOUND)
44
+ end
45
+
46
+ end
47
+
48
+ describe Nashorn::Ruby::Object do
49
+
50
+ before do
51
+ @wrapper = Nashorn::Ruby::Object.wrap @object = Object.new
52
+ end
53
+
54
+ it "unwraps a ruby object" do
55
+ @wrapper.unwrap.should be(@object)
56
+ end
57
+
58
+ it_should_behave_like Nashorn::Ruby::Scriptable
59
+
60
+ class UII < Object
61
+
62
+ attr_reader :reader
63
+ attr_writer :writer
64
+
65
+ def method; nil end
66
+
67
+ def self.a_class_method; end
68
+
69
+ end
70
+
71
+ it "returns the ruby class name" do
72
+ rb_object = Nashorn::Ruby::Object.wrap UII.new
73
+ rb_object.getClassName.should == UII.name
74
+ end
75
+
76
+ it "reports being a ruby object on toString" do
77
+ rb_object = Nashorn::Ruby::Object.wrap UII.new
78
+ rb_object.toString.should == '[ruby UII]'
79
+ end
80
+
81
+ it "puts a non-existent attr (delegates to start)" do
82
+ start = mock('start')
83
+ start.expects(:put).once
84
+ rb_object = Nashorn::Ruby::Object.wrap UII.new
85
+
86
+ rb_object.put('nonExistingAttr', start, 42)
87
+ end
88
+
89
+ it "js members include ruby class methods" do
90
+ rb_object = Nashorn::Ruby::Object.wrap UII.new
91
+
92
+ rb_object.keySet.should include("reader")
93
+ rb_object.keySet.should include("method")
94
+ rb_object.keySet.to_a.should_not include('writer=')
95
+ rb_object.keySet.to_a.should include("writer")
96
+ end
97
+
98
+ it "js members include ruby instance methods" do
99
+ rb_object = Nashorn::Ruby::Object.wrap object = UII.new
100
+ object.instance_eval do
101
+ def foo; 'foo'; end
102
+ end
103
+
104
+ rb_object.keySet.to_a.should include('foo')
105
+ end
106
+
107
+ it "js members include writers as attr names" do
108
+ rb_object = Nashorn::Ruby::Object.wrap object = UII.new
109
+
110
+ rb_object.keys.should include('writer')
111
+ rb_object.keys.should_not include('writer=')
112
+
113
+ object.instance_eval do
114
+ def foo=(foo); 'foo' end
115
+ end
116
+
117
+ rb_object.keySet.to_a.should include('foo')
118
+ rb_object.keySet.to_a.should_not include('foo=')
119
+ end
120
+
121
+ it "js members include class methods for wrapped class" do
122
+ rb_object = Nashorn::Ruby::Object.wrap object = UII
123
+
124
+ rb_object.keys.should include('a_class_method')
125
+
126
+ rb_object.keys.should_not include('method')
127
+ rb_object.keys.should_not include('writer')
128
+ end
129
+
130
+ it "is aliased to RubyObject" do
131
+ Nashorn::RubyObject.should be(Nashorn::Ruby::Object)
132
+ end
133
+
134
+ end
135
+
136
+ describe Nashorn::Ruby::Function do
137
+
138
+ before do
139
+ @wrapper = Nashorn::Ruby::Function.wrap @method = Object.new.method(:to_s)
140
+ end
141
+
142
+ it "unwraps a ruby method" do
143
+ @wrapper.unwrap.should be(@method)
144
+ end
145
+
146
+ it_should_behave_like Nashorn::Ruby::Scriptable
147
+
148
+ it "is (JavaScript) callable as a function" do
149
+ rb_function = Nashorn::Ruby::Function.wrap 'foo'.method(:upcase)
150
+ this = nil
151
+ rb_function.call(this).should == 'FOO'
152
+ end
153
+
154
+ it 'is Ruby callable' do
155
+ rb_function = Nashorn::Ruby::Function.wrap 'foo'.method(:upcase)
156
+ rb_function.call.should == 'FOO'
157
+ end
158
+
159
+ it 'is Ruby callable passing arguments' do
160
+ rb_function = Nashorn::Ruby::Function.wrap 'foo'.method(:scan)
161
+ rb_function.call('o').should == ['o', 'o']
162
+ end
163
+
164
+ # it "args get converted before delegating a ruby function call" do
165
+ # klass = Class.new(Object) do
166
+ # def foo(array)
167
+ # array.all? { |elem| elem.is_a?(String) }
168
+ # end
169
+ # end
170
+ # rb_function = Nashorn::Ruby::Function.wrap klass.new.method(:foo)
171
+ # this = nil
172
+ # args = [ '1'.to_java, java.lang.String.new('2') ].to_java
173
+ # # args = [ Rhino::JS::NativeArray.new(args) ].to_java
174
+ # rb_function.call(this, args).should be(true)
175
+ # end
176
+
177
+ it "returned value gets converted to javascript" do
178
+ klass = Class.new(Object) do
179
+ def foo
180
+ [ 42 ]
181
+ end
182
+ end
183
+ rb_function = Nashorn::Ruby::Function.wrap klass.new.method(:foo)
184
+ this = nil; args = [].to_java
185
+ rb_function.call(this, args).should be_a(Nashorn::JS::JSObject)
186
+ rb_function.call(this, args).isArray.should be true
187
+ # rb_function.call(this, args).array?.should be true
188
+ end
189
+
190
+ it "slices redundant args when delegating call" do
191
+ klass = Class.new(Object) do
192
+ def foo(a1)
193
+ a1
194
+ end
195
+ end
196
+ rb_function = Nashorn::Ruby::Function.wrap klass.new.method(:foo)
197
+ this = nil
198
+
199
+ args = [ 1.to_java, 2.to_java, 3.to_java ].to_java; js_return = nil
200
+ lambda { js_return = rb_function.call(this, args) }.should_not raise_error
201
+ js_return.should == 1
202
+ end
203
+
204
+ it "fills missing args when delegating call" do
205
+ klass = Class.new(Object) do
206
+ def foo(a1, a2)
207
+ [ a1, a2 ]
208
+ end
209
+ end
210
+ rb_function = Nashorn::Ruby::Function.wrap klass.new.method(:foo)
211
+ this = nil
212
+
213
+ args = [ 1.to_java ].to_java; js_return = nil
214
+ lambda { js_return = rb_function.call(this, args) }.should_not raise_error
215
+ # js_return.toArray.to_a.should == [ 1, nil ]
216
+ js_return.to_a.should == [ 1, nil ]
217
+
218
+ args = [ ].to_java; js_return = nil
219
+ lambda { js_return = rb_function.call(this, args) }.should_not raise_error
220
+ # js_return.toArray.to_a.should == [ nil, nil ]
221
+ js_return.should == [ nil, nil ]
222
+ end
223
+
224
+ it "fills missing args when delegating call that ends with varargs" do
225
+ klass = Class.new(Object) do
226
+ def foo(a1, a2, *args)
227
+ [ a1, a2, args ].flatten
228
+ end
229
+ end
230
+ rb_function = Nashorn::Ruby::Function.wrap klass.new.method(:foo)
231
+ this = nil
232
+
233
+ args = [ ].to_java; js_return = nil
234
+ lambda { js_return = rb_function.call(this, args) }.should_not raise_error
235
+ js_return.should == [ nil, nil ]
236
+
237
+ args = [ 1.to_java ].to_java; js_return = nil
238
+ lambda { js_return = rb_function.call(this, args) }.should_not raise_error
239
+ js_return.should == [ 1, nil ]
240
+
241
+ args = [ 1.to_java, 2.to_java ].to_java; js_return = nil
242
+ lambda { js_return = rb_function.call(this, args) }.should_not raise_error
243
+ js_return.to_a.should == [ 1, 2 ]
244
+
245
+ args = [ 1.to_java, 2.to_java, 3.to_java ].to_java; js_return = nil
246
+ lambda { js_return = rb_function.call(this, args) }.should_not raise_error
247
+ js_return.to_a.should == [ 1, 2, 3 ]
248
+ end
249
+
250
+ it "returns correct arity and length" do
251
+ klass = Class.new(Object) do
252
+ def foo(a1, a2)
253
+ a1 || a2
254
+ end
255
+ end
256
+ rb_function = Nashorn::Ruby::Function.wrap klass.new.method(:foo)
257
+ rb_function.arity.should == 2
258
+ rb_function.length.should == 2
259
+ end
260
+
261
+ it "reports arity and length of 0 for varargs only method" do
262
+ klass = Class.new(Object) do
263
+ def foo(*args); args; end
264
+ end
265
+ rb_function = Nashorn::Ruby::Function.wrap klass.new.method(:foo)
266
+ rb_function.arity.should == 0
267
+ rb_function.length.should == 0
268
+ end
269
+
270
+ it "reports correct arity and length for ending varargs" do
271
+ klass = Class.new(Object) do
272
+ def foo(a1, *args); [ a1, args ]; end
273
+ end
274
+ rb_function = Nashorn::Ruby::Function.wrap klass.new.method(:foo)
275
+ rb_function.arity.should == 1
276
+ rb_function.length.should == 1
277
+ end
278
+
279
+ it "is aliased to RubyFunction" do
280
+ Nashorn::RubyFunction.should be(Nashorn::Ruby::Function)
281
+ end
282
+
283
+ end
284
+
285
+ describe Nashorn::Ruby::Constructor do
286
+
287
+ before do
288
+ @wrapper = Nashorn::Ruby::Constructor.wrap @class = Class.new(Object)
289
+ end
290
+
291
+ it "unwraps a ruby method" do
292
+ @wrapper.unwrap.should be(@class)
293
+ end
294
+
295
+ it_should_behave_like Nashorn::Ruby::Scriptable
296
+
297
+ class Foo < Object
298
+ end
299
+
300
+ it "is callable as a function" do
301
+ rb_new = Nashorn::Ruby::Constructor.wrap Foo
302
+ this = nil
303
+ rb_new.__call__().should be_a(Nashorn::Ruby::Object)
304
+ # rb_new.__call__(this).should be_a(Nashorn::Ruby::Object)
305
+ rb_new.__call__(this, [].to_java).unwrap.should be_a(Foo)
306
+ end
307
+
308
+ it "returns correct arity and length" do
309
+ rb_new = Nashorn::Ruby::Constructor.wrap Foo
310
+ rb_new.arity.should == 0
311
+ rb_new.length.should == 0
312
+ end
313
+
314
+ it "reports arity and length of 0 for varargs" do
315
+ klass = Class.new do
316
+ def initialize(*args); args; end
317
+ end
318
+ rb_new = Nashorn::Ruby::Constructor.wrap klass
319
+ rb_new.arity.should == 0
320
+ rb_new.length.should == 0
321
+ end
322
+
323
+ it "is aliased to RubyConstructor" do
324
+ (!! defined? Nashorn::RubyConstructor).should == true
325
+ Nashorn::RubyConstructor.should be(Nashorn::Ruby::Constructor)
326
+ end
327
+
328
+ end
329
+
330
+ #describe Nashorn::Ruby::Exception do
331
+ #
332
+ # it 'outcomes as ruby errors in function calls' do
333
+ # klass = Class.new(Object) do
334
+ # def foo(arg)
335
+ # raise TypeError, "don't foo me with #{arg}" unless arg.is_a?(String)
336
+ # end
337
+ # end
338
+ # rb_function = Rhino::Ruby::Function.wrap klass.new.method(:foo)
339
+ # this = nil; args = [ 42.to_java ].to_java
340
+ # begin
341
+ # rb_function.call(context, scope, this, args)
342
+ # rescue java.lang.Exception => e
343
+ # e.should be_a(Rhino::Ruby::Exception)
344
+ # e.getValue.should be_a(Rhino::Ruby::Object)
345
+ # e.value.unwrap.should be_a(TypeError)
346
+ # e.value.unwrap.message == "don't foo me with 42"
347
+ # else
348
+ # fail "#{Rhino::Ruby::Exception} expected to be raised"
349
+ # end
350
+ # end
351
+ #
352
+ #end
@@ -0,0 +1,23 @@
1
+ require File.expand_path('spec_helper', File.dirname(__FILE__))
2
+
3
+ describe Nashorn do
4
+
5
+ it 'allows to eval js on Nashorn' do
6
+ expect( Nashorn.eval '"4" + 2' ).to eql '42'
7
+ expect( Nashorn.eval_js 'true + 10' ).to eql '11'
8
+ end
9
+
10
+ class NashornStub
11
+ include Nashorn
12
+
13
+ def do_eval_js str
14
+ eval_js str
15
+ end
16
+
17
+ end
18
+
19
+ it 'allows to eval js when mixed-in' do
20
+ expect( NashornStub.new.do_eval_js "'1' + '' * 2" ).to eql '10'
21
+ end
22
+
23
+ end
@@ -0,0 +1,38 @@
1
+ require 'nashorn'
2
+ require 'nashorn/rhino'
3
+
4
+ begin
5
+ require 'mocha/api'
6
+ rescue LoadError
7
+ require 'mocha'
8
+ end
9
+
10
+ =begin
11
+ require 'redjs'
12
+
13
+ module RedJS
14
+ Context = Rhino::Context
15
+ Error = Rhino::JSError
16
+ end
17
+ =end
18
+
19
+ module Nashorn
20
+ module SpecHelpers
21
+
22
+ def context_factory
23
+ @context_factory ||= Nashorn::ContextFactory.new
24
+ end
25
+
26
+ def context
27
+ @context || context_factory.call { |ctx| @context = ctx }
28
+ @context
29
+ end
30
+
31
+ end
32
+ end
33
+
34
+ RSpec.configure do |config|
35
+ config.filter_run_excluding :compat => /(0.5.0)|(0.6.0)/ # RedJS
36
+ config.include Nashorn::SpecHelpers
37
+ config.deprecation_stream = 'spec/deprecations.log'
38
+ end
metadata ADDED
@@ -0,0 +1,105 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: dienashorner
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.1
5
+ platform: ruby
6
+ authors:
7
+ - Karol Bucek
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2016-03-17 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ requirement: !ruby/object:Gem::Requirement
15
+ requirements:
16
+ - - "~>"
17
+ - !ruby/object:Gem::Version
18
+ version: 2.14.1
19
+ name: rspec
20
+ prerelease: false
21
+ type: :development
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: 2.14.1
27
+ - !ruby/object:Gem::Dependency
28
+ requirement: !ruby/object:Gem::Requirement
29
+ requirements:
30
+ - - "~>"
31
+ - !ruby/object:Gem::Version
32
+ version: 0.13.3
33
+ name: mocha
34
+ prerelease: false
35
+ type: :development
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: 0.13.3
41
+ description: Nashorn, a Rhino's sucessor, allows embeded JavaScript interaction from Ruby.
42
+ email:
43
+ - self@kares.org
44
+ executables: []
45
+ extensions: []
46
+ extra_rdoc_files:
47
+ - README.md
48
+ - LICENSE
49
+ files:
50
+ - ".gitignore"
51
+ - ".rspec"
52
+ - Gemfile
53
+ - LICENSE
54
+ - README.md
55
+ - Rakefile
56
+ - dienashorner.gemspec
57
+ - lib/nashorn.rb
58
+ - lib/nashorn/context.rb
59
+ - lib/nashorn/convert.rb
60
+ - lib/nashorn/error.rb
61
+ - lib/nashorn/ext.rb
62
+ - lib/nashorn/rhino.rb
63
+ - lib/nashorn/rhino/less.rb
64
+ - lib/nashorn/ruby.rb
65
+ - lib/nashorn/ruby/access.rb
66
+ - lib/nashorn/ruby/attribute_access.rb
67
+ - lib/nashorn/ruby/default_access.rb
68
+ - lib/nashorn/version.rb
69
+ - spec/nashorn/access_spec.rb
70
+ - spec/nashorn/error_spec.rb
71
+ - spec/nashorn/integration/bar.js
72
+ - spec/nashorn/integration/foo.js
73
+ - spec/nashorn/integration/index.js
74
+ - spec/nashorn/integration/loop.js
75
+ - spec/nashorn/integration/loop/element1.js
76
+ - spec/nashorn/integration/loop/element2.js
77
+ - spec/nashorn/integration_spec.rb
78
+ - spec/nashorn/ruby_spec.rb
79
+ - spec/nashorn_spec.rb
80
+ - spec/spec_helper.rb
81
+ homepage: https://github.com/kares/dienashorner
82
+ licenses:
83
+ - Apache-2.0
84
+ metadata: {}
85
+ post_install_message:
86
+ rdoc_options: []
87
+ require_paths:
88
+ - lib
89
+ required_ruby_version: !ruby/object:Gem::Requirement
90
+ requirements:
91
+ - - ">="
92
+ - !ruby/object:Gem::Version
93
+ version: '0'
94
+ required_rubygems_version: !ruby/object:Gem::Requirement
95
+ requirements:
96
+ - - ">="
97
+ - !ruby/object:Gem::Version
98
+ version: '0'
99
+ requirements: []
100
+ rubyforge_project:
101
+ rubygems_version: 2.4.8
102
+ signing_key:
103
+ specification_version: 4
104
+ summary: The Nashorn JavaScript interpreter for JRuby
105
+ test_files: []