ParserTest
This commit is contained in:
parent
7bd3bc0909
commit
9cca9bc8f8
17
pom.xml
17
pom.xml
@ -21,9 +21,10 @@
|
|||||||
|
|
||||||
<dependencies>
|
<dependencies>
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>junit</groupId>
|
<groupId>org.junit.jupiter</groupId>
|
||||||
<artifactId>junit</artifactId>
|
<artifactId>junit-jupiter-api</artifactId>
|
||||||
<version>3.8.1</version>
|
<version>5.13.1</version>
|
||||||
|
<scope>test</scope>
|
||||||
</dependency>
|
</dependency>
|
||||||
</dependencies>
|
</dependencies>
|
||||||
|
|
||||||
@ -69,6 +70,16 @@
|
|||||||
</plugin>
|
</plugin>
|
||||||
</plugins>
|
</plugins>
|
||||||
</pluginManagement>
|
</pluginManagement>
|
||||||
|
<plugins>
|
||||||
|
<plugin>
|
||||||
|
<groupId>org.apache.maven.plugins</groupId>
|
||||||
|
<artifactId>maven-compiler-plugin</artifactId>
|
||||||
|
<configuration>
|
||||||
|
<source>9</source>
|
||||||
|
<target>9</target>
|
||||||
|
</configuration>
|
||||||
|
</plugin>
|
||||||
|
</plugins>
|
||||||
</build>
|
</build>
|
||||||
|
|
||||||
<reporting>
|
<reporting>
|
||||||
|
|||||||
22
src/main/java/de/vimo/Main.java
Normal file
22
src/main/java/de/vimo/Main.java
Normal file
@ -0,0 +1,22 @@
|
|||||||
|
package de.vimo;
|
||||||
|
|
||||||
|
import de.vimo.core.Line;
|
||||||
|
import de.vimo.core.Parser;
|
||||||
|
|
||||||
|
import java.util.Scanner;
|
||||||
|
|
||||||
|
public class Main {
|
||||||
|
public static void main(String[] args) {
|
||||||
|
Scanner scanner = new Scanner(System.in);
|
||||||
|
Parser parser = null;
|
||||||
|
|
||||||
|
while (true) {
|
||||||
|
String raw = scanner.nextLine();
|
||||||
|
Line line = parser.parse(raw);
|
||||||
|
|
||||||
|
if (line.getCommand() != null) {
|
||||||
|
// line.getCommand().exec(line.getFlags(), line.getArgs());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
26
src/main/java/de/vimo/command/CdCommand.java
Normal file
26
src/main/java/de/vimo/command/CdCommand.java
Normal file
@ -0,0 +1,26 @@
|
|||||||
|
package de.vimo.command;
|
||||||
|
|
||||||
|
import de.vimo.core.Argument;
|
||||||
|
import de.vimo.core.Command;
|
||||||
|
import de.vimo.core.Flag;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
public class CdCommand implements Command {
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public int exec(List<Flag> flags, List<Argument> args) {
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String getName() {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public List<Flag> getFlags() {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
25
src/main/java/de/vimo/command/HelpCommand.java
Normal file
25
src/main/java/de/vimo/command/HelpCommand.java
Normal file
@ -0,0 +1,25 @@
|
|||||||
|
package de.vimo.command;
|
||||||
|
|
||||||
|
import de.vimo.core.Argument;
|
||||||
|
import de.vimo.core.Command;
|
||||||
|
import de.vimo.core.Flag;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
public class HelpCommand implements Command {
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public int exec(List<Flag> flags, List<Argument> args) {
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String getName() {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public List<Flag> getFlags() {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
25
src/main/java/de/vimo/command/LsCommand.java
Normal file
25
src/main/java/de/vimo/command/LsCommand.java
Normal file
@ -0,0 +1,25 @@
|
|||||||
|
package de.vimo.command;
|
||||||
|
|
||||||
|
import de.vimo.core.Argument;
|
||||||
|
import de.vimo.core.Command;
|
||||||
|
import de.vimo.core.Flag;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
public class LsCommand implements Command {
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public int exec(List<Flag> flags, List<Argument> args) {
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String getName() {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public List<Flag> getFlags() {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
25
src/main/java/de/vimo/command/MkdirCommand.java
Normal file
25
src/main/java/de/vimo/command/MkdirCommand.java
Normal file
@ -0,0 +1,25 @@
|
|||||||
|
package de.vimo.command;
|
||||||
|
|
||||||
|
import de.vimo.core.Argument;
|
||||||
|
import de.vimo.core.Command;
|
||||||
|
import de.vimo.core.Flag;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
public class MkdirCommand implements Command {
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public int exec(List<Flag> flags, List<Argument> args) {
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String getName() {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public List<Flag> getFlags() {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
15
src/main/java/de/vimo/core/Argument.java
Normal file
15
src/main/java/de/vimo/core/Argument.java
Normal file
@ -0,0 +1,15 @@
|
|||||||
|
package de.vimo.core;
|
||||||
|
|
||||||
|
public class Argument {
|
||||||
|
|
||||||
|
private final String value;
|
||||||
|
|
||||||
|
public Argument(String value) {
|
||||||
|
this.value = value;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getValue() {
|
||||||
|
return value;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
12
src/main/java/de/vimo/core/Command.java
Normal file
12
src/main/java/de/vimo/core/Command.java
Normal file
@ -0,0 +1,12 @@
|
|||||||
|
package de.vimo.core;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
public interface Command {
|
||||||
|
|
||||||
|
int exec(List<Flag> flags, List<Argument> args);
|
||||||
|
|
||||||
|
String getName();
|
||||||
|
|
||||||
|
List<Flag> getFlags();
|
||||||
|
}
|
||||||
28
src/main/java/de/vimo/core/Flag.java
Normal file
28
src/main/java/de/vimo/core/Flag.java
Normal file
@ -0,0 +1,28 @@
|
|||||||
|
package de.vimo.core;
|
||||||
|
|
||||||
|
public class Flag {
|
||||||
|
|
||||||
|
private final String shortName;
|
||||||
|
|
||||||
|
private final String longName;
|
||||||
|
|
||||||
|
private final String description;
|
||||||
|
|
||||||
|
public Flag(String shortName, String longName, String description) {
|
||||||
|
this.shortName = shortName;
|
||||||
|
this.longName = longName;
|
||||||
|
this.description = description;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getShortName() {
|
||||||
|
return shortName;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getLongName() {
|
||||||
|
return longName;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getDescription() {
|
||||||
|
return description;
|
||||||
|
}
|
||||||
|
}
|
||||||
73
src/main/java/de/vimo/core/Line.java
Normal file
73
src/main/java/de/vimo/core/Line.java
Normal file
@ -0,0 +1,73 @@
|
|||||||
|
package de.vimo.core;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
public class Line {
|
||||||
|
|
||||||
|
private final String command;
|
||||||
|
|
||||||
|
private final List<String> flags;
|
||||||
|
|
||||||
|
private final List<String> args;
|
||||||
|
|
||||||
|
public Line(String command, List<String> flags, List<String> args) {
|
||||||
|
this.command = command;
|
||||||
|
this.flags = flags;
|
||||||
|
this.args = args;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getCommand() {
|
||||||
|
return command;
|
||||||
|
}
|
||||||
|
|
||||||
|
public List<String> getFlags() {
|
||||||
|
return flags;
|
||||||
|
}
|
||||||
|
|
||||||
|
public List<String> getArgs() {
|
||||||
|
return args;
|
||||||
|
}
|
||||||
|
|
||||||
|
// @Override
|
||||||
|
// public Line parse(String raw) {
|
||||||
|
// String regex = "[\\s]";
|
||||||
|
// String[] myArray = raw.split(regex);
|
||||||
|
//
|
||||||
|
// for (int i = 1; i < myArray.length; i++) {
|
||||||
|
// if (myArray[i].contains("-")) {
|
||||||
|
// flags.add(myArray[i]);
|
||||||
|
// } else {
|
||||||
|
// args.add(myArray[i]);
|
||||||
|
// }
|
||||||
|
// }
|
||||||
|
//
|
||||||
|
// return new Line(myArray[0], flags, args);
|
||||||
|
// }
|
||||||
|
|
||||||
|
//
|
||||||
|
// --- Please ignore this ---
|
||||||
|
//
|
||||||
|
// private final Command command;
|
||||||
|
//
|
||||||
|
// private final List<Flag> flags;
|
||||||
|
//
|
||||||
|
// private final List<Argument> args;
|
||||||
|
//
|
||||||
|
// public Line(Command command, List<Flag> flags, List<Argument> args) {
|
||||||
|
// this.command = command;
|
||||||
|
// this.flags = flags;
|
||||||
|
// this.args = args;
|
||||||
|
// }
|
||||||
|
//
|
||||||
|
// public Command getCommand() {
|
||||||
|
// return command;
|
||||||
|
// }
|
||||||
|
//
|
||||||
|
// public List<Flag> getFlags() {
|
||||||
|
// return flags;
|
||||||
|
// }
|
||||||
|
//
|
||||||
|
// public List<Argument> getArgs() {
|
||||||
|
// return args;
|
||||||
|
// }
|
||||||
|
}
|
||||||
6
src/main/java/de/vimo/core/Parser.java
Normal file
6
src/main/java/de/vimo/core/Parser.java
Normal file
@ -0,0 +1,6 @@
|
|||||||
|
package de.vimo.core;
|
||||||
|
|
||||||
|
public interface Parser {
|
||||||
|
|
||||||
|
Line parse(String raw);
|
||||||
|
}
|
||||||
@ -1,38 +0,0 @@
|
|||||||
package de.vimo;
|
|
||||||
|
|
||||||
import junit.framework.Test;
|
|
||||||
import junit.framework.TestCase;
|
|
||||||
import junit.framework.TestSuite;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Unit test for simple App.
|
|
||||||
*/
|
|
||||||
public class AppTest
|
|
||||||
extends TestCase
|
|
||||||
{
|
|
||||||
/**
|
|
||||||
* Create the test case
|
|
||||||
*
|
|
||||||
* @param testName name of the test case
|
|
||||||
*/
|
|
||||||
public AppTest( String testName )
|
|
||||||
{
|
|
||||||
super( testName );
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @return the suite of tests being tested
|
|
||||||
*/
|
|
||||||
public static Test suite()
|
|
||||||
{
|
|
||||||
return new TestSuite( AppTest.class );
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Rigourous Test :-)
|
|
||||||
*/
|
|
||||||
public void testApp()
|
|
||||||
{
|
|
||||||
assertTrue( true );
|
|
||||||
}
|
|
||||||
}
|
|
||||||
57
src/test/java/de/vimo/core/ParserTest.java
Normal file
57
src/test/java/de/vimo/core/ParserTest.java
Normal file
@ -0,0 +1,57 @@
|
|||||||
|
package de.vimo.core;
|
||||||
|
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.Arrays;
|
||||||
|
import java.util.Collections;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
import static org.junit.jupiter.api.Assertions.*;
|
||||||
|
|
||||||
|
|
||||||
|
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);
|
||||||
|
|
||||||
|
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
|
||||||
|
|
||||||
|
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"));
|
||||||
|
}
|
||||||
|
|
||||||
|
private void testInput(Parser parser, String input, String expectedCommand, List<String> expectedFlags, List<String> expectedArgs) {
|
||||||
|
Line line = parser.parse(input);
|
||||||
|
|
||||||
|
assertEquals(expectedCommand, line.getCommand(), "Wrong command");
|
||||||
|
assertEquals(expectedFlags, line.getFlags(), "Wrong flags");
|
||||||
|
assertEquals(expectedArgs, line.getArgs(), "Wrong args");
|
||||||
|
}
|
||||||
|
}
|
||||||
BIN
target/classes/de/vimo/InputTextTillCommand.class
Normal file
BIN
target/classes/de/vimo/InputTextTillCommand.class
Normal file
Binary file not shown.
Binary file not shown.
BIN
target/classes/de/vimo/Main.class
Normal file
BIN
target/classes/de/vimo/Main.class
Normal file
Binary file not shown.
BIN
target/classes/de/vimo/command/CdCommand.class
Normal file
BIN
target/classes/de/vimo/command/CdCommand.class
Normal file
Binary file not shown.
BIN
target/classes/de/vimo/command/HelpCommand.class
Normal file
BIN
target/classes/de/vimo/command/HelpCommand.class
Normal file
Binary file not shown.
BIN
target/classes/de/vimo/command/LsCommand.class
Normal file
BIN
target/classes/de/vimo/command/LsCommand.class
Normal file
Binary file not shown.
BIN
target/classes/de/vimo/command/MkdirCommand.class
Normal file
BIN
target/classes/de/vimo/command/MkdirCommand.class
Normal file
Binary file not shown.
BIN
target/classes/de/vimo/core/Argument.class
Normal file
BIN
target/classes/de/vimo/core/Argument.class
Normal file
Binary file not shown.
BIN
target/classes/de/vimo/core/Command.class
Normal file
BIN
target/classes/de/vimo/core/Command.class
Normal file
Binary file not shown.
BIN
target/classes/de/vimo/core/Flag.class
Normal file
BIN
target/classes/de/vimo/core/Flag.class
Normal file
Binary file not shown.
BIN
target/classes/de/vimo/core/Line.class
Normal file
BIN
target/classes/de/vimo/core/Line.class
Normal file
Binary file not shown.
BIN
target/classes/de/vimo/core/Parser.class
Normal file
BIN
target/classes/de/vimo/core/Parser.class
Normal file
Binary file not shown.
BIN
target/test-classes/de/vimo/core/ParserTest$1.class
Normal file
BIN
target/test-classes/de/vimo/core/ParserTest$1.class
Normal file
Binary file not shown.
BIN
target/test-classes/de/vimo/core/ParserTest.class
Normal file
BIN
target/test-classes/de/vimo/core/ParserTest.class
Normal file
Binary file not shown.
Loading…
Reference in New Issue
Block a user