Skip to content

Commit 2d4bcfd

Browse files
committed
Update stub push towards immutable robots
As mentioned here: exercism#146 (comment) I like the idea of pushing towards a solution with immutable robots. It's a different way of using structs than our other exercises do. As a side benefit, it shortens the test functions. Students can always edit the test file or the stub file, if they so wish. So they could still implement mutable robots, though I don't think many (or any, probably) will do so.
1 parent 46371d7 commit 2d4bcfd

File tree

2 files changed

+22
-40
lines changed

2 files changed

+22
-40
lines changed

exercises/robot-simulator/src/lib.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,19 +13,19 @@ impl Robot {
1313
unimplemented!()
1414
}
1515

16-
pub fn turn_right(&mut self) {
16+
pub fn turn_right(self) -> Self {
1717
unimplemented!()
1818
}
1919

20-
pub fn turn_left(&mut self) {
20+
pub fn turn_left(self) -> Self {
2121
unimplemented!()
2222
}
2323

24-
pub fn advance(&mut self) {
24+
pub fn advance(self) -> Self {
2525
unimplemented!()
2626
}
2727

28-
pub fn instructions(&mut self, instructions: &str) {
28+
pub fn instructions(self, instructions: &str) -> Self {
2929
unimplemented!()
3030
}
3131

exercises/robot-simulator/tests/robot-simulator.rs

Lines changed: 18 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -20,146 +20,128 @@ fn positions_can_be_negative() {
2020
#[test]
2121
#[ignore]
2222
fn turning_right_does_not_change_position() {
23-
let mut robot = Robot::new(0, 0, &Direction::North);
24-
robot.turn_right();
23+
let robot = Robot::new(0, 0, &Direction::North).turn_right();
2524
assert_eq!((0, 0), robot.position());
2625
}
2726

2827
#[test]
2928
#[ignore]
3029
fn turning_right_from_north_points_the_robot_east() {
31-
let mut robot = Robot::new(0, 0, &Direction::North);
32-
robot.turn_right();
30+
let robot = Robot::new(0, 0, &Direction::North).turn_right();
3331
assert_eq!(Direction::East, robot.direction());
3432
}
3533

3634
#[test]
3735
#[ignore]
3836
fn turning_right_from_east_points_the_robot_south() {
39-
let mut robot = Robot::new(0, 0, &Direction::East);
40-
robot.turn_right();
37+
let robot = Robot::new(0, 0, &Direction::East).turn_right();
4138
assert_eq!(Direction::South, robot.direction());
4239
}
4340

4441
#[test]
4542
#[ignore]
4643
fn turning_right_from_south_points_the_robot_west() {
47-
let mut robot = Robot::new(0, 0, &Direction::South);
48-
robot.turn_right();
44+
let robot = Robot::new(0, 0, &Direction::South).turn_right();
4945
assert_eq!(Direction::West, robot.direction());
5046
}
5147

5248
#[test]
5349
#[ignore]
5450
fn turning_right_from_west_points_the_robot_north() {
55-
let mut robot = Robot::new(0, 0, &Direction::West);
56-
robot.turn_right();
51+
let robot = Robot::new(0, 0, &Direction::West).turn_right();
5752
assert_eq!(Direction::North, robot.direction());
5853
}
5954

6055
#[test]
6156
#[ignore]
6257
fn turning_left_does_not_change_position() {
63-
let mut robot = Robot::new(0, 0, &Direction::North);
64-
robot.turn_left();
58+
let robot = Robot::new(0, 0, &Direction::North).turn_left();
6559
assert_eq!((0, 0), robot.position());
6660
}
6761

6862
#[test]
6963
#[ignore]
7064
fn turning_left_from_north_points_the_robot_west() {
71-
let mut robot = Robot::new(0, 0, &Direction::North);
72-
robot.turn_left();
65+
let robot = Robot::new(0, 0, &Direction::North).turn_left();
7366
assert_eq!(Direction::East, robot.direction());
7467
}
7568

7669
#[test]
7770
#[ignore]
7871
fn turning_left_from_west_points_the_robot_south() {
79-
let mut robot = Robot::new(0, 0, &Direction::East);
80-
robot.turn_left();
72+
let robot = Robot::new(0, 0, &Direction::East).turn_left();
8173
assert_eq!(Direction::South, robot.direction());
8274
}
8375

8476
#[test]
8577
#[ignore]
8678
fn turning_left_from_south_points_the_robot_east() {
87-
let mut robot = Robot::new(0, 0, &Direction::South);
88-
robot.turn_left();
79+
let robot = Robot::new(0, 0, &Direction::South).turn_left();
8980
assert_eq!(Direction::East, robot.direction());
9081
}
9182

9283
#[test]
9384
#[ignore]
9485
fn turning_left_from_east_points_the_robot_north() {
95-
let mut robot = Robot::new(0, 0, &Direction::East);
96-
robot.turn_left();
86+
let robot = Robot::new(0, 0, &Direction::East).turn_left();
9787
assert_eq!(Direction::North, robot.direction());
9888
}
9989

10090
#[test]
10191
#[ignore]
10292
fn advance_does_not_change_the_direction() {
103-
let mut robot = Robot::new(0, 0, &Direction::North);
104-
robot.advance();
93+
let robot = Robot::new(0, 0, &Direction::North).advance();
10594
assert_eq!(Direction::North, robot.direction());
10695
}
10796

10897
#[test]
10998
#[ignore]
11099
fn advance_increases_the_y_coordinate_by_one_when_facing_north() {
111-
let mut robot = Robot::new(0, 0, &Direction::North);
112-
robot.advance();
100+
let robot = Robot::new(0, 0, &Direction::North).advance();
113101
assert_eq!((0, 1), robot.position());
114102
}
115103

116104
#[test]
117105
#[ignore]
118106
fn advance_decreases_the_y_coordinate_by_one_when_facing_south() {
119-
let mut robot = Robot::new(0, 0, &Direction::South);
120-
robot.advance();
107+
let robot = Robot::new(0, 0, &Direction::South).advance();
121108
assert_eq!((0, -1), robot.position());
122109
}
123110

124111
#[test]
125112
#[ignore]
126113
fn advance_increases_the_x_coordinate_by_one_when_facing_east() {
127-
let mut robot = Robot::new(0, 0, &Direction::East);
128-
robot.advance();
114+
let robot = Robot::new(0, 0, &Direction::East).advance();
129115
assert_eq!((1, 0), robot.position());
130116
}
131117

132118
#[test]
133119
#[ignore]
134120
fn advance_decreases_the_x_coordinate_by_one_when_facing_west() {
135-
let mut robot = Robot::new(0, 0, &Direction::West);
136-
robot.advance();
121+
let robot = Robot::new(0, 0, &Direction::West).advance();
137122
assert_eq!((-1, 0), robot.position());
138123
}
139124

140125
#[test]
141126
#[ignore]
142127
fn follow_instructions_to_move_west_and_north() {
143-
let mut robot = Robot::new(0, 0, &Direction::North);
144-
robot.instructions("LAAARALA");
128+
let robot = Robot::new(0, 0, &Direction::North).instructions("LAAARALA");
145129
assert_eq!((-4, 1), robot.position());
146130
assert_eq!(Direction::West, robot.direction());
147131
}
148132

149133
#[test]
150134
#[ignore]
151135
fn follow_instructions_to_move_west_and_south() {
152-
let mut robot = Robot::new(0, 0, &Direction::East);
153-
robot.instructions("RRAAAAALA");
136+
let robot = Robot::new(0, 0, &Direction::North).instructions("RRAAAAALA");
154137
assert_eq!((-3, -8), robot.position());
155138
assert_eq!(Direction::South, robot.direction());
156139
}
157140

158141
#[test]
159142
#[ignore]
160143
fn follow_instructions_to_move_east_and_north() {
161-
let mut robot = Robot::new(0, 0, &Direction::South);
162-
robot.instructions("LAAARRRALLLL");
144+
let robot = Robot::new(0, 0, &Direction::North).instructions("LAAARRRALLLL");
163145
assert_eq!((11, 5), robot.position());
164146
assert_eq!(Direction::North, robot.direction());
165147
}

0 commit comments

Comments
 (0)