1 31 package org.jruby.test; 32 33 import java.io.ByteArrayOutputStream ; 34 import java.io.PrintStream ; 35 import java.io.StringReader ; 36 37 import junit.framework.TestCase; 38 39 import org.jruby.Ruby; 40 import org.jruby.RubyIO; 41 42 45 public class TestRubyBase extends TestCase { 46 protected Ruby runtime; 47 private PrintStream out; 48 49 public TestRubyBase() { 50 } 51 52 public TestRubyBase(String name) { 53 super(name); 54 } 55 56 61 protected String eval(String script) throws Exception { 62 ByteArrayOutputStream result = new ByteArrayOutputStream (); 63 out = new PrintStream (result); 64 RubyIO lStream = new RubyIO(runtime, out); 65 runtime.getGlobalVariables().set("$stdout", lStream); 66 runtime.getGlobalVariables().set("$>", lStream); 67 runtime.getGlobalVariables().set("$stderr", lStream); 68 69 runtime.loadScript("test", new StringReader (script), false); 70 StringBuffer sb = new StringBuffer (new String (result.toByteArray())); 71 for (int idx = sb.indexOf("\n"); idx != -1; idx = sb.indexOf("\n")) { 72 sb.deleteCharAt(idx); 73 } 74 75 return sb.toString(); 76 } 77 78 public void tearDown() { 79 if (out != null) 80 out.close(); 81 } 82 } 83 | Popular Tags |