From b00a2649cb01c2e7c4346ac54ce4662104941c76 Mon Sep 17 00:00:00 2001 From: Ric Emery Date: Sun, 1 Oct 2017 13:42:51 -0700 Subject: [PATCH] Create RobotSimulatorTestGenerator.scala. Update RobotSimulatorTest.scala. Refs #370, #331 --- .../src/test/scala/RobotSimulatorTest.scala | 127 ++++++++++++++++++ .../src/test/scala/RobotTest.scala | 61 --------- .../scala/RobotSimulatorTestGenerator.scala | 99 ++++++++++++++ 3 files changed, 226 insertions(+), 61 deletions(-) create mode 100644 exercises/robot-simulator/src/test/scala/RobotSimulatorTest.scala delete mode 100644 exercises/robot-simulator/src/test/scala/RobotTest.scala create mode 100644 testgen/src/main/scala/RobotSimulatorTestGenerator.scala diff --git a/exercises/robot-simulator/src/test/scala/RobotSimulatorTest.scala b/exercises/robot-simulator/src/test/scala/RobotSimulatorTest.scala new file mode 100644 index 00000000..c55b8028 --- /dev/null +++ b/exercises/robot-simulator/src/test/scala/RobotSimulatorTest.scala @@ -0,0 +1,127 @@ +import org.scalatest.{Matchers, FunSuite} + +/** @version 1.0.0 */ +class RobotSimulatorTest extends FunSuite with Matchers { + + test( + "A robot is created with a position and a direction - Robots are created with a position and direction") { + Robot(Bearing.North, (0, 0)) should be(Robot(Bearing.North, (0, 0))) + } + + test( + "A robot is created with a position and a direction - Negative positions are allowed") { + pending + Robot(Bearing.South, (-1, -1)) should be(Robot(Bearing.South, (-1, -1))) + } + + test( + "rotates the robot's direction 90 degrees clockwise - does not change the position") { + pending + Robot(Bearing.North, (0, 0)).turnRight.coordinates should be((0, 0)) + } + + test( + "rotates the robot's direction 90 degrees clockwise - changes the direction from north to east") { + pending + Robot(Bearing.North, (0, 0)).turnRight.bearing should be(Bearing.East) + } + + test( + "rotates the robot's direction 90 degrees clockwise - changes the direction from east to south") { + pending + Robot(Bearing.East, (0, 0)).turnRight.bearing should be(Bearing.South) + } + + test( + "rotates the robot's direction 90 degrees clockwise - changes the direction from south to west") { + pending + Robot(Bearing.South, (0, 0)).turnRight.bearing should be(Bearing.West) + } + + test( + "rotates the robot's direction 90 degrees clockwise - changes the direction from west to north") { + pending + Robot(Bearing.West, (0, 0)).turnRight.bearing should be(Bearing.North) + } + + test( + "rotates the robot's direction 90 degrees counter-clockwise - does not change the position") { + pending + Robot(Bearing.North, (0, 0)).turnLeft.coordinates should be((0, 0)) + } + + test( + "rotates the robot's direction 90 degrees counter-clockwise - changes the direction from north to west") { + pending + Robot(Bearing.North, (0, 0)).turnLeft.bearing should be(Bearing.West) + } + + test( + "rotates the robot's direction 90 degrees counter-clockwise - changes the direction from west to south") { + pending + Robot(Bearing.West, (0, 0)).turnLeft.bearing should be(Bearing.South) + } + + test( + "rotates the robot's direction 90 degrees counter-clockwise - changes the direction from south to east") { + pending + Robot(Bearing.South, (0, 0)).turnLeft.bearing should be(Bearing.East) + } + + test( + "rotates the robot's direction 90 degrees counter-clockwise - changes the direction from east to north") { + pending + Robot(Bearing.East, (0, 0)).turnLeft.bearing should be(Bearing.North) + } + + test( + "moves the robot forward 1 space in the direction it is pointing - does not change the direction") { + pending + Robot(Bearing.North, (0, 0)).advance.bearing should be(Bearing.North) + } + + test( + "moves the robot forward 1 space in the direction it is pointing - increases the y coordinate one when facing north") { + pending + Robot(Bearing.North, (0, 0)).advance.coordinates should be((0, 1)) + } + + test( + "moves the robot forward 1 space in the direction it is pointing - decreases the y coordinate by one when facing south") { + pending + Robot(Bearing.South, (0, 0)).advance.coordinates should be((0, -1)) + } + + test( + "moves the robot forward 1 space in the direction it is pointing - increases the x coordinate by one when facing east") { + pending + Robot(Bearing.East, (0, 0)).advance.coordinates should be((1, 0)) + } + + test( + "moves the robot forward 1 space in the direction it is pointing - decreases the x coordinate by one when facing west") { + pending + Robot(Bearing.West, (0, 0)).advance.coordinates should be((-1, 0)) + } + + test( + "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") { + pending + Robot(Bearing.North, (0, 0)).simulate("LAAARALA") should be( + Robot(Bearing.West, (-4, 1))) + } + + test( + "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") { + pending + Robot(Bearing.East, (2, -7)).simulate("RRAAAAALA") should be( + Robot(Bearing.South, (-3, -8))) + } + + test( + "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") { + pending + Robot(Bearing.South, (8, 4)).simulate("LAAARRRALLLL") should be( + Robot(Bearing.North, (11, 5))) + } +} diff --git a/exercises/robot-simulator/src/test/scala/RobotTest.scala b/exercises/robot-simulator/src/test/scala/RobotTest.scala deleted file mode 100644 index 934ece04..00000000 --- a/exercises/robot-simulator/src/test/scala/RobotTest.scala +++ /dev/null @@ -1,61 +0,0 @@ -import org.scalatest.{Matchers, FunSuite} - -class RobotTest extends FunSuite with Matchers { - test("create") { - val robot = Robot(Bearing.North, (0, 0)) - robot.bearing should equal(Bearing.North) - robot.coordinates should equal((0, 0)) - } - - test("advance - positive") { - pending - Robot(Bearing.North, (0, 0)).advance should equal(Robot(Bearing.North, (0, 1))) - Robot(Bearing.East, (0, 0)).advance should equal(Robot(Bearing.East, (1, 0))) - } - - test("advance - negative") { - pending - Robot(Bearing.South, (0, 0)).advance should equal(Robot(Bearing.South, (0, -1))) - Robot(Bearing.West, (0, 0)).advance should equal(Robot(Bearing.West, (-1, 0))) - } - - test("turning") { - pending - Robot(Bearing.South, (0, 0)).turnRight should equal(Robot(Bearing.West, (0, 0))) - Robot(Bearing.West, (0, 0)).turnLeft() should equal(Robot(Bearing.South, (0, 0))) - } - - test("turning - edge cases") { - pending - Robot(Bearing.West, (0, 0)).turnRight should equal(Robot(Bearing.North, (0, 0))) - Robot(Bearing.North, (0, 0)).turnLeft should equal(Robot(Bearing.West, (0, 0))) - } - - test("simulate Seurat") { - pending - val seurat = Robot(Bearing.East, (-2, 1)) - val movedSeurat = seurat.simulate("RLAALAL") - movedSeurat should equal(Robot(Bearing.West, (0, 2))) - } - - test("simulate Erasmus") { - pending - val erasmus = Robot(Bearing.North, (0, 0)) - val movedErasmus = erasmus.simulate("LAAARALA") - movedErasmus should equal(Robot(Bearing.West, (-4, 1))) - } - - test("simulate Chirox") { - pending - val chirox = Robot(Bearing.East, (2, -7)) - val movedChirox = chirox.simulate("RRAAAAALA") - movedChirox should equal(Robot(Bearing.South, (-3, -8))) - } - - test("simulate Awesomo") { - pending - val awesomo = Robot(Bearing.South, (8, 4)) - val movedAwesomo = awesomo.simulate("LAAARRRALLLL") - movedAwesomo should equal(Robot(Bearing.North, (11, 5))) - } -} diff --git a/testgen/src/main/scala/RobotSimulatorTestGenerator.scala b/testgen/src/main/scala/RobotSimulatorTestGenerator.scala new file mode 100644 index 00000000..5505e759 --- /dev/null +++ b/testgen/src/main/scala/RobotSimulatorTestGenerator.scala @@ -0,0 +1,99 @@ +import java.io.File + +import testgen.TestSuiteBuilder._ +import testgen._ + +object RobotSimulatorTestGenerator { + def main(args: Array[String]): Unit = { + val file = new File("src/main/resources/robot-simulator.json") + + def getPositionArgs(robotMap: Map[String, String]): String = + robotMap("position").stripPrefix("(").stripSuffix(")") + + def getRobotMap(labeledTest: LabeledTest): Map[String, String] = { + labeledTest.result("robot"). + asInstanceOf[Map[String, String]] + } + + def getExpectedMap(labeledTest: LabeledTest): Map[String, String] = { + labeledTest.result("expected"). + asInstanceOf[Map[String, String]] + } + + def directionToBearing(direction: String): String = + direction.toLowerCase match { + case "north" => "Bearing.North" + case "south" => "Bearing.South" + case "east" => "Bearing.East" + case "west" => "Bearing.West" + } + + def toCreateSutCall(labeledTest: LabeledTest): String = { + val robotMap = getRobotMap(labeledTest) + val bearing = directionToBearing(robotMap("direction")) + s"Robot($bearing, (${getPositionArgs(robotMap)}))" + } + + def toCreateExpected(labeledTest: LabeledTest): String = { + val robotMap = getExpectedMap(labeledTest) + val bearing = directionToBearing(robotMap("direction")) + s"Robot($bearing, (${getPositionArgs(robotMap)}))" + } + + def toTurnFunction(map: Map[String, String]) = + map.get("direction") match { + case Some(_) => "bearing" + case None => "coordinates" + } + + def toTurnSutCall(labeledTest: LabeledTest): String = { + val property = labeledTest.property + val expected = getExpectedMap(labeledTest) + s"${toCreateSutCall(labeledTest)}.$property.${toTurnFunction(expected)}" + } + + def toTurnExpected(labeledTest: LabeledTest): String = { + val expected = getExpectedMap(labeledTest) + expected.get("direction") match { + case Some(s) => directionToBearing(s) + case None => s"(${getPositionArgs(expected)})" + } + } + + def toInstructSutCall(labeledTest: LabeledTest): String = { + val property = labeledTest.property + val instructions = labeledTest.result("instructions") + s"""${toCreateSutCall(labeledTest)}.simulate("$instructions")""" + } + + def toInstructExpected(labeledTest: LabeledTest): String = + toCreateExpected(labeledTest) + + def fromLabeledTest(argNames: String*): ToTestCaseData = + withLabeledTest { sut => + labeledTest => + val property = labeledTest.property + val (sutCall, expected) = property match { + case "create" => + (toCreateSutCall(labeledTest), + toCreateExpected(labeledTest)) + case "turnRight" | "turnLeft" | "advance" => + (toTurnSutCall(labeledTest), + toTurnExpected(labeledTest)) + case "instructions" => + (toInstructSutCall(labeledTest), + toInstructExpected(labeledTest)) + case _ => throw new IllegalStateException() + } + TestCaseData(labeledTest.parentDescriptions.mkString(" - ") + " - " + labeledTest.description, + sutCall, expected) + } + + val code = + TestSuiteBuilder.build(file, fromLabeledTest()) + + println(s"-------------") + println(code) + println(s"-------------") + } +}