Skip to content

Commit a91f478

Browse files
committed
Create QueenAttackTestGenerator.scala. Update QueenAttackTest.scala. Refs exercism#370, exercism#331
1 parent 6b5eb7d commit a91f478

File tree

5 files changed

+164
-85
lines changed

5 files changed

+164
-85
lines changed

exercises/queen-attack/example.scala

Lines changed: 14 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,5 @@
1-
object Queens {
2-
3-
def boardString(white: Option[Position], black: Option[Position]): String = {
4-
5-
def rowString(row: Int): String =
6-
(0 until 8).flatMap(col => tileString(row, col)).mkString
7-
8-
def tileString(row: Int, col: Int): String =
9-
tileChar(Position(row, col)).toString + tileSep(col)
10-
11-
def tileChar(pos: Position): Char =
12-
Some(pos) match {
13-
case `white` => 'W'
14-
case `black` => 'B'
15-
case _ => '_'
16-
}
17-
18-
def tileSep(col: Int) =
19-
if (col == 7) '\n'
20-
else ' '
21-
22-
(0 until 8).flatMap(row => rowString(row)).mkString
23-
}
24-
25-
def canAttack(white: Position, black: Position): Boolean = {
1+
object QueenAttack {
2+
def canAttack(white: Queen, black: Queen): Boolean = {
263
val canAttackHoriz = white.x == black.x
274
val canAttackVert = white.y == black.y
285
val deltaRow = Math.abs(white.x - black.x)
@@ -33,5 +10,16 @@ object Queens {
3310
}
3411
}
3512

36-
case class Position(x: Int, y: Int)
13+
case class Queen(x: Int, y: Int)
14+
15+
object Queen {
16+
def create(x: Int, y: Int): Option[Queen] = {
17+
val min = 0
18+
val max = 7
19+
if (x >= min && x <= max && y >= min && y <= max)
20+
Some(Queen(x, y))
21+
else
22+
None
23+
}
24+
}
3725

exercises/queen-attack/src/main/scala/Queens.scala

Lines changed: 0 additions & 8 deletions
This file was deleted.
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
import org.scalatest.{Matchers, FunSuite}
2+
3+
/** @version 1.0.0 */
4+
class QueenAttackTest extends FunSuite with Matchers {
5+
6+
private def create(x: Int, y: Int): Queen = {
7+
Queen.create(x, y) match {
8+
case Some(q) => q
9+
case None => fail("Error creating queen")
10+
}
11+
}
12+
13+
test("queen with a valid position") {
14+
Queen.create(2,2) should be (Some(Queen(2,2)))
15+
}
16+
17+
test("queen must have positive rank") {
18+
pending
19+
Queen.create(-2,2) should be (None)
20+
}
21+
22+
test("queen must have rank on board") {
23+
pending
24+
Queen.create(8,4) should be (None)
25+
}
26+
27+
test("queen must have positive file") {
28+
pending
29+
Queen.create(2,-2) should be (None)
30+
}
31+
32+
test("queen must have file on board") {
33+
pending
34+
Queen.create(4,8) should be (None)
35+
}
36+
37+
test("can not attack") {
38+
pending
39+
QueenAttack.canAttack(create(2,4), create(6,6)) should be (false)
40+
}
41+
42+
test("can attack on same rank") {
43+
pending
44+
QueenAttack.canAttack(create(2,4), create(2,6)) should be (true)
45+
}
46+
47+
test("can attack on same file") {
48+
pending
49+
QueenAttack.canAttack(create(4,5), create(2,5)) should be (true)
50+
}
51+
52+
test("can attack on first diagonal") {
53+
pending
54+
QueenAttack.canAttack(create(2,2), create(0,4)) should be (true)
55+
}
56+
57+
test("can attack on second diagonal") {
58+
pending
59+
QueenAttack.canAttack(create(2,2), create(3,1)) should be (true)
60+
}
61+
62+
test("can attack on third diagonal") {
63+
pending
64+
QueenAttack.canAttack(create(2,2), create(1,1)) should be (true)
65+
}
66+
67+
test("can attack on fourth diagonal") {
68+
pending
69+
QueenAttack.canAttack(create(2,2), create(5,5)) should be (true)
70+
}
71+
}

exercises/queen-attack/src/test/scala/QueensTest.scala

Lines changed: 0 additions & 51 deletions
This file was deleted.
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
import java.io.File
2+
3+
import testgen.TestSuiteBuilder._
4+
import testgen._
5+
6+
object QueenAttackTestGenerator {
7+
def main(args: Array[String]): Unit = {
8+
val file = new File("src/main/resources/queen-attack.json")
9+
10+
def getPositionArgs(queenMap: Map[String, String]): String =
11+
queenMap("position").stripPrefix("(").stripSuffix(")")
12+
13+
def getQueen(labeledTest: LabeledTest): Map[String, String] = {
14+
labeledTest.result("queen").
15+
asInstanceOf[Map[String, String]]
16+
}
17+
18+
def getWhiteQueen(labeledTest: LabeledTest): Map[String, String] = {
19+
labeledTest.result("white_queen").
20+
asInstanceOf[Map[String, String]]
21+
}
22+
23+
def getBlackQueen(labeledTest: LabeledTest): Map[String, String] = {
24+
labeledTest.result("black_queen").
25+
asInstanceOf[Map[String, String]]
26+
}
27+
28+
def getCanAttackArgs(labeledTest: LabeledTest): String = {
29+
val whiteQueenArgs = getPositionArgs(getWhiteQueen(labeledTest))
30+
val blackQueenArgs = getPositionArgs(getBlackQueen(labeledTest))
31+
s"create($whiteQueenArgs), create($blackQueenArgs)"
32+
}
33+
34+
def toCreateExpected(labeledTest: LabeledTest): String = {
35+
val expected = labeledTest.expected
36+
expected match {
37+
case Right(-1) => s"None"
38+
case Right(0) =>
39+
val args = getPositionArgs(getQueen(labeledTest))
40+
s"Some(Queen($args))"
41+
case _ => throw new IllegalStateException
42+
}
43+
}
44+
45+
46+
def fromLabeledTest(argNames: String*): ToTestCaseData =
47+
withLabeledTest { sut =>
48+
labeledTest =>
49+
val property = labeledTest.property
50+
val (clazz, args, expected) = property match {
51+
case "create" =>
52+
("Queen",
53+
getPositionArgs(getQueen(labeledTest)),
54+
toCreateExpected(labeledTest))
55+
case "canAttack" =>
56+
("QueenAttack",
57+
getCanAttackArgs(labeledTest),
58+
labeledTest.expected.fold(_ => "false", _.toString))
59+
case _ => throw new IllegalStateException()
60+
}
61+
val sutCall = s"""$clazz.$property($args)"""
62+
TestCaseData(labeledTest.description, sutCall, expected)
63+
}
64+
65+
val code =
66+
TestSuiteBuilder.build(file, fromLabeledTest(),
67+
Seq(),
68+
Seq(" private def create(x: Int, y: Int): Queen = {",
69+
" Queen.create(x, y) match {",
70+
" case Some(q) => q",
71+
" case None => fail(\"Error creating queen\")",
72+
" }",
73+
" }"))
74+
75+
println(s"-------------")
76+
println(code)
77+
println(s"-------------")
78+
}
79+
}

0 commit comments

Comments
 (0)