initial commit
[svjatoslav_commons.git] / src / test / java / eu / svjatoslav / commons / commandline / parameterparser / ParserTest.java
1 /*
2  * Svjatoslav Commons - shared library of common functionality.
3  * Copyright (C) 2012, 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 2 of the GNU General Public License
7  * 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.arguments.IntegerArgument;
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 Parameter helpParameter = new Parameter("Show help screen", "-h",
35                                 "--help");
36                 parser.addParameter(helpParameter);
37
38                 final Parameter compileParameter = new Parameter("Compile code", "-c",
39                                 "--compile");
40                 parser.addParameter(compileParameter);
41
42                 final Parameter bitrateParameter = new Parameter(false, true, false,
43                                 new IntegerArgument(), "Target bitrate", "-b", "--bitrate");
44                 parser.addParameter(bitrateParameter);
45
46                 // check help generation
47                 parser.showHelp();
48
49                 // parse arguments
50                 parser.parse(new String[] { "--help", "-b", "123" });
51
52                 // --help was in the arguments
53                 assertTrue(helpParameter.isParameterSpecified());
54
55                 // compile was not present
56                 assertFalse(compileParameter.isParameterSpecified());
57
58                 // bitrate is given as 123
59                 assertTrue(bitrateParameter.isParameterSpecified());
60
61                 assertEquals(123, (int) bitrateParameter.getArgumentsAsIntegers()
62                                 .get(0));
63         }
64
65 }