This commit is contained in:
2017-04-14 17:39:38 +08:00
commit bb1cf0bc87
64 changed files with 3940 additions and 0 deletions
@@ -0,0 +1,32 @@
package au.com.seek;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import java.io.ByteArrayOutputStream;
import java.io.PrintStream;
import static org.hamcrest.CoreMatchers.containsString;
import static org.junit.Assert.assertThat;
public class RobotSimulatorTest {
private final ByteArrayOutputStream outputContent = new ByteArrayOutputStream();
@Before
public void setUpStreams() {
System.setOut(new PrintStream(outputContent));
}
@After
public void cleanUpStreams() {
System.setOut(null);
}
@Test
public void should_run_commands_and_print_result_when_use_simple_input_file() throws Exception {
RobotSimulator.main(new String[]{});
assertThat(outputContent.toString(), containsString("3,3,NORTH"));
}
}
@@ -0,0 +1,24 @@
package au.com.seek.command;
import au.com.seek.domain.Robot;
import au.com.seek.domain.Coordinate;
import au.com.seek.domain.Direction;
import org.junit.Test;
import static org.hamcrest.CoreMatchers.equalTo;
import static org.junit.Assert.assertThat;
public class MoveForwardCommandTest {
@Test
public void should_update_place_after_execute() throws Exception {
Coordinate coordinate = new Coordinate(0, 0);
Robot robot = new Robot(coordinate, Direction.NORTH);
assertThat(robot.report(), equalTo("0,0,NORTH"));
MoveForwardCommand command = new MoveForwardCommand();
command.execute(robot);
assertThat(robot.report(), equalTo("0,1,NORTH"));
}
}
@@ -0,0 +1,39 @@
package au.com.seek.command;
import au.com.seek.domain.Robot;
import au.com.seek.domain.Coordinate;
import au.com.seek.domain.Direction;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import java.io.ByteArrayOutputStream;
import java.io.PrintStream;
import static org.hamcrest.CoreMatchers.containsString;
import static org.junit.Assert.assertThat;
public class ReportCommandTest {
private final ByteArrayOutputStream outContent = new ByteArrayOutputStream();
@Before
public void setUpStreams() {
System.setOut(new PrintStream(outContent));
}
@After
public void cleanUpStreams() {
System.setOut(null);
}
@Test
public void should_print_location_after_execute() throws Exception {
Coordinate coordinate = new Coordinate(0, 0);
Robot robot = new Robot(coordinate, Direction.NORTH);
ReportCommand command = new ReportCommand();
command.execute(robot);
assertThat(outContent.toString(), containsString("0,0,NORTH"));
}
}
@@ -0,0 +1,25 @@
package au.com.seek.command;
import au.com.seek.domain.Robot;
import au.com.seek.domain.Coordinate;
import au.com.seek.domain.Direction;
import org.junit.Test;
import static org.hamcrest.CoreMatchers.equalTo;
import static org.junit.Assert.assertThat;
public class TurnLeftCommandTest {
@Test
public void should_update_place_after_execute() throws Exception {
Coordinate coordinate = new Coordinate(0, 0);
Robot robot = new Robot(coordinate, Direction.NORTH);
assertThat(robot.report(), equalTo("0,0,NORTH"));
TurnLeftCommand command = new TurnLeftCommand();
command.execute(robot);
assertThat(robot.report(), equalTo("0,0,WEST"));
}
}
@@ -0,0 +1,24 @@
package au.com.seek.command;
import au.com.seek.domain.Robot;
import au.com.seek.domain.Coordinate;
import au.com.seek.domain.Direction;
import org.junit.Test;
import static org.hamcrest.core.IsEqual.equalTo;
import static org.junit.Assert.assertThat;
public class TurnRightCommandTest {
@Test
public void should_update_place_after_execute() throws Exception {
Coordinate coordinate = new Coordinate(0, 0);
Robot robot = new Robot(coordinate, Direction.NORTH);
assertThat(robot.report(), equalTo("0,0,NORTH"));
TurnRightCommand command = new TurnRightCommand();
command.execute(robot);
assertThat(robot.report(), equalTo("0,0,EAST"));
}
}
@@ -0,0 +1,84 @@
package au.com.seek.domain;
import org.junit.Test;
import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.core.Is.is;
import static org.junit.Assert.assertThat;
public class DirectionTest {
@Test
public void should_return_new_direction_when_do_turn_left() throws Exception {
assertThat(Direction.NORTH.turnLeft(), is(Direction.WEST));
assertThat(Direction.WEST.turnLeft(), is(Direction.SOUTH));
assertThat(Direction.SOUTH.turnLeft(), is(Direction.EAST));
assertThat(Direction.EAST.turnLeft(), is(Direction.NORTH));
}
@Test
public void should_return_new_direction_when_do_turn_right() throws Exception {
assertThat(Direction.NORTH.turnRight(), is(Direction.EAST));
assertThat(Direction.EAST.turnRight(), is(Direction.SOUTH));
assertThat(Direction.SOUTH.turnRight(), is(Direction.WEST));
assertThat(Direction.WEST.turnRight(), is(Direction.NORTH));
}
@Test
public void should_return_step_value_on_x_axis() throws Exception {
assertThat(Direction.NORTH.stepValueOnXAxis(), is(0));
assertThat(Direction.EAST.stepValueOnXAxis(), is(1));
assertThat(Direction.SOUTH.stepValueOnXAxis(), is(0));
assertThat(Direction.WEST.stepValueOnXAxis(), is(-1));
}
@Test
public void should_return_step_value_on_y_axis() throws Exception {
assertThat(Direction.NORTH.stepValueOnYAxis(), is(1));
assertThat(Direction.EAST.stepValueOnYAxis(), is(0));
assertThat(Direction.SOUTH.stepValueOnYAxis(), is(-1));
assertThat(Direction.WEST.stepValueOnYAxis(), is(0));
}
@Test
public void should_return_direction_string_as_enum() throws Exception {
assertThat(Direction.NORTH.toString(), equalTo("NORTH"));
assertThat(Direction.WEST.toString(), equalTo("WEST"));
assertThat(Direction.SOUTH.toString(), equalTo("SOUTH"));
assertThat(Direction.EAST.toString(), equalTo("EAST"));
}
@Test
public void should_return_all_the_values_of_direction() throws Exception {
Direction[] directions = {Direction.NORTH, Direction.EAST, Direction.SOUTH, Direction.WEST};
assertThat(Direction.values(), equalTo(directions));
}
@Test
public void test_value_of() throws Exception {
Direction east = Direction.valueOf("EAST");
assertThat(east, is(Direction.EAST));
}
}
@@ -0,0 +1,83 @@
package au.com.seek.domain;
import org.junit.Test;
import static org.hamcrest.CoreMatchers.equalTo;
import static org.junit.Assert.assertThat;
public class RobotTest {
@Test
public void should_report_robot_location() throws Exception {
Coordinate coordinate = new Coordinate(0, 0);
Robot robot = new Robot(coordinate, Direction.EAST);
String report = robot.report();
assertThat(report, equalTo("0,0,EAST"));
}
@Test
public void should_report_robot_location_when_x_and_y_axis_is_not_zero() throws Exception {
Coordinate coordinate = new Coordinate(3, 5);
Robot robot = new Robot(coordinate, Direction.SOUTH);
String report = robot.report();
assertThat(report, equalTo("3,5,SOUTH"));
}
@Test
public void should_change_direction_when_do_turn_left() throws Exception {
Coordinate coordinate = new Coordinate(3, 3);
Robot robot = new Robot(coordinate, Direction.NORTH);
robot.turnLeft();
assertThat(robot.report(), equalTo("3,3,WEST"));
}
@Test
public void should_change_direction_when_do_turn_right() throws Exception {
Coordinate coordinate = new Coordinate(0, 1);
Robot robot = new Robot(coordinate, Direction.NORTH);
robot.turnRight();
assertThat(robot.report(), equalTo("0,1,EAST"));
}
@Test
public void should_change_coordinates_when_do_move_and_facing_north() throws Exception {
Coordinate coordinate = new Coordinate(0, 1);
Robot robot = new Robot(coordinate, Direction.NORTH);
robot.moveForward();
assertThat(robot.report(), equalTo("0,2,NORTH"));
}
@Test
public void should_change_coordinates_when_do_move_and_facing_east() throws Exception {
Coordinate coordinate = new Coordinate(0, 1);
Robot robot = new Robot(coordinate, Direction.EAST);
robot.moveForward();
assertThat(robot.report(), equalTo("1,1,EAST"));
}
@Test
public void should_remain_same_coordinates_if_robot_is_going_to_falling_in_x_axis() throws Exception {
Coordinate coordinate = new Coordinate(5, 0);
Robot robot = new Robot(coordinate, Direction.EAST);
robot.moveForward();
assertThat(robot.report(), equalTo("5,0,EAST"));
}
@Test
public void should_remain_same_coordinates_if_robot_is_going_to_falling_in_y_axis() throws Exception {
Coordinate coordinate = new Coordinate(0, 5);
Robot robot = new Robot(coordinate, Direction.NORTH);
robot.moveForward();
assertThat(robot.report(), equalTo("0,5,NORTH"));
}
}
@@ -0,0 +1,33 @@
package au.com.seek.domain;
import org.junit.Test;
import static org.hamcrest.core.Is.is;
import static org.junit.Assert.assertThat;
public class TabletopTest {
@Test
public void just_for_coverage() throws Exception {
Tabletop tabletop = new Tabletop();
assertThat(tabletop == null, is(false));
}
@Test
public void should_return_true_if_coordinate_are_too_large() throws Exception {
Coordinate coordinate = new Coordinate(6, 5);
assertThat(Tabletop.isCoordinateOutOfTabletop(coordinate), is(true));
}
@Test
public void should_return_false_if_coordinate_is_on_the_border_tabletop() throws Exception {
Coordinate coordinate = new Coordinate(5, 5);
assertThat(Tabletop.isCoordinateOutOfTabletop(coordinate), is(false));
}
@Test
public void should_return_false_if_coordinate_is_within_tabletop() throws Exception {
Coordinate coordinate = new Coordinate(3, 1);
assertThat(Tabletop.isCoordinateOutOfTabletop(coordinate), is(false));
}
}
@@ -0,0 +1,57 @@
package au.com.seek.parser;
import au.com.seek.domain.Robot;
import au.com.seek.command.RobotCommand;
import org.junit.Test;
import java.util.List;
import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.core.Is.is;
import static org.junit.Assert.assertThat;
public class InputParserTest {
@Test
public void should_return_commands_and_robot() throws Exception {
InputParser parser = new InputParser("simple_input.txt");
Robot robot = parser.getRobot();
List<RobotCommand> commands = parser.parseCommands();
assertThat(robot.report(), equalTo("0,0,NORTH"));
assertThat(commands.size(),is(3));
assertThat(commands.get(0).getClass().getSimpleName(), equalTo("MoveForwardCommand"));
assertThat(commands.get(1).getClass().getSimpleName(), equalTo("TurnRightCommand"));
assertThat(commands.get(2).getClass().getSimpleName(), equalTo("ReportCommand"));
}
@Test
public void should_skip_the_commands_before_place_robot() throws Exception {
InputParser parser = new InputParser("has_invalid_inputs.txt");
Robot robot = parser.getRobot();
List<RobotCommand> commands = parser.parseCommands();
assertThat(robot.report(), equalTo("2,4,EAST"));
assertThat(commands.size(),is(3));
assertThat(commands.get(0).getClass().getSimpleName(), equalTo("TurnLeftCommand"));
assertThat(commands.get(1).getClass().getSimpleName(), equalTo("MoveForwardCommand"));
assertThat(commands.get(2).getClass().getSimpleName(), equalTo("ReportCommand"));
}
@Test
public void should_replace_the_robot_when_there_is_another_place_command() throws Exception {
InputParser parser = new InputParser("double_place_commands.txt");
Robot robot = parser.getRobot();
List<RobotCommand> commands = parser.parseCommands();
assertThat(robot.report(), equalTo("3,3,EAST"));
assertThat(commands.size(),is(4));
assertThat(commands.get(0).getClass().getSimpleName(), equalTo("MoveForwardCommand"));
assertThat(commands.get(1).getClass().getSimpleName(), equalTo("TurnRightCommand"));
assertThat(commands.get(2).getClass().getSimpleName(), equalTo("MoveForwardCommand"));
assertThat(commands.get(3).getClass().getSimpleName(), equalTo("ReportCommand"));
}
// @Test
// public void should_throw_exception_if_file_is_not_found() throws Exception {
// InputParser parser = new InputParser("empty.txt");
// }
}