Created
July 20, 2011 17:52
-
-
Save shihgianlee/1095486 to your computer and use it in GitHub Desktop.
RedBridge for Nokogiri
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package canna; | |
/** | |
* modified after yokolet - http://gist.github.com/424212 | |
*/ | |
import java.util.Arrays; | |
import java.util.List; | |
import org.jruby.embed.LocalContextScope; | |
import org.jruby.embed.ScriptingContainer; | |
public class JavaToRubyTestRunner { | |
private String jrubyhome = "/Users/sglee77/work/git/jruby"; | |
private String basePath = "/Users/sglee77/work/git/nokogiri"; | |
private String minitestPath = jrubyhome + "/lib/ruby/gems/1.8/gems/minitest-2.3.1/lib/"; | |
private JavaToRubyTestRunner() { | |
ScriptingContainer container = new ScriptingContainer(LocalContextScope.SINGLETHREAD); | |
container.setHomeDirectory(jrubyhome); | |
String[] paths = {basePath + "/test", basePath + "/lib", minitestPath}; | |
List<String> loadPaths = Arrays.asList(paths); | |
container.setLoadPaths(loadPaths); | |
container.runScriptlet("require 'rubygems'"); | |
container.runScriptlet("require 'helper'"); | |
runTest(container, "xml", "test_dtd.rb"); | |
runInspect(container); | |
container.terminate(); | |
} | |
private void runTest(ScriptingContainer container, String dir, String test_name) { | |
try { | |
container.runScriptlet("ROOT_DIR = File.dirname(__FILE__)"); | |
container.runScriptlet("MODULE_DIR = File.join('" + basePath + "', 'test', '" + dir + "')"); | |
container.runScriptlet("load File.join(MODULE_DIR, '" + test_name + "')"); | |
} catch (Exception e) { | |
e.printStackTrace(); | |
} | |
} | |
private void runInspect(ScriptingContainer container) { | |
System.out.println("[inspect]"); | |
String script = | |
"node = Nokogiri::XML::Text.new('hello world', Nokogiri::XML::Document.new)\n" + | |
"p \"#<#{node.class.name}:#{sprintf(\"0x%x\",node.object_id)} #{node.text.inspect}>\", node.inspect"; | |
container.runScriptlet(script); | |
} | |
public static void main(String[] args) { | |
new JavaToRubyTestRunner(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment