1 34 package org.jruby.test; 35 36 import java.io.ByteArrayOutputStream ; 37 import java.io.PrintStream ; 38 import java.util.ArrayList ; 39 import java.util.Arrays ; 40 41 import org.jruby.Ruby; 42 import org.jruby.RubyArray; 43 import org.jruby.RubyException; 44 import org.jruby.RubyInstanceConfig; 45 import org.jruby.RubyIO; 46 import org.jruby.RubyString; 47 import org.jruby.exceptions.RaiseException; 48 49 54 public class TestRuby extends TestRubyBase { 55 56 public TestRuby(String name) { 57 super(name); 58 } 59 60 public void setUp() { 61 runtime = Ruby.getDefaultInstance(); 62 } 63 64 public void testVarAndMet() throws Exception { 65 runtime.getLoadService().init(new ArrayList ()); 66 eval("load 'test/testVariableAndMethod.rb'"); 67 assertEquals("Hello World", eval("puts($a)")); 68 assertEquals("dlroW olleH", eval("puts $b")); 69 assertEquals("Hello World", eval("puts $d.reverse, $c, $e.reverse")); 70 assertEquals("135 20 3", eval("puts $f, \" \", $g, \" \", $h")); 71 } 72 73 public void testPrintErrorWithNilBacktrace() throws Exception { 74 testPrintErrorWithBacktrace("nil"); 75 } 76 77 public void testPrintErrorWithStringBacktrace() throws Exception { 78 testPrintErrorWithBacktrace("\"abc\""); 79 } 80 81 private void testPrintErrorWithBacktrace(String backtrace) throws Exception { 82 RubyIO oldStderr = (RubyIO)runtime.getGlobalVariables().get("$stderr"); 83 try { 84 ByteArrayOutputStream stderrOutput = new ByteArrayOutputStream (); 85 RubyIO newStderr = new RubyIO(runtime, stderrOutput); 86 runtime.getGlobalVariables().set("$stderr", newStderr); 87 88 try { 89 eval("class MyError < StandardError ; def backtrace ; " + backtrace + " ; end ; end ; raise MyError.new "); 90 fail("Expected MyError to be raised"); 91 } catch (RaiseException re) { 92 runtime.printError(re.getException()); 94 } 95 } finally { 96 runtime.getGlobalVariables().set("$stderr", oldStderr); 97 } 98 } 99 100 public void testPrintErrorShouldPrintErrorMessageAndStacktraceWhenBacktraceIsPresent() { 101 final ByteArrayOutputStream err = new ByteArrayOutputStream (); 102 RubyInstanceConfig config = new RubyInstanceConfig() {{ 103 setInput(System.in); setOutput(System.out); setError(new PrintStream (err)); setObjectSpaceEnabled(false); 104 }}; 105 Ruby ruby = Ruby.newInstance(config); 106 RubyException exception = new RubyException(ruby, ruby.getClass("NameError"), "A message"); 107 RubyString[] lines = new RubyString[]{ 108 RubyString.newString(ruby, "Line 1"), 109 RubyString.newString(ruby, "Line 2"), 110 }; 111 RubyArray backtrace = RubyArray.newArray(ruby, Arrays.asList(lines)); 112 exception.set_backtrace(backtrace); 113 ruby.printError(exception); 114 assertEquals("Line 1: A message (NameError)\n\tfrom Line 2\n", err.toString()); 115 } 116 117 public void testPrintErrorShouldOnlyPrintErrorMessageWhenBacktraceIsNil() { 118 final ByteArrayOutputStream err = new ByteArrayOutputStream (); 119 RubyInstanceConfig config = new RubyInstanceConfig() {{ 120 setInput(System.in); setOutput(System.out); setError(new PrintStream (err)); setObjectSpaceEnabled(false); 121 }}; 122 Ruby ruby = Ruby.newInstance(config); 123 RubyException exception = new RubyException(ruby, ruby.getClass("NameError"), "A message"); 124 ruby.printError(exception); 125 } 127 } 128 | Popular Tags |