001: /*
002: Copyright 2004-2007 Paul R. Holser, Jr. All rights reserved.
003: Licensed under the Academic Free License version 3.0
004: */
005:
006: package joptsimple;
007:
008: import java.io.ByteArrayOutputStream;
009: import java.io.StringWriter;
010:
011: /**
012: * @author <a href="mailto:pholser@alumni.rice.edu">Paul Holser</a>
013: * @version $Id: OptionParserHelpTest.java,v 1.10 2007/04/10 20:06:26 pholser Exp $
014: */
015: public class OptionParserHelpTest extends AbstractOptionParserFixture {
016: private StringWriter sink;
017:
018: protected void setUp() throws Exception {
019: super .setUp();
020:
021: sink = new StringWriter();
022: }
023:
024: public void testUnconfiguredParser() throws Exception {
025: parser.printHelpOn(sink);
026:
027: assertEquals("No options specified", sink.toString());
028: }
029:
030: public void testOneOptionNoArgNoDescription() throws Exception {
031: parser.accepts("apple");
032:
033: parser.printHelpOn(sink);
034:
035: String[] expectedLines = { "Option Description ",
036: "------- ----------- ", "--apple ", "" };
037: assertEquals(Helpers
038: .join(expectedLines, Helpers.LINE_SEPARATOR), sink
039: .toString());
040: }
041:
042: public void testOneOptionNoArgWithDescription() throws Exception {
043: parser.accepts("a", "some description");
044:
045: parser.printHelpOn(sink);
046:
047: String[] expectedLines = { "Option Description ",
048: "------ ---------------- ", "-a some description ",
049: "" };
050: assertEquals(Helpers
051: .join(expectedLines, Helpers.LINE_SEPARATOR), sink
052: .toString());
053: }
054:
055: public void testTwoOptionsNoArgWithDescription() throws Exception {
056: parser.accepts("a", "some description");
057: parser.accepts("verbose", "even more description");
058:
059: parser.printHelpOn(sink);
060:
061: String[] expectedLines = { "Option Description ",
062: "--------- --------------------- ",
063: "-a some description ",
064: "--verbose even more description ", "" };
065: assertEquals(Helpers
066: .join(expectedLines, Helpers.LINE_SEPARATOR), sink
067: .toString());
068: }
069:
070: public void testOneOptionRequiredArgNoDescription()
071: throws Exception {
072: parser.accepts("a").withRequiredArg();
073:
074: parser.printHelpOn(sink);
075:
076: String[] expectedLines = { "Option Description ",
077: "------ ----------- ", "-a ", "" };
078: assertEquals(Helpers
079: .join(expectedLines, Helpers.LINE_SEPARATOR), sink
080: .toString());
081: }
082:
083: public void testOneOptionRequiredArgNoDescriptionWithType()
084: throws Exception {
085: parser.accepts("a").withRequiredArg().ofType(Integer.class);
086:
087: parser.printHelpOn(sink);
088:
089: String[] expectedLines = { "Option Description ",
090: "------------ ----------- ",
091: "-a <Integer> ", "" };
092: assertEquals(Helpers
093: .join(expectedLines, Helpers.LINE_SEPARATOR), sink
094: .toString());
095: }
096:
097: public void testOneOptionRequiredArgWithDescription()
098: throws Exception {
099: parser.accepts("a", "some value you need").withRequiredArg()
100: .describedAs("numerical");
101:
102: parser.printHelpOn(sink);
103:
104: String[] expectedLines = {
105: "Option Description ",
106: "-------------- ------------------- ",
107: "-a <numerical> some value you need ", "" };
108: assertEquals(Helpers
109: .join(expectedLines, Helpers.LINE_SEPARATOR), sink
110: .toString());
111: }
112:
113: public void testOneOptionRequiredArgWithDescriptionAndType()
114: throws Exception {
115: parser.accepts("a", "some value you need").withRequiredArg()
116: .describedAs("numerical").ofType(Integer.class);
117:
118: parser.printHelpOn(sink);
119:
120: String[] expectedLines = {
121: "Option Description ",
122: "----------------------- ------------------- ",
123: "-a <Integer: numerical> some value you need ", "" };
124: assertEquals(Helpers
125: .join(expectedLines, Helpers.LINE_SEPARATOR), sink
126: .toString());
127: }
128:
129: public void testOneOptionOptionalArgNoDescription()
130: throws Exception {
131: parser.accepts("threshold").withOptionalArg();
132:
133: parser.printHelpOn(sink);
134:
135: String[] expectedLines = { "Option Description ",
136: "----------- ----------- ", "--threshold ",
137: "" };
138: assertEquals(Helpers
139: .join(expectedLines, Helpers.LINE_SEPARATOR), sink
140: .toString());
141: }
142:
143: public void testOneOptionOptionalArgNoDescriptionWithType()
144: throws Exception {
145: parser.accepts("a").withOptionalArg().ofType(Float.class);
146:
147: parser.printHelpOn(sink);
148:
149: String[] expectedLines = { "Option Description ",
150: "---------- ----------- ", "-a [Float] ",
151: "" };
152: assertEquals(Helpers
153: .join(expectedLines, Helpers.LINE_SEPARATOR), sink
154: .toString());
155: }
156:
157: public void testOneOptionOptionalArgWithDescription()
158: throws Exception {
159: parser.accepts("threshold", "some value you need")
160: .withOptionalArg().describedAs("positive integer");
161:
162: parser.printHelpOn(sink);
163:
164: String[] expectedLines = {
165: "Option Description ",
166: "------------------------------ ------------------- ",
167: "--threshold [positive integer] some value you need ",
168: "" };
169: assertEquals(Helpers
170: .join(expectedLines, Helpers.LINE_SEPARATOR), sink
171: .toString());
172: }
173:
174: public void testOneOptionOptionalArgWithDescriptionAndType()
175: throws Exception {
176: parser.accepts("threshold", "some value you need")
177: .withOptionalArg().describedAs(
178: "positive decimal number").ofType(Double.class);
179:
180: parser.printHelpOn(sink);
181:
182: String[] expectedLines = {
183: "Option Description ",
184: "--------------------------------------------- ------------------- ",
185: "--threshold [Double: positive decimal number] some value you need ",
186: "" };
187: assertEquals(Helpers
188: .join(expectedLines, Helpers.LINE_SEPARATOR), sink
189: .toString());
190: }
191:
192: public void testAlternativeLongOptions() throws Exception {
193: parser.recognizeAlternativeLongOptions(true);
194:
195: parser.printHelpOn(sink);
196:
197: String[] expectedLines = {
198: "Option Description ",
199: "-------------- -------------------------------- ",
200: "-W <opt=value> Alternative form of long options ", "" };
201: assertEquals(Helpers
202: .join(expectedLines, Helpers.LINE_SEPARATOR), sink
203: .toString());
204: }
205:
206: public void testOnOutputStream() throws Exception {
207: ByteArrayOutputStream bytesOut = new ByteArrayOutputStream();
208:
209: parser.printHelpOn(bytesOut);
210:
211: assertEquals("No options specified", bytesOut.toString());
212: }
213: }
|