aboutsummaryrefslogtreecommitdiff
path: root/test/org/gnunet/util/getopt/GetoptTest.java
diff options
context:
space:
mode:
Diffstat (limited to 'test/org/gnunet/util/getopt/GetoptTest.java')
-rw-r--r--test/org/gnunet/util/getopt/GetoptTest.java217
1 files changed, 217 insertions, 0 deletions
diff --git a/test/org/gnunet/util/getopt/GetoptTest.java b/test/org/gnunet/util/getopt/GetoptTest.java
new file mode 100644
index 0000000..9f8b223
--- /dev/null
+++ b/test/org/gnunet/util/getopt/GetoptTest.java
@@ -0,0 +1,217 @@
1/*
2 This file is part of GNUnet.
3 (C) 2011, 2012 Christian Grothoff (and other contributing authors)
4
5 GNUnet is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published
7 by the Free Software Foundation; either version 3, or (at your
8 option) any later version.
9
10 GNUnet is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with GNUnet; see the file COPYING. If not, write to the
17 Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA.
19 */
20
21package org.gnunet.util.getopt;
22
23
24import org.junit.Assert;
25import org.junit.Test;
26
27class Target {
28 @Option(
29 action = OptionAction.STORE_STRING,
30 shortname = "s",
31 longname = "string",
32 argumentName = "SOME_STRING",
33 description = "just some string"
34 )
35 String someString;
36
37 @Option(
38 action = OptionAction.SET,
39 shortname = "y",
40 longname = "set",
41 description = "enable, default disabled"
42 )
43 boolean set = false;
44
45 @Option(
46 action = OptionAction.RESET,
47 shortname = "n",
48 longname = "reset",
49 description = "disable, default enabled"
50 )
51 boolean reset = true;
52
53
54 @Option(
55 action = OptionAction.INCREMENT,
56 shortname = "i",
57 longname = "inc",
58 description = "increment a counter"
59 )
60 int counter = 0;
61
62 @Option(
63 action = OptionAction.STORE_NUMBER,
64 shortname = "w",
65 longname = "value",
66 description = "some value"
67 )
68 int intVal = 0;
69}
70
71public class GetoptTest {
72
73 @Test
74 public void test_str() {
75 Target t = new Target();
76 Parser p = new Parser(t);
77
78 t.someString = null;
79
80 // argument after shortopt
81 p.parse(new String[]{"-s", "foo"});
82 Assert.assertEquals("foo", t.someString);
83
84 t.someString = null;
85
86 // argument directly with shortopt
87 p.parse(new String[]{"-sfoo"});
88 Assert.assertEquals("foo", t.someString);
89
90 t.someString = null;
91
92 p.parse(new String[]{"--string=foo"});
93 Assert.assertEquals("foo", t.someString);
94
95 t.someString = null;
96
97 p.parse(new String[]{"--string", "foo"});
98 Assert.assertEquals("foo", t.someString);
99
100
101 t.someString = null;
102
103 // last argument counts
104 p.parse(new String[]{"--string", "bar", "--string", "foo"});
105 Assert.assertEquals("foo", t.someString);
106
107 t.someString = null;
108
109 boolean thrown;
110
111 thrown = false;
112
113 // absence of argument throws ArgumentError (longopt)
114 try {
115 p.parse(new String[]{"--string"});
116 } catch (Parser.ArgumentError e) {
117 thrown = true;
118 }
119
120 Assert.assertTrue(thrown);
121
122 thrown = false;
123 // absence of argument throws ArgumentError (shortopt)
124 try {
125 p.parse(new String[]{"-s"});
126 } catch (Parser.ArgumentError e) {
127 thrown = true;
128 }
129
130 Assert.assertTrue(thrown);
131
132 t.someString = null;
133
134 // a way to specify an empty string
135 p.parse(new String[]{"--string="});
136 Assert.assertEquals("", t.someString);
137 }
138
139 @Test
140 public void test_help() {
141 Target t = new Target();
142 Parser p = new Parser(t);
143
144 String help = p.getHelp();
145
146 Assert.assertTrue(help.contains("--string"));
147 Assert.assertTrue(help.contains("-s"));
148
149 Assert.assertTrue(help.contains("-y"));
150 Assert.assertTrue(help.contains("--set"));
151 }
152
153
154 @Test
155 public void test_bool() {
156 Target t = new Target();
157 Parser p = new Parser(t);
158
159 p.parse(new String[]{"--set", "--reset"});
160 Assert.assertTrue(t.set);
161 Assert.assertTrue(!t.reset);
162
163 t.set = false;
164 t.reset = true;
165
166 p.parse(new String[]{"-y", "-n"});
167
168 Assert.assertTrue(t.set);
169 Assert.assertTrue(!t.reset);
170
171 t.set = false;
172 t.reset = true;
173
174 p.parse(new String[]{"-yn"});
175
176 Assert.assertTrue(t.set);
177 Assert.assertTrue(!t.reset);
178 }
179
180 @Test
181 public void test_positional() {
182 Target t = new Target();
183 Parser p = new Parser(t);
184
185 String[] rest = p.parse(new String[]{"--string=bla", "foo", "bar", "--set", "--", "--reset", "baz"});
186
187 Assert.assertArrayEquals(new String[]{"foo", "bar", "--reset", "baz"}, rest);
188 }
189
190 @Test
191 public void test_long() {
192 Target t = new Target();
193 Parser p = new Parser(t);
194
195 String[] rest;
196
197 rest = p.parse(new String[]{"-w5"});
198 Assert.assertEquals(5, t.intVal);
199
200 rest = p.parse(new String[]{"--value=6"});
201 Assert.assertEquals(6, t.intVal);
202
203 rest = p.parse(new String[]{"--value", "7"});
204 Assert.assertEquals(7, t.intVal);
205
206 rest = p.parse(new String[]{"--value", "-7"});
207 Assert.assertEquals(-7, t.intVal);
208
209 boolean thrown = false;
210 try {
211 rest = p.parse(new String[]{"--value", "x"});
212 } catch (Parser.ArgumentError e) {
213 thrown = true;
214 }
215 Assert.assertTrue(thrown);
216 }
217}