You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@commons.apache.org by jk...@apache.org on 2005/09/06 01:18:48 UTC

svn commit: r278882 - in /jakarta/commons/proper/cli/trunk/src: java/org/apache/commons/cli2/util/HelpFormatter.java test/org/apache/commons/cli2/util/HelpFormatterTest.java

Author: jkeyes
Date: Mon Sep  5 16:18:36 2005
New Revision: 278882

URL: http://svn.apache.org/viewcvs?rev=278882&view=rev
Log:
- added more HelpFormatter tests
- fixed default gutter bug

Modified:
    jakarta/commons/proper/cli/trunk/src/java/org/apache/commons/cli2/util/HelpFormatter.java
    jakarta/commons/proper/cli/trunk/src/test/org/apache/commons/cli2/util/HelpFormatterTest.java

Modified: jakarta/commons/proper/cli/trunk/src/java/org/apache/commons/cli2/util/HelpFormatter.java
URL: http://svn.apache.org/viewcvs/jakarta/commons/proper/cli/trunk/src/java/org/apache/commons/cli2/util/HelpFormatter.java?rev=278882&r1=278881&r2=278882&view=diff
==============================================================================
--- jakarta/commons/proper/cli/trunk/src/java/org/apache/commons/cli2/util/HelpFormatter.java (original)
+++ jakarta/commons/proper/cli/trunk/src/java/org/apache/commons/cli2/util/HelpFormatter.java Mon Sep  5 16:18:36 2005
@@ -145,34 +145,20 @@
         final int fullWidth) {
         
         // default the left gutter to empty string
-        if (gutterLeft == null) {
-            this.gutterLeft = "";
-        }
-        else {
-            this.gutterLeft = gutterLeft;
-        }
+        this.gutterLeft = (gutterLeft == null) ? DEFAULT_GUTTER_LEFT : gutterLeft;
 
-        // default the center gutter to empty string
-        if (gutterCenter == null) {
-            this.gutterCenter = "";
-        }
-        else {
-            this.gutterCenter = gutterCenter;
-        }
+        // default the center gutter to a single space
+        this.gutterCenter = (gutterCenter == null) ? DEFAULT_GUTTER_CENTER : gutterCenter;
 
         // default the right gutter to empty string
-        if (gutterRight == null) {
-            this.gutterRight = "";
-        }
-        else {
-            this.gutterRight = gutterRight;
-        }
+        this.gutterRight = (gutterRight == null) ? DEFAULT_GUTTER_RIGHT : gutterRight;
 
         // calculate the available page width
-        this.pageWidth = fullWidth - gutterLeft.length() - gutterRight.length();
+        this.pageWidth = fullWidth - this.gutterLeft.length() - this.gutterRight.length();
         
         // check available page width is valid
-        if (fullWidth - pageWidth + gutterCenter.length() < 2) {
+        int availableWidth = fullWidth - pageWidth + this.gutterCenter.length(); 
+        if ( availableWidth < 2 ) {
             throw new IllegalArgumentException(
                 "The gutter strings leave no space for output! "
                     + "Supply shorter gutters or more width.");

Modified: jakarta/commons/proper/cli/trunk/src/test/org/apache/commons/cli2/util/HelpFormatterTest.java
URL: http://svn.apache.org/viewcvs/jakarta/commons/proper/cli/trunk/src/test/org/apache/commons/cli2/util/HelpFormatterTest.java?rev=278882&r1=278881&r2=278882&view=diff
==============================================================================
--- jakarta/commons/proper/cli/trunk/src/test/org/apache/commons/cli2/util/HelpFormatterTest.java (original)
+++ jakarta/commons/proper/cli/trunk/src/test/org/apache/commons/cli2/util/HelpFormatterTest.java Mon Sep  5 16:18:36 2005
@@ -20,11 +20,15 @@
 import java.io.PrintWriter;
 import java.io.StringReader;
 import java.io.StringWriter;
+import java.util.Collections;
+import java.util.HashSet;
 import java.util.Iterator;
 import java.util.List;
+import java.util.Set;
 
 import junit.framework.TestCase;
 
+import org.apache.commons.cli2.DisplaySetting;
 import org.apache.commons.cli2.Group;
 import org.apache.commons.cli2.Option;
 import org.apache.commons.cli2.OptionException;
@@ -74,10 +78,36 @@
 
     public void testPrint() throws IOException {
         final StringWriter writer = new StringWriter();
-        helpFormatter.setPrintWriter(new PrintWriter(writer));
+        final PrintWriter pw = new PrintWriter(writer);
+        helpFormatter.setPrintWriter(pw);
         helpFormatter.print();
 
-        //System.out.println(writer.toString());
+        // test group
+        assertEquals("incorrect group", this.options, helpFormatter.getGroup());
+
+        // test pagewidth
+        assertEquals("incorrect page width", 76, helpFormatter.getPageWidth());
+
+        // test pw
+        assertEquals("incorrect print writer", pw, helpFormatter.getPrintWriter());
+
+        // test divider
+        assertEquals("incorrect divider", 
+                "+------------------------------------------------------------------------------+",
+                helpFormatter.getDivider());
+
+        // test header
+        assertEquals("incorrect header", "Jakarta Commons CLI", helpFormatter.getHeader());
+
+        // test footer
+        assertEquals("incorrect footer", "Copyright 2003\nApache Software Foundation",
+                helpFormatter.getFooter());
+
+        // test gutters
+        assertEquals("incorrect left gutter", "|*", helpFormatter.getGutterLeft());
+        assertEquals("incorrect right gutter", "*|", helpFormatter.getGutterRight());
+        assertEquals("incorrect center gutter", "*-*", helpFormatter.getGutterCenter());
+
 
         final BufferedReader reader =
             new BufferedReader(new StringReader(writer.toString()));
@@ -396,5 +426,107 @@
         final StringWriter writer = new StringWriter();
         HelpFormatter.pad("hello world", -5, writer);
         assertEquals("hello world", writer.toString());
+    }
+    
+    public void testGutters() throws IOException {
+        helpFormatter = new HelpFormatter(null, null, null, 80);
+        helpFormatter.setShellCommand("ant");
+        final Set lusage = new HashSet();
+        lusage.add(DisplaySetting.DISPLAY_ALIASES);
+        lusage.add(DisplaySetting.DISPLAY_GROUP_NAME);
+        helpFormatter.setLineUsageSettings(lusage);
+        
+        // test line usage
+        assertEquals("incorrect line usage", lusage, helpFormatter.getLineUsageSettings());
+
+        final Set fusage = new HashSet();
+        fusage.add(DisplaySetting.DISPLAY_PARENT_CHILDREN);
+        fusage.add(DisplaySetting.DISPLAY_GROUP_ARGUMENT);
+        fusage.add(DisplaySetting.DISPLAY_GROUP_OUTER);
+        fusage.add(DisplaySetting.DISPLAY_GROUP_EXPANDED);
+        fusage.add(DisplaySetting.DISPLAY_ARGUMENT_BRACKETED);
+        fusage.add(DisplaySetting.DISPLAY_ARGUMENT_NUMBERED);
+        fusage.add(DisplaySetting.DISPLAY_SWITCH_ENABLED);
+        fusage.add(DisplaySetting.DISPLAY_SWITCH_DISABLED);
+        fusage.add(DisplaySetting.DISPLAY_PROPERTY_OPTION);
+        fusage.add(DisplaySetting.DISPLAY_PARENT_CHILDREN);
+        fusage.add(DisplaySetting.DISPLAY_PARENT_ARGUMENT);
+        fusage.add(DisplaySetting.DISPLAY_OPTIONAL);
+        helpFormatter.setFullUsageSettings(fusage);
+
+        // test line usage
+        assertEquals("incorrect full usage", fusage, helpFormatter.getFullUsageSettings());
+
+        final Set dsettings = new HashSet();
+        dsettings.add(DisplaySetting.DISPLAY_GROUP_NAME);
+        dsettings.add(DisplaySetting.DISPLAY_GROUP_EXPANDED);
+        dsettings.add(DisplaySetting.DISPLAY_GROUP_ARGUMENT);
+        
+        helpFormatter.setDisplaySettings(dsettings);
+
+        verbose =
+            new DefaultOptionBuilder()
+                .withLongName("verbose")
+                .withDescription("print the version information and exit")
+                .create();
+
+        options = new GroupBuilder()
+            .withName("options")
+            .withOption(DefaultOptionTest.buildHelpOption())
+            .withOption(ArgumentTest.buildTargetsArgument())
+            .withOption(
+                new DefaultOptionBuilder()
+                    .withLongName("diagnostics")
+                    .withDescription("print information that might be helpful to diagnose or report problems.")
+                    .create())
+            .withOption(
+                new DefaultOptionBuilder()
+                    .withLongName("projecthelp")
+                    .withDescription("print project help information")
+                    .create())
+            .withOption(verbose)
+            .create();        
+        
+        helpFormatter.setGroup(options);
+
+        // test default gutters
+        assertEquals("incorrect left gutter", HelpFormatter.DEFAULT_GUTTER_LEFT, helpFormatter.getGutterLeft());
+        assertEquals("incorrect right gutter", HelpFormatter.DEFAULT_GUTTER_RIGHT, helpFormatter.getGutterRight());
+        assertEquals("incorrect center gutter", HelpFormatter.DEFAULT_GUTTER_CENTER, helpFormatter.getGutterCenter());
+    
+        final StringWriter writer = new StringWriter();
+        helpFormatter.setPrintWriter(new PrintWriter(writer));
+        helpFormatter.print();
+
+        final BufferedReader reader =
+            new BufferedReader(new StringReader(writer.toString()));
+        assertEquals(
+            "Usage:                                                                          ",
+            reader.readLine());
+        assertEquals(
+            "ant [--help --diagnostics --projecthelp --verbose] [<target1> [<target2> ...]]  ",
+            reader.readLine());
+        assertEquals(
+            "options                                                                         ",
+            reader.readLine());
+        assertEquals(
+            "  --help (-?,-h)         Displays the help                                      ",
+            reader.readLine());
+        assertEquals(
+            "  --diagnostics          print information that might be helpful to diagnose or ",
+            reader.readLine());
+        assertEquals(
+            "                         report problems.                                       ",
+            reader.readLine());
+        assertEquals(
+            "  --projecthelp          print project help information                         ",
+            reader.readLine());
+        assertEquals(
+            "  --verbose              print the version information and exit                 ",
+            reader.readLine());
+        assertEquals(
+            "  target [target ...]    The targets ant should build                           ",
+            reader.readLine());
+        assertNull(reader.readLine());
     }
 }



---------------------------------------------------------------------
To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-dev-help@jakarta.apache.org