Skip to content

Commit

Permalink
start rewriting specs2 tests into munit
Browse files Browse the repository at this point in the history
  • Loading branch information
ornicar committed Oct 15, 2023
1 parent 8975550 commit d426994
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 23 deletions.
20 changes: 20 additions & 0 deletions test-kit/src/test/scala/ChessTest.scala
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,26 @@ trait ChessTestCommon:

trait ChessTest extends munit.FunSuite with ChessTestCommon:

import alleycats.Zero

def assertMatch[A](a: A)(f: PartialFunction[A, Boolean]) =
assert(f.lift(a) | false, s"$a does not match expectations")

def assertCloseTo[T](a: T, b: T, delta: Double)(using n: Numeric[T]) =
assert(isCloseTo(a, b, delta), s"$a is not close to $b by $delta")

private def isCloseTo[T](a: T, b: T, delta: Double)(using n: Numeric[T]) =
(n.toDouble(a) - n.toDouble(b)).abs <= delta

extension [A](a: A)
def matchZero[B: Zero](f: PartialFunction[A, B]): B =
f.lift(a) | Zero[B].zero

extension [E, A](v: Either[E, A])
def assertRight(f: A => Any): Any = v match
case Right(r) => f(r)
case Left(e) => fail(s"Expected Right but received $v")

end ChessTest

trait ChessSpecs extends Specification with EitherMatchers with ChessTestCommon:
Expand Down
46 changes: 23 additions & 23 deletions test-kit/src/test/scala/HistoryTest.scala
Original file line number Diff line number Diff line change
@@ -1,27 +1,27 @@
package chess

class HistoryTest extends ChessSpecs:
class ThreefoldRepetitionTest extends ChessTest:

"threefold repetition" should:
def toHash(a: Int) = PositionHash(Array(a.toByte, 0.toByte, 0.toByte))
def makeHistory(positions: List[Int]) =
(positions map toHash).foldLeft(defaultHistory()) { (history, hash) =>
history.copy(positionHashes = hash.combine(history.positionHashes))
}
"empty history" in:
defaultHistory().threefoldRepetition must_== false
"not 3 same elements" in:
val history = makeHistory(List(1, 2, 3, 4, 5, 2, 5, 6, 23, 55))
history.threefoldRepetition must_== false
"not 3 elements same to the last one" in:
val history = makeHistory(List(1, 2, 3, 4, 5, 2, 5, 6, 23, 2, 55))
history.threefoldRepetition must_== false
"positive" in:
val history = makeHistory(List(1, 2, 3, 4, 5, 2, 5, 6, 23, 2))
history.threefoldRepetition must_== true
def toHash(a: Int) = PositionHash(Array(a.toByte, 0.toByte, 0.toByte))
def makeHistory(positions: List[Int]) =
(positions map toHash).foldLeft(defaultHistory()): (history, hash) =>
history.copy(positionHashes = hash.combine(history.positionHashes))

"set half move clock" should:
"set 0" in:
defaultHistory().setHalfMoveClock(HalfMoveClock.initial).halfMoveClock must_== HalfMoveClock(0)
"set 5" in:
defaultHistory().setHalfMoveClock(HalfMoveClock(5)).halfMoveClock must_== HalfMoveClock(5)
test("empty history"):
assert(!defaultHistory().threefoldRepetition)
test("not 3 same elements"):
val history = makeHistory(List(1, 2, 3, 4, 5, 2, 5, 6, 23, 55))
assert(!history.threefoldRepetition)
test("not 3 elements same to the last one"):
val history = makeHistory(List(1, 2, 3, 4, 5, 2, 5, 6, 23, 2, 55))
assert(!history.threefoldRepetition)
test("positive"):
val history = makeHistory(List(1, 2, 3, 4, 5, 2, 5, 6, 23, 2))
assert(!history.threefoldRepetition)

class HalfMoveClockTest extends ChessTest:

test("set 0"):
assertEquals(defaultHistory().setHalfMoveClock(HalfMoveClock.initial).halfMoveClock, HalfMoveClock(0))
test("set 5"):
assertEquals(defaultHistory().setHalfMoveClock(HalfMoveClock(5)).halfMoveClock, HalfMoveClock(5))

0 comments on commit d426994

Please sign in to comment.