Skip to content

Commit 366fcd2

Browse files
Merge pull request exercism#462 from ricemery/robot
Create RobotSimulatorTestGenerator.scala. Update RobotSimulatorTest.s…
2 parents 8c6067c + b00a264 commit 366fcd2

File tree

3 files changed

+226
-61
lines changed

3 files changed

+226
-61
lines changed
Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
1+
import org.scalatest.{Matchers, FunSuite}
2+
3+
/** @version 1.0.0 */
4+
class RobotSimulatorTest extends FunSuite with Matchers {
5+
6+
test(
7+
"A robot is created with a position and a direction - Robots are created with a position and direction") {
8+
Robot(Bearing.North, (0, 0)) should be(Robot(Bearing.North, (0, 0)))
9+
}
10+
11+
test(
12+
"A robot is created with a position and a direction - Negative positions are allowed") {
13+
pending
14+
Robot(Bearing.South, (-1, -1)) should be(Robot(Bearing.South, (-1, -1)))
15+
}
16+
17+
test(
18+
"rotates the robot's direction 90 degrees clockwise - does not change the position") {
19+
pending
20+
Robot(Bearing.North, (0, 0)).turnRight.coordinates should be((0, 0))
21+
}
22+
23+
test(
24+
"rotates the robot's direction 90 degrees clockwise - changes the direction from north to east") {
25+
pending
26+
Robot(Bearing.North, (0, 0)).turnRight.bearing should be(Bearing.East)
27+
}
28+
29+
test(
30+
"rotates the robot's direction 90 degrees clockwise - changes the direction from east to south") {
31+
pending
32+
Robot(Bearing.East, (0, 0)).turnRight.bearing should be(Bearing.South)
33+
}
34+
35+
test(
36+
"rotates the robot's direction 90 degrees clockwise - changes the direction from south to west") {
37+
pending
38+
Robot(Bearing.South, (0, 0)).turnRight.bearing should be(Bearing.West)
39+
}
40+
41+
test(
42+
"rotates the robot's direction 90 degrees clockwise - changes the direction from west to north") {
43+
pending
44+
Robot(Bearing.West, (0, 0)).turnRight.bearing should be(Bearing.North)
45+
}
46+
47+
test(
48+
"rotates the robot's direction 90 degrees counter-clockwise - does not change the position") {
49+
pending
50+
Robot(Bearing.North, (0, 0)).turnLeft.coordinates should be((0, 0))
51+
}
52+
53+
test(
54+
"rotates the robot's direction 90 degrees counter-clockwise - changes the direction from north to west") {
55+
pending
56+
Robot(Bearing.North, (0, 0)).turnLeft.bearing should be(Bearing.West)
57+
}
58+
59+
test(
60+
"rotates the robot's direction 90 degrees counter-clockwise - changes the direction from west to south") {
61+
pending
62+
Robot(Bearing.West, (0, 0)).turnLeft.bearing should be(Bearing.South)
63+
}
64+
65+
test(
66+
"rotates the robot's direction 90 degrees counter-clockwise - changes the direction from south to east") {
67+
pending
68+
Robot(Bearing.South, (0, 0)).turnLeft.bearing should be(Bearing.East)
69+
}
70+
71+
test(
72+
"rotates the robot's direction 90 degrees counter-clockwise - changes the direction from east to north") {
73+
pending
74+
Robot(Bearing.East, (0, 0)).turnLeft.bearing should be(Bearing.North)
75+
}
76+
77+
test(
78+
"moves the robot forward 1 space in the direction it is pointing - does not change the direction") {
79+
pending
80+
Robot(Bearing.North, (0, 0)).advance.bearing should be(Bearing.North)
81+
}
82+
83+
test(
84+
"moves the robot forward 1 space in the direction it is pointing - increases the y coordinate one when facing north") {
85+
pending
86+
Robot(Bearing.North, (0, 0)).advance.coordinates should be((0, 1))
87+
}
88+
89+
test(
90+
"moves the robot forward 1 space in the direction it is pointing - decreases the y coordinate by one when facing south") {
91+
pending
92+
Robot(Bearing.South, (0, 0)).advance.coordinates should be((0, -1))
93+
}
94+
95+
test(
96+
"moves the robot forward 1 space in the direction it is pointing - increases the x coordinate by one when facing east") {
97+
pending
98+
Robot(Bearing.East, (0, 0)).advance.coordinates should be((1, 0))
99+
}
100+
101+
test(
102+
"moves the robot forward 1 space in the direction it is pointing - decreases the x coordinate by one when facing west") {
103+
pending
104+
Robot(Bearing.West, (0, 0)).advance.coordinates should be((-1, 0))
105+
}
106+
107+
test(
108+
"Where R = Turn Right, L = Turn Left and A = Advance, the robot can follow a series of instructions and end up with the correct position and direction - instructions to move west and north") {
109+
pending
110+
Robot(Bearing.North, (0, 0)).simulate("LAAARALA") should be(
111+
Robot(Bearing.West, (-4, 1)))
112+
}
113+
114+
test(
115+
"Where R = Turn Right, L = Turn Left and A = Advance, the robot can follow a series of instructions and end up with the correct position and direction - instructions to move west and south") {
116+
pending
117+
Robot(Bearing.East, (2, -7)).simulate("RRAAAAALA") should be(
118+
Robot(Bearing.South, (-3, -8)))
119+
}
120+
121+
test(
122+
"Where R = Turn Right, L = Turn Left and A = Advance, the robot can follow a series of instructions and end up with the correct position and direction - instructions to move east and north") {
123+
pending
124+
Robot(Bearing.South, (8, 4)).simulate("LAAARRRALLLL") should be(
125+
Robot(Bearing.North, (11, 5)))
126+
}
127+
}

