Skip to content

Create RobotSimulatorTestGenerator.scala. Update RobotSimulatorTest.s… #462

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

Merged
merged 1 commit into from
Oct 2, 2017
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
127 changes: 127 additions & 0 deletions exercises/robot-simulator/src/test/scala/RobotSimulatorTest.scala
Original file line number Diff line number Diff line change
@@ -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)))
}
}
61 changes: 0 additions & 61 deletions exercises/robot-simulator/src/test/scala/RobotTest.scala

This file was deleted.

99 changes: 99 additions & 0 deletions testgen/src/main/scala/RobotSimulatorTestGenerator.scala
Original file line number Diff line number Diff line change
@@ -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"-------------")
}
}