Increase test coverage and tweak error messages

This commit is contained in:
Arne Keller 2020-02-18 18:11:40 +01:00
parent 71fb077c7a
commit 2125588a0d
8 changed files with 67 additions and 18 deletions

View File

@ -69,7 +69,7 @@ public class RailwayNetwork {
*/ */
public int addSwitch(final Vector2D start, final Vector2D end1, final Vector2D end2) throws InvalidInputException { 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; throw new InvalidInputException("switch has length zero");
} }
if (rails.isEmpty()) { if (rails.isEmpty()) {
Switch newSwitch = new Switch(start, end1, end2, 1); Switch newSwitch = new Switch(start, end1, end2, 1);
@ -80,7 +80,7 @@ public class RailwayNetwork {
long end1PossibleConnections = rails.values().stream().filter((rail) -> rail.canConnectTo(end1)).count(); long end1PossibleConnections = rails.values().stream().filter((rail) -> rail.canConnectTo(end1)).count();
long end2PossibleConnections = rails.values().stream().filter((rail) -> rail.canConnectTo(end2)).count(); long end2PossibleConnections = rails.values().stream().filter((rail) -> rail.canConnectTo(end2)).count();
if (startPossibleConnections == 2 || end1PossibleConnections == 2 || end2PossibleConnections == 2) { if (startPossibleConnections == 2 || end1PossibleConnections == 2 || end2PossibleConnections == 2) {
return -1; // TODO better error msg? throw new InvalidInputException("switch endpoint would connect to two other rails");
} }
for (Rail rail : rails.values()) { for (Rail rail : rails.values()) {
if (rail.canConnectTo(start) || rail.canConnectTo(end1) || rail.canConnectTo(end2)) { if (rail.canConnectTo(start) || rail.canConnectTo(end1) || rail.canConnectTo(end2)) {
@ -93,7 +93,7 @@ public class RailwayNetwork {
return newSwitch.getIdentifier(); return newSwitch.getIdentifier();
} }
} }
return -1; throw new InvalidInputException("switch not connected to other rails");
} }
} }

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;
/** /**
@ -17,12 +19,12 @@ public final class Track extends Rail {
* @param start start position of the track * @param start start position of the track
* @param end end position of the track * @param end end position of the track
* @param id identifier to use * @param id identifier to use
* @throws IllegalArgumentException if the positions are not on a straight line * @throws InvalidInputException if the positions are not on a straight line
*/ */
public Track(final Vector2D start, final Vector2D end, final int id) throws IllegalArgumentException { public Track(final Vector2D start, final Vector2D end, final int id) throws InvalidInputException {
super(id); super(id);
if (start.getX() != end.getX() && start.getY() != end.getY()) { if (start.getX() != end.getX() && start.getY() != end.getY()) {
throw new IllegalArgumentException("start and end have to be in a straight line!"); throw new InvalidInputException("invalid track segment: not a straight line");
} }
this.start = start; this.start = start;
this.end = end; this.end = end;
@ -40,10 +42,7 @@ public final class Track extends Rail {
@Override @Override
public boolean canConnectToRail(Rail rail) { public boolean canConnectToRail(Rail rail) {
if (rail == null) { return rail != null && rail.canConnectTo(start) || rail.canConnectTo(end);
System.out.println("hey"); // TODO: remove
}
return rail.canConnectTo(start) || rail.canConnectTo(end);
} }
@Override @Override
@ -82,7 +81,7 @@ public final class Track extends Rail {
return new Vector2D(Long.signum((long) start.getX() - (long) end.getX()), return new Vector2D(Long.signum((long) start.getX() - (long) end.getX()),
Long.signum((long) start.getY() - (long) end.getY())); Long.signum((long) start.getY() - (long) end.getY()));
} else { } else {
throw new IllegalArgumentException(); throw new IllegalArgumentException("can only compute direction from track ends");
} }
} }

View File

