From ad9f73c7ee965b40fb3e7d4c55c45d16168b5925 Mon Sep 17 00:00:00 2001 From: Joshua Peek Date: Tue, 13 Sep 2011 10:00:30 -0500 Subject: [PATCH] Merge test files --- test/execjs_test.rb | 9 --- test/test_execjs.rb | 135 ++++++++++++++++++++++++++++++++++++++----- test/test_runtime.rb | 115 ------------------------------------ 3 files changed, 120 insertions(+), 139 deletions(-) delete mode 100644 test/execjs_test.rb delete mode 100644 test/test_runtime.rb diff --git a/test/execjs_test.rb b/test/execjs_test.rb deleted file mode 100644 index e17269a..0000000 --- a/test/execjs_test.rb +++ /dev/null @@ -1,9 +0,0 @@ -require "test/unit" -require "execjs/module" - -begin - require "execjs" -rescue ExecJS::RuntimeUnavailable => e - warn e - exit 2 -end diff --git a/test/test_execjs.rb b/test/test_execjs.rb index f7706f5..6a0d763 100644 --- a/test/test_execjs.rb +++ b/test/test_execjs.rb @@ -1,14 +1,15 @@ -require "execjs_test" +# -*- coding: utf-8 -*- +require "test/unit" +require "execjs/module" + +begin + require "execjs" +rescue ExecJS::RuntimeUnavailable => e + warn e + exit 2 +end 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 - def test_runtime_available runtime = ExecJS::ExternalRuntime.new(:command => "nonexistent") assert !runtime.available? @@ -26,12 +27,8 @@ class TestExecJS < Test::Unit::TestCase runtime = ExecJS::ExternalRuntime.new(:command => "ruby") ExecJS.runtime = runtime assert_equal runtime, ExecJS.runtime - end - - def test_compile - context = ExecJS.compile("foo = function() { return \"bar\"; }") - assert_equal "bar", context.exec("return foo()") - assert_equal "bar", context.eval("foo()") + ensure + ExecJS.runtime = original_runtime end def test_context_call @@ -50,4 +47,112 @@ class TestExecJS < Test::Unit::TestCase context.call("missing") end end + + 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 + + if defined? Encoding + 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 + + assert_raise Encoding::UndefinedConversionError do + binary = "\xde\xad\xbe\xef".force_encoding("BINARY") + ExecJS.eval(binary) + end + end + + def test_encoding_compile + utf8 = Encoding.find('UTF-8') + + context = ExecJS.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") + context.eval(binary) + end + end + end + + def test_compile + 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_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 + + def test_compile_large_scripts + body = "var foo = 'bar';\n" * 100_000 + assert ExecJS.exec("function foo() {\n#{body}\n};\nreturn true") + end + + def test_syntax_error + assert_raise ExecJS::RuntimeError do + ExecJS.exec(")") + end + end + + def test_thrown_exception + assert_raise ExecJS::ProgramError do + ExecJS.exec("throw 'hello'") + end + end end diff --git a/test/test_runtime.rb b/test/test_runtime.rb deleted file mode 100644 index 6777c68..0000000 --- a/test/test_runtime.rb +++ /dev/null @@ -1,115 +0,0 @@ -# -*- coding: utf-8 -*- -require "execjs_test" - -class TestRuntime < Test::Unit::TestCase - def setup - @runtime = ExecJS::Runtimes.autodetect - end - - 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}")) - assert_equal "café", @runtime.exec("return 'café'") - assert_equal "☃", @runtime.exec('return "☃"') - assert_equal "☃", @runtime.exec('return "\u2603"') - assert_equal "\\", @runtime.exec('return "\\\\"') - 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]") - assert_equal [1, nil], @runtime.eval("[1, function() {}]") - assert_equal "hello", @runtime.eval("'hello'") - assert_equal({"a"=>1,"b"=>2}, @runtime.eval("{a:1,b:2}")) - assert_equal({"a"=>true}, @runtime.eval("{a:true,b:function (){}}")) - assert_equal "café", @runtime.eval("'café'") - assert_equal "☃", @runtime.eval('"☃"') - assert_equal "☃", @runtime.eval('"\u2603"') - assert_equal "\\", @runtime.eval('"\\\\"') - end - - if defined? Encoding - def test_encoding - utf8 = Encoding.find('UTF-8') - - assert_equal utf8, @runtime.exec("return 'hello'").encoding - assert_equal utf8, @runtime.eval("'☃'").encoding - - 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 - end - - 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") - context.eval(binary) - end - end - end - - def test_compile - context = @runtime.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_this_is_global_scope - assert_equal true, @runtime.eval("this === (function() {return this})()") - assert_equal true, @runtime.exec("return this === (function() {return this})()") - end - - 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 - - def test_compile_large_scripts - body = "var foo = 'bar';\n" * 100_000 - assert @runtime.exec("function foo() {\n#{body}\n};\nreturn true") - end - - 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