Changed license to LGPLv3 or later.
[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                                 .createStringParameter("Show help screen")
36                                 .addAliases("--help", "-h").setMandatory();
37
38                 final StringParameter compileParameter = parser.createStringParameter(
39                                 "Compile code").addAliases("--compile", "-c");
40
41                 // check help generation
42                 parser.showHelp();
43
44                 // parse arguments
45                 parser.parse(new String[] { "--help", "section" });
46
47                 // --help was in the arguments
48                 assertTrue(helpParameter.isParameterSpecified());
49
50                 // compile was not present
51                 assertFalse(compileParameter.isParameterSpecified());
52
53                 // validate that help argument was "section"
54                 assertEquals("section", helpParameter.getValue());
55
56         }
57 }