1
0
Fork 0
mirror of https://github.com/rubyjs/therubyracer synced 2023-03-27 23:21:42 -04:00
therubyracer/spec/c/function_spec.rb

37 lines
1.2 KiB
Ruby
Raw Normal View History

2012-05-18 11:56:15 -05:00
require 'spec_helper'
describe V8::C::Function do
it "can be called" do
fn = run '(function() {return "foo"})'
2012-06-12 07:06:25 -05:00
fn.Call(@cxt.Global(), []).Utf8Value().should eql "foo"
end
it "can be called with arguments and context" do
fn = run '(function(one, two, three) {this.one = one; this.two = two; this.three = three})'
one = V8::C::Object::New()
two = V8::C::Object::New()
fn.Call(@cxt.Global(), [one, two, 3])
@cxt.Global().Get("one").should eql one
@cxt.Global().Get("two").should eql two
@cxt.Global().Get("three").should eql 3
2012-05-18 11:56:15 -05:00
end
it "can be called as a constructor" do
fn = run '(function() {this.foo = "foo"})'
fn.NewInstance().Get(V8::C::String::New('foo')).Utf8Value().should eql "foo"
2012-05-18 11:56:15 -05:00
end
it "can be called as a constructor with arguments" do
fn = run '(function(foo) {this.foo = foo})'
2012-06-12 07:06:25 -05:00
object = fn.NewInstance([V8::C::String::New("bar")])
object.Get(V8::C::String::New('foo')).Utf8Value().should eql "bar"
2012-05-18 11:56:15 -05:00
end
def run(source)
source = V8::C::String::New(source.to_s)
filename = V8::C::String::New("<eval>")
script = V8::C::Script::New(source, filename)
result = script.Run()
result.kind_of?(V8::C::String) ? result.Utf8Value() : result
end
end