ParserTest

This commit is contained in:
happymeal2024 2025-06-24 17:05:30 +02:00
parent 7bd3bc0909
commit 9cca9bc8f8
27 changed files with 328 additions and 41 deletions

17
pom.xml
View File

@ -21,9 +21,10 @@
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<version>5.13.1</version>
<scope>test</scope>
</dependency>
</dependencies>
@ -69,6 +70,16 @@
</plugin>
</plugins>
</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>
<reporting>

View 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());
}
}
}
}

View 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;
}
}

View 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;
}
}

View 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;
}
}

View 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;
}
}

View 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;
}
}

View 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();
}

View 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;
}
}

View 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;
// }
}

View File

@ -0,0 +1,6 @@
package de.vimo.core;
public interface Parser {
Line parse(String raw);
}

View File

@ -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 );
}
}

View 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");
}
}

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.