mirror of
https://github.com/rubyjs/therubyrhino
synced 2023-03-27 23:21:34 -04:00
extract specs that can be shared into a common repository so that it can be used by other implementations
This commit is contained in:
parent
9ca6bb7374
commit
efa9a75d37
4 changed files with 5 additions and 265 deletions
|
@ -95,6 +95,10 @@ module Rhino
|
|||
raise Rhino::RhinoError, e
|
||||
end if open?
|
||||
end
|
||||
|
||||
def evaluate(*args) # :nodoc:
|
||||
self.eval(*args)
|
||||
end
|
||||
|
||||
# Read the contents of <tt>filename</tt> and evaluate it as javascript. Returns the result of evaluating the
|
||||
# javascript. e.g.
|
||||
|
@ -105,7 +109,7 @@ module Rhino
|
|||
#
|
||||
def load(filename)
|
||||
File.open(filename) do |file|
|
||||
eval file, filename, 1
|
||||
evaluate file, filename, 1
|
||||
end
|
||||
end
|
||||
|
||||
|
|
|
@ -4,32 +4,6 @@ include Rhino
|
|||
|
||||
describe Rhino::Context do
|
||||
|
||||
it "can evaluate some javascript" do
|
||||
Context.open do |cxt|
|
||||
cxt.eval("5 + 3").should == 8
|
||||
end
|
||||
end
|
||||
|
||||
it "treats nil and the empty string as the same thing when it comes to eval" do
|
||||
Context.open do |cxt|
|
||||
cxt.eval(nil).should == cxt.eval('')
|
||||
end
|
||||
end
|
||||
|
||||
it "can embed primitive ruby object into javascript" do
|
||||
Context.open do |cxt|
|
||||
cxt['foo'] = "Hello World"
|
||||
cxt.eval("foo").should == "Hello World"
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
it "won't let you do some operations unless the context is open" do
|
||||
Context.new.tap do |closed|
|
||||
lambda {closed.eval('1')}.should raise_error(ContextError)
|
||||
end
|
||||
end
|
||||
|
||||
describe "Initalizing Standard Javascript Objects" do
|
||||
it "provides the standard objects without java integration by default" do
|
||||
Context.open do |cxt|
|
||||
|
@ -69,101 +43,4 @@ describe Rhino::Context do
|
|||
end
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
it "can call ruby functions from javascript" do
|
||||
Context.open do |cxt|
|
||||
cxt["say"] = lambda {|word, times| word * times}
|
||||
cxt.eval("say('Hello',2)").should == "HelloHello"
|
||||
end
|
||||
end
|
||||
|
||||
it "can eval javascript with a given ruby object as the scope." do
|
||||
# pending
|
||||
scope = Class.new.class_eval do
|
||||
def plus(lhs, rhs)
|
||||
lhs + rhs
|
||||
end
|
||||
|
||||
def minus(lhs, rhs)
|
||||
lhs - rhs
|
||||
end
|
||||
|
||||
new
|
||||
end
|
||||
|
||||
Context.open(:with => scope) do |cxt|
|
||||
cxt.eval("plus(1,2)").should == 3
|
||||
cxt.eval("minus(10, 20)").should == -10
|
||||
cxt.eval("this").should be(scope)
|
||||
end
|
||||
end
|
||||
|
||||
it "extends object to allow for the arbitrary execution of javascript with any object as the scope" do
|
||||
Class.new.class_eval do
|
||||
|
||||
def initialize
|
||||
@lhs = 5
|
||||
end
|
||||
|
||||
def timesfive(rhs)
|
||||
@lhs * rhs
|
||||
end
|
||||
|
||||
new.eval_js("timesfive(6)").should == 30
|
||||
end
|
||||
end
|
||||
|
||||
it "can limit the number of instructions that are executed in the context" do
|
||||
lambda {
|
||||
Context.open do |cxt|
|
||||
cxt.instruction_limit = 100 * 1000
|
||||
timeout(1) do
|
||||
cxt.eval('while (true);')
|
||||
end
|
||||
end
|
||||
}.should raise_error(Rhino::RunawayScriptError)
|
||||
end
|
||||
|
||||
it "has a private constructor" do
|
||||
lambda {
|
||||
Context.new(nil)
|
||||
}.should raise_error
|
||||
end
|
||||
|
||||
describe "loading javascript source into the interpreter" do
|
||||
|
||||
it "can take an IO object in the eval method instead of a string" do
|
||||
source = StringIO.new(<<-EOJS)
|
||||
/*
|
||||
* we want to have a fairly verbose function so that we can be assured tha
|
||||
* we overflow the buffer size so that we see that the reader is chunking
|
||||
* it's payload in at least several fragments.
|
||||
*
|
||||
* That's why we're wasting space here
|
||||
*/
|
||||
function five() {
|
||||
return 5
|
||||
}
|
||||
foo = 'bar'
|
||||
five();
|
||||
EOJS
|
||||
Context.open do |cxt|
|
||||
cxt.eval(source, "StringIO").should == 5
|
||||
cxt['foo'].should == "bar"
|
||||
end
|
||||
end
|
||||
|
||||
it "can load a file into the runtime" do
|
||||
mock(:JavascriptSourceFile).tap do |file|
|
||||
File.should_receive(:open).with("path/to/mysource.js").and_yield(file)
|
||||
Context.open do |cxt|
|
||||
cxt.should_receive(:eval).with(file, "path/to/mysource.js", 1)
|
||||
cxt.load("path/to/mysource.js")
|
||||
end
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
end
|
||||
end
|
|
@ -1,55 +0,0 @@
|
|||
|
||||
require File.dirname(__FILE__) + '/../spec_helper'
|
||||
|
||||
include Rhino
|
||||
|
||||
describe Rhino::NativeObject do
|
||||
|
||||
before(:each) do
|
||||
@j = J::NativeObject.new
|
||||
@o = NativeObject.new(@j)
|
||||
end
|
||||
|
||||
it "wraps a native javascript object" do
|
||||
@o["foo"] = 'bar'
|
||||
@j.get("foo", @j).should == "bar"
|
||||
@j.put("blue",@j, "blam")
|
||||
@o["blue"].should == "blam"
|
||||
end
|
||||
|
||||
it "doesn't matter if you use a symbol or a string to set a value" do
|
||||
@o[:foo] = "bar"
|
||||
@o['foo'].should == "bar"
|
||||
@o['baz'] = "bang"
|
||||
@o[:baz].should == "bang"
|
||||
end
|
||||
|
||||
it "returns nil when the value is null, null, or not defined" do
|
||||
@o[:foo].should be_nil
|
||||
end
|
||||
|
||||
|
||||
it "traverses the prototype chain when hash accessing properties from the ruby object" do
|
||||
Rhino::Context.open do |cxt|
|
||||
cxt.eval(<<EOJS)['bar'].should == "baz"
|
||||
function Foo() {}
|
||||
Foo.prototype.bar = 'baz'
|
||||
new Foo()
|
||||
EOJS
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
describe Enumerable do
|
||||
it "enumerates according to native keys and values" do
|
||||
@j.put("foo", @j, 'bar')
|
||||
@j.put("bang", @j, 'baz')
|
||||
@j.put(5, @j, 'flip')
|
||||
@o.inject({}) {|i,p| k,v = p; i.tap {i[k] = v}}.should == {"foo" => 'bar', "bang" => 'baz', 5 => 'flip'}
|
||||
end
|
||||
|
||||
it "should traverse prototype chain when enumerating keys and values"
|
||||
|
||||
end
|
||||
|
||||
end
|
|
@ -1,86 +0,0 @@
|
|||
require File.dirname(__FILE__) + '/../spec_helper'
|
||||
|
||||
describe Rhino::RubyObject do
|
||||
|
||||
before(:each) do
|
||||
@class = Class.new
|
||||
@instance = @class.new
|
||||
end
|
||||
|
||||
it "can call public locally defined ruby methods" do
|
||||
class_eval do
|
||||
def voo
|
||||
"doo"
|
||||
end
|
||||
end
|
||||
eval("o.voo").should_not be_nil
|
||||
eval("o.voo()").should == "doo"
|
||||
end
|
||||
|
||||
it "translates ruby naming conventions into javascript naming conventions, but you can still access them by their original names" do
|
||||
class_eval do
|
||||
def my_special_method
|
||||
"hello"
|
||||
end
|
||||
end
|
||||
eval("o.mySpecialMethod").should_not be_nil
|
||||
eval("o.mySpecialMethod()").should == "hello"
|
||||
eval("o.my_special_method").should_not be_nil
|
||||
eval("o.my_special_method()").should == "hello"
|
||||
end
|
||||
|
||||
it "hides methods not defined directly on this instance's class" do
|
||||
class_eval do
|
||||
def bar
|
||||
end
|
||||
end
|
||||
eval("o.to_s").should be_nil
|
||||
end
|
||||
|
||||
it "translated camel case properties are enumerated by default, but perl case are not" do
|
||||
class_eval do
|
||||
def foo_bar
|
||||
end
|
||||
|
||||
def baz_bang
|
||||
end
|
||||
end
|
||||
pending "why the hell isn't the return value of getIds() being respected?!?"
|
||||
eval(<<-EOJS).should == ["fooBar,bazBang"]
|
||||
var names = [];
|
||||
for (var p in o) {
|
||||
names.push(p);
|
||||
}
|
||||
names;
|
||||
EOJS
|
||||
end
|
||||
|
||||
it "will see a method that appears after the wrapper was first created" do
|
||||
Rhino::Context.open do |cxt|
|
||||
cxt['o'] = @instance
|
||||
class_eval do
|
||||
def bar
|
||||
"baz!"
|
||||
end
|
||||
end
|
||||
cxt.eval("o.bar").should_not be_nil
|
||||
cxt.eval("o.bar()").should == "baz!"
|
||||
end
|
||||
end
|
||||
|
||||
it "treats ruby methods that have an arity of 0 as javascript properties by default"
|
||||
|
||||
it "will call ruby accesssor function when setting a property from javascript"
|
||||
|
||||
def eval(str)
|
||||
Rhino::Context.open do |cxt|
|
||||
cxt['puts'] = lambda {|o| puts o.inspect}
|
||||
cxt['o'] = @instance
|
||||
cxt.eval(str)
|
||||
end
|
||||
end
|
||||
|
||||
def class_eval(&body)
|
||||
@class.class_eval &body
|
||||
end
|
||||
end
|
Loading…
Reference in a new issue