Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Sync robot-simulator with problem-specifications #1829

Merged
merged 3 commits into from
Dec 19, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions exercises/practice/robot-simulator/.meta/test_template.tera
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
use {{ crate_name }}::*;

{% for test in cases %}
#[test]
{% if loop.index != 1 -%}
#[ignore]
{% endif -%}
fn {{ test.description | slugify | replace(from="-", to="_") }}() {
{%- if test.property == "create" %}
let robot = Robot::new({{ test.input.position.x }}, {{ test.input.position.y }}, Direction::{{ test.input.direction | title }});
assert_eq!(robot.position(), ({{ test.expected.position.x }}, {{ test.expected.position.y }}));
assert_eq!(robot.direction(), &Direction::{{ test.expected.direction | title }});
}
{% continue %}
{% endif -%}

let robot_start = Robot::new({{ test.input.position.x }}, {{ test.input.position.y }}, Direction::{{ test.input.direction | title }});
{%- if test.input.instructions == "R" %}
let robot_end = robot_start.turn_right();
{%- elif test.input.instructions == "L" %}
let robot_end = robot_start.turn_left();
{%- elif test.input.instructions == "A" %}
let robot_end = robot_start.advance();
{%- else %}
let robot_end = robot_start.instructions({{ test.input.instructions | json_encode() }});
{% endif -%}
assert_eq!(robot_end.position(), ({{ test.expected.position.x }}, {{ test.expected.position.y }}));
assert_eq!(robot_end.direction(), &Direction::{{ test.expected.direction | title }});
}
{% endfor -%}
67 changes: 64 additions & 3 deletions exercises/practice/robot-simulator/.meta/tests.toml
Original file line number Diff line number Diff line change
@@ -1,3 +1,64 @@
# This is an auto-generated file. Regular comments will be removed when this
# file is regenerated. Regenerating will not touch any manually added keys,
# so comments can be added in a "comment" key.
# This is an auto-generated file.
#
# Regenerating this file via `configlet sync` will:
# - Recreate every `description` key/value pair
# - Recreate every `reimplements` key/value pair, where they exist in problem-specifications
# - Remove any `include = true` key/value pair (an omitted `include` key implies inclusion)
# - Preserve any other key/value pair
#
# As user-added comments (using the # character) will be removed when this file
# is regenerated, comments can be added via a `comment` key.

[c557c16d-26c1-4e06-827c-f6602cd0785c]
description = "Create robot -> at origin facing north"

[bf0dffce-f11c-4cdb-8a5e-2c89d8a5a67d]
description = "Create robot -> at negative position facing south"

[8cbd0086-6392-4680-b9b9-73cf491e67e5]
description = "Rotating clockwise -> changes north to east"

[8abc87fc-eab2-4276-93b7-9c009e866ba1]
description = "Rotating clockwise -> changes east to south"

[3cfe1b85-bbf2-4bae-b54d-d73e7e93617a]
description = "Rotating clockwise -> changes south to west"

[5ea9fb99-3f2c-47bd-86f7-46b7d8c3c716]
description = "Rotating clockwise -> changes west to north"

[fa0c40f5-6ba3-443d-a4b3-58cbd6cb8d63]
description = "Rotating counter-clockwise -> changes north to west"

[da33d734-831f-445c-9907-d66d7d2a92e2]
description = "Rotating counter-clockwise -> changes west to south"

[bd1ca4b9-4548-45f4-b32e-900fc7c19389]
description = "Rotating counter-clockwise -> changes south to east"

[2de27b67-a25c-4b59-9883-bc03b1b55bba]
description = "Rotating counter-clockwise -> changes east to north"

[f0dc2388-cddc-4f83-9bed-bcf46b8fc7b8]
description = "Moving forward one -> facing north increments Y"

[2786cf80-5bbf-44b0-9503-a89a9c5789da]
description = "Moving forward one -> facing south decrements Y"

[84bf3c8c-241f-434d-883d-69817dbd6a48]
description = "Moving forward one -> facing east increments X"

[bb69c4a7-3bbf-4f64-b415-666fa72d7b04]
description = "Moving forward one -> facing west decrements X"

[e34ac672-4ed4-4be3-a0b8-d9af259cbaa1]
description = "Follow series of instructions -> moving east and north from README"

[f30e4955-4b47-4aa3-8b39-ae98cfbd515b]
description = "Follow series of instructions -> moving west and north"

[3e466bf6-20ab-4d79-8b51-264165182fca]
description = "Follow series of instructions -> moving west and south"

