more elegant parameter initialization
[svjatoslav_commons.git] / src / test / java / eu / svjatoslav / commons / commandline / parameterparser / ParserTest.java
1 /*
2  * Svjatoslav Commons - shared library of common functionality.
3  * Copyright ©2012-2014, Svjatoslav Agejenko, svjatoslav@svjatoslav.eu
4  *
5  * This program is free software; you can redistribute it and/or
6  * modify it under the terms of version 3 of the GNU Lesser General Public License
7  * or later as published by the Free Software Foundation.
8  */
9
10 package eu.svjatoslav.commons.commandline.parameterparser;
11
12 import static org.junit.Assert.assertEquals;
13 import static org.junit.Assert.assertFalse;
14 import static org.junit.Assert.assertTrue;
15
16 import org.junit.Before;
17 import org.junit.Test;
18
19 import eu.svjatoslav.commons.commandline.parameterparser.parameter.StringParameter;
20
21 public class ParserTest {
22
23         Parser parser;
24
25         @Before
26         public void setUp() throws Exception {
27                 parser = new Parser();
28         }
29
30         @Test
31         public void testParse() {
32
33                 // define allowed parameters
34                 final StringParameter helpParameter = parser
35                                 .add(new StringParameter("Show help screen"))
36                                 .addAliases("--help", "-h").setMandatory();
37
38                 final StringParameter compileParameter = parser.add(
39                                 new StringParameter("Compile code")).addAliases("--compile",
40                                 "-c");
41
42                 // check help generation
43                 parser.showHelp();
44
45                 // parse arguments
46                 parser.parse(new String[] { "--help", "section" });
47
48                 // --help was in the arguments
49                 assertTrue(helpParameter.isParameterSpecified());
50
51                 // compile was not present
52                 assertFalse(compileParameter.isParameterSpecified());
53
54                 // validate that help argument was "section"
55                 assertEquals("section", helpParameter.getValue());
56
57         }
58 }