Increase test coverage and change error message wording

This commit is contained in:
Arne Keller 2020-02-18 15:57:04 +01:00
parent cc4a247fec
commit 71fb077c7a
17 changed files with 171 additions and 55 deletions

View File

@ -8,7 +8,7 @@ OK
t 1 (0,0) -> (-5,0) 5
t 2 (-5,0) -> (-5,5) 5
s 3 (0,5) -> (0,0),(-5,5) 5
Error, track is not connected to other tracks
Error, track would connect to two other rails
A-B
train-set A-B added to train 1
OK

View File

@ -1,9 +1,9 @@
Error, track has length 0
Error, track has length zero
1
2
3
4
Error, track is not connected to other tracks
Error, track would connect to two other rails
T3-Emma
1
2

View File

@ -1,6 +1,7 @@
package edu.kit.informatik.model;
import edu.kit.informatik.ui.CoachType;
import edu.kit.informatik.ui.InvalidInputException;
/**
* A coach.
@ -61,9 +62,10 @@ public class Coach extends RollingStock {
* @param length length of coach
* @param couplingFront whether the coach should have a front coupling
* @param couplingBack whether the coach should have a back coupling
* @throws InvalidInputException on invalid user input (zero-sized coach)
*/
public Coach(final int identifier, final CoachType type, final int length,
final boolean couplingFront, final boolean couplingBack) {
final boolean couplingFront, final boolean couplingBack) throws InvalidInputException {
super(length, couplingFront, couplingBack);
this.identifier = identifier;
this.type = type;

View File

@ -1,5 +1,7 @@
package edu.kit.informatik.model;
import edu.kit.informatik.ui.InvalidInputException;
/**
* Diesel engine.
*
@ -26,9 +28,10 @@ public class DieselEngine extends Engine {
* @param length length of engine
* @param couplingFront whether the engine should have a front coupling
* @param couplingBack whether the engine should have a back coupling
* @throws InvalidInputException on invalid user input (e.g. zero-sized engine)
*/
public DieselEngine(final String series, final String name, final int length,
final boolean couplingFront, final boolean couplingBack) {
final boolean couplingFront, final boolean couplingBack) throws InvalidInputException {
super(series, name, length, couplingFront, couplingBack);
}

View File

@ -1,5 +1,7 @@
package edu.kit.informatik.model;
import edu.kit.informatik.ui.InvalidInputException;
/**
* Electrical engine.
*
@ -28,9 +30,10 @@ public class ElectricalEngine extends Engine {
* @param length length of engine
* @param couplingFront whether the engine should have a front coupling
* @param couplingBack whether the engine should have a back coupling
* @throws InvalidInputException on invalid user input (e.g. zero-sized engine)
*/
public ElectricalEngine(final String series, final String name, final int length,
final boolean couplingFront, final boolean couplingBack) {
final boolean couplingFront, final boolean couplingBack) throws InvalidInputException {
super(series, name, length, couplingFront, couplingBack);
}

View File

@ -1,5 +1,7 @@
package edu.kit.informatik.model;
import edu.kit.informatik.ui.InvalidInputException;
/**
* Generic engine, is usually either diesel, steam or electric.
*
@ -10,14 +12,23 @@ public abstract class Engine extends RollingStock {
/**
* Series/class of this engine.
*/
private String series;
private final String series;
/**
* Name of this engine.
*/
private String name;
private final String name;
/**
* Initialize an engine.
* @param series series/class of this engine
* @param name name of this engine
* @param length length of this engine
* @param couplingFront whether this engine should have a front coupling
* @param couplingBack whether this engine should have a back coupling
* @throws InvalidInputException on invalid user input (zero-sized coach)
*/
protected Engine(final String series, final String name, final int length,
final boolean couplingFront, final boolean couplingBack) {
final boolean couplingFront, final boolean couplingBack) throws InvalidInputException {
super(length, couplingFront, couplingBack);
this.series = series;
this.name = name;

View File

@ -46,9 +46,10 @@ public class ModelRailwaySimulation {
* Add a new track to the rail network of this simulation.
* @param start start position of the new track
* @param end end position of the new track
* @return the positive identifier of the new track if successful, -1 otherwise
* @return the positive identifier of the new track if successful, -1 if none available
* @throws InvalidInputException if user input is invalid (e.g. zero length track)
*/
public int addTrack(final Vector2D start, final Vector2D end) {
public int addTrack(final Vector2D start, final Vector2D end) throws InvalidInputException {
return railNetwork.addTrack(start, end);
}
@ -146,10 +147,11 @@ public class ModelRailwaySimulation {
* @param length length of the coach
* @param couplingFront whether the coach should have a front coupling
* @param couplingBack whether the coach should have a back coupling
* @return the identifier of the coach if successfully added, -1 otherwise
* @return the identifier of the coach if successfully added, -1 if none available
* @throws InvalidInputException if user input is invalid (e.g. zero-sized coach)
*/
public int createCoach(final CoachType coachType, final int length,
final boolean couplingFront, final boolean couplingBack) {
final boolean couplingFront, final boolean couplingBack) throws InvalidInputException {
int id = getNextCoachIdentifier();
if (id < 0) {
return -1;

View File

@ -27,16 +27,17 @@ public class RailwayNetwork {
* Add a new track to the rail network.
* @param start start position of the new track
* @param end end position of the new track
* @return the positive identifier of the new track if successful, -1 otherwise
* @return the positive identifier of the new track if successful, -1 if none available
* @throws InvalidInputException if the user provides incorrect input
*/
public int addTrack(final Vector2D start, final Vector2D end) {
public int addTrack(final Vector2D start, final Vector2D end) throws InvalidInputException {
if (start.distanceTo(end) == 0) {
return -1;
throw new InvalidInputException("track has length zero");
}
long startPossibleConnections = rails.values().stream().filter((rail) -> rail.canConnectTo(start)).count();
long endPossibleConnections = rails.values().stream().filter((rail) -> rail.canConnectTo(end)).count();
if (startPossibleConnections == 2 || endPossibleConnections == 2) {
return -1; // TODO better error msg?
throw new InvalidInputException("track would connect to two other rails");
}
if (rails.isEmpty()) {
Track newTrack = new Track(start, end, 1);
@ -54,7 +55,7 @@ public class RailwayNetwork {
return newTrack.getIdentifier();
}
}
return -1;
throw new InvalidInputException("track is not connected to other tracks");
}
}
@ -104,13 +105,6 @@ public class RailwayNetwork {
public boolean removeRail(final int id) {
if (rails.size() == 0) {
return false;
} else if (rails.size() == 1) {
if (rails.get(id) != null) {
rails.clear();
return true;
} else {
return false;
}
}
Rail toRemove = rails.get(id);
if (toRemove == null) {
@ -121,7 +115,7 @@ public class RailwayNetwork {
// locate one other rail: TODO use rail.connectedrails
Rail otherRail = null;
for (Rail anotherRail : rails.values()) {
if (anotherRail.getIdentifier() != id && toRemove.canConnectToRail(anotherRail)) {
if (anotherRail.getIdentifier() != id && anotherRail.canConnectToRail(toRemove)) {
otherRail = anotherRail;
break;
}

View File

@ -1,5 +1,7 @@
package edu.kit.informatik.model;
import edu.kit.informatik.ui.InvalidInputException;
/**
* A rolling stock. Is usually an engine, train set or coach.
*
@ -10,17 +12,28 @@ public abstract class RollingStock {
/**
* Length of this rolling stock.
*/
private int length;
private final int length;
/**
* Whether this rolling stock has a front coupling.
*/
private boolean couplingFront;
private final boolean couplingFront;
/**
* Whether this rolling stack has a back coupling.
*/
private boolean couplingBack;
private final boolean couplingBack;
protected RollingStock(final int length, final boolean couplingFront, final boolean couplingBack) {
/**
* Initialize a rolling stock.
* @param length length of this rolling stock
* @param couplingFront whether this rolling stock should have a front coupling
* @param couplingBack whether this rolling stock should have a back coupling
* @throws InvalidInputException on invalid user input (zero-sized coach)
*/
protected RollingStock(final int length, final boolean couplingFront, final boolean couplingBack)
throws InvalidInputException {
if (length < 1) {
throw new InvalidInputException("rolling stock length has to be positive");
}
this.length = length;
this.couplingFront = couplingFront;
this.couplingBack = couplingBack;

View File

@ -1,5 +1,7 @@
package edu.kit.informatik.model;
import edu.kit.informatik.ui.InvalidInputException;
/**
* Steam engine.
*
@ -26,9 +28,10 @@ public class SteamEngine extends Engine {
* @param length length of engine
* @param couplingFront whether the engine should have a front coupling
* @param couplingBack whether the engine should have a back coupling
* @throws InvalidInputException on invalid user input (e.g. zero-sized engine)
*/
public SteamEngine(final String series, final String name, final int length,
final boolean couplingFront, final boolean couplingBack) {
final boolean couplingFront, final boolean couplingBack) throws InvalidInputException {
super(series, name, length, couplingFront, couplingBack);
}

View File

@ -208,8 +208,9 @@ public final class Train {
RollingStock last = rollingStocks.get(rollingStocks.size() - 1);
// the first or last rolling stock HAVE TO BE an engine OR a train-set!
// therefore, no other rolling stock types should be allowed at all.
// TODO: consider using first.canBeAtFront() ???
return (first instanceof Engine || first instanceof TrainSet)
|| (last instanceof Engine || last instanceof TrainSet);
|| last instanceof Engine;
}
/**
@ -248,11 +249,7 @@ public final class Train {
* @return the rear direction of this train
*/
public Vector2D getRearDirection() {
if (position.size() == 1) {
return direction.negated();
} else {
return position.getLast().subtract(position.get(position.size() - 2)); // TODO
}
return position.getLast().subtract(position.get(position.size() - 2)); // TODO: consider longer trains
}
/**
@ -319,10 +316,7 @@ public final class Train {
// we evidently just moved into another rail
Rail[] nextTouchingRails = railNetwork.findTouchingRails(nextPosition);
Rail[] alreadyTouchingRails = railNetwork.findTouchingRails(secondToLastPosition);
if (alreadyTouchingRails.length == 0) {
// we evidently just derailed, be we should still crash with trains entering our last rail
assert !occupiedRails.isEmpty();
} else if (nextTouchingRails[0] == alreadyTouchingRails[0]) {
if (nextTouchingRails[0] == alreadyTouchingRails[0]) {
occupiedRails.add(nextTouchingRails[0]);
} else if (nextTouchingRails.length == 2) {
if (nextTouchingRails[1] == alreadyTouchingRails[0]) {

View File

@ -1,5 +1,7 @@
package edu.kit.informatik.model;
import edu.kit.informatik.ui.InvalidInputException;
/**
* Train set.
*
@ -37,9 +39,10 @@ public class TrainSet extends RollingStock {
* @param length length of train set
* @param couplingFront whether the train set should have a front coupling
* @param couplingBack whether the train set should have a back coupling
* @throws InvalidInputException on invalid user input (e.g. zero-sized train set)
*/
public TrainSet(final String series, final String name, final int length,
final boolean couplingFront, final boolean couplingBack) {
final boolean couplingFront, final boolean couplingBack) throws InvalidInputException {
super(length, couplingFront, couplingBack);
this.name = name;
this.series = series;

View File

@ -3,6 +3,7 @@ package edu.kit.informatik.ui.command;
import edu.kit.informatik.model.ModelRailwaySimulation;
import edu.kit.informatik.model.Vector2D;
import edu.kit.informatik.Terminal;
import edu.kit.informatik.ui.InvalidInputException;
/**
* Command used to add a track to the rail network.
@ -32,14 +33,10 @@ public class AddTrack extends Command {
}
@Override
public void apply(final ModelRailwaySimulation simulation) {
if (start.equals(end)) {
Terminal.printError("track has length 0");
return;
}
public void apply(final ModelRailwaySimulation simulation) throws InvalidInputException {
int id = simulation.addTrack(start, end);
if (id == -1) {
Terminal.printError("track is not connected to other tracks");
Terminal.printError("id space exhausted");
} else {
Terminal.printLine(id);
}

View File

@ -117,9 +117,6 @@ public class CommandFactory {
}
String name = matcher.group(3);
int length = Integer.parseInt(matcher.group(4));
if (length < 1) {
throw new InvalidInputException("engine has to be positive length");
}
boolean couplingFront = Boolean.parseBoolean(matcher.group(5));
boolean couplingBack = Boolean.parseBoolean(matcher.group(6));
return new CreateEngine(type, series, name, length, couplingFront, couplingBack);

View File

@ -3,6 +3,7 @@ package edu.kit.informatik.ui.command;
import edu.kit.informatik.ui.CoachType;
import edu.kit.informatik.model.ModelRailwaySimulation;
import edu.kit.informatik.Terminal;
import edu.kit.informatik.ui.InvalidInputException;
/**
* Command used to create a single coach.
@ -47,10 +48,10 @@ public class CreateCoach extends Command {
}
@Override
public void apply(final ModelRailwaySimulation simulation) {
public void apply(final ModelRailwaySimulation simulation) throws InvalidInputException {
int id = simulation.createCoach(type, length, couplingFront, couplingBack);
if (id == -1) {
Terminal.printError("could not add coach");
Terminal.printError("id space exhausted");
} else {
Terminal.printLine(id);
}

View File

@ -38,6 +38,7 @@ create train-set T2 Epsilon 1 false false
create train-set T2 Gamma 1 false false
create train-set T2 Lambda 1 true true
add train 3 T2-Lambda
show train 3
add train 5 T2-Epsilon
list trains
create coach passenger 1 true true
@ -103,4 +104,46 @@ 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
step all over my legos, you dumb program
delete track 5
delete track 3
delete track 2
delete track 1
delete track 4
add switch (0,0) -> (-10,0),(0,10)
add track (0,-10) -> (0,0)
add track (-20,0) -> (-10,0)
add track (0,20) -> (0,10)
add track (0,30) -> (0,20)
set switch 1 position (20,20)
set switch 1 position (0,10)
set switch 1 position (-10,0)
put train 5 at (-5,0) in direction -1,0
step 36
delete track 5
delete track 2
delete track 3
delete track 6
delete track 4
delete track 6
delete track 1
add track (0,0) -> (1,0)
add track (1,0) -> (2,0)
add track (2,0) -> (3,0)
put train 5 at (1,0) in direction 1,0
step 3
add track (0,0) -> (0,0)
add track (1,1) -> (1,0)
add track (1,0) -> (1,1)
create train-set T4 Einstein 1 false true
create train-set T4 Newton 1 true false
add train 7 T4-Einstein
add train 7 T4-Newton
create engine diesel T5 Hartnick 1 true false
add train 6 T5-Hartnick
put train 6 at (2,0) in direction 1,0
step 2
delete train 6
add train 6 T5-Hartnick
put train 6 at (0,0) in direction -1,0
step 1

View File

@ -31,7 +31,7 @@ OK
OK
1
OK
Error, engine has to be positive length
Error, rolling stock length has to be positive
T2-Gamma
diesel engine T2-Gamma added to train 2
OK
@ -45,6 +45,14 @@ Error, identifier already used
Error, identifier already used
T2-Lambda
train-set T2-Lambda added to train 3
++
||
_________||_________
| ___ ___ ___ ___ |
| |_| |_| |_| |_| |
|__________________|
|__________________|
(O) (O)
Error, new train identifier must be next free identifier
1 T2-Phi T2-Rho T2-Omega W1 W2 W3
2 T2-Gamma
@ -121,3 +129,45 @@ Error, invalid train direction
Error, invalid add track argument syntax
Error, unknown command
Error, invalid step argument
OK
OK
OK
OK
OK
1
2
3
4
5
Error, could not set switch
OK
OK
OK
Crash of train 5
OK
OK
OK
Error, could not delete rail segment
OK
Error, could not delete rail segment
OK
1
2
3
OK
Crash of train 5
Error, track has length zero
Error, track would connect to two other rails
Error, track would connect to two other rails
T4-Einstein
T4-Newton
train-set T4-Einstein added to train 7
train-set T4-Newton added to train 7
T5-Hartnick
diesel engine T5-Hartnick added to train 6
OK
Crash of train 6
OK
diesel engine T5-Hartnick added to train 6
OK
Crash of train 6