/
rhino_ext_spec.rb
266 lines (209 loc) · 7.46 KB
/
rhino_ext_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
require File.expand_path('../spec_helper', File.dirname(__FILE__))
module RhinoHelpers
module_function
def add_prototype_key(hash, recurse = false)
hash['prototype'] ||= {}
hash.keys.each do |key|
val = hash[key] unless key == 'prototype'
add_prototype_key(val, recurse) if val.is_a?(Hash)
end if recurse
end
end
shared_examples_for 'ScriptableObject', :shared => true do
it "acts like a hash" do
@object['foo'] = 'bar'
@object['foo'].should == 'bar'
end
it "might be converted to a hash with string keys" do
@object[42] = '42'
@object[:foo] = 'bar'
expect = @object.respond_to?(:to_h_properties) ? @object.to_h_properties : {}
@object.to_h.should == expect.merge('42' => '42', 'foo' => 'bar')
end
it "yields properties with each" do
@object['1'] = 1
@object['3'] = 3
@object['2'] = 2
@object.each do |key, val|
case key
when '1' then val.should == 1
when '2' then val.should == 2
when '3' then val.should == 3
end
end
end
end
describe "NativeObject" do
before do
@object = Rhino::JS::NativeObject.new
end
it_should_behave_like 'ScriptableObject'
end
describe "FunctionObject" do
before do
factory = Rhino::JS::ContextFactory.new
context, scope = nil, nil
factory.call do |ctx|
context = ctx
scope = context.initStandardObjects(nil, false)
end
factory.enterContext(context)
to_string = java.lang.Object.new.getClass.getMethod(:toString)
@object = Rhino::JS::FunctionObject.new('to_string', to_string, scope)
@object.instance_eval do
def to_h_properties
h = { "arguments"=> nil, "name"=> "to_string", "arity"=> 0, "length"=> 0 }
RhinoHelpers.add_prototype_key(h) if Rhino.implementation_version < '1.7R4'
h
end
end
end
after do
Rhino::JS::Context.exit
end
it_should_behave_like 'ScriptableObject'
end
describe "NativeObject (scoped)" do
before do
factory = Rhino::JS::ContextFactory.new
context, scope = nil, nil
factory.call do |ctx|
context = ctx
scope = context.initStandardObjects(nil, false)
end
factory.enterContext(context)
@object = context.newObject(scope)
end
after do
Rhino::JS::Context.exit
end
it_should_behave_like 'ScriptableObject'
it 'routes rhino methods' do
@object.prototype.should == {}
@object.getTypeOf.should == 'object'
end
it 'raises on missing method' do
lambda { @object.aMissingMethod }.should raise_error(NoMethodError)
end
it 'invokes JS function' do
@object.hasOwnProperty('foo').should == false
@object.toLocaleString.should == '[object Object]'
end
it 'puts JS property' do
@object.has('foo', @object).should == false
@object.foo = 'bar'
@object.has('foo', @object).should == true
end
it 'gets JS property' do
@object.put('foo', @object, 42)
@object.foo.should == 42
end
end
describe "NativeFunction" do
before do
factory = Rhino::JS::ContextFactory.new
@context, @scope = nil, nil
factory.call do |ctx|
@context = ctx
@scope = @context.initStandardObjects(nil, false)
end
factory.enterContext(@context)
object = @context.newObject(@scope)
@object = Rhino::JS::ScriptableObject.getProperty(object, 'toString')
@object.instance_eval do
def to_h_properties
h = { "arguments"=> nil, "name"=> "toString", "arity"=> 0, "length"=> 0 }
RhinoHelpers.add_prototype_key(h) if Rhino.implementation_version < '1.7R4'
h
end
end
end
after do
Rhino::JS::Context.exit
end
it_should_behave_like 'ScriptableObject'
it 'is (Ruby) callable' do
# NOTE: no implicit or bound this thus this === global
@object.call.should == '[object global]'
end
it 'is (Ruby) callable passing arguments' do
js = "( function foo(arg) { return 'foo' + arg; } )"
foo = @context.evaluateString(@scope, js, '<eval>', 0, nil)
foo.call(42).should == 'foo42'
end
it 'is (Ruby) callable converting result' do
js = "( function foo(arg) { return [ 1, 2, arg ]; } )"
foo = @context.evaluateString(@scope, js, '<eval>', 0, nil)
foo.call('x').should == [ 1, 2, 'x' ]
end
it 'might be bind and called' do
@object.bind(@object).should be_a(Rhino::JS::Function)
@object.bind(@object).call.should == '[object Function]'
end
it 'might be bind to a this context' do
@object.bind(@object).should be_a(Rhino::JS::Function)
@object.bind(@object).call.should == '[object Function]'
end
it 'might be bind to a this with args' do
array = @context.newArray(@scope, [].to_java)
push = Rhino::JS::ScriptableObject.getProperty(array, 'push')
this = @context.newArray(@scope, [ 0 ].to_java)
push.bind(this, 1, 2).call(3, 4)
this.length.should == 5
5.times { |i| this.get(i, this).should == i }
end
it 'might be applied' do
an_obj = @context.newObject(@scope)
@object.apply(an_obj).should == '[object Object]'
array = @context.newArray(@scope, [].to_java)
push = Rhino::JS::ScriptableObject.getProperty(array, 'splice')
this = @context.newArray(@scope, [ 0, 3, 4 ].to_java)
push.apply(this, 1, 0, 1, 2)
this.length.should == 5
5.times { |i| this.get(i, this).should == i }
end
it 'might get method-called' do
@object.method(:apply).should == @object.method(:methodcall)
end
end
describe "NativeFunction (constructor)" do
before do
factory = Rhino::JS::ContextFactory.new
context, scope = nil, nil
factory.call do |ctx|
context = ctx
scope = context.initStandardObjects(nil, false)
end
factory.enterContext(context)
@object = Rhino::JS::ScriptableObject.getProperty(context.newObject(scope), 'constructor')
@object.instance_eval do
def to_h_properties
h = {
"arguments"=>nil, "prototype"=>{}, "name"=>"Object", "arity"=>1, "length"=>1,
"getPrototypeOf"=> { "arguments"=>nil, "name"=>"getPrototypeOf", "arity"=>1, "length"=>1},
"keys"=>{"arguments"=>nil, "name"=>"keys", "arity"=>1, "length"=>1},
"getOwnPropertyNames"=>{"arguments"=>nil, "name"=>"getOwnPropertyNames", "arity"=>1, "length"=>1},
"getOwnPropertyDescriptor"=>{"arguments"=>nil, "name"=>"getOwnPropertyDescriptor", "arity"=>2, "length"=>2},
"defineProperty"=>{"arguments"=>nil, "name"=>"defineProperty", "arity"=>3, "length"=>3},
"isExtensible"=>{"arguments"=>nil, "name"=>"isExtensible", "arity"=>1, "length"=>1},
"preventExtensions"=>{"arguments"=>nil, "name"=>"preventExtensions", "arity"=>1, "length"=>1},
"defineProperties"=>{"arguments"=>nil, "name"=>"defineProperties", "arity"=>2, "length"=>2},
"create"=>{"arguments"=>nil, "name"=>"create", "arity"=>2, "length"=>2},
"isSealed"=>{"arguments"=>nil, "name"=>"isSealed", "arity"=>1, "length"=>1},
"isFrozen"=>{"arguments"=>nil, "name"=>"isFrozen", "arity"=>1, "length"=>1},
"seal"=>{"arguments"=>nil, "name"=>"seal", "arity"=>1, "length"=>1},
"freeze"=>{"arguments"=>nil, "name"=>"freeze", "arity"=>1, "length"=>1}
}
RhinoHelpers.add_prototype_key(h, :recurse) if Rhino.implementation_version < '1.7R4'
h
end
end
end
after do
Rhino::JS::Context.exit
end
it_should_behave_like 'ScriptableObject'
it 'is constructable' do
@object.new.should == {}
end
end