2011-02-09 01:13:30 -05:00
|
|
|
# -*- coding: utf-8 -*-
|
2011-02-06 19:59:37 -05:00
|
|
|
require "execjs"
|
|
|
|
require "test/unit"
|
|
|
|
|
2011-02-06 20:39:30 -05:00
|
|
|
module TestRuntime
|
2011-02-06 19:59:37 -05:00
|
|
|
def test_exec
|
|
|
|
assert_nil @runtime.exec("1")
|
|
|
|
assert_nil @runtime.exec("return")
|
|
|
|
assert_nil @runtime.exec("return null")
|
|
|
|
assert_nil @runtime.exec("return function() {}")
|
|
|
|
assert_equal 0, @runtime.exec("return 0")
|
|
|
|
assert_equal true, @runtime.exec("return true")
|
|
|
|
assert_equal [1, 2], @runtime.exec("return [1, 2]")
|
|
|
|
assert_equal "hello", @runtime.exec("return 'hello'")
|
|
|
|
assert_equal({"a"=>1,"b"=>2}, @runtime.exec("return {a:1,b:2}"))
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_eval
|
|
|
|
assert_nil @runtime.eval("")
|
|
|
|
assert_nil @runtime.eval(" ")
|
|
|
|
assert_nil @runtime.eval("null")
|
|
|
|
assert_nil @runtime.eval("function() {}")
|
|
|
|
assert_equal 0, @runtime.eval("0")
|
|
|
|
assert_equal true, @runtime.eval("true")
|
|
|
|
assert_equal [1, 2], @runtime.eval("[1, 2]")
|
|
|
|
assert_equal "hello", @runtime.eval("'hello'")
|
|
|
|
assert_equal({"a"=>1,"b"=>2}, @runtime.eval("{a:1,b:2}"))
|
|
|
|
end
|
|
|
|
|
2011-02-09 01:20:27 -05:00
|
|
|
def test_this_is_global_scope
|
|
|
|
assert_equal true, @runtime.eval("this === (function() {return this})()")
|
|
|
|
assert_equal true, @runtime.exec("return this === (function() {return this})()")
|
|
|
|
end
|
|
|
|
|
2011-02-06 19:59:37 -05:00
|
|
|
def test_syntax_error
|
|
|
|
assert_raise ExecJS::RuntimeError do
|
|
|
|
@runtime.exec(")")
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_thrown_exception
|
|
|
|
assert_raise ExecJS::ProgramError do
|
|
|
|
@runtime.exec("throw 'hello'")
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2011-02-06 20:39:30 -05:00
|
|
|
|
2011-02-09 01:30:39 -05:00
|
|
|
def test_runtime(name, description = nil)
|
2011-02-06 21:35:37 -05:00
|
|
|
runtime = ExecJS::Runtimes.const_get(name)
|
2011-02-09 01:30:39 -05:00
|
|
|
title = [name, description].join(" ")
|
2011-02-09 01:13:30 -05:00
|
|
|
ok = runtime.available?
|
|
|
|
|
2011-02-09 01:30:39 -05:00
|
|
|
warn "%s %-16s %s" %
|
2011-02-09 01:13:30 -05:00
|
|
|
if ok
|
2011-02-09 01:30:39 -05:00
|
|
|
["✓", title, "Found"]
|
2011-02-09 01:13:30 -05:00
|
|
|
else
|
2011-02-09 01:30:39 -05:00
|
|
|
[" ", title, "Not found"]
|
2011-02-09 01:13:30 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
if ok
|
2011-02-06 21:35:37 -05:00
|
|
|
Class.new(Test::Unit::TestCase) do
|
|
|
|
(class << self; self end).send(:define_method, :name) do
|
|
|
|
"#{name}Test"
|
|
|
|
end
|
2011-02-06 20:44:32 -05:00
|
|
|
|
2011-02-06 21:35:37 -05:00
|
|
|
include TestRuntime
|
2011-02-06 20:39:30 -05:00
|
|
|
|
2011-02-06 21:35:37 -05:00
|
|
|
define_method(:setup) do
|
|
|
|
instance_variable_set(:@runtime, runtime)
|
|
|
|
end
|
|
|
|
end
|
2011-02-06 21:13:24 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2011-02-09 01:13:30 -05:00
|
|
|
warn "Runtime support:"
|
2011-02-09 01:30:39 -05:00
|
|
|
test_runtime :RubyRacer, "(V8)"
|
|
|
|
test_runtime :RubyRhino
|
2011-02-06 23:52:13 -05:00
|
|
|
test_runtime :V8
|
2011-02-09 01:30:39 -05:00
|
|
|
test_runtime :Node, "(V8)"
|
|
|
|
test_runtime :JavaScriptCore
|
2011-02-06 21:35:37 -05:00
|
|
|
test_runtime :Spidermonkey
|
2011-02-07 18:04:05 -05:00
|
|
|
test_runtime :JScript
|
2011-02-09 01:13:30 -05:00
|
|
|
warn ""
|