Permalink
Browse files

Add Runtime#available?

  • Loading branch information...
sstephenson committed Feb 7, 2011
1 parent c371fb4 commit b22082fc860f1ba27811963b64ce33120b841938
Showing with 32 additions and 26 deletions.
  1. +6 −0 lib/execjs/runtime.rb
  2. +8 −0 test/test_execjs.rb
  3. +18 −26 test/test_runtimes.rb
View
@@ -20,6 +20,12 @@ def eval(source)
end
end
+ def available?
+ command = @command.split(/\s+/).first
+ `which #{command}`
+ $? == 0
+ end
+
protected
def compile(source)
runner_source.sub('#{source}', source)
View
@@ -9,4 +9,12 @@ def test_exec
def test_eval
assert_equal ["red", "yellow", "blue"], ExecJS.eval("'red yellow blue'.split(' ')")
end
+
+ def test_runtime_available
+ runtime = ExecJS::Runtime.new(:command => "nonexistent")
+ assert_equal false, runtime.available?
+
+ runtime = ExecJS::Runtime.new(:command => "ruby")
+ assert_equal true, runtime.available?
+ end
end
View
@@ -39,34 +39,26 @@ def test_thrown_exception
end
end
-class TestJSCRuntime < Test::Unit::TestCase
- include TestRuntime
+def test_runtime(name)
+ runtime = ExecJS::Runtimes.const_get(name)
+ if runtime.available?
+ Class.new(Test::Unit::TestCase) do
+ (class << self; self end).send(:define_method, :name) do
+ "#{name}Test"
+ end
- def setup
- @runtime = ExecJS::Runtimes::JSC
- end
-end
-
-class TestNodeRuntime < Test::Unit::TestCase
- include TestRuntime
-
- def setup
- @runtime = ExecJS::Runtimes::Node
- end
-end
+ include TestRuntime
-class TestSpidermonkeyRuntime < Test::Unit::TestCase
- include TestRuntime
-
- def setup
- @runtime = ExecJS::Runtimes::Spidermonkey
+ define_method(:setup) do
+ instance_variable_set(:@runtime, runtime)
+ end
+ end
+ else
+ warn "#{name} runtime is unavailable, skipping"
end
end
-class TestV8Runtime < Test::Unit::TestCase
- include TestRuntime
-
- def setup
- @runtime = ExecJS::Runtimes::V8
- end
-end
+test_runtime :JSC
+test_runtime :Node
+test_runtime :Spidermonkey
+test_runtime :V8

0 comments on commit b22082f

Please sign in to comment.