mirror of
https://github.com/rails/execjs
synced 2023-03-27 23:21:20 -04:00
Merge pull request #168 from sstephenson/more-tests
Test more values across all interfaces
This commit is contained in:
commit
898dc5e32e
1 changed files with 83 additions and 55 deletions
|
@ -55,42 +55,45 @@ class TestExecJS < Test
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_exec
|
{
|
||||||
assert_nil ExecJS.exec("1")
|
"function() {}" => nil,
|
||||||
assert_nil ExecJS.exec("return")
|
"0" => 0,
|
||||||
assert_nil ExecJS.exec("return null")
|
"null" => nil,
|
||||||
assert_nil ExecJS.exec("return function() {}")
|
"undefined" => nil,
|
||||||
assert_equal 0, ExecJS.exec("return 0")
|
"true" => true,
|
||||||
assert_equal true, ExecJS.exec("return true")
|
"false" => false,
|
||||||
assert_equal [1, 2], ExecJS.exec("return [1, 2]")
|
"[1, 2]" => [1, 2],
|
||||||
assert_equal "hello", ExecJS.exec("return 'hello'")
|
"[1, function() {}]" => [1, nil],
|
||||||
assert_equal({"a"=>1,"b"=>2}, ExecJS.exec("return {a:1,b:2}"))
|
"'hello'" => "hello",
|
||||||
assert_equal "café", ExecJS.exec("return 'café'")
|
"'red yellow blue'.split(' ')" => ["red", "yellow", "blue"],
|
||||||
assert_equal "☃", ExecJS.exec('return "☃"')
|
"{a:1,b:2}" => {"a"=>1,"b"=>2},
|
||||||
assert_equal "☃", ExecJS.exec('return "\u2603"')
|
"{a:true,b:function (){}}" => {"a"=>true},
|
||||||
assert_equal "\\", ExecJS.exec('return "\\\\"')
|
"'café'" => "café",
|
||||||
|
'"☃"' => "☃",
|
||||||
|
'"\u2603"' => "☃",
|
||||||
|
"'\u{1f604}'".encode("UTF-8") => "\u{1f604}".encode("UTF-8"), # Smiling emoji
|
||||||
|
"'\u{1f1fa}\u{1f1f8}'".encode("UTF-8") => "\u{1f1fa}\u{1f1f8}".encode("UTF-8"), # US flag
|
||||||
|
'"\\\\"' => "\\"
|
||||||
|
}.each_with_index do |(input, output), index|
|
||||||
|
define_method("test_exec_string_#{index}") do
|
||||||
|
assert_equal output, ExecJS.exec("return #{input}")
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_eval
|
define_method("test_eval_string_#{index}") do
|
||||||
assert_nil ExecJS.eval("")
|
assert_equal output, ExecJS.eval(input)
|
||||||
assert_nil ExecJS.eval(" ")
|
end
|
||||||
assert_nil ExecJS.eval("null")
|
|
||||||
assert_nil ExecJS.eval("function() {}")
|
define_method("test_compile_return_string_#{index}") do
|
||||||
assert_equal 0, ExecJS.eval("0")
|
context = ExecJS.compile("var a = #{input};")
|
||||||
assert_equal true, ExecJS.eval("true")
|
assert_equal output, context.eval("a")
|
||||||
assert_equal [1, 2], ExecJS.eval("[1, 2]")
|
end
|
||||||
assert_equal [1, nil], ExecJS.eval("[1, function() {}]")
|
|
||||||
assert_equal "hello", ExecJS.eval("'hello'")
|
define_method("test_compile_call_string_#{index}") do
|
||||||
assert_equal ["red", "yellow", "blue"], ExecJS.eval("'red yellow blue'.split(' ')")
|
context = ExecJS.compile("function a() { return #{input}; }")
|
||||||
assert_equal({"a"=>1,"b"=>2}, ExecJS.eval("{a:1,b:2}"))
|
assert_equal output, context.call("a")
|
||||||
assert_equal({"a"=>true}, ExecJS.eval("{a:true,b:function (){}}"))
|
end
|
||||||
assert_equal "café", ExecJS.eval("'café'")
|
|
||||||
assert_equal "☃", ExecJS.eval('"☃"')
|
|
||||||
assert_equal "☃", ExecJS.eval('"\u2603"')
|
|
||||||
assert_equal "\\", ExecJS.eval('"\\\\"')
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_json_values
|
|
||||||
[
|
[
|
||||||
nil,
|
nil,
|
||||||
true,
|
true,
|
||||||
|
@ -110,21 +113,46 @@ class TestExecJS < Test
|
||||||
{ "a" => 1, "b" => 2},
|
{ "a" => 1, "b" => 2},
|
||||||
{ "a" => 1, "b" => [2, 3]},
|
{ "a" => 1, "b" => [2, 3]},
|
||||||
{ "a" => true }
|
{ "a" => true }
|
||||||
].each do |value|
|
].each_with_index do |value, index|
|
||||||
json_value = JSON.generate(value, quirks_mode: true)
|
json_value = JSON.generate(value, quirks_mode: true)
|
||||||
|
|
||||||
|
define_method("test_json_value_#{index}") do
|
||||||
assert_equal value, JSON.parse(json_value, quirks_mode: true)
|
assert_equal value, JSON.parse(json_value, quirks_mode: true)
|
||||||
|
end
|
||||||
|
|
||||||
|
define_method("test_exec_value_#{index}") do
|
||||||
assert_equal value, ExecJS.exec("return #{json_value}")
|
assert_equal value, ExecJS.exec("return #{json_value}")
|
||||||
assert_equal value, ExecJS.eval("#{json_value}")
|
end
|
||||||
|
|
||||||
|
define_method("test_eval_value_#{index}") do
|
||||||
|
assert_equal value, ExecJS.eval("#{json_value}")
|
||||||
|
end
|
||||||
|
|
||||||
|
define_method("test_strinigfy_value_#{index}") do
|
||||||
context = ExecJS.compile("function json(obj) { return JSON.stringify(obj); }")
|
context = ExecJS.compile("function json(obj) { return JSON.stringify(obj); }")
|
||||||
assert_equal json_value, context.call("json", value)
|
assert_equal json_value, context.call("json", value)
|
||||||
|
end
|
||||||
|
|
||||||
|
define_method("test_call_value_#{index}") do
|
||||||
context = ExecJS.compile("function id(obj) { return obj; }")
|
context = ExecJS.compile("function id(obj) { return obj; }")
|
||||||
assert_equal value, context.call("id", value)
|
assert_equal value, context.call("id", value)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def test_eval_blank
|
||||||
|
assert_nil ExecJS.eval("")
|
||||||
|
assert_nil ExecJS.eval(" ")
|
||||||
|
assert_nil ExecJS.eval(" ")
|
||||||
|
end
|
||||||
|
|
||||||
|
def test_exec_return
|
||||||
|
assert_nil ExecJS.exec("return")
|
||||||
|
end
|
||||||
|
|
||||||
|
def test_exec_no_return
|
||||||
|
assert_nil ExecJS.exec("1")
|
||||||
|
end
|
||||||
|
|
||||||
def test_encoding
|
def test_encoding
|
||||||
utf8 = Encoding.find('UTF-8')
|
utf8 = Encoding.find('UTF-8')
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue