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
|
2011-12-07 06:41:18 -05: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
|
|
|
|
Rhino::To.ruby(Rhino::JS::Undefined.instance).should be_nil
|
|
|
|
end
|
|
|
|
|
|
|
|
it "does return javascript object" do
|
|
|
|
Rhino::JS::NativeObject.new.tap do |js_obj|
|
|
|
|
Rhino::To.ruby(js_obj).tap do |rb_obj|
|
|
|
|
rb_obj.should be(js_obj)
|
2009-10-06 14:20:37 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2009-11-11 21:12:20 -05:00
|
|
|
it "wraps native javascript arrays into a ruby NativeArray wrapper" do
|
2011-12-07 13:59:39 -05:00
|
|
|
Rhino::JS::NativeArray.new([1,2,4].to_java).tap do |js_array|
|
|
|
|
Rhino::To.ruby(js_array).should == [1,2,4]
|
2009-11-11 21:12:20 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2011-12-07 13:59:39 -05:00
|
|
|
it "does return javascript function" do
|
2009-11-11 21:12:20 -05:00
|
|
|
|
2011-12-07 13:59:39 -05:00
|
|
|
klass = Class.new(Rhino::JS::BaseFunction)
|
2009-11-11 21:12:20 -05:00
|
|
|
|
2011-12-07 13:59:39 -05:00
|
|
|
klass.new.tap do |js_fn|
|
|
|
|
Rhino::To.ruby(js_fn).tap do |rb_fn|
|
|
|
|
rb_fn.should be(js_fn)
|
2009-11-11 21:12:20 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|
2009-10-06 14:20:37 -04:00
|
|
|
it "leaves native ruby objects alone" do
|
|
|
|
Object.new.tap do |o|
|
2011-12-07 06:41:18 -05:00
|
|
|
Rhino::To.ruby(o).should be(o)
|
2009-10-06 14:20:37 -04:00
|
|
|
end
|
|
|
|
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")
|
|
|
|
Rhino::JS::NativeJavaObject.new(scope, j_str, j_str.getClass()).tap do |o|
|
|
|
|
Rhino::To.ruby(o).should == "Hello World"
|
2009-10-06 14:20:37 -04:00
|
|
|
end
|
|
|
|
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-07 13:59:39 -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|
|
|
|
|
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-07 06:41:18 -05:00
|
|
|
Rhino::To.javascript([1,2,3,4,5]).tap do |a|
|
|
|
|
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-07 06:41:18 -05:00
|
|
|
Rhino::To.javascript({ :bare => true }).tap do |h|
|
|
|
|
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 !
|
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
|
|
|
|
Rhino::To.javascript([1,2,3,4,5], @scope).tap do |a|
|
|
|
|
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
|
|
|
|
Rhino::To.javascript({ :bare => true }, @scope).tap do |h|
|
|
|
|
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-07 13:59:39 -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)
|
2009-11-11 21:12:20 -05:00
|
|
|
f.call(nil, nil, nil, [7,6]).should be(42)
|
|
|
|
end
|
2011-12-07 13:59:39 -05:00
|
|
|
|
|
|
|
Rhino::To.javascript("foo,bar,baz".method(:split)).tap do |m|
|
2011-12-07 06:41:18 -05:00
|
|
|
m.should be_kind_of(Rhino::JS::Function)
|
|
|
|
Rhino::To.ruby(m.call(nil, nil, nil, ',')).should == ['foo', 'bar', 'baz']
|
2009-11-11 21:12:20 -05:00
|
|
|
end
|
|
|
|
end
|
2009-11-09 10:45:23 -05:00
|
|
|
|
|
|
|
it "creates a prototype for the object based on its class" do
|
2011-12-07 13:59:39 -05:00
|
|
|
Class.new.tap do |klass|
|
|
|
|
klass.class_eval do
|
2009-11-09 10:45:23 -05:00
|
|
|
def foo(one, two)
|
|
|
|
"1: #{one}, 2: #{two}"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2011-12-07 13:59:39 -05:00
|
|
|
Rhino::To.javascript(klass.new).tap do |o|
|
2011-12-07 06:41:18 -05:00
|
|
|
o.should be_kind_of(Rhino::RubyObject)
|
2009-11-09 10:45:23 -05:00
|
|
|
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
|
|
|
end
|
|
|
|
|
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
|