2011-06-01 11:49:54 -04:00
|
|
|
require "execjs_test"
|
2011-02-06 20:10:05 -05:00
|
|
|
|
|
|
|
class TestExecJS < Test::Unit::TestCase
|
|
|
|
def test_exec
|
|
|
|
assert_equal true, ExecJS.exec("return true")
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_eval
|
|
|
|
assert_equal ["red", "yellow", "blue"], ExecJS.eval("'red yellow blue'.split(' ')")
|
|
|
|
end
|
2011-02-06 21:35:37 -05:00
|
|
|
|
|
|
|
def test_runtime_available
|
2011-02-06 21:57:51 -05:00
|
|
|
runtime = ExecJS::ExternalRuntime.new(:command => "nonexistent")
|
2011-02-06 23:52:13 -05:00
|
|
|
assert !runtime.available?
|
2011-02-06 21:35:37 -05:00
|
|
|
|
2011-02-06 21:57:51 -05:00
|
|
|
runtime = ExecJS::ExternalRuntime.new(:command => "ruby")
|
2011-02-06 23:52:13 -05:00
|
|
|
assert runtime.available?
|
2011-02-06 21:35:37 -05:00
|
|
|
end
|
2011-02-11 12:39:06 -05:00
|
|
|
|
2011-05-17 11:08:41 -04:00
|
|
|
def test_runtime_assignment
|
|
|
|
original_runtime = ExecJS.runtime
|
|
|
|
runtime = ExecJS::ExternalRuntime.new(:command => "nonexistent")
|
|
|
|
assert_raises(ExecJS::RuntimeUnavailable) { ExecJS.runtime = runtime }
|
|
|
|
assert_equal original_runtime, ExecJS.runtime
|
|
|
|
|
|
|
|
runtime = ExecJS::ExternalRuntime.new(:command => "ruby")
|
|
|
|
ExecJS.runtime = runtime
|
|
|
|
assert_equal runtime, ExecJS.runtime
|
|
|
|
end
|
|
|
|
|
2011-02-11 12:39:06 -05:00
|
|
|
def test_compile
|
|
|
|
context = ExecJS.compile("foo = function() { return \"bar\"; }")
|
|
|
|
assert_equal "bar", context.exec("return foo()")
|
|
|
|
assert_equal "bar", context.eval("foo()")
|
|
|
|
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-02-06 20:10:05 -05:00
|
|
|
end
|