Add lots of tests for error messages

This commit is contained in:
Arne Keller 2020-02-18 15:03:48 +01:00
parent 618cee8e70
commit cc4a247fec
7 changed files with 81 additions and 12 deletions

View File

@ -58,8 +58,9 @@ public class ModelRailwaySimulation {
* @param end1 end position 1 of the switch * @param end1 end position 1 of the switch
* @param end2 end position 2 of the switch * @param end2 end position 2 of the switch
* @return the positive identifier of the switch if successful, -1 otherwise * @return the positive identifier of the switch if successful, -1 otherwise
* @throws InvalidInputException if the new switch would be invalid
*/ */
public int addSwitch(final Vector2D start, final Vector2D end1, final Vector2D end2) { public int addSwitch(final Vector2D start, final Vector2D end1, final Vector2D end2) throws InvalidInputException {
return railNetwork.addSwitch(start, end1, end2); return railNetwork.addSwitch(start, end1, end2);
} }

View File

@ -1,6 +1,7 @@
package edu.kit.informatik.model; package edu.kit.informatik.model;
import edu.kit.informatik.Terminal; import edu.kit.informatik.Terminal;
import edu.kit.informatik.ui.InvalidInputException;
import java.util.HashMap; import java.util.HashMap;
import java.util.HashSet; import java.util.HashSet;
@ -63,8 +64,9 @@ public class RailwayNetwork {
* @param end1 end position 1 of the switch * @param end1 end position 1 of the switch
* @param end2 end position 2 of the switch * @param end2 end position 2 of the switch
* @return the positive identifier of the switch if successful, -1 otherwise * @return the positive identifier of the switch if successful, -1 otherwise
* @throws InvalidInputException when the new switch would be invalid
*/ */
public int addSwitch(final Vector2D start, final Vector2D end1, final Vector2D end2) { public int addSwitch(final Vector2D start, final Vector2D end1, final Vector2D end2) throws InvalidInputException {
if (start.distanceTo(end1) == 0 || start.distanceTo(end2) == 0 || end1.distanceTo(end2) == 0) { if (start.distanceTo(end1) == 0 || start.distanceTo(end2) == 0 || end1.distanceTo(end2) == 0) {
return -1; return -1;
} }

View File

@ -1,5 +1,7 @@
package edu.kit.informatik.model; package edu.kit.informatik.model;
import edu.kit.informatik.ui.InvalidInputException;
import java.util.Objects; import java.util.Objects;
/** /**
@ -32,14 +34,14 @@ public final class Switch extends Rail {
* @param end1 end position 1 * @param end1 end position 1
* @param end2 end position 2 * @param end2 end position 2
* @param id identifier of this switch * @param id identifier of this switch
* @throws IllegalArgumentException if the switch is not composed of straight lines * @throws InvalidInputException if the switch is not composed of straight lines
*/ */
public Switch(final Vector2D start, final Vector2D end1, final Vector2D end2, final int id) public Switch(final Vector2D start, final Vector2D end1, final Vector2D end2, final int id)
throws IllegalArgumentException { throws InvalidInputException {
super(id); super(id);
if (start.getX() != end1.getX() && start.getY() != end1.getY() if (start.getX() != end1.getX() && start.getY() != end1.getY()
|| start.getX() != end2.getX() && start.getY() != end2.getY()) { || start.getX() != end2.getX() && start.getY() != end2.getY()) {
throw new IllegalArgumentException("start has to be connected in straight lines to end positions!"); throw new InvalidInputException("start has to be connected in straight lines to end positions!");
} }
// make sure caller can not modify internal state after calling // make sure caller can not modify internal state after calling
this.start = new Vector2D(start); this.start = new Vector2D(start);

View File

@ -3,6 +3,7 @@ package edu.kit.informatik.ui.command;
import edu.kit.informatik.model.ModelRailwaySimulation; import edu.kit.informatik.model.ModelRailwaySimulation;
import edu.kit.informatik.model.Vector2D; import edu.kit.informatik.model.Vector2D;
import edu.kit.informatik.Terminal; import edu.kit.informatik.Terminal;
import edu.kit.informatik.ui.InvalidInputException;
/** /**
* Command used to add a switch to the rail network. * Command used to add a switch to the rail network.
@ -38,7 +39,7 @@ public class AddSwitch extends Command {
} }
@Override @Override
public void apply(final ModelRailwaySimulation simulation) { public void apply(final ModelRailwaySimulation simulation) throws InvalidInputException {
int id = simulation.addSwitch(start, end1, end2); int id = simulation.addSwitch(start, end1, end2);
if (id == -1) { if (id == -1) {
Terminal.printError("switch not connected to existing rails"); Terminal.printError("switch not connected to existing rails");

View File

@ -82,12 +82,7 @@ public class CommandFactory {
Vector2D start = Vector2D.parse(matcher.group(1)); Vector2D start = Vector2D.parse(matcher.group(1));
Vector2D end1 = Vector2D.parse(matcher.group(2)); Vector2D end1 = Vector2D.parse(matcher.group(2));
Vector2D end2 = Vector2D.parse(matcher.group(3)); Vector2D end2 = Vector2D.parse(matcher.group(3));
if ((start.getX() == end1.getX() || start.getY() == end1.getY())
&& (start.getX() == end2.getX() || start.getY() == end2.getY())) {
return new AddSwitch(start, end1, end2); return new AddSwitch(start, end1, end2);
} else {
throw new InvalidInputException("switch rails have to be straight lines");
}
} else if (command.startsWith(DELETE_TRACK)) { } else if (command.startsWith(DELETE_TRACK)) {
String argument = command.substring(DELETE_TRACK.length()); String argument = command.substring(DELETE_TRACK.length());
if (!argument.matches(POSITIVE_NUMBER)) { if (!argument.matches(POSITIVE_NUMBER)) {

View File

@ -70,3 +70,37 @@ put train 1 at (10,0) in direction 1,0
put train 2 at (15,0) in direction 1,0 put train 2 at (15,0) in direction 1,0
put train 5 at (20,0) in direction -1,0 put train 5 at (20,0) in direction -1,0
step 1 step 1
add track (-10,0) -> (0,0)
add track (30,0) -> (40,0)
create train-set T1 Albert 1 true true
create train-set T1 Berta 2 true true
add train 3 T1-Albert
add train 4 T1-Berta
put train 4 at (10,0) in direction 1,0
put train 1 at (0,0) in direction 1,0
put train 3 at (20,0) in direction -1,0
put train 2 at (30,0) in direction -1,0
put train 5 at (15,0) in direction 1,0
step 1
add switch (40,0) -> (10,10),(40,40)
add switch (40,0) -> (40,40),(10,10)
add switch (40,0),(10,0),(0,10)
delete track first
list tracks all of them
create engine really good one pls
list engines I own
create coach with water slide
list coaches with food
create train-set with electric chair
create train-set W 15 1 true true
list train-sets in this city
delete rolling stock your mom
add train to my model railway
delete train please do not sell it
list trains using advanced AI
show train to all my friends
put train on my head
put train 1 at (1,0) in direction 42,42
add track to canada
please just do as I say >:(
step all over my legos, you dumb program

View File

@ -87,3 +87,37 @@ OK
OK OK
OK OK
Crash of train 1,2,5 Crash of train 1,2,5
4
5
T1-Albert
T1-Berta
train-set T1-Albert added to train 3
train-set T1-Berta added to train 4
OK
OK
OK
OK
OK
Crash of train 1,2,3,4,5
Error, start has to be connected in straight lines to end positions!
Error, start has to be connected in straight lines to end positions!
Error, invalid add switch argument syntax
Error, invalid/missing delete track argument
Error, too many arguments for list tracks
Error, invalid create engine argument syntax
Error, too many list engines arguments
Error, invalid create coach arguments
Error, too many list coaches arguments
Error, invalid create train-set arguments
Error, invalid train-set class/series
Error, too many list train-sets arguments
Error, invalid delete rolling stock argument
Error, invalid add train arguments
Error, invalid delete train argument
Error, too many list trains arguments
Error, invalid show train argument
Error, invalid put train arguments
Error, invalid train direction
Error, invalid add track argument syntax
Error, unknown command
Error, invalid step argument