1
0
Fork 0
mirror of https://github.com/rails/execjs synced 2023-03-27 23:21:20 -04:00
execjs/test/test_execjs.rb

234 lines
6.9 KiB
Ruby
Raw Normal View History

2011-09-13 11:00:30 -04:00
# -*- coding: utf-8 -*-
2014-05-19 09:46:58 -04:00
require "minitest/autorun"
2011-09-13 11:00:30 -04:00
require "execjs/module"
2014-12-11 01:16:36 -05:00
require "json"
2011-02-06 20:10:05 -05:00
2011-09-13 11:00:30 -04:00
begin
require "execjs"
rescue ExecJS::RuntimeUnavailable => e
warn e
exit 2
end
2011-02-06 21:35:37 -05:00
2014-05-19 09:46:58 -04:00
if defined? Minitest::Test
Test = Minitest::Test
elsif defined? MiniTest::Unit::TestCase
Test = MiniTest::Unit::TestCase
end
class TestExecJS < Test
2011-02-06 21:35:37 -05:00
def test_runtime_available
2014-05-19 18:03:58 -04:00
runtime = ExecJS::ExternalRuntime.new(command: "nonexistent")
assert !runtime.available?
2011-02-06 21:35:37 -05:00
2014-05-19 18:03:58 -04:00
runtime = ExecJS::ExternalRuntime.new(command: "ruby")
assert runtime.available?
2011-02-06 21:35:37 -05:00
end
2011-02-11 12:39:06 -05:00
def test_runtime_assignment
original_runtime = ExecJS.runtime
2014-05-19 18:03:58 -04:00
runtime = ExecJS::ExternalRuntime.new(command: "nonexistent")
assert_raises(ExecJS::RuntimeUnavailable) { ExecJS.runtime = runtime }
assert_equal original_runtime, ExecJS.runtime
2014-05-19 18:03:58 -04:00
runtime = ExecJS::ExternalRuntime.new(command: "ruby")
ExecJS.runtime = runtime
assert_equal runtime, ExecJS.runtime
2011-09-13 11:00:30 -04:00
ensure
ExecJS.runtime = original_runtime
2011-02-11 12:39:06 -05:00
end
2011-02-11 12:51:59 -05:00
2011-02-12 13:27:08 -05:00
def test_context_call
context = ExecJS.compile("id = function(v) { return v; }")
assert_equal "bar", context.call("id", "bar")
end
def test_nested_context_call
context = ExecJS.compile("a = {}; a.b = {}; a.b.id = function(v) { return v; }")
assert_equal "bar", context.call("a.b.id", "bar")
end
def test_context_call_missing_function
context = ExecJS.compile("")
assert_raises ExecJS::ProgramError do
context.call("missing")
end
end
2011-09-13 11:00:30 -04:00
def test_exec
assert_nil ExecJS.exec("1")
assert_nil ExecJS.exec("return")
assert_nil ExecJS.exec("return null")
assert_nil ExecJS.exec("return function() {}")
assert_equal 0, ExecJS.exec("return 0")
assert_equal true, ExecJS.exec("return true")
assert_equal [1, 2], ExecJS.exec("return [1, 2]")
assert_equal "hello", ExecJS.exec("return 'hello'")
assert_equal({"a"=>1,"b"=>2}, ExecJS.exec("return {a:1,b:2}"))
assert_equal "café", ExecJS.exec("return 'café'")
assert_equal "", ExecJS.exec('return "☃"')
assert_equal "", ExecJS.exec('return "\u2603"')
assert_equal "\\", ExecJS.exec('return "\\\\"')
end
def test_eval
assert_nil ExecJS.eval("")
assert_nil ExecJS.eval(" ")
assert_nil ExecJS.eval("null")
assert_nil ExecJS.eval("function() {}")
assert_equal 0, ExecJS.eval("0")
assert_equal true, ExecJS.eval("true")
assert_equal [1, 2], ExecJS.eval("[1, 2]")
assert_equal [1, nil], ExecJS.eval("[1, function() {}]")
assert_equal "hello", ExecJS.eval("'hello'")
assert_equal ["red", "yellow", "blue"], ExecJS.eval("'red yellow blue'.split(' ')")
assert_equal({"a"=>1,"b"=>2}, ExecJS.eval("{a:1,b:2}"))
assert_equal({"a"=>true}, ExecJS.eval("{a:true,b:function (){}}"))
assert_equal "café", ExecJS.eval("'café'")
assert_equal "", ExecJS.eval('"☃"')
assert_equal "", ExecJS.eval('"\u2603"')
assert_equal "\\", ExecJS.eval('"\\\\"')
end
2014-12-11 01:14:01 -05:00
def test_json_values
[
nil,
true,
false,
1,
3.14,
"hello",
"\\",
"café",
"",
2014-12-11 14:40:41 -05:00
"\u{1f604}".force_encoding("UTF-8"), # Smiling emoji
"\u{1f1fa}\u{1f1f8}".force_encoding("UTF-8"), # US flag
2014-12-11 01:14:01 -05:00
[1, 2, 3],
[1, [2, 3]],
[1, [2, [3]]],
["red", "yellow", "blue"],
{ "a" => 1, "b" => 2},
{ "a" => 1, "b" => [2, 3]},
{ "a" => true }
].each do |value|
json_value = JSON.generate(value, quirks_mode: true)
assert_equal value, JSON.parse(json_value, quirks_mode: true)
assert_equal value, ExecJS.exec("return #{json_value}")
assert_equal value, ExecJS.eval("#{json_value}")
context = ExecJS.compile("function json(obj) { return JSON.stringify(obj); }")
assert_equal json_value, context.call("json", value)
context = ExecJS.compile("function id(obj) { return obj; }")
assert_equal value, context.call("id", value)
end
end
2014-05-19 09:45:03 -04:00
def test_encoding
utf8 = Encoding.find('UTF-8')
assert_equal utf8, ExecJS.exec("return 'hello'").encoding
assert_equal utf8, ExecJS.eval("'☃'").encoding
ascii = "'hello'".encode('US-ASCII')
result = ExecJS.eval(ascii)
assert_equal "hello", result
assert_equal utf8, result.encoding
2014-05-19 09:46:58 -04:00
assert_raises Encoding::UndefinedConversionError do
2014-05-19 09:45:03 -04:00
binary = "\xde\xad\xbe\xef".force_encoding("BINARY")
ExecJS.eval(binary)
2011-09-13 11:00:30 -04:00
end
2014-05-19 09:45:03 -04:00
end
2011-09-13 11:00:30 -04:00
2014-05-19 09:45:03 -04:00
def test_encoding_compile
utf8 = Encoding.find('UTF-8')
2011-09-13 11:00:30 -04:00
2014-05-19 09:45:03 -04:00
context = ExecJS.compile("foo = function(v) { return '¶' + v; }".encode("ISO8859-15"))
2011-09-13 11:00:30 -04:00
2014-05-19 09:45:03 -04:00
assert_equal utf8, context.exec("return foo('hello')").encoding
assert_equal utf8, context.eval("foo('☃')").encoding
2011-09-13 11:00:30 -04:00
2014-05-19 09:45:03 -04:00
ascii = "foo('hello')".encode('US-ASCII')
result = context.eval(ascii)
assert_equal "¶hello", result
assert_equal utf8, result.encoding
2011-09-13 11:00:30 -04:00
2014-05-19 09:46:58 -04:00
assert_raises Encoding::UndefinedConversionError do
2014-05-19 09:45:03 -04:00
binary = "\xde\xad\xbe\xef".force_encoding("BINARY")
context.eval(binary)
2011-09-13 11:00:30 -04:00
end
end
2014-12-11 13:53:11 -05:00
def test_surrogate_pairs
# Smiling emoji
2014-12-11 14:40:41 -05:00
str = "\u{1f604}".encode("UTF-8")
2014-12-11 13:53:11 -05:00
assert_equal 2, ExecJS.eval("'#{str}'.length")
assert_equal str, ExecJS.eval("'#{str}'")
# US flag emoji
2014-12-11 14:40:41 -05:00
str = "\u{1f1fa}\u{1f1f8}".force_encoding("UTF-8")
2014-12-11 13:53:11 -05:00
assert_equal 4, ExecJS.eval("'#{str}'.length")
assert_equal str, ExecJS.eval("'#{str}'")
end
def test_compile_anonymous_function
2011-09-13 11:00:30 -04:00
context = ExecJS.compile("foo = function() { return \"bar\"; }")
assert_equal "bar", context.exec("return foo()")
assert_equal "bar", context.eval("foo()")
assert_equal "bar", context.call("foo")
end
def test_compile_named_function
context = ExecJS.compile("function foo() { return \"bar\"; }")
assert_equal "bar", context.exec("return foo()")
assert_equal "bar", context.eval("foo()")
assert_equal "bar", context.call("foo")
end
2011-09-13 11:00:30 -04:00
def test_this_is_global_scope
assert_equal true, ExecJS.eval("this === (function() {return this})()")
assert_equal true, ExecJS.exec("return this === (function() {return this})()")
end
def test_commonjs_vars_are_undefined
assert ExecJS.eval("typeof module == 'undefined'")
assert ExecJS.eval("typeof exports == 'undefined'")
assert ExecJS.eval("typeof require == 'undefined'")
end
2012-05-20 13:26:19 -04:00
def test_console_is_undefined
assert ExecJS.eval("typeof console == 'undefined'")
end
2011-09-13 11:00:30 -04:00
def test_compile_large_scripts
body = "var foo = 'bar';\n" * 100_000
assert ExecJS.exec("function foo() {\n#{body}\n};\nreturn true")
end
2014-12-06 15:26:08 -05:00
def test_exec_syntax_error
2014-05-19 09:46:58 -04:00
assert_raises ExecJS::RuntimeError do
2011-09-13 11:00:30 -04:00
ExecJS.exec(")")
end
end
2014-12-06 15:26:08 -05:00
def test_eval_syntax_error
assert_raises ExecJS::RuntimeError do
ExecJS.eval(")")
end
end
2011-09-13 11:00:30 -04:00
def test_thrown_exception
2014-05-19 09:46:58 -04:00
assert_raises ExecJS::ProgramError do
2011-09-13 11:00:30 -04:00
ExecJS.exec("throw 'hello'")
end
end
2012-05-20 13:35:12 -04:00
def test_coffeescript
2014-10-14 16:31:25 -04:00
assert source = File.read(File.expand_path("../fixtures/coffee-script.js", __FILE__))
2012-05-20 13:35:12 -04:00
context = ExecJS.compile(source)
assert_equal 64, context.call("CoffeeScript.eval", "((x) -> x * x)(8)")
end
2011-02-06 20:10:05 -05:00
end