update new tests
This commit is contained in:
parent
9cca9bc8f8
commit
2c0ba72e17
97
src/main/java/de/vimo/core/ParserVivi.java
Normal file
97
src/main/java/de/vimo/core/ParserVivi.java
Normal file
@ -0,0 +1,97 @@
|
||||
package de.vimo.core;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
||||
public class ParserVivi implements Parser {
|
||||
|
||||
private int positionFlag;
|
||||
private String regex = "\\s";
|
||||
private List<String> flagsS;
|
||||
private String command = null;
|
||||
private String[] splitFlag;
|
||||
private String result;
|
||||
|
||||
@Override
|
||||
public Line parse(String raw) {
|
||||
|
||||
List<String> flagAll = new ArrayList<>(List.of());
|
||||
List<String> argsS = new ArrayList<>(List.of());
|
||||
Line resultLine = null;
|
||||
|
||||
String[] myArray = raw.split(regex);
|
||||
|
||||
for (int i = 0; i < myArray.length; i++) {
|
||||
|
||||
if (!myArray[i].isEmpty()) {
|
||||
|
||||
if (myArray[0].equals("-")) {
|
||||
|
||||
resultLine = null;
|
||||
|
||||
} else if (myArray[i].contains("./") || myArray[i].contains("/") ||
|
||||
myArray[i].contains(".")) {
|
||||
|
||||
argsS.add(myArray[i]);
|
||||
resultLine = new Line(command, flagAll, argsS);
|
||||
|
||||
} else if (myArray.length == 1 && myArray[0].equals("ls")) {
|
||||
|
||||
resultLine = new Line(myArray[0], flagAll, argsS);
|
||||
|
||||
} else if (myArray[0].equals("ls") && myArray[1].equals("-")) {
|
||||
|
||||
resultLine = null;
|
||||
|
||||
} else if (myArray[0].equals("ls") && myArray[1].equals("--")) {
|
||||
|
||||
resultLine = null;
|
||||
|
||||
} else if (myArray[i].equals("super") && !myArray[i].contains("-") &&
|
||||
positionFlag == 0) {
|
||||
|
||||
command = myArray[i];
|
||||
resultLine = new Line(command, flagAll, argsS);
|
||||
|
||||
} else if (myArray[i].equals("ls") || myArray[i].equals("rm") ||
|
||||
myArray[i].equals("cd") || myArray[i].equals("mkdir")) {
|
||||
|
||||
command = myArray[i];
|
||||
resultLine = new Line(command, flagAll, argsS);
|
||||
|
||||
} else if (myArray[i].contains("-")) {
|
||||
positionFlag = i;
|
||||
|
||||
splitFlag = myArray[i].split("-");
|
||||
|
||||
if (splitFlag[0].isEmpty()) {
|
||||
result = myArray[i].replace("-", "");
|
||||
|
||||
flagsS = List.of(result.split(""));
|
||||
flagAll.addAll(flagsS);
|
||||
|
||||
resultLine = new Line(command, flagAll, argsS);
|
||||
} else {
|
||||
argsS.add(myArray[i]);
|
||||
resultLine = new Line(command, flagAll, argsS);
|
||||
}
|
||||
|
||||
} else if (!myArray[i].contains("-") && i > positionFlag && i != 0) {
|
||||
|
||||
argsS.add(myArray[i]);
|
||||
resultLine = new Line(command, flagAll, argsS);
|
||||
} else {
|
||||
command = myArray[i];
|
||||
resultLine = null;
|
||||
}
|
||||
} else {
|
||||
if (myArray.length == 1) {
|
||||
resultLine = null;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return resultLine;
|
||||
}
|
||||
}
|
||||
@ -14,29 +14,38 @@ public class ParserTest
|
||||
{
|
||||
@Test
|
||||
public void testParser() {
|
||||
Parser parser = new Parser() {
|
||||
@Override
|
||||
public Line parse(String raw) {
|
||||
String regex = "[\\s]";
|
||||
List<String> flagsS = new ArrayList<>(List.of());
|
||||
List<String> flagAll = new ArrayList<>(List.of());
|
||||
String result = "";
|
||||
List<String> argsS = new ArrayList<>(List.of());
|
||||
String[] myArray = raw.split(regex);
|
||||
// Parser parser = new Parser() {
|
||||
// @Override
|
||||
// public Line parse(String raw) {
|
||||
// String regex = "[\\s]";
|
||||
// List<String> flagsS = new ArrayList<>(List.of());
|
||||
// List<String> flagAll = new ArrayList<>(List.of());
|
||||
// String result = "";
|
||||
// List<String> argsS = new ArrayList<>(List.of());
|
||||
// String[] myArray = raw.split(regex);
|
||||
//
|
||||
// for (int i = 1; i < myArray.length; i++) {
|
||||
// if (myArray[i].contains("-")) {
|
||||
// result = myArray[i].replace("-","");
|
||||
// flagsS = List.of(result.split(""));
|
||||
// flagAll.addAll(flagsS);
|
||||
// } else {
|
||||
// argsS.add(myArray[i]);
|
||||
// }
|
||||
// }
|
||||
// return new Line(myArray[0], flagAll, argsS);
|
||||
// }
|
||||
// }; // <-- Your implementation goes here
|
||||
|
||||
for (int i = 1; i < myArray.length; i++) {
|
||||
if (myArray[i].contains("-")) {
|
||||
result = myArray[i].replace("-","");
|
||||
flagsS = List.of(result.split(""));
|
||||
flagAll.addAll(flagsS);
|
||||
} else {
|
||||
argsS.add(myArray[i]);
|
||||
}
|
||||
}
|
||||
return new Line(myArray[0], flagAll, argsS);
|
||||
}
|
||||
}; // <-- Your implementation goes here
|
||||
Parser parser = new ParserVivi(); // <-- Your implementation goes here
|
||||
|
||||
// test invalid strings -> should return null
|
||||
assertNull(parser.parse(""));
|
||||
assertNull(parser.parse("-"));
|
||||
assertNull(parser.parse("ls -"));
|
||||
assertNull(parser.parse("ls --"));
|
||||
|
||||
// test valid string
|
||||
testInput(parser, "ls", "ls", List.of(), List.of());
|
||||
testInput(parser, " ls -la", "ls", List.of("l", "a"), List.of());
|
||||
testInput(parser, "ls ./hello/world ", "ls", List.of(), List.of("./hello/world"));
|
||||
@ -45,6 +54,10 @@ public class ParserTest
|
||||
testInput(parser, " cd . ", "cd", List.of(), List.of("."));
|
||||
testInput(parser, " mkdir . ", "mkdir", List.of(), List.of("."));
|
||||
testInput(parser, "super -abc -de -f super nice danke paul", "super", List.of("a", "b", "c", "d", "e", "f"), List.of("super", "nice", "danke", "paul"));
|
||||
|
||||
// new tests
|
||||
|
||||
testInput(parser, "print hello-world", "print", List.of(), List.of("hello-world"));
|
||||
}
|
||||
|
||||
private void testInput(Parser parser, String input, String expectedCommand, List<String> expectedFlags, List<String> expectedArgs) {
|
||||
|
||||
BIN
target/classes/de/vimo/core/ParserVivi.class
Normal file
BIN
target/classes/de/vimo/core/ParserVivi.class
Normal file
Binary file not shown.
Binary file not shown.
Binary file not shown.
Loading…
Reference in New Issue
Block a user