/*
* Svjatoslav Commons - shared library of common functionality.
- * Copyright (C) 2012, Svjatoslav Agejenko, svjatoslav@svjatoslav.eu
- *
+ * Copyright ©2012-2014, Svjatoslav Agejenko, svjatoslav@svjatoslav.eu
+ *
* This program is free software; you can redistribute it and/or
- * modify it under the terms of version 2 of the GNU General Public License
- * as published by the Free Software Foundation.
+ * modify it under the terms of version 3 of the GNU Lesser General Public License
+ * or later as published by the Free Software Foundation.
*/
package eu.svjatoslav.commons.commandline.parameterparser;
-import static org.junit.Assert.assertEquals;
-import static org.junit.Assert.assertFalse;
-import static org.junit.Assert.assertTrue;
-
+import eu.svjatoslav.commons.commandline.parameterparser.parameter.StringParameter;
import org.junit.Before;
import org.junit.Test;
-import eu.svjatoslav.commons.commandline.parameterparser.arguments.IntegerArgument;
+import static org.junit.Assert.*;
public class ParserTest {
- Parser parser;
-
- @Before
- public void setUp() throws Exception {
- parser = new Parser();
- }
-
- @Test
- public void testParse() {
+ Parser parser;
- // define allowed parameters
- final Parameter helpParameter = new Parameter("Show help screen", "-h",
- "--help");
- parser.addParameter(helpParameter);
+ @Before
+ public void setUp() throws Exception {
+ parser = new Parser();
+ }
- final Parameter compileParameter = new Parameter("Compile code", "-c",
- "--compile");
- parser.addParameter(compileParameter);
+ @Test
+ public void testParse() {
- final Parameter bitrateParameter = new Parameter(false, true, false,
- new IntegerArgument(), "Target bitrate", "-b", "--bitrate");
- parser.addParameter(bitrateParameter);
+ // define allowed parameters
+ final StringParameter helpParameter = parser
+ .add(new StringParameter("Show help screen"))
+ .addAliases("--help", "-h").setMandatory();
- // check help generation
- parser.showHelp();
+ final StringParameter compileParameter = parser.add(
+ new StringParameter("Compile code")).addAliases("--compile",
+ "-c");
- // parse arguments
- parser.parse(new String[] { "--help", "-b", "123" });
+ // check help generation
+ parser.showHelp();
- // --help was in the arguments
- assertTrue(helpParameter.isParameterSpecified());
+ // parse arguments
+ parser.parse(new String[]{"--help", "section"});
- // compile was not present
- assertFalse(compileParameter.isParameterSpecified());
+ // --help was in the arguments
+ assertTrue(helpParameter.isParameterSpecified());
- // bitrate is given as 123
- assertTrue(bitrateParameter.isParameterSpecified());
+ // compile was not present
+ assertFalse(compileParameter.isParameterSpecified());
- assertEquals(123, (int) bitrateParameter.getArgumentsAsIntegers()
- .get(0));
- }
+ // validate that help argument was "section"
+ assertEquals("section", helpParameter.getValue());
+ }
}