Skip to content

Commit 3d11b55

Browse files
committed
More tests for the Option class covering the hasArgs, hasArgName and getValue methods
git-svn-id: https://svn.apache.org/repos/asf/commons/proper/cli/branches/cli-1.x@661726 13f79535-47bb-0310-9956-ffa450edef68
1 parent 05e7cd0 commit 3d11b55

1 file changed

Lines changed: 77 additions & 20 deletions

File tree

src/test/org/apache/commons/cli/OptionTest.java

Lines changed: 77 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -22,29 +22,35 @@
2222
/**
2323
* @author brianegge
2424
*/
25-
public class OptionTest extends TestCase {
26-
27-
private static class TestOption extends Option {
28-
public TestOption(String opt, boolean hasArg, String description) throws IllegalArgumentException {
25+
public class OptionTest extends TestCase
26+
{
27+
private static class TestOption extends Option
28+
{
29+
public TestOption(String opt, boolean hasArg, String description) throws IllegalArgumentException
30+
{
2931
super(opt, hasArg, description);
3032
}
31-
public boolean addValue(String value) {
33+
34+
public boolean addValue(String value)
35+
{
3236
addValueForProcessing(value);
3337
return true;
3438
}
35-
}
39+
}
3640

37-
public void testClear() {
38-
TestOption option = new TestOption("x", true, "");
39-
assertEquals(0, option.getValuesList().size());
40-
option.addValue("a");
41-
assertEquals(1, option.getValuesList().size());
42-
option.clearValues();
43-
assertEquals(0, option.getValuesList().size());
44-
}
41+
public void testClear()
42+
{
43+
TestOption option = new TestOption("x", true, "");
44+
assertEquals(0, option.getValuesList().size());
45+
option.addValue("a");
46+
assertEquals(1, option.getValuesList().size());
47+
option.clearValues();
48+
assertEquals(0, option.getValuesList().size());
49+
}
4550

4651
// See http://issues.apache.org/jira/browse/CLI-21
47-
public void testClone() throws CloneNotSupportedException {
52+
public void testClone() throws CloneNotSupportedException
53+
{
4854
TestOption a = new TestOption("a", true, "");
4955
TestOption b = (TestOption) a.clone();
5056
assertEquals(a, b);
@@ -59,25 +65,76 @@ public void testClone() throws CloneNotSupportedException {
5965
assertEquals(2, b.getValues().length);
6066
}
6167

62-
private static class DefaultOption extends Option {
63-
68+
private static class DefaultOption extends Option
69+
{
6470
private final String defaultValue;
6571

66-
public DefaultOption(String opt, String description, String defaultValue) throws IllegalArgumentException {
72+
public DefaultOption(String opt, String description, String defaultValue) throws IllegalArgumentException
73+
{
6774
super(opt, true, description);
6875
this.defaultValue = defaultValue;
6976
}
7077

71-
public String getValue() {
78+
public String getValue()
79+
{
7280
return super.getValue() != null ? super.getValue() : defaultValue;
7381
}
7482
}
7583

76-
public void testSubclass() throws CloneNotSupportedException {
84+
public void testSubclass() throws CloneNotSupportedException
85+
{
7786
Option option = new DefaultOption("f", "file", "myfile.txt");
7887
Option clone = (Option) option.clone();
7988
assertEquals("myfile.txt", clone.getValue());
8089
assertEquals(DefaultOption.class, clone.getClass());
8190
}
8291

92+
public void testHasArgName()
93+
{
94+
Option option = new Option("f", null);
95+
96+
option.setArgName(null);
97+
assertFalse(option.hasArgName());
98+
99+
option.setArgName("");
100+
assertFalse(option.hasArgName());
101+
102+
option.setArgName("file");
103+
assertTrue(option.hasArgName());
104+
}
105+
106+
public void testHasArgs()
107+
{
108+
Option option = new Option("f", null);
109+
110+
option.setArgs(0);
111+
assertFalse(option.hasArgs());
112+
113+
option.setArgs(1);
114+
assertFalse(option.hasArgs());
115+
116+
option.setArgs(10);
117+
assertTrue(option.hasArgs());
118+
119+
option.setArgs(Option.UNLIMITED_VALUES);
120+
assertTrue(option.hasArgs());
121+
122+
option.setArgs(Option.UNINITIALIZED);
123+
assertFalse(option.hasArgs());
124+
}
125+
126+
public void testGetValue()
127+
{
128+
Option option = new Option("f", null);
129+
option.setArgs(Option.UNLIMITED_VALUES);
130+
131+
assertEquals("default", option.getValue("default"));
132+
assertEquals(null, option.getValue(0));
133+
134+
option.addValueForProcessing("foo");
135+
136+
assertEquals("foo", option.getValue());
137+
assertEquals("foo", option.getValue(0));
138+
assertEquals("foo", option.getValue("default"));
139+
}
83140
}

0 commit comments

Comments
 (0)