1 32 package org.jruby.test; 33 34 import java.util.ArrayList ; 35 36 import org.jruby.Ruby; 37 import org.jruby.RubyException; 38 import org.jruby.RubyFixnum; 39 import org.jruby.RubyObject; 40 import org.jruby.exceptions.RaiseException; 41 42 45 public class TestKernel extends TestRubyBase { 46 47 public TestKernel(String name) { 48 super(name); 49 } 50 51 public void setUp() { 52 runtime = Ruby.getDefaultInstance(); 53 runtime.getLoadService().init(new ArrayList ()); 54 } 55 56 public void testLoad() throws Exception { 57 assertEquals("0", eval("load 'test/loadTest.rb'")); 59 assertEquals("load did not load the same file several times", "1", eval("load 'test/loadTest.rb'")); 60 } 61 62 public void testRequire() throws Exception { 63 eval("$loadTest = nil"); 65 assertEquals("failed to load the file test/loadTest", "0", eval("require 'test/loadTest'")); 66 assertEquals("incorrectly reloaded the file test/loadTest", "", eval("require 'test/loadTest'")); 67 68 assertEquals("incorrect value for $\" variable", "test/loadTest.rb", eval("print $\".sort")); 69 } 70 71 public void testPrintf() throws Exception { 72 assertEquals("hello", eval("printf(\"%s\", \"hello\")")); 73 assertEquals("", eval("printf(\"%s\", nil)")); 74 } 75 76 public void testExit() throws Exception { 77 verifyExit(RubyFixnum.zero(runtime), "true"); 78 verifyExit(RubyFixnum.one(runtime), "false"); 79 verifyExit(RubyFixnum.one(runtime), ""); 80 verifyExit(new RubyFixnum(runtime, 7), "7"); 81 } 82 83 private void verifyExit(RubyObject expectedStatus, String argument) throws Exception { 84 try { 85 eval("exit " + argument); 86 fail("Expected a SystemExit to be thrown by calling exit."); 87 } catch (RaiseException re) { 88 RubyException raisedException = re.getException(); 89 if (raisedException.isKindOf(runtime.getClass("SystemExit"))) { 90 RubyObject status = (RubyObject)raisedException.getInstanceVariable("status"); 91 assertEquals(expectedStatus, status); 92 } else { 93 throw re; 94 } 95 } 96 } 97 98 public void tearDown() { 99 super.tearDown(); 100 } 101 102 } 103 | Popular Tags |