Code cleanup and formatting. Migrated to java 1.8.
[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 eu.svjatoslav.commons.commandline.parameterparser.parameter.StringParameter;
13 import org.junit.Before;
14 import org.junit.Test;
15
16 import static org.junit.Assert.*;
17
18 public class ParserTest {
19
20     Parser parser;
21
22     @Before
23     public void setUp() throws Exception {
24         parser = new Parser();
25     }
26
27     @Test
28     public void testParse() {
29
30         // define allowed parameters
31         final StringParameter helpParameter = parser
32                 .add(new StringParameter("Show help screen"))
33                 .addAliases("--help", "-h").setMandatory();
34
35         final StringParameter compileParameter = parser.add(
36                 new StringParameter("Compile code")).addAliases("--compile",
37                 "-c");
38
39         // check help generation
40         parser.showHelp();
41
42         // parse arguments
43         parser.parse(new String[]{"--help", "section"});
44
45         // --help was in the arguments
46         assertTrue(helpParameter.isParameterSpecified());
47
48         // compile was not present
49         assertFalse(compileParameter.isParameterSpecified());
50
51         // validate that help argument was "section"
52         assertEquals("section", helpParameter.getValue());
53
54     }
55 }