Skip to content

Instantly share code, notes, and snippets.

@shihgianlee
Created July 20, 2011 17:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save shihgianlee/1095486 to your computer and use it in GitHub Desktop.
Save shihgianlee/1095486 to your computer and use it in GitHub Desktop.
RedBridge for Nokogiri
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