/*
- * Svjatoslav Commons - shared library of common functionality.
- * Copyright ©2012-2017, Svjatoslav Agejenko, svjatoslav@svjatoslav.eu
- *
- * This program is free software; you can redistribute it and/or
- * modify it under the terms of version 3 of the GNU Lesser General Public License
- * or later as published by the Free Software Foundation.
+ * Svjatoslav Commons - shared library of common functionality. Author: Svjatoslav Agejenko.
+ * This project is released under Creative Commons Zero (CC0) license.
*/
-
package eu.svjatoslav.commons.commandline.parameterparser;
+import eu.svjatoslav.commons.commandline.parameterparser.parameter.FileParameter;
import eu.svjatoslav.commons.commandline.parameterparser.parameter.StringParameter;
import org.junit.Before;
import org.junit.Test;
+import java.io.File;
+import java.io.FileWriter;
+import java.io.IOException;
+
import static org.junit.Assert.*;
public class ParserTest {
Parser parser;
@Before
- public void setUp() throws Exception {
+ public void setUp() {
parser = new Parser();
}
@Test
- public void testParse() {
+ public void testParse() throws IOException {
// define allowed parameters
- final StringParameter helpParameter = parser
- .add(new StringParameter("Show help screen"))
- .addAliases("--help", "-h").setMandatory();
+ final StringParameter helpParameter = parser.add(new StringParameter("Show help screen")
+ .addAliases("--help", "-h").setMandatory());
+
+ final StringParameter compileParameter = parser.add(new StringParameter("Compile code"))
+ .addAliases("--compile", "-c");
+
+ FileParameter fileParameter = parser.add(new FileParameter("Input file")
+ .addAliases("-i").mustExist());
- final StringParameter compileParameter = parser.add(
- new StringParameter("Compile code")).addAliases("--compile",
- "-c");
+ createTemporaryFile();
// check help generation
parser.showHelp();
// parse arguments
- parser.parse(new String[]{"--help", "section"});
+ parser.parse(new String[]{"--help", "section", "-i", "/tmp/file with spaces"});
// --help was in the arguments
- assertTrue(helpParameter.isParameterSpecified());
+ assertTrue(helpParameter.isSpecified());
// compile was not present
- assertFalse(compileParameter.isParameterSpecified());
+ assertFalse(compileParameter.isSpecified());
// validate that help argument was "section"
assertEquals("section", helpParameter.getValue());
+ assertTrue(fileParameter.isSpecified());
+ assertEquals("/tmp/file with spaces", fileParameter.getValue().getAbsolutePath());
+
+ }
+
+ private void createTemporaryFile() throws IOException {
+ File fileWithSpaces = new File("/tmp/file with spaces");
+ FileWriter writer = new FileWriter(fileWithSpaces);
+ writer.write("test");
+ writer.close();
}
}