Skip to content

Commit

Permalink
Merge pull request #16 from ZenoxTek/feature/parser
Browse files Browse the repository at this point in the history
Feature/parser
  • Loading branch information
ZenoxTek authored Nov 30, 2019
2 parents 77d5939 + 430d690 commit c78e8c4
Show file tree
Hide file tree
Showing 3 changed files with 101 additions and 3 deletions.
3 changes: 0 additions & 3 deletions src/main/java/Bot.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.util.Date;
import java.util.Locale;

public class Bot {

Expand Down
33 changes: 33 additions & 0 deletions src/main/java/Parser.java
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;
}
}
68 changes: 68 additions & 0 deletions src/test/java/ParserTest.java
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());
}
}

0 comments on commit c78e8c4

Please sign in to comment.