1 28 package org.jruby.test; 29 30 import java.util.ArrayList ; 31 32 import org.jruby.Ruby; 33 import org.jruby.exceptions.RaiseException; 34 35 39 public class TestRequire extends TestRubyBase { 40 public TestRequire(String name) { 41 super(name); 42 } 43 public void setUp() { 44 runtime = Ruby.getDefaultInstance(); 45 runtime.getLoadService().init(new ArrayList ()); 46 } 47 48 public void tearDown() { 49 super.tearDown(); 50 } 51 52 public void testRubyRequire() throws Exception { 53 String result = eval("require 'A/C'; puts A::C.new.meth"); 54 assertEquals("ok", result); 55 result = eval("$: << 'A'; require 'B'; puts B.new.meth"); 56 assertEquals("ok", result); 57 } 58 59 public void testLoadErrorsDuringRequireShouldRaise() throws Exception { 60 try { 61 eval("require 'test/load_error'"); 62 fail("should have raised LoadError"); 63 } catch (RaiseException re) { 64 assertTrue(re.getException().toString().indexOf("bogus_missing_lib") >= 0); 65 assertEquals("LoadError", re.getException().getMetaClass().toString()); 66 } 67 } 68 69 public void testFailedRequireInRescueClauseStillRaisesException() throws Exception { 70 try { 71 eval( 72 "begin\n" 73 + "require 'test/load_error'\n" + 74 "rescue LoadError => e\n" 75 + " require 'test/load_error'\n" + 76 "end"); 77 fail("should raise exception"); 78 } catch (RaiseException re) { 79 assertEquals("LoadError", re.getException().getMetaClass().toString()); 80 assertTrue(re.getException().toString().indexOf("bogus_missing_lib") >= 0); 81 } 82 } 83 84 public void testParseErrorsDuringRequireShouldRaise() throws Exception { 85 try { 86 eval("require 'test/parse_error'"); 87 fail("should have raised SyntaxError"); 88 } catch (RaiseException re) { 89 assertEquals("SyntaxError", re.getException().getMetaClass().toString()); 90 } 91 } 92 } 93 | Popular Tags |