1 31 package org.jruby.test; 32 33 import java.io.ByteArrayOutputStream ; 34 import java.io.PrintStream ; 35 36 import junit.framework.TestCase; 37 38 import org.jruby.Main; 39 import org.jruby.util.CommandlineParser; 40 41 public class TestCommandlineParser extends TestCase { 42 private PrintStream out; 43 private PrintStream err; 44 45 public void setUp() { 46 out = new PrintStream (new ByteArrayOutputStream ()); 47 err = new PrintStream (new ByteArrayOutputStream ()); 48 } 49 public void testParsing() { 50 CommandlineParser c = new CommandlineParser(new Main(System.in, out, err), new String [] { "-e", "hello", "-e", "world" }); 51 assertEquals("hello\nworld\n", c.inlineScript()); 52 assertNull(c.getScriptFileName()); 53 assertEquals("-e", c.displayedFileName()); 54 55 c = new CommandlineParser(new Main(System.in, out, err), new String [] { "--version" }); 56 assertTrue(c.isShowVersion()); 57 58 c = new CommandlineParser(new Main(System.in, out, err), new String [] { "-n", "myfile.rb" }); 59 assertTrue(c.isAssumeLoop()); 60 assertEquals("myfile.rb", c.getScriptFileName()); 61 assertEquals("myfile.rb", c.displayedFileName()); 62 63 c = new CommandlineParser(new Main(System.in, out, err), new String [0]); 64 assertEquals("-", c.displayedFileName()); 65 } 66 67 68 69 public void testParsingWithDashDash() { 70 class TestableCommandlineParser extends CommandlineParser { 71 72 public TestableCommandlineParser(String [] arguments) { 73 super(new Main(System.in, out, err), arguments); 74 } 75 } 76 CommandlineParser c = new TestableCommandlineParser(new String [] { "-I", "someLoadPath", "--", "simple.rb", "-v", "--version" }); 77 assertEquals("someLoadPath", c.loadPaths().get(0)); 78 assertEquals("simple.rb",c.getScriptFileName()); 79 assertEquals("simple.rb", c.displayedFileName()); 80 assertTrue("Should not be verbose. The -v flag should be a parameter to the script, not the jruby interpreter", !c.isVerbose()); 81 assertEquals("Script should have two parameters",2,c.getScriptArguments().length); 82 assertEquals("-v",c.getScriptArguments()[0]); 83 assertEquals("--version",c.getScriptArguments()[1]); 84 } 85 86 public void testPrintVersionDoesNotRunInterpreter() { 87 String [] args = new String [] { "-v" }; 88 CommandlineParser parser = new CommandlineParser(new Main(System.in, out, err), args); 89 assertTrue(parser.isShowVersion()); 90 assertFalse(parser.isShouldRunInterpreter()); 91 92 args = new String [] { "--version" }; 93 parser = new CommandlineParser(new Main(System.in, out, err), args); 94 assertTrue(parser.isShowVersion()); 95 assertFalse(parser.isShouldRunInterpreter()); 96 } 97 98 public void testHelpDoesNotRunIntepreter() { 99 String [] args = new String [] { "-h" }; 100 CommandlineParser parser = new CommandlineParser(new Main(System.in, out, err), args); 101 assertFalse(parser.isShouldRunInterpreter()); 102 103 args = new String [] { "--help" }; 104 parser = new CommandlineParser(new Main(System.in, out, err), args); 105 assertFalse(parser.isShouldRunInterpreter()); 106 } 107 108 public void testCommandTakesOneArgument() { 109 String [] args = new String [] { "--command", "gem" }; 110 CommandlineParser parser = new CommandlineParser(new Main(System.in, out, err), args); 111 assertEquals(1, parser.requiredLibraries().size()); 112 assertEquals("jruby/commands", parser.requiredLibraries().get(0)); 113 assertEquals("JRuby::Commands.gem\n", parser.inlineScript()); 114 } 115 116 public void testCommandAllowedOnlyOnceAndRemainderAreScriptArgs() { 117 String [] args = new String [] { "--command", "gem", "--command", "irb" }; 118 CommandlineParser parser = new CommandlineParser(new Main(System.in, out, err), args); 119 assertEquals(2, parser.getScriptArguments().length); 120 assertEquals("--command", parser.getScriptArguments()[0]); 121 assertEquals("irb", parser.getScriptArguments()[1]); 122 } 123 } 124 | Popular Tags |