2011-02-09 01:13:30 -05:00
|
|
|
# -*- coding: utf-8 -*-
|
2011-06-01 11:49:54 -04:00
|
|
|
require "execjs_test"
|
|
|
|
|
|
|
|
class TestRuntime < Test::Unit::TestCase
|
|
|
|
def setup
|
|
|
|
@runtime = ExecJS::Runtimes.autodetect
|
|
|
|
end
|
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}"))
|
2011-02-10 10:16:11 -05:00
|
|
|
assert_equal "café", @runtime.exec("return 'café'")
|
2011-05-12 11:24:21 -04:00
|
|
|
assert_equal "☃", @runtime.exec('return "☃"')
|
2011-08-01 13:36:19 -04:00
|
|
|
assert_equal "☃", @runtime.exec('return "\u2603"')
|
2011-03-10 18:19:18 -05:00
|
|
|
assert_equal "\\", @runtime.exec('return "\\\\"')
|
2011-02-06 19:59:37 -05:00
|
|
|
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]")
|
2011-08-03 10:51:25 -04:00
|
|
|
assert_equal [1, nil], @runtime.eval("[1, function() {}]")
|
2011-02-06 19:59:37 -05:00
|
|
|
assert_equal "hello", @runtime.eval("'hello'")
|
|
|
|
assert_equal({"a"=>1,"b"=>2}, @runtime.eval("{a:1,b:2}"))
|
2011-08-03 10:51:25 -04:00
|
|
|
assert_equal({"a"=>true}, @runtime.eval("{a:true,b:function (){}}"))
|
2011-02-10 10:16:11 -05:00
|
|
|
assert_equal "café", @runtime.eval("'café'")
|
2011-05-12 11:24:21 -04:00
|
|
|
assert_equal "☃", @runtime.eval('"☃"')
|
2011-08-01 13:36:19 -04:00
|
|
|
assert_equal "☃", @runtime.eval('"\u2603"')
|
2011-03-10 18:19:18 -05:00
|
|
|
assert_equal "\\", @runtime.eval('"\\\\"')
|
2011-02-06 19:59:37 -05:00
|
|
|
end
|
|
|
|
|
2011-05-12 12:06:17 -04:00
|
|
|
if defined? Encoding
|
|
|
|
def test_encoding
|
|
|
|
utf8 = Encoding.find('UTF-8')
|
2011-05-12 14:12:38 -04:00
|
|
|
|
2011-05-12 12:06:17 -04:00
|
|
|
assert_equal utf8, @runtime.exec("return 'hello'").encoding
|
|
|
|
assert_equal utf8, @runtime.eval("'☃'").encoding
|
2011-05-12 14:12:38 -04:00
|
|
|
|
|
|
|
ascii = "'hello'".encode('US-ASCII')
|
|
|
|
result = @runtime.eval(ascii)
|
|
|
|
assert_equal "hello", result
|
|
|
|
assert_equal utf8, result.encoding
|
|
|
|
|
|
|
|
assert_raise Encoding::UndefinedConversionError do
|
|
|
|
binary = "\xde\xad\xbe\xef".force_encoding("BINARY")
|
|
|
|
@runtime.eval(binary)
|
|
|
|
end
|
2011-05-12 12:06:17 -04:00
|
|
|
end
|
2011-06-05 14:00:59 -04:00
|
|
|
|
|
|
|
def test_encoding_compile
|
|
|
|
utf8 = Encoding.find('UTF-8')
|
|
|
|
|
|
|
|
context = @runtime.compile("foo = function(v) { return '¶' + v; }".encode("ISO8859-15"))
|
|
|
|
|
|
|
|
assert_equal utf8, context.exec("return foo('hello')").encoding
|
|
|
|
assert_equal utf8, context.eval("foo('☃')").encoding
|
|
|
|
|
|
|
|
ascii = "foo('hello')".encode('US-ASCII')
|
|
|
|
result = context.eval(ascii)
|
|
|
|
assert_equal "¶hello", result
|
|
|
|
assert_equal utf8, result.encoding
|
|
|
|
|
|
|
|
assert_raise Encoding::UndefinedConversionError do
|
|
|
|
binary = "\xde\xad\xbe\xef".force_encoding("BINARY")
|
2011-06-07 22:36:06 -04:00
|
|
|
context.eval(binary)
|
2011-06-05 14:00:59 -04:00
|
|
|
end
|
|
|
|
end
|
2011-05-12 12:06:17 -04:00
|
|
|
end
|
|
|
|
|
2011-02-11 12:39:06 -05:00
|
|
|
def test_compile
|
|
|
|
context = @runtime.compile("foo = function() { return \"bar\"; }")
|
|
|
|
assert_equal "bar", context.exec("return foo()")
|
|
|
|
assert_equal "bar", context.eval("foo()")
|
2011-02-12 13:27:08 -05:00
|
|
|
assert_equal "bar", context.call("foo")
|
2011-02-11 12:39:06 -05:00
|
|
|
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-09-12 17:56:27 -04:00
|
|
|
def test_commonjs_vars_are_undefined
|
|
|
|
assert @runtime.eval("typeof module == 'undefined'")
|
|
|
|
assert @runtime.eval("typeof exports == 'undefined'")
|
|
|
|
assert @runtime.eval("typeof require == 'undefined'")
|
|
|
|
end
|
|
|
|
|
2011-06-07 22:57:12 -04:00
|
|
|
def test_compile_large_scripts
|
|
|
|
body = "var foo = 'bar';\n" * 100_000
|
|
|
|
assert @runtime.exec("function foo() {\n#{body}\n};\nreturn true")
|
|
|
|
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
|