[41f0bb96-c617-4e6b-acff-a4b279d44514]
description = "Follow series of instructions -> moving east and north"
157 changes: 86 additions & 71 deletions exercises/practice/robot-simulator/tests/robot-simulator.rs
Original file line number Diff line number Diff line change
@@ -1,145 +1,160 @@
use robot_simulator::*;

#[test]
fn robots_are_created_with_position_and_direction() {
fn at_origin_facing_north() {
let robot = Robot::new(0, 0, Direction::North);
assert_eq!((0, 0), robot.position());
assert_eq!(&Direction::North, robot.direction());
assert_eq!(robot.position(), (0, 0));
assert_eq!(robot.direction(), &Direction::North);
}

#[test]
#[ignore]
fn positions_can_be_negative() {
fn at_negative_position_facing_south() {
let robot = Robot::new(-1, -1, Direction::South);
assert_eq!((-1, -1), robot.position());
assert_eq!(&Direction::South, robot.direction());
assert_eq!(robot.position(), (-1, -1));
assert_eq!(robot.direction(), &Direction::South);
}

#[test]
#[ignore]
fn turning_right_does_not_change_position() {
let robot = Robot::new(0, 0, Direction::North).turn_right();
assert_eq!((0, 0), robot.position());
fn changes_north_to_east() {
let robot_start = Robot::new(0, 0, Direction::North);
let robot_end = robot_start.turn_right();
assert_eq!(robot_end.position(), (0, 0));
assert_eq!(robot_end.direction(), &Direction::East);
}

#[test]
#[ignore]
fn turning_right_from_north_points_the_robot_east() {
let robot = Robot::new(0, 0, Direction::North).turn_right();
assert_eq!(&Direction::East, robot.direction());
fn changes_east_to_south() {
let robot_start = Robot::new(0, 0, Direction::East);
let robot_end = robot_start.turn_right();
assert_eq!(robot_end.position(), (0, 0));
assert_eq!(robot_end.direction(), &Direction::South);
}

#[test]
#[ignore]
fn turning_right_from_east_points_the_robot_south() {
let robot = Robot::new(0, 0, Direction::East).turn_right();
assert_eq!(&Direction::South, robot.direction());
fn changes_south_to_west() {
let robot_start = Robot::new(0, 0, Direction::South);
let robot_end = robot_start.turn_right();
assert_eq!(robot_end.position(), (0, 0));
assert_eq!(robot_end.direction(), &Direction::West);
}

#[test]
#[ignore]
fn turning_right_from_south_points_the_robot_west() {
let robot = Robot::new(0, 0, Direction::South).turn_right();
assert_eq!(&Direction::West, robot.direction());
fn changes_west_to_north() {
let robot_start = Robot::new(0, 0, Direction::West);
let robot_end = robot_start.turn_right();
assert_eq!(robot_end.position(), (0, 0));
assert_eq!(robot_end.direction(), &Direction::North);
}

#[test]
#[ignore]
fn turning_right_from_west_points_the_robot_north() {
let robot = Robot::new(0, 0, Direction::West).turn_right();
assert_eq!(&Direction::North, robot.direction());
fn changes_north_to_west() {
let robot_start = Robot::new(0, 0, Direction::North);
let robot_end = robot_start.turn_left();
assert_eq!(robot_end.position(), (0, 0));
assert_eq!(robot_end.direction(), &Direction::West);
}

#[test]
#[ignore]
fn turning_left_does_not_change_position() {
let robot = Robot::new(0, 0, Direction::North).turn_left();
assert_eq!((0, 0), robot.position());
fn changes_west_to_south() {
let robot_start = Robot::new(0, 0, Direction::West);
let robot_end = robot_start.turn_left();
assert_eq!(robot_end.position(), (0, 0));
assert_eq!(robot_end.direction(), &Direction::South);
}

#[test]
#[ignore]
fn turning_left_from_north_points_the_robot_west() {
let robot = Robot::new(0, 0, Direction::North).turn_left();
assert_eq!(&Direction::West, robot.direction());
fn changes_south_to_east() {
let robot_start = Robot::new(0, 0, Direction::South);
let robot_end = robot_start.turn_left();
assert_eq!(robot_end.position(), (0, 0));
assert_eq!(robot_end.direction(), &Direction::East);
}

#[test]
#[ignore]
fn turning_left_from_west_points_the_robot_south() {
let robot = Robot::new(0, 0, Direction::West).turn_left();
assert_eq!(&Direction::South, robot.direction());
fn changes_east_to_north() {
let robot_start = Robot::new(0, 0, Direction::East);
let robot_end = robot_start.turn_left();
assert_eq!(robot_end.position(), (0, 0));
assert_eq!(robot_end.direction(), &Direction::North);
}

#[test]
#[ignore]
fn turning_left_from_south_points_the_robot_east() {
let robot = Robot::new(0, 0, Direction::South).turn_left();
assert_eq!(&Direction::East, robot.direction());
fn facing_north_increments_y() {
let robot_start = Robot::new(0, 0, Direction::North);
let robot_end = robot_start.advance();
assert_eq!(robot_end.position(), (0, 1));
assert_eq!(robot_end.direction(), &Direction::North);
}

#[test]
#[ignore]
fn turning_left_from_east_points_the_robot_north() {
let robot = Robot::new(0, 0, Direction::East).turn_left();
assert_eq!(&Direction::North, robot.direction());
fn facing_south_decrements_y() {
let robot_start = Robot::new(0, 0, Direction::South);
let robot_end = robot_start.advance();
assert_eq!(robot_end.position(), (0, -1));
assert_eq!(robot_end.direction(), &Direction::South);
}

#[test]
#[ignore]
fn advance_does_not_change_the_direction() {
let robot = Robot::new(0, 0, Direction::North).advance();
assert_eq!(&Direction::North, robot.direction());
fn facing_east_increments_x() {
let robot_start = Robot::new(0, 0, Direction::East);
let robot_end = robot_start.advance();
assert_eq!(robot_end.position(), (1, 0));
assert_eq!(robot_end.direction(), &Direction::East);
}

#[test]
#[ignore]
fn advance_increases_the_y_coordinate_by_one_when_facing_north() {
let robot = Robot::new(0, 0, Direction::North).advance();
assert_eq!((0, 1), robot.position());
fn facing_west_decrements_x() {
let robot_start = Robot::new(0, 0, Direction::West);
let robot_end = robot_start.advance();
assert_eq!(robot_end.position(), (-1, 0));
assert_eq!(robot_end.direction(), &Direction::West);
}

#[test]
#[ignore]
fn advance_decreases_the_y_coordinate_by_one_when_facing_south() {
let robot = Robot::new(0, 0, Direction::South).advance();
assert_eq!((0, -1), robot.position());
fn moving_east_and_north_from_readme() {
let robot_start = Robot::new(7, 3, Direction::North);
let robot_end = robot_start.instructions("RAALAL");
assert_eq!(robot_end.position(), (9, 4));
assert_eq!(robot_end.direction(), &Direction::West);
}

#[test]
#[ignore]
fn advance_increases_the_x_coordinate_by_one_when_facing_east() {
let robot = Robot::new(0, 0, Direction::East).advance();
assert_eq!((1, 0), robot.position());
fn moving_west_and_north() {
let robot_start = Robot::new(0, 0, Direction::North);
let robot_end = robot_start.instructions("LAAARALA");
assert_eq!(robot_end.position(), (-4, 1));
assert_eq!(robot_end.direction(), &Direction::West);
}

#[test]
#[ignore]
fn advance_decreases_the_x_coordinate_by_one_when_facing_west() {
let robot = Robot::new(0, 0, Direction::West).advance();
assert_eq!((-1, 0), robot.position());
fn moving_west_and_south() {
let robot_start = Robot::new(2, -7, Direction::East);
let robot_end = robot_start.instructions("RRAAAAALA");
assert_eq!(robot_end.position(), (-3, -8));
assert_eq!(robot_end.direction(), &Direction::South);
}

#[test]
#[ignore]
fn follow_instructions_to_move_west_and_north() {
let robot = Robot::new(0, 0, Direction::North).instructions("LAAARALA");
assert_eq!((-4, 1), robot.position());
assert_eq!(&Direction::West, robot.direction());
}

#[test]
#[ignore]
fn follow_instructions_to_move_west_and_south() {
let robot = Robot::new(2, -7, Direction::East).instructions("RRAAAAALA");
assert_eq!((-3, -8), robot.position());
assert_eq!(&Direction::South, robot.direction());
}

#[test]
#[ignore]
fn follow_instructions_to_move_east_and_north() {
let robot = Robot::new(8, 4, Direction::South).instructions("LAAARRRALLLL");
assert_eq!((11, 5), robot.position());
assert_eq!(&Direction::North, robot.direction());
fn moving_east_and_north() {
let robot_start = Robot::new(8, 4, Direction::South);
let robot_end = robot_start.instructions("LAAARRRALLLL");
assert_eq!(robot_end.position(), (11, 5));
assert_eq!(robot_end.direction(), &Direction::North);
}