-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #16 from ZenoxTek/feature/parser
Feature/parser
- Loading branch information
Showing
3 changed files
with
101 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
public class Parser { | ||
|
||
private String[] argumentsReceived; | ||
private List<String> argumentsToReturn = new ArrayList<>(); | ||
|
||
Parser(String[] args) { | ||
this.argumentsReceived = args; | ||
} | ||
|
||
public String getArgumentsAtIndex(int index){ | ||
if (this.argumentsToReturn != null){ | ||
if (index < this.argumentsToReturn.size() && index >= 0){ | ||
return this.argumentsToReturn.get(index); | ||
} | ||
} | ||
return null; | ||
} | ||
|
||
public boolean parse(){ | ||
if (this.argumentsReceived != null){ | ||
for (int i = 0 ; i < this.argumentsReceived.length ; i++){ | ||
if (this.argumentsReceived[i].equals("-p") && i + 1 < this.argumentsReceived.length){ | ||
this.argumentsToReturn.add(this.argumentsReceived[i + 1]); | ||
return true; | ||
} | ||
} | ||
} | ||
return false; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
import org.junit.Test; | ||
|
||
import static org.junit.Assert.*; | ||
import static org.junit.Assert.assertEquals; | ||
|
||
public class ParserTest { | ||
|
||
@Test | ||
public void getArgumentsAtIndex() { | ||
String[] args = {"-p", "toto"}; | ||
Parser parser = new Parser(args); | ||
String expected = "toto"; | ||
|
||
parser.parse(); | ||
assertEquals(expected, parser.getArgumentsAtIndex(0)); | ||
} | ||
|
||
@Test | ||
public void getArgumentsAtOutIndex() { | ||
String[] args = {"-p", "toto"}; | ||
Parser parser = new Parser(args); | ||
|
||
parser.parse(); | ||
assertNull(parser.getArgumentsAtIndex(3)); | ||
} | ||
|
||
@Test | ||
public void getArgumentsAtNegativeIndex() { | ||
String[] args = {"-p", "toto"}; | ||
Parser parser = new Parser(args); | ||
|
||
parser.parse(); | ||
assertNull(parser.getArgumentsAtIndex(-3)); | ||
} | ||
|
||
@Test | ||
public void parse() { | ||
String[] args = {"-p", "toto"}; | ||
Parser parser = new Parser(args); | ||
|
||
assertTrue(parser.parse()); | ||
} | ||
|
||
@Test | ||
public void parseEmptyArgs() { | ||
String[] args = {}; | ||
Parser parser = new Parser(args); | ||
|
||
assertFalse(parser.parse()); | ||
} | ||
|
||
|
||
@Test | ||
public void parseWithOnlyOption() { | ||
String[] args = {"-p"}; | ||
Parser parser = new Parser(args); | ||
|
||
assertFalse(parser.parse()); | ||
} | ||
|
||
@Test | ||
public void parseNullArgs() { | ||
String[] args = null; | ||
Parser parser = new Parser(args); | ||
|
||
assertFalse(parser.parse()); | ||
} | ||
} |