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

Fix a test failure and minitest warnings

This commit is contained in:
Jean Boussier 2021-05-07 10:22:31 +02:00
parent 8357a6973c
commit 70e8498ebc
2 changed files with 20 additions and 9 deletions

View file

@ -6,6 +6,7 @@ module ExecJS
def initialize(runtime, source = "", options={}) def initialize(runtime, source = "", options={})
source = encode(source) source = encode(source)
@context = ::MiniRacer::Context.new @context = ::MiniRacer::Context.new
@context.eval("delete this.console");
translate do translate do
@context.eval(source) @context.eval(source)
end end

View file

@ -104,21 +104,21 @@ class TestExecJS < Test
'"\\\\"' => "\\" '"\\\\"' => "\\"
}.each_with_index do |(input, output), index| }.each_with_index do |(input, output), index|
define_method("test_exec_string_#{index}") do define_method("test_exec_string_#{index}") do
assert_equal output, ExecJS.exec("return #{input}") assert_output output, ExecJS.exec("return #{input}")
end end
define_method("test_eval_string_#{index}") do define_method("test_eval_string_#{index}") do
assert_equal output, ExecJS.eval(input) assert_output output, ExecJS.eval(input)
end end
define_method("test_compile_return_string_#{index}") do define_method("test_compile_return_string_#{index}") do
context = ExecJS.compile("var a = #{input};") context = ExecJS.compile("var a = #{input};")
assert_equal output, context.eval("a") assert_output output, context.eval("a")
end end
define_method("test_compile_call_string_#{index}") do define_method("test_compile_call_string_#{index}") do
context = ExecJS.compile("function a() { return #{input}; }") context = ExecJS.compile("function a() { return #{input}; }")
assert_equal output, context.call("a") assert_output output, context.call("a")
end end
end end
@ -145,25 +145,25 @@ class TestExecJS < Test
json_value = JSON.generate(value, quirks_mode: true) json_value = JSON.generate(value, quirks_mode: true)
define_method("test_json_value_#{index}") do define_method("test_json_value_#{index}") do
assert_equal value, JSON.parse(json_value, quirks_mode: true) assert_output value, JSON.parse(json_value, quirks_mode: true)
end end
define_method("test_exec_value_#{index}") do define_method("test_exec_value_#{index}") do
assert_equal value, ExecJS.exec("return #{json_value}") assert_output value, ExecJS.exec("return #{json_value}")
end end
define_method("test_eval_value_#{index}") do define_method("test_eval_value_#{index}") do
assert_equal value, ExecJS.eval("#{json_value}") assert_output value, ExecJS.eval("#{json_value}")
end end
define_method("test_strinigfy_value_#{index}") do 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_output json_value, context.call("json", value)
end end
define_method("test_call_value_#{index}") do 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_output value, context.call("id", value)
end end
end end
@ -421,4 +421,14 @@ class TestExecJS < Test
assert_equal "function foo(bar){return bar}", assert_equal "function foo(bar){return bar}",
context.call("uglify", "function foo(bar) {\n return bar;\n}") context.call("uglify", "function foo(bar) {\n return bar;\n}")
end end
private
def assert_output(expected, actual)
if expected.nil?
assert_nil actual
else
assert_equal expected, actual
end
end
end end