@ -195,7 +195,7 @@ public final class Train {
* @return whether this train is placed on a rail network * @return whether this train is placed on a rail network
*/ */
public boolean isPlaced() { public boolean isPlaced() {
return position != null && direction != null; return position != null && direction != null && occupiedRails != null;
} }
/** /**
@ -316,6 +316,7 @@ public final class Train {
// we evidently just moved into another rail // we evidently just moved into another rail
Rail[] nextTouchingRails = railNetwork.findTouchingRails(nextPosition); Rail[] nextTouchingRails = railNetwork.findTouchingRails(nextPosition);
Rail[] alreadyTouchingRails = railNetwork.findTouchingRails(secondToLastPosition); Rail[] alreadyTouchingRails = railNetwork.findTouchingRails(secondToLastPosition);
// TODO: do we need all of these cases???
if (nextTouchingRails[0] == alreadyTouchingRails[0]) { if (nextTouchingRails[0] == alreadyTouchingRails[0]) {
occupiedRails.add(nextTouchingRails[0]); occupiedRails.add(nextTouchingRails[0]);
} else if (nextTouchingRails.length == 2) { } else if (nextTouchingRails.length == 2) {

View File

@ -132,7 +132,7 @@ public class Vector2D {
@Override @Override
public boolean equals(final Object obj) { public boolean equals(final Object obj) {
if (obj != null && getClass().equals(obj.getClass())) { if (obj != null && getClass().equals(obj.getClass())) {
final Vector2D point = (Vector2D) obj; Vector2D point = (Vector2D) obj;
return x == point.x && y == point.y; return x == point.x && y == point.y;
} }

View File

@ -25,7 +25,7 @@ public class CommandLine {
ModelRailwaySimulation simulation = new ModelRailwaySimulation(); ModelRailwaySimulation simulation = new ModelRailwaySimulation();
while (true) { while (true) {
final String input = Terminal.readLine(); String input = Terminal.readLine();
if (input == null) { if (input == null) {
break; // TODO remove break; // TODO remove
} }
@ -33,9 +33,14 @@ public class CommandLine {
continue; // TODO remove continue; // TODO remove
} }
if (input.equals(EXIT)) { if (input.startsWith(EXIT)) {
if (input.length() != EXIT.length()) {
Terminal.printError("space after exit command");
continue;
} else {
break; break;
} }
}
try { try {
Command command = CommandFactory.getCommand(input); Command command = CommandFactory.getCommand(input);

View File

@ -2,4 +2,4 @@
2 2
3 3
4 4
Error, switch not connected to existing rails Error, switch endpoint would connect to two other rails

View File

@ -105,6 +105,7 @@ put train 1 at (1,0) in direction 42,42
add track to canada add track to canada
please just do as I say >:( please just do as I say >:(
step all over my legos, you dumb program step all over my legos, you dumb program
exit
delete track 5 delete track 5
delete track 3 delete track 3
delete track 2 delete track 2
@ -147,3 +148,24 @@ delete train 6
add train 6 T5-Hartnick add train 6 T5-Hartnick
put train 6 at (0,0) in direction -1,0 put train 6 at (0,0) in direction -1,0
step 1 step 1
add switch (0,0) -> (0,0),(1,0)
add switch (0,0) -> (1,0),(0,0)
add switch (0,0) -> (1,0),(1,0)
delete track 1
delete track 2
delete track 3
add track (0,0) -> (1,0)
add track (0,0) -> (-1,0)
add switch (0,0) -> (0,-1),(0,1)
add switch (0,1) -> (0,0),(0,2)
add switch (0,1) -> (0,2),(0,0)
add switch (1000,0) -> (1000,1),(1001,0)
delete track 1
delete track 2
add track (0,0) -> (0,2)
add track (0,2) -> (2,2)
add track (2,2) -> (2,0)
add track (2,0) -> (0,0)
create engine steam T5 Worsch 9 true true
add train 8 T5-Worsch
put train 8 at (0,0) in direction 0,-1

View File

@ -129,6 +129,7 @@ Error, invalid train direction
Error, invalid add track argument syntax Error, invalid add track argument syntax
Error, unknown command Error, unknown command
Error, invalid step argument Error, invalid step argument
Error, space after exit command
OK OK
OK OK
OK OK
@ -171,3 +172,24 @@ OK
diesel engine T5-Hartnick added to train 6 diesel engine T5-Hartnick added to train 6
OK OK
Crash of train 6 Crash of train 6
Error, switch has length zero
Error, switch has length zero
Error, switch has length zero
OK
OK
OK
1
2
Error, switch endpoint would connect to two other rails
Error, switch endpoint would connect to two other rails
Error, switch endpoint would connect to two other rails
Error, switch not connected to other rails
OK
OK
1
2
3
4
T5-Worsch
steam engine T5-Worsch added to train 8
Error, could not place train