exercises/robot-simulator/src/test/scala/RobotTest.scala

Lines changed: 0 additions & 61 deletions
This file was deleted.
Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
import java.io.File
2+
3+
import testgen.TestSuiteBuilder._
4+
import testgen._
5+
6+
object RobotSimulatorTestGenerator {
7+
def main(args: Array[String]): Unit = {
8+
val file = new File("src/main/resources/robot-simulator.json")
9+
10+
def getPositionArgs(robotMap: Map[String, String]): String =
11+
robotMap("position").stripPrefix("(").stripSuffix(")")
12+
13+
def getRobotMap(labeledTest: LabeledTest): Map[String, String] = {
14+
labeledTest.result("robot").
15+
asInstanceOf[Map[String, String]]
16+
}
17+
18+
def getExpectedMap(labeledTest: LabeledTest): Map[String, String] = {
19+
labeledTest.result("expected").
20+
asInstanceOf[Map[String, String]]
21+
}
22+
23+
def directionToBearing(direction: String): String =
24+
direction.toLowerCase match {
25+
case "north" => "Bearing.North"
26+
case "south" => "Bearing.South"
27+
case "east" => "Bearing.East"
28+
case "west" => "Bearing.West"
29+
}
30+
31+
def toCreateSutCall(labeledTest: LabeledTest): String = {
32+
val robotMap = getRobotMap(labeledTest)
33+
val bearing = directionToBearing(robotMap("direction"))
34+
s"Robot($bearing, (${getPositionArgs(robotMap)}))"
35+
}
36+
37+
def toCreateExpected(labeledTest: LabeledTest): String = {
38+
val robotMap = getExpectedMap(labeledTest)
39+
val bearing = directionToBearing(robotMap("direction"))
40+
s"Robot($bearing, (${getPositionArgs(robotMap)}))"
41+
}
42+
43+
def toTurnFunction(map: Map[String, String]) =
44+
map.get("direction") match {
45+
case Some(_) => "bearing"
46+
case None => "coordinates"
47+
}
48+
49+
def toTurnSutCall(labeledTest: LabeledTest): String = {
50+
val property = labeledTest.property
51+
val expected = getExpectedMap(labeledTest)
52+
s"${toCreateSutCall(labeledTest)}.$property.${toTurnFunction(expected)}"
53+
}
54+
55+
def toTurnExpected(labeledTest: LabeledTest): String = {
56+
val expected = getExpectedMap(labeledTest)
57+
expected.get("direction") match {
58+
case Some(s) => directionToBearing(s)
59+
case None => s"(${getPositionArgs(expected)})"
60+
}
61+
}
62+
63+
def toInstructSutCall(labeledTest: LabeledTest): String = {
64+
val property = labeledTest.property
65+
val instructions = labeledTest.result("instructions")
66+
s"""${toCreateSutCall(labeledTest)}.simulate("$instructions")"""
67+
}
68+
69+
def toInstructExpected(labeledTest: LabeledTest): String =
70+
toCreateExpected(labeledTest)
71+
72+
def fromLabeledTest(argNames: String*): ToTestCaseData =
73+
withLabeledTest { sut =>
74+
labeledTest =>
75+
val property = labeledTest.property
76+
val (sutCall, expected) = property match {
77+
case "create" =>
78+
(toCreateSutCall(labeledTest),
79+
toCreateExpected(labeledTest))
80+
case "turnRight" | "turnLeft" | "advance" =>
81+
(toTurnSutCall(labeledTest),
82+
toTurnExpected(labeledTest))
83+
case "instructions" =>
84+
(toInstructSutCall(labeledTest),
85+
toInstructExpected(labeledTest))
86+
case _ => throw new IllegalStateException()
87+
}
88+
TestCaseData(labeledTest.parentDescriptions.mkString(" - ") + " - " + labeledTest.description,
89+
sutCall, expected)
90+
}
91+
92+
val code =
93+
TestSuiteBuilder.build(file, fromLabeledTest())
94+
95+
println(s"-------------")
96+
println(code)
97+
println(s"-------------")
98+
}
99+
}

0 commit comments

Comments
 (0)