2010-04-21 19:09:13 -04:00
|
|
|
require "#{File.dirname(__FILE__)}/../spec_helper.rb"
|
|
|
|
|
|
|
|
include V8
|
|
|
|
|
|
|
|
describe C::Function do
|
|
|
|
it "is callable" do
|
2010-05-19 08:55:11 -04:00
|
|
|
Context.new do |cxt|
|
2010-04-21 19:09:13 -04:00
|
|
|
f = cxt.eval('(function() {return "Hello World"})', '<eval>');
|
2010-05-18 06:27:23 -04:00
|
|
|
f.call(nil).should == "Hello World"
|
2010-04-21 19:09:13 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
it "receives proper argument length from ruby" do
|
2010-05-19 08:55:11 -04:00
|
|
|
Context.new do |cxt|
|
2010-04-21 19:09:13 -04:00
|
|
|
f = cxt.eval('(function() {return arguments.length})', 'eval')
|
2010-05-18 06:27:23 -04:00
|
|
|
f.call(nil,1, 2, 3).should == 3
|
2010-04-21 19:09:13 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
it "maps all arguments from ruby" do
|
2010-05-19 08:55:11 -04:00
|
|
|
Context.new do |cxt|
|
2010-04-21 19:09:13 -04:00
|
|
|
f = cxt.eval('(function(one, two, three) {return one + two + three})', 'eval')
|
2010-05-18 06:27:23 -04:00
|
|
|
f.call(nil, 1,2,3).should == 6
|
2010-04-21 19:09:13 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
it "properly maps ruby objects back and forth from arguments to return value" do
|
2010-05-19 08:55:11 -04:00
|
|
|
Context.new do |cxt|
|
2010-04-21 19:09:13 -04:00
|
|
|
Object.new.tap do |this|
|
|
|
|
f = cxt.eval('(function() {return this})', 'eval')
|
2010-05-18 06:27:23 -04:00
|
|
|
f.call(this).should be(this)
|
2010-04-21 19:09:13 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
it "can be called outside of a context" do
|
2010-05-19 08:55:11 -04:00
|
|
|
Context.new do |cxt|
|
2010-04-21 19:09:13 -04:00
|
|
|
@f = cxt.eval('(function() {return "Call Me"})', 'eval')
|
|
|
|
end
|
2010-05-18 06:27:23 -04:00
|
|
|
@f.call(nil).should == "Call Me"
|
2010-04-21 19:09:13 -04:00
|
|
|
end
|
2010-05-19 08:55:11 -04:00
|
|
|
|
|
|
|
it "is reflected properly" do
|
|
|
|
Context.new do |cxt|
|
|
|
|
cxt['say'] = lambda {|word, times| word * times}
|
|
|
|
cxt.eval('say("Hello", 3)').should == "HelloHelloHello"
|
|
|
|
end
|
|
|
|
end
|
2010-04-21 19:09:13 -04:00
|
|
|
end
|