2 * Svjatoslav Commons - shared library of common functionality. Author: Svjatoslav Agejenko.
3 * This project is released under Creative Commons Zero (CC0) license.
5 package eu.svjatoslav.commons.commandline.parameterparser;
7 import eu.svjatoslav.commons.commandline.parameterparser.parameter.FileParameter;
8 import eu.svjatoslav.commons.commandline.parameterparser.parameter.StringParameter;
9 import org.junit.Before;
10 import org.junit.Test;
13 import java.io.FileWriter;
14 import java.io.IOException;
16 import static org.junit.Assert.*;
18 public class ParserTest {
24 parser = new Parser();
28 public void testParse() throws IOException {
30 // define allowed parameters
31 final StringParameter helpParameter = parser.add(new StringParameter("Show help screen")
32 .addAliases("--help", "-h").setMandatory());
34 final StringParameter compileParameter = parser.add(new StringParameter("Compile code"))
35 .addAliases("--compile", "-c");
37 FileParameter fileParameter = parser.add(new FileParameter("Input file")
38 .addAliases("-i").mustExist());
40 createTemporaryFile();
42 // check help generation
46 parser.parse(new String[]{"--help", "section", "-i", "/tmp/file with spaces"});
48 // --help was in the arguments
49 assertTrue(helpParameter.isSpecified());
51 // compile was not present
52 assertFalse(compileParameter.isSpecified());
54 // validate that help argument was "section"
55 assertEquals("section", helpParameter.getValue());
57 assertTrue(fileParameter.isSpecified());
58 assertEquals("/tmp/file with spaces", fileParameter.getValue().getAbsolutePath());
62 private void createTemporaryFile() throws IOException {
63 File fileWithSpaces = new File("/tmp/file with spaces");
64 FileWriter writer = new FileWriter(fileWithSpaces);