2011-12-07 13:59:39 -05:00
|
|
|
require File.expand_path('../spec_helper', File.dirname(__FILE__))
|
2009-10-06 14:20:37 -04:00
|
|
|
|
|
|
|
describe Rhino::To do
|
2011-12-07 13:59:39 -05:00
|
|
|
|
2009-10-06 14:20:37 -04:00
|
|
|
describe "ruby translation" do
|
2011-12-07 13:59:39 -05:00
|
|
|
|
2009-10-06 14:20:37 -04:00
|
|
|
it "converts javascript NOT_FOUND to ruby nil" do
|
2012-08-24 05:54:49 -04:00
|
|
|
Rhino.to_ruby(Rhino::JS::Scriptable::NOT_FOUND).should be nil
|
2009-10-06 14:20:37 -04:00
|
|
|
end
|
|
|
|
|
2011-12-07 13:59:39 -05:00
|
|
|
it "converts javascript undefined into nil" do
|
2012-08-24 05:54:49 -04:00
|
|
|
Rhino.to_ruby(Rhino::JS::Undefined.instance).should be nil
|
2011-12-07 13:59:39 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
it "does return javascript object" do
|
|
|
|
Rhino::JS::NativeObject.new.tap do |js_obj|
|
2012-08-24 05:54:49 -04:00
|
|
|
Rhino.to_ruby(js_obj).should be(js_obj)
|
2009-10-06 14:20:37 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2009-11-11 21:12:20 -05:00
|
|
|
it "wraps native javascript arrays into a ruby NativeArray wrapper" do
|
2012-08-24 05:54:49 -04:00
|
|
|
js_array = Rhino::JS::NativeArray.new([1,2,4].to_java)
|
|
|
|
Rhino.to_ruby(js_array).should == [1,2,4]
|
2009-11-11 21:12:20 -05:00
|
|
|
end
|
|
|
|
|
2012-08-24 05:54:49 -04:00
|
|
|
it "returns a javascript function" do
|
2011-12-07 13:59:39 -05:00
|
|
|
klass = Class.new(Rhino::JS::BaseFunction)
|
2012-08-24 05:54:49 -04:00
|
|
|
function = klass.new
|
|
|
|
Rhino.to_ruby(function).should be(function)
|
2009-11-11 21:12:20 -05:00
|
|
|
end
|
|
|
|
|
2009-10-06 14:20:37 -04:00
|
|
|
it "leaves native ruby objects alone" do
|
2012-08-24 05:54:49 -04:00
|
|
|
obj = Object.new
|
|
|
|
Rhino.to_ruby(obj).should be(obj)
|
2009-10-06 14:20:37 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it "it unwraps wrapped java objects" do
|
2011-12-07 06:41:18 -05:00
|
|
|
Rhino::Context.open do |cx|
|
2009-11-11 01:50:36 -05:00
|
|
|
scope = cx.scope
|
2011-12-07 13:59:39 -05:00
|
|
|
j_str = java.lang.String.new("Hello World")
|
2012-08-24 05:54:49 -04:00
|
|
|
native_obj = Rhino::JS::NativeJavaObject.new(scope, j_str, j_str.getClass())
|
|
|
|
Rhino.to_ruby(native_obj).should == "Hello World"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
if (Java::JavaClass.for_name('org.mozilla.javascript.ConsString') rescue nil)
|
|
|
|
it "converts a cons string" do
|
|
|
|
cons_string = org.mozilla.javascript.ConsString.new('1', '2')
|
|
|
|
Rhino.to_ruby(cons_string).should == '12'
|
2009-10-06 14:20:37 -04:00
|
|
|
end
|
|
|
|
end
|
2009-10-09 19:36:42 -04:00
|
|
|
|
2009-10-06 14:20:37 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
describe "javascript translation" do
|
2009-11-11 21:12:20 -05:00
|
|
|
|
|
|
|
it "passes primitives through to the js layer to let jruby and rhino do he thunking" do
|
2011-12-09 01:52:47 -05:00
|
|
|
Rhino.to_javascript(1).should be(1)
|
|
|
|
Rhino.to_javascript(2.5).should == 2.5
|
|
|
|
Rhino.to_javascript("foo").should == "foo"
|
|
|
|
Rhino.to_javascript(true).should be(true)
|
|
|
|
Rhino.to_javascript(false).should be(false)
|
|
|
|
Rhino.to_javascript(nil).should be_nil
|
2009-10-06 14:20:37 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it "leaves native javascript objects alone" do
|
2011-12-07 06:41:18 -05:00
|
|
|
Rhino::JS::NativeObject.new.tap do |o|
|
2011-12-09 01:52:47 -05:00
|
|
|
Rhino.to_javascript(o).should be(o)
|
2009-10-06 14:20:37 -04:00
|
|
|
end
|
|
|
|
end
|
2009-11-11 21:12:20 -05:00
|
|
|
|
|
|
|
it "converts ruby arrays into javascript arrays" do
|
2011-12-09 01:52:47 -05:00
|
|
|
Rhino.to_javascript([1,2,3,4,5]).tap do |a|
|
2011-12-07 06:41:18 -05:00
|
|
|
a.should be_kind_of(Rhino::JS::NativeArray)
|
2009-11-11 21:12:20 -05:00
|
|
|
a.get(0,a).should be(1)
|
|
|
|
a.get(1,a).should be(2)
|
|
|
|
a.get(2,a).should be(3)
|
|
|
|
a.get(3,a).should be(4)
|
2011-12-07 13:59:39 -05:00
|
|
|
a.prototype.should be_nil # this is how Rhino works !
|
2009-11-11 21:12:20 -05:00
|
|
|
end
|
|
|
|
end
|
2011-12-07 13:59:39 -05:00
|
|
|
|
2011-06-28 09:35:10 -04:00
|
|
|
it "converts ruby hashes into native objects" do
|
2011-12-09 01:52:47 -05:00
|
|
|
Rhino.to_javascript({ :bare => true }).tap do |h|
|
2011-12-07 06:41:18 -05:00
|
|
|
h.should be_kind_of(Rhino::JS::NativeObject)
|
2011-06-28 09:35:10 -04:00
|
|
|
h.get("bare", h).should be(true)
|
2011-12-07 13:59:39 -05:00
|
|
|
h.prototype.should be_nil # this is how Rhino works !
|
2012-02-09 11:37:38 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
it "converts deeply nested ruby hashes into native objects" do
|
|
|
|
hash = {
|
|
|
|
:array => [
|
|
|
|
{
|
|
|
|
:breed => "Pug"
|
|
|
|
},
|
|
|
|
{
|
|
|
|
:breed => "English Bulldog"
|
|
|
|
},
|
|
|
|
{
|
|
|
|
:breed => [
|
|
|
|
"Pug",
|
|
|
|
"Beagle"
|
|
|
|
]
|
|
|
|
}
|
|
|
|
]
|
|
|
|
}
|
|
|
|
|
|
|
|
Rhino.to_javascript(hash).tap do |h|
|
|
|
|
h.should be_kind_of(Rhino::JS::NativeObject)
|
|
|
|
|
|
|
|
a = h.get("array", h)
|
|
|
|
a.should be_kind_of(Rhino::JS::NativeArray)
|
|
|
|
|
|
|
|
element0 = a.get(0,a)
|
|
|
|
element0.should be_kind_of(Rhino::JS::NativeObject)
|
|
|
|
element0.get("breed", element0).should == "Pug"
|
|
|
|
|
|
|
|
element2 = a.get(2,a)
|
|
|
|
element2.should be_kind_of(Rhino::JS::NativeObject)
|
|
|
|
|
|
|
|
nested_array = element2.get("breed", element2)
|
|
|
|
nested_array.should be_kind_of(Rhino::JS::NativeArray)
|
|
|
|
nested_array.get(0,nested_array).should == "Pug"
|
|
|
|
nested_array.get(1,nested_array).should == "Beagle"
|
|
|
|
|
|
|
|
h.prototype.should be_nil # this is how Rhino works !
|
2011-06-28 09:35:10 -04:00
|
|
|
end
|
|
|
|
end
|
2009-11-11 21:12:20 -05:00
|
|
|
|
2011-12-07 13:59:39 -05:00
|
|
|
describe "with a scope" do
|
|
|
|
|
|
|
|
before do
|
|
|
|
factory = Rhino::JS::ContextFactory.new
|
|
|
|
context = nil
|
|
|
|
factory.call do |ctx|
|
|
|
|
context = ctx
|
|
|
|
@scope = context.initStandardObjects(nil, false)
|
|
|
|
end
|
|
|
|
factory.enterContext(context)
|
|
|
|
end
|
|
|
|
|
|
|
|
after do
|
|
|
|
Rhino::JS::Context.exit
|
|
|
|
end
|
|
|
|
|
|
|
|
it "converts ruby arrays into javascript arrays" do
|
2011-12-09 01:52:47 -05:00
|
|
|
Rhino.to_javascript([1,2,3,4,5], @scope).tap do |a|
|
2011-12-07 13:59:39 -05:00
|
|
|
a.should be_kind_of(Rhino::JS::NativeArray)
|
|
|
|
a.get(0,a).should be(1)
|
|
|
|
a.get(1,a).should be(2)
|
|
|
|
a.get(2,a).should be(3)
|
|
|
|
a.get(3,a).should be(4)
|
|
|
|
a.prototype.should_not be_nil
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
it "converts ruby hashes into native objects" do
|
2011-12-09 01:52:47 -05:00
|
|
|
Rhino.to_javascript({ :bare => true }, @scope).tap do |h|
|
2011-12-07 13:59:39 -05:00
|
|
|
h.should be_kind_of(Rhino::JS::NativeObject)
|
|
|
|
h.get("bare", h).should be(true)
|
|
|
|
h.prototype.should_not be_nil
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|
2009-11-11 21:12:20 -05:00
|
|
|
it "converts procs and methods into native functions" do
|
2011-12-09 01:52:47 -05:00
|
|
|
Rhino.to_javascript(lambda {|lhs,rhs| lhs * rhs}).tap do |f|
|
2011-12-07 06:41:18 -05:00
|
|
|
f.should be_kind_of(Rhino::JS::Function)
|
2012-09-08 09:39:57 -04:00
|
|
|
f.call(context, scope, nil, [7, 6].to_java).should be(42)
|
2009-11-11 21:12:20 -05:00
|
|
|
end
|
2011-12-07 13:59:39 -05:00
|
|
|
|
2012-02-15 06:36:16 -05:00
|
|
|
Rhino.to_javascript("foo,bar,baz".method(:split)).tap do |f|
|
|
|
|
f.should be_kind_of(Rhino::JS::Function)
|
2012-09-08 09:39:57 -04:00
|
|
|
Rhino.to_ruby(f.call(context, scope, nil, [','].to_java)).should == ['foo', 'bar', 'baz']
|
2009-11-11 21:12:20 -05:00
|
|
|
end
|
|
|
|
end
|
2009-11-09 10:45:23 -05:00
|
|
|
|
2011-12-08 11:28:23 -05:00
|
|
|
# it "creates a prototype for the object based on its class" do
|
|
|
|
# klass = Class.new do
|
|
|
|
# def foo(one, two)
|
|
|
|
# "1: #{one}, 2: #{two}"
|
|
|
|
# end
|
|
|
|
# end
|
|
|
|
#
|
2011-12-09 01:52:47 -05:00
|
|
|
# Rhino.to_javascript(klass.new).tap do |o|
|
2011-12-08 11:28:23 -05:00
|
|
|
# o.should be_kind_of(Rhino::RubyObject)
|
|
|
|
# o.prototype.tap do |p|
|
|
|
|
# p.should_not be_nil
|
|
|
|
# p.get("foo", p).should_not be_nil
|
|
|
|
# p.get("toString", p).should_not be_nil
|
|
|
|
# end
|
|
|
|
# end
|
|
|
|
# end
|
2011-12-07 13:59:39 -05:00
|
|
|
|
2009-11-11 21:12:20 -05:00
|
|
|
end
|
2011-12-07 06:41:18 -05:00
|
|
|
|
2011-06-08 16:01:27 -04:00
|
|
|
end
|