Skip to content

Commit ae8e1d8

Browse files
author
John Keyes
committed
- added more HelpFormatter tests
- fixed default gutter bug git-svn-id: https://svn.apache.org/repos/asf/jakarta/commons/proper/cli/trunk@278882 13f79535-47bb-0310-9956-ffa450edef68
1 parent 96b7034 commit ae8e1d8

2 files changed

Lines changed: 141 additions & 23 deletions

File tree

src/java/org/apache/commons/cli2/util/HelpFormatter.java

Lines changed: 7 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -145,34 +145,20 @@ public HelpFormatter(
145145
final int fullWidth) {
146146

147147
// default the left gutter to empty string
148-
if (gutterLeft == null) {
149-
this.gutterLeft = "";
150-
}
151-
else {
152-
this.gutterLeft = gutterLeft;
153-
}
148+
this.gutterLeft = (gutterLeft == null) ? DEFAULT_GUTTER_LEFT : gutterLeft;
154149

155-
// default the center gutter to empty string
156-
if (gutterCenter == null) {
157-
this.gutterCenter = "";
158-
}
159-
else {
160-
this.gutterCenter = gutterCenter;
161-
}
150+
// default the center gutter to a single space
151+
this.gutterCenter = (gutterCenter == null) ? DEFAULT_GUTTER_CENTER : gutterCenter;
162152

163153
// default the right gutter to empty string
164-
if (gutterRight == null) {
165-
this.gutterRight = "";
166-
}
167-
else {
168-
this.gutterRight = gutterRight;
169-
}
154+
this.gutterRight = (gutterRight == null) ? DEFAULT_GUTTER_RIGHT : gutterRight;
170155

171156
// calculate the available page width
172-
this.pageWidth = fullWidth - gutterLeft.length() - gutterRight.length();
157+
this.pageWidth = fullWidth - this.gutterLeft.length() - this.gutterRight.length();
173158

174159
// check available page width is valid
175-
if (fullWidth - pageWidth + gutterCenter.length() < 2) {
160+
int availableWidth = fullWidth - pageWidth + this.gutterCenter.length();
161+
if ( availableWidth < 2 ) {
176162
throw new IllegalArgumentException(
177163
"The gutter strings leave no space for output! "
178164
+ "Supply shorter gutters or more width.");

src/test/org/apache/commons/cli2/util/HelpFormatterTest.java

Lines changed: 134 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,15 @@
2020
import java.io.PrintWriter;
2121
import java.io.StringReader;
2222
import java.io.StringWriter;
23+
import java.util.Collections;
24+
import java.util.HashSet;
2325
import java.util.Iterator;
2426
import java.util.List;
27+
import java.util.Set;
2528

2629
import junit.framework.TestCase;
2730

31+
import org.apache.commons.cli2.DisplaySetting;
2832
import org.apache.commons.cli2.Group;
2933
import org.apache.commons.cli2.Option;
3034
import org.apache.commons.cli2.OptionException;
@@ -74,10 +78,36 @@ public void setUp() {
7478

7579
public void testPrint() throws IOException {
7680
final StringWriter writer = new StringWriter();
77-
helpFormatter.setPrintWriter(new PrintWriter(writer));
81+
final PrintWriter pw = new PrintWriter(writer);
82+
helpFormatter.setPrintWriter(pw);
7883
helpFormatter.print();
7984

80-
//System.out.println(writer.toString());
85+
// test group
86+
assertEquals("incorrect group", this.options, helpFormatter.getGroup());
87+
88+
// test pagewidth
89+
assertEquals("incorrect page width", 76, helpFormatter.getPageWidth());
90+
91+
// test pw
92+
assertEquals("incorrect print writer", pw, helpFormatter.getPrintWriter());
93+
94+
// test divider
95+
assertEquals("incorrect divider",
96+
"+------------------------------------------------------------------------------+",
97+
helpFormatter.getDivider());
98+
99+
// test header
100+
assertEquals("incorrect header", "Jakarta Commons CLI", helpFormatter.getHeader());
101+
102+
// test footer
103+
assertEquals("incorrect footer", "Copyright 2003\nApache Software Foundation",
104+
helpFormatter.getFooter());
105+
106+
// test gutters
107+
assertEquals("incorrect left gutter", "|*", helpFormatter.getGutterLeft());
108+
assertEquals("incorrect right gutter", "*|", helpFormatter.getGutterRight());
109+
assertEquals("incorrect center gutter", "*-*", helpFormatter.getGutterCenter());
110+
81111

82112
final BufferedReader reader =
83113
new BufferedReader(new StringReader(writer.toString()));
@@ -397,4 +427,106 @@ public void testPad_TooShort() throws IOException {
397427
HelpFormatter.pad("hello world", -5, writer);
398428
assertEquals("hello world", writer.toString());
399429
}
430+
431+
public void testGutters() throws IOException {
432+
helpFormatter = new HelpFormatter(null, null, null, 80);
433+
helpFormatter.setShellCommand("ant");
434+
final Set lusage = new HashSet();
435+
lusage.add(DisplaySetting.DISPLAY_ALIASES);
436+
lusage.add(DisplaySetting.DISPLAY_GROUP_NAME);
437+
helpFormatter.setLineUsageSettings(lusage);
438+
439+
// test line usage
440+
assertEquals("incorrect line usage", lusage, helpFormatter.getLineUsageSettings());
441+
442+
final Set fusage = new HashSet();
443+
fusage.add(DisplaySetting.DISPLAY_PARENT_CHILDREN);
444+
fusage.add(DisplaySetting.DISPLAY_GROUP_ARGUMENT);
445+
fusage.add(DisplaySetting.DISPLAY_GROUP_OUTER);
446+
fusage.add(DisplaySetting.DISPLAY_GROUP_EXPANDED);
447+
fusage.add(DisplaySetting.DISPLAY_ARGUMENT_BRACKETED);
448+
fusage.add(DisplaySetting.DISPLAY_ARGUMENT_NUMBERED);
449+
fusage.add(DisplaySetting.DISPLAY_SWITCH_ENABLED);
450+
fusage.add(DisplaySetting.DISPLAY_SWITCH_DISABLED);
451+
fusage.add(DisplaySetting.DISPLAY_PROPERTY_OPTION);
452+
fusage.add(DisplaySetting.DISPLAY_PARENT_CHILDREN);
453+
fusage.add(DisplaySetting.DISPLAY_PARENT_ARGUMENT);
454+
fusage.add(DisplaySetting.DISPLAY_OPTIONAL);
455+
helpFormatter.setFullUsageSettings(fusage);
456+
457+
// test line usage
458+
assertEquals("incorrect full usage", fusage, helpFormatter.getFullUsageSettings());
459+
460+
final Set dsettings = new HashSet();
461+
dsettings.add(DisplaySetting.DISPLAY_GROUP_NAME);
462+
dsettings.add(DisplaySetting.DISPLAY_GROUP_EXPANDED);
463+
dsettings.add(DisplaySetting.DISPLAY_GROUP_ARGUMENT);
464+
465+
helpFormatter.setDisplaySettings(dsettings);
466+
467+
verbose =
468+
new DefaultOptionBuilder()
469+
.withLongName("verbose")
470+
.withDescription("print the version information and exit")
471+
.create();
472+
473+
options = new GroupBuilder()
474+
.withName("options")
475+
.withOption(DefaultOptionTest.buildHelpOption())
476+
.withOption(ArgumentTest.buildTargetsArgument())
477+
.withOption(
478+
new DefaultOptionBuilder()
479+
.withLongName("diagnostics")
480+
.withDescription("print information that might be helpful to diagnose or report problems.")
481+
.create())
482+
.withOption(
483+
new DefaultOptionBuilder()
484+
.withLongName("projecthelp")
485+
.withDescription("print project help information")
486+
.create())
487+
.withOption(verbose)
488+
.create();
489+
490+
helpFormatter.setGroup(options);
491+
492+
// test default gutters
493+
assertEquals("incorrect left gutter", HelpFormatter.DEFAULT_GUTTER_LEFT, helpFormatter.getGutterLeft());
494+
assertEquals("incorrect right gutter", HelpFormatter.DEFAULT_GUTTER_RIGHT, helpFormatter.getGutterRight());
495+
assertEquals("incorrect center gutter", HelpFormatter.DEFAULT_GUTTER_CENTER, helpFormatter.getGutterCenter());
496+
497+
final StringWriter writer = new StringWriter();
498+
helpFormatter.setPrintWriter(new PrintWriter(writer));
499+
helpFormatter.print();
500+
501+
final BufferedReader reader =
502+
new BufferedReader(new StringReader(writer.toString()));
503+
assertEquals(
504+
"Usage: ",
505+
reader.readLine());
506+
assertEquals(
507+
"ant [--help --diagnostics --projecthelp --verbose] [<target1> [<target2> ...]] ",
508+
reader.readLine());
509+
assertEquals(
510+
"options ",
511+
reader.readLine());
512+
assertEquals(
513+
" --help (-?,-h) Displays the help ",
514+
reader.readLine());
515+
assertEquals(
516+
" --diagnostics print information that might be helpful to diagnose or ",
517+
reader.readLine());
518+
assertEquals(
519+
" report problems. ",
520+
reader.readLine());
521+
assertEquals(
522+
" --projecthelp print project help information ",
523+
reader.readLine());
524+
assertEquals(
525+
" --verbose print the version information and exit ",
526+
reader.readLine());
527+
assertEquals(
528+
" target [target ...] The targets ant should build ",
529+
reader.readLine());
530+
assertNull(reader.readLine());
531+
}
400532
}

0 commit comments

Comments
 (0)