@@ -151,8 +151,99 @@ public Stream<? extends Arguments> provideArguments(final ExtensionContext conte
151151 }
152152 }
153153
154+ @ Override
155+ @ BeforeEach
156+ public void setUp () {
157+ super .setUp ();
158+ parser = new DefaultParser ();
159+ }
160+
161+ @ Test
162+ void testBuilder () {
163+ // @formatter:off
164+ final Builder builder = DefaultParser .builder ()
165+ .setStripLeadingAndTrailingQuotes (false )
166+ .setAllowPartialMatching (false )
167+ .setDeprecatedHandler (null );
168+ // @formatter:on
169+ parser = builder .build ();
170+ assertEquals (DefaultParser .class , parser .getClass ());
171+ parser = builder .get ();
172+ assertEquals (DefaultParser .class , parser .getClass ());
173+ }
174+
175+ @ Test
176+ void testDeprecated () throws ParseException {
177+ final Set <Option > handler = new HashSet <>();
178+ parser = DefaultParser .builder ().setDeprecatedHandler (handler ::add ).build ();
179+ final Option opt1 = Option .builder ().option ("d1" ).deprecated ().build ();
180+ // @formatter:off
181+ final Option opt2 = Option .builder ().option ("d2" ).deprecated (DeprecatedAttributes .builder ()
182+ .setForRemoval (true )
183+ .setSince ("1.0" )
184+ .setDescription ("Do this instead." ).get ()).build ();
185+ // @formatter:on
186+ final Option opt3 = Option .builder ().option ("a" ).build ();
187+ // @formatter:off
188+ final CommandLine cl = parser .parse (new Options ()
189+ .addOption (opt1 )
190+ .addOption (opt2 )
191+ .addOption (opt3 ),
192+ new String [] {"-d1" , "-d2" , "-a" });
193+ // @formatter:on
194+ // Trigger handler:
195+ assertTrue (cl .hasOption (opt1 .getOpt ()));
196+ assertTrue (cl .hasOption (opt2 .getOpt ()));
197+ assertTrue (cl .hasOption (opt3 .getOpt ()));
198+ // Assert handler was triggered
199+ assertTrue (handler .contains (opt1 ));
200+ assertTrue (handler .contains (opt2 ));
201+ assertFalse (handler .contains (opt3 ));
202+ }
203+
204+ @ Test
205+ void testLegacyStopAtNonOption () throws ParseException {
206+ final Option a = Option .builder ().option ("a" ).longOpt ("first-letter" ).build ();
207+ final Option b = Option .builder ().option ("b" ).longOpt ("second-letter" ).build ();
208+ final Option c = Option .builder ().option ("c" ).longOpt ("third-letter" ).build ();
209+
210+ final Options options = new Options ();
211+ options .addOption (a );
212+ options .addOption (b );
213+ options .addOption (c );
214+
215+ final String [] args = {"-a" , "-b" , "-c" , "-d" , "arg1" , "arg2" }; // -d is rogue option
216+
217+ final DefaultParser parser = new DefaultParser ();
218+
219+ final CommandLine commandLine = parser .parse (options , args , null , true );
220+ assertEquals (3 , commandLine .getOptions ().length );
221+ assertEquals (3 , commandLine .getArgs ().length );
222+ assertTrue (commandLine .getArgList ().contains ("-d" ));
223+ assertTrue (commandLine .getArgList ().contains ("arg1" ));
224+ assertTrue (commandLine .getArgList ().contains ("arg2" ));
225+
226+ final UnrecognizedOptionException e = assertThrows (UnrecognizedOptionException .class , () -> parser .parse (options , args , null , false ));
227+ assertTrue (e .getMessage ().contains ("-d" ));
228+ }
229+
230+ @ Override
231+ @ Test
232+ @ Disabled ("Test case handled in the parameterized tests as \" DEFAULT behavior\" " )
233+ void testLongOptionWithEqualsQuoteHandling () throws Exception {
234+ }
235+
236+ @ ParameterizedTest (name = "{index}. {0}" )
237+ @ ArgumentsSource (ExternalArgumentsProvider .class )
238+ void testParameterized (final String testName , final CommandLineParser parser , final String [] args , final String expected ,
239+ final String option , final String message ) throws Exception {
240+ final CommandLine cl = parser .parse (options , args );
241+
242+ assertEquals (expected , cl .getOptionValue (option ), message );
243+ }
244+
154245 @ Test
155- void chainingParsersIgnoreHappyPath () throws ParseException {
246+ void testParseIgnoreHappyPath () throws ParseException {
156247 final Option a = Option .builder ().option ("a" ).longOpt ("first-letter" ).build ();
157248 final Option b = Option .builder ().option ("b" ).longOpt ("second-letter" ).build ();
158249 final Option c = Option .builder ().option ("c" ).longOpt ("third-letter" ).build ();
@@ -201,7 +292,7 @@ void chainingParsersIgnoreHappyPath() throws ParseException {
201292 }
202293
203294 @ Test
204- void chainingParsersIgnoreNonHappyPath () throws ParseException {
295+ void testParseIgnoreNonHappyPath () throws ParseException {
205296 final Option a = Option .builder ().option ("a" ).longOpt ("first-letter" ).build ();
206297 final Option b = Option .builder ().option ("b" ).longOpt ("second-letter" ).build ();
207298 final Option c = Option .builder ().option ("c" ).longOpt ("third-letter" ).build ();
@@ -228,7 +319,13 @@ void chainingParsersIgnoreNonHappyPath() throws ParseException {
228319 }
229320
230321 @ Test
231- void chainingParsersSkipHappyPath () throws ParseException {
322+ void testParseNullOption () throws ParseException {
323+ // Edge case
324+ assertThrows (NullPointerException .class , () -> new DefaultParser ().parse (null , null , DefaultParser .NonOptionAction .IGNORE , "-a" ));
325+ }
326+
327+ @ Test
328+ void testParseSkipHappyPath () throws ParseException {
232329 final Option a = Option .builder ().option ("a" ).longOpt ("first-letter" ).build ();
233330 final Option b = Option .builder ().option ("b" ).longOpt ("second-letter" ).build ();
234331 final Option c = Option .builder ().option ("c" ).longOpt ("third-letter" ).build ();
@@ -277,7 +374,7 @@ void chainingParsersSkipHappyPath() throws ParseException {
277374 }
278375
279376 @ Test
280- void chainingParsersSkipNonHappyPath () throws ParseException {
377+ void testParseSkipNonHappyPath () throws ParseException {
281378 final Option a = Option .builder ().option ("a" ).longOpt ("first-letter" ).build ();
282379 final Option b = Option .builder ().option ("b" ).longOpt ("second-letter" ).build ();
283380 final Option c = Option .builder ().option ("c" ).longOpt ("third-letter" ).build ();
@@ -303,103 +400,6 @@ void chainingParsersSkipNonHappyPath() throws ParseException {
303400 assertTrue (e .getMessage ().contains ("-d" ));
304401 }
305402
306- @ Test
307- void legacyStopAtNonOption () throws ParseException {
308- final Option a = Option .builder ().option ("a" ).longOpt ("first-letter" ).build ();
309- final Option b = Option .builder ().option ("b" ).longOpt ("second-letter" ).build ();
310- final Option c = Option .builder ().option ("c" ).longOpt ("third-letter" ).build ();
311-
312- final Options options = new Options ();
313- options .addOption (a );
314- options .addOption (b );
315- options .addOption (c );
316-
317- final String [] args = {"-a" , "-b" , "-c" , "-d" , "arg1" , "arg2" }; // -d is rogue option
318-
319- final DefaultParser parser = new DefaultParser ();
320-
321- final CommandLine commandLine = parser .parse (options , args , null , true );
322- assertEquals (3 , commandLine .getOptions ().length );
323- assertEquals (3 , commandLine .getArgs ().length );
324- assertTrue (commandLine .getArgList ().contains ("-d" ));
325- assertTrue (commandLine .getArgList ().contains ("arg1" ));
326- assertTrue (commandLine .getArgList ().contains ("arg2" ));
327-
328- final UnrecognizedOptionException e = assertThrows (UnrecognizedOptionException .class , () -> parser .parse (options , args , null , false ));
329- assertTrue (e .getMessage ().contains ("-d" ));
330- }
331-
332- @ Override
333- @ BeforeEach
334- public void setUp () {
335- super .setUp ();
336- parser = new DefaultParser ();
337- }
338-
339- @ Test
340- void testBuilder () {
341- // @formatter:off
342- final Builder builder = DefaultParser .builder ()
343- .setStripLeadingAndTrailingQuotes (false )
344- .setAllowPartialMatching (false )
345- .setDeprecatedHandler (null );
346- // @formatter:on
347- parser = builder .build ();
348- assertEquals (DefaultParser .class , parser .getClass ());
349- parser = builder .get ();
350- assertEquals (DefaultParser .class , parser .getClass ());
351- }
352-
353- @ Test
354- void testDeprecated () throws ParseException {
355- final Set <Option > handler = new HashSet <>();
356- parser = DefaultParser .builder ().setDeprecatedHandler (handler ::add ).build ();
357- final Option opt1 = Option .builder ().option ("d1" ).deprecated ().build ();
358- // @formatter:off
359- final Option opt2 = Option .builder ().option ("d2" ).deprecated (DeprecatedAttributes .builder ()
360- .setForRemoval (true )
361- .setSince ("1.0" )
362- .setDescription ("Do this instead." ).get ()).build ();
363- // @formatter:on
364- final Option opt3 = Option .builder ().option ("a" ).build ();
365- // @formatter:off
366- final CommandLine cl = parser .parse (new Options ()
367- .addOption (opt1 )
368- .addOption (opt2 )
369- .addOption (opt3 ),
370- new String [] {"-d1" , "-d2" , "-a" });
371- // @formatter:on
372- // Trigger handler:
373- assertTrue (cl .hasOption (opt1 .getOpt ()));
374- assertTrue (cl .hasOption (opt2 .getOpt ()));
375- assertTrue (cl .hasOption (opt3 .getOpt ()));
376- // Assert handler was triggered
377- assertTrue (handler .contains (opt1 ));
378- assertTrue (handler .contains (opt2 ));
379- assertFalse (handler .contains (opt3 ));
380- }
381-
382- @ Override
383- @ Test
384- @ Disabled ("Test case handled in the parameterized tests as \" DEFAULT behavior\" " )
385- void testLongOptionWithEqualsQuoteHandling () throws Exception {
386- }
387-
388- @ ParameterizedTest (name = "{index}. {0}" )
389- @ ArgumentsSource (ExternalArgumentsProvider .class )
390- void testParameterized (final String testName , final CommandLineParser parser , final String [] args , final String expected ,
391- final String option , final String message ) throws Exception {
392- final CommandLine cl = parser .parse (options , args );
393-
394- assertEquals (expected , cl .getOptionValue (option ), message );
395- }
396-
397- @ Test
398- void testParseNullOption () throws ParseException {
399- // Edge case
400- assertThrows (NullPointerException .class , () -> new DefaultParser ().parse (null , null , DefaultParser .NonOptionAction .IGNORE , "-a" ));
401- }
402-
403403 @ Override
404404 @ Test
405405 @ Disabled ("Test case handled in the parameterized tests as \" DEFAULT behavior\" " )
0 commit comments