From 2125588a0d41c1da879450e7cc9488508877283c Mon Sep 17 00:00:00 2001 From: Arne Keller Date: Tue, 18 Feb 2020 18:11:40 +0100 Subject: [PATCH] Increase test coverage and tweak error messages --- .../kit/informatik/model/RailwayNetwork.java | 6 ++--- src/edu/kit/informatik/model/Track.java | 15 ++++++------ src/edu/kit/informatik/model/Train.java | 3 ++- src/edu/kit/informatik/model/Vector2D.java | 2 +- src/edu/kit/informatik/ui/CommandLine.java | 11 ++++++--- stophittingyourself_output.txt | 2 +- testcoverage_input.txt | 24 ++++++++++++++++++- testcoverage_output.txt | 22 +++++++++++++++++ 8 files changed, 67 insertions(+), 18 deletions(-) diff --git a/src/edu/kit/informatik/model/RailwayNetwork.java b/src/edu/kit/informatik/model/RailwayNetwork.java index 805bfa8..623e01e 100644 --- a/src/edu/kit/informatik/model/RailwayNetwork.java +++ b/src/edu/kit/informatik/model/RailwayNetwork.java @@ -69,7 +69,7 @@ public class RailwayNetwork { */ 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) { - return -1; + throw new InvalidInputException("switch has length zero"); } if (rails.isEmpty()) { 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 end2PossibleConnections = rails.values().stream().filter((rail) -> rail.canConnectTo(end2)).count(); 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()) { if (rail.canConnectTo(start) || rail.canConnectTo(end1) || rail.canConnectTo(end2)) { @@ -93,7 +93,7 @@ public class RailwayNetwork { return newSwitch.getIdentifier(); } } - return -1; + throw new InvalidInputException("switch not connected to other rails"); } } diff --git a/src/edu/kit/informatik/model/Track.java b/src/edu/kit/informatik/model/Track.java index ae86f68..12fd9f0 100644 --- a/src/edu/kit/informatik/model/Track.java +++ b/src/edu/kit/informatik/model/Track.java @@ -1,5 +1,7 @@ package edu.kit.informatik.model; +import edu.kit.informatik.ui.InvalidInputException; + import java.util.Objects; /** @@ -17,12 +19,12 @@ public final class Track extends Rail { * @param start start position of the track * @param end end position of the track * @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); 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.end = end; @@ -40,10 +42,7 @@ public final class Track extends Rail { @Override public boolean canConnectToRail(Rail rail) { - if (rail == null) { - System.out.println("hey"); // TODO: remove - } - return rail.canConnectTo(start) || rail.canConnectTo(end); + return rail != null && rail.canConnectTo(start) || rail.canConnectTo(end); } @Override @@ -82,7 +81,7 @@ public final class Track extends Rail { return new Vector2D(Long.signum((long) start.getX() - (long) end.getX()), Long.signum((long) start.getY() - (long) end.getY())); } else { - throw new IllegalArgumentException(); + throw new IllegalArgumentException("can only compute direction from track ends"); } } diff --git a/src/edu/kit/informatik/model/Train.java b/src/edu/kit/informatik/model/Train.java index ad7ed1e..0e8974f 100644 --- a/src/edu/kit/informatik/model/Train.java +++ b/src/edu/kit/informatik/model/Train.java @@ -195,7 +195,7 @@ public final class Train { * @return whether this train is placed on a rail network */ 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 Rail[] nextTouchingRails = railNetwork.findTouchingRails(nextPosition); Rail[] alreadyTouchingRails = railNetwork.findTouchingRails(secondToLastPosition); + // TODO: do we need all of these cases??? if (nextTouchingRails[0] == alreadyTouchingRails[0]) { occupiedRails.add(nextTouchingRails[0]); } else if (nextTouchingRails.length == 2) { diff --git a/src/edu/kit/informatik/model/Vector2D.java b/src/edu/kit/informatik/model/Vector2D.java index 9922f87..1eb836d 100644 --- a/src/edu/kit/informatik/model/Vector2D.java +++ b/src/edu/kit/informatik/model/Vector2D.java @@ -132,7 +132,7 @@ public class Vector2D { @Override public boolean equals(final Object obj) { if (obj != null && getClass().equals(obj.getClass())) { - final Vector2D point = (Vector2D) obj; + Vector2D point = (Vector2D) obj; return x == point.x && y == point.y; } diff --git a/src/edu/kit/informatik/ui/CommandLine.java b/src/edu/kit/informatik/ui/CommandLine.java index 4564c5a..dea367c 100644 --- a/src/edu/kit/informatik/ui/CommandLine.java +++ b/src/edu/kit/informatik/ui/CommandLine.java @@ -25,7 +25,7 @@ public class CommandLine { ModelRailwaySimulation simulation = new ModelRailwaySimulation(); while (true) { - final String input = Terminal.readLine(); + String input = Terminal.readLine(); if (input == null) { break; // TODO remove } @@ -33,8 +33,13 @@ public class CommandLine { continue; // TODO remove } - if (input.equals(EXIT)) { - break; + if (input.startsWith(EXIT)) { + if (input.length() != EXIT.length()) { + Terminal.printError("space after exit command"); + continue; + } else { + break; + } } try { diff --git a/stophittingyourself_output.txt b/stophittingyourself_output.txt index 06842d6..c8ea273 100644 --- a/stophittingyourself_output.txt +++ b/stophittingyourself_output.txt @@ -2,4 +2,4 @@ 2 3 4 -Error, switch not connected to existing rails +Error, switch endpoint would connect to two other rails diff --git a/testcoverage_input.txt b/testcoverage_input.txt index c1a8c83..09ed67a 100644 --- a/testcoverage_input.txt +++ b/testcoverage_input.txt @@ -105,6 +105,7 @@ 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 +exit delete track 5 delete track 3 delete track 2 @@ -146,4 +147,25 @@ step 2 delete train 6 add train 6 T5-Hartnick put train 6 at (0,0) in direction -1,0 -step 1 \ No newline at end of file +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 diff --git a/testcoverage_output.txt b/testcoverage_output.txt index de2cbec..51680f4 100644 --- a/testcoverage_output.txt +++ b/testcoverage_output.txt @@ -129,6 +129,7 @@ Error, invalid train direction Error, invalid add track argument syntax Error, unknown command Error, invalid step argument +Error, space after exit command OK OK OK @@ -171,3 +172,24 @@ OK diesel engine T5-Hartnick added to train 6 OK 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