diff options
author | Raphael Moll <ralf@android.com> | 2011-07-01 11:51:36 -0400 |
---|---|---|
committer | Raphael Moll <ralf@android.com> | 2011-07-06 09:07:24 -0400 |
commit | c3d9c89734408c881cae44c3a0e95401a00f1bf8 (patch) | |
tree | 1759493abed213c145d49273069ce8306f1d5c7c /sdkmanager/app/tests | |
parent | 4dc629abd72fa54e6de9e48fe4f55cacfeff5474 (diff) | |
download | sdk-c3d9c89734408c881cae44c3a0e95401a00f1bf8.zip sdk-c3d9c89734408c881cae44c3a0e95401a00f1bf8.tar.gz sdk-c3d9c89734408c881cae44c3a0e95401a00f1bf8.tar.bz2 |
SdkManager: pretty-print the 'android' command line help.
This reflows the command line help so that it wraps nicely
at 78 chars. Also properly finish sentences with dots.
Mere details...
Also added a proper unit test to test various edge cases.
Change-Id: Id2f9eab5014ea9c5f4aae5ed4427b9aebc3c70bf
Diffstat (limited to 'sdkmanager/app/tests')
-rw-r--r-- | sdkmanager/app/tests/com/android/sdkmanager/CommandLineProcessorTest.java | 96 |
1 files changed, 71 insertions, 25 deletions
diff --git a/sdkmanager/app/tests/com/android/sdkmanager/CommandLineProcessorTest.java b/sdkmanager/app/tests/com/android/sdkmanager/CommandLineProcessorTest.java index 688ce52..02d905e 100644 --- a/sdkmanager/app/tests/com/android/sdkmanager/CommandLineProcessorTest.java +++ b/sdkmanager/app/tests/com/android/sdkmanager/CommandLineProcessorTest.java @@ -25,7 +25,7 @@ import junit.framework.TestCase; public class CommandLineProcessorTest extends TestCase { private StdSdkLog mLog; - + /** * A mock version of the {@link CommandLineProcessor} class that does not * exits and captures its stdout/stderr output. @@ -35,7 +35,7 @@ public class CommandLineProcessorTest extends TestCase { private boolean mHelpCalled; private String mStdOut = ""; private String mStdErr = ""; - + public MockCommandLineProcessor(ISdkLog logger) { super(logger, new String[][] { @@ -47,45 +47,45 @@ public class CommandLineProcessorTest extends TestCase { define(Mode.STRING, true /*mandatory*/, "verb1", "action1", "2", "second", "mandatory flag", null); } - + @Override public void printHelpAndExitForAction(String verb, String directObject, String errorFormat, Object... args) { mHelpCalled = true; super.printHelpAndExitForAction(verb, directObject, errorFormat, args); } - + @Override protected void exit() { mExitCalled = true; } - + @Override protected void stdout(String format, Object... args) { String s = String.format(format, args); mStdOut += s + "\n"; // don't call super to avoid printing stuff } - + @Override protected void stderr(String format, Object... args) { String s = String.format(format, args); mStdErr += s + "\n"; // don't call super to avoid printing stuff } - + public boolean wasHelpCalled() { return mHelpCalled; } - + public boolean wasExitCalled() { return mExitCalled; } - + public String getStdOut() { return mStdOut; } - + public String getStdErr() { return mStdErr; } @@ -102,8 +102,8 @@ public class CommandLineProcessorTest extends TestCase { super.tearDown(); } - public final void testPrintHelpAndExit() { - MockCommandLineProcessor c = new MockCommandLineProcessor(mLog); + public void testPrintHelpAndExit() { + MockCommandLineProcessor c = new MockCommandLineProcessor(mLog); assertFalse(c.wasExitCalled()); assertFalse(c.wasHelpCalled()); assertTrue(c.getStdOut().equals("")); @@ -114,7 +114,7 @@ public class CommandLineProcessorTest extends TestCase { assertTrue(c.getStdErr().equals("")); assertTrue(c.wasExitCalled()); - c = new MockCommandLineProcessor(mLog); + c = new MockCommandLineProcessor(mLog); assertFalse(c.wasExitCalled()); assertTrue(c.getStdOut().equals("")); assertTrue(c.getStdErr().indexOf("Missing parameter") == -1); @@ -124,9 +124,9 @@ public class CommandLineProcessorTest extends TestCase { assertFalse(c.getStdOut().equals("")); assertTrue(c.getStdErr().indexOf("Missing parameter") != -1); } - - public final void testVerbose() { - MockCommandLineProcessor c = new MockCommandLineProcessor(mLog); + + public void testVerbose() { + MockCommandLineProcessor c = new MockCommandLineProcessor(mLog); assertFalse(c.isVerbose()); c.parseArgs(new String[] { "-v" }); @@ -135,31 +135,31 @@ public class CommandLineProcessorTest extends TestCase { assertTrue(c.wasHelpCalled()); assertTrue(c.getStdErr().indexOf("Missing verb name.") != -1); - c = new MockCommandLineProcessor(mLog); + c = new MockCommandLineProcessor(mLog); c.parseArgs(new String[] { "--verbose" }); assertTrue(c.isVerbose()); assertTrue(c.wasExitCalled()); assertTrue(c.wasHelpCalled()); assertTrue(c.getStdErr().indexOf("Missing verb name.") != -1); } - - public final void testHelp() { - MockCommandLineProcessor c = new MockCommandLineProcessor(mLog); + + public void testHelp() { + MockCommandLineProcessor c = new MockCommandLineProcessor(mLog); c.parseArgs(new String[] { "-h" }); assertTrue(c.wasExitCalled()); assertTrue(c.wasHelpCalled()); assertTrue(c.getStdErr().indexOf("Missing verb name.") == -1); - c = new MockCommandLineProcessor(mLog); + c = new MockCommandLineProcessor(mLog); c.parseArgs(new String[] { "--help" }); assertTrue(c.wasExitCalled()); assertTrue(c.wasHelpCalled()); assertTrue(c.getStdErr().indexOf("Missing verb name.") == -1); } - public final void testMandatory() { - MockCommandLineProcessor c = new MockCommandLineProcessor(mLog); + public void testMandatory() { + MockCommandLineProcessor c = new MockCommandLineProcessor(mLog); c.parseArgs(new String[] { "verb1", "action1", "-1", "value1", "-2", "value2" }); assertFalse(c.wasExitCalled()); @@ -168,7 +168,7 @@ public class CommandLineProcessorTest extends TestCase { assertEquals("value1", c.getValue("verb1", "action1", "first")); assertEquals("value2", c.getValue("verb1", "action1", "second")); - c = new MockCommandLineProcessor(mLog); + c = new MockCommandLineProcessor(mLog); c.parseArgs(new String[] { "verb1", "action1", "-2", "value2" }); assertFalse(c.wasExitCalled()); assertFalse(c.wasHelpCalled()); @@ -176,7 +176,7 @@ public class CommandLineProcessorTest extends TestCase { assertEquals(null, c.getValue("verb1", "action1", "first")); assertEquals("value2", c.getValue("verb1", "action1", "second")); - c = new MockCommandLineProcessor(mLog); + c = new MockCommandLineProcessor(mLog); c.parseArgs(new String[] { "verb1", "action1" }); assertTrue(c.wasExitCalled()); assertTrue(c.wasHelpCalled()); @@ -184,4 +184,50 @@ public class CommandLineProcessorTest extends TestCase { assertEquals(null, c.getValue("verb1", "action1", "first")); assertEquals(null, c.getValue("verb1", "action1", "second")); } + + public void testReflowLine() { + MockCommandLineProcessor c = new MockCommandLineProcessor(mLog); + + boolean gotNpe = false; + try { + c.reflowLine(null); + } catch(NullPointerException e) { + gotNpe = true; + } finally { + assertTrue(gotNpe); + } + + assertEquals("", c.reflowLine("")); + + assertEquals("1234567", c.reflowLine("1234567")); + + assertEquals( + "-- verbose, -v: This description for this flag fits in exactly 78 characters.", + c.reflowLine("-- verbose, -v: This description for this flag fits in exactly 78 characters.")); + + assertEquals( + "--verbose, -v : This description for this flag fits in more than 78\n" + + " characters and needs to wrap up at the colon.", + c.reflowLine("--verbose, -v : This description for this flag fits in more than 78 characters and needs to wrap up at the colon.")); + + assertEquals( + "If the line needs to wrap but there's no colon marker, the line will just wrap\n" + + " with 4 spaces.", + c.reflowLine("If the line needs to wrap but there's no colon marker, the line will just wrap with 4 spaces.")); + + assertEquals( + "--blah: More than 78 characters and lots of\n" + + " spaces. ", + c.reflowLine("--blah: More than 78 characters and lots of spaces. ")); + + assertEquals( + "In this case the colon is at the very end of the string and it's not going to\n" + + " wrap as expected:", + c.reflowLine("In this case the colon is at the very end of the string and it's not going to wrap as expected:")); + + assertEquals( + "--flag:In-this-case-there-is-no-whitespace-and-wrapping-will-cut-just-at-the-7\n" + + " 8-mark.", + c.reflowLine("--flag:In-this-case-there-is-no-whitespace-and-wrapping-will-cut-just-at-the-78-mark.")); + } } |