diff --git a/src/main/java/de/vimo/core/ParserVivi.java b/src/main/java/de/vimo/core/ParserVivi.java new file mode 100644 index 0000000..2d54cf6 --- /dev/null +++ b/src/main/java/de/vimo/core/ParserVivi.java @@ -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 flagsS; + private String command = null; + private String[] splitFlag; + private String result; + + @Override + public Line parse(String raw) { + + List flagAll = new ArrayList<>(List.of()); + List 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; + } +} diff --git a/src/test/java/de/vimo/core/ParserTest.java b/src/test/java/de/vimo/core/ParserTest.java index d865a2b..59eaa92 100644 --- a/src/test/java/de/vimo/core/ParserTest.java +++ b/src/test/java/de/vimo/core/ParserTest.java @@ -14,37 +14,50 @@ public class ParserTest { @Test public void testParser() { - Parser parser = new Parser() { - @Override - public Line parse(String raw) { - String regex = "[\\s]"; - List flagsS = new ArrayList<>(List.of()); - List flagAll = new ArrayList<>(List.of()); - String result = ""; - List argsS = new ArrayList<>(List.of()); - String[] myArray = raw.split(regex); +// Parser parser = new Parser() { +// @Override +// public Line parse(String raw) { +// String regex = "[\\s]"; +// List flagsS = new ArrayList<>(List.of()); +// List flagAll = new ArrayList<>(List.of()); +// String result = ""; +// List 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")); - testInput(parser, "ls -la ./what", "ls", List.of("l", "a"), List.of("./what")); - testInput(parser, "rm -r /bin /opt", "rm", List.of("r"), List.of("/bin", "/opt")); - 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")); + testInput(parser, " ls -la", "ls", List.of("l", "a"), List.of()); + testInput(parser, "ls ./hello/world ", "ls", List.of(), List.of("./hello/world")); + testInput(parser, "ls -la ./what", "ls", List.of("l", "a"), List.of("./what")); + testInput(parser, "rm -r /bin /opt", "rm", List.of("r"), List.of("/bin", "/opt")); + 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 expectedFlags, List expectedArgs) { diff --git a/target/classes/de/vimo/core/ParserVivi.class b/target/classes/de/vimo/core/ParserVivi.class new file mode 100644 index 0000000..7b71357 Binary files /dev/null and b/target/classes/de/vimo/core/ParserVivi.class differ diff --git a/target/test-classes/de/vimo/core/ParserTest$1.class b/target/test-classes/de/vimo/core/ParserTest$1.class deleted file mode 100644 index b90da57..0000000 Binary files a/target/test-classes/de/vimo/core/ParserTest$1.class and /dev/null differ diff --git a/target/test-classes/de/vimo/core/ParserTest.class b/target/test-classes/de/vimo/core/ParserTest.class index d2f70ba..1f79fec 100644 Binary files a/target/test-classes/de/vimo/core/ParserTest.class and b/target/test-classes/de/vimo/core/ParserTest.class differ