Skip to content
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

Implement tests for game.Event.PossibleMoves #15710

Merged
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
2 changes: 1 addition & 1 deletion build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,7 @@ lazy val user = module("user",

lazy val game = module("game",
Seq(tree, rating, memo),
Seq(compression) ++ tests.bundle
Seq(compression) ++ tests.bundle ++ Seq(scalacheck, munitCheck, chess.testKit)
)

lazy val gameSearch = module("gameSearch",
Expand Down
1 change: 1 addition & 0 deletions modules/game/src/main/Event.scala
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,7 @@ object Event:
)

object PossibleMoves:
// We need to keep this implementation for compatibility
def json(moves: Map[Square, Bitboard]): JsValue =
if moves.isEmpty then JsNull
else
Expand Down
34 changes: 34 additions & 0 deletions modules/game/src/test/EventTest.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package lila.game

import chess.*
import chess.bitboard.Bitboard

import cats.syntax.all.*
import chess.CoreArbitraries.given
import org.scalacheck.Prop.{ forAll, propBoolean }
import play.api.libs.json.*

class EventTest extends munit.ScalaCheckSuite:

test("PossibleMoves anti regression"):
val moves = Map(Square.E2 -> Bitboard(List(Square.E4, Square.D4, Square.E3)))
val str = stringFromPossibleMoves(moves)
// If We somehow change the order of destinations, We may need to update this test
assertEquals(str, "e2e3d4e4")

test("PossibleMoves with empty map"):
assertEquals(Event.PossibleMoves.json(Map.empty), JsNull)

test("PossibleMoves writes every square"):
forAll: (m: Map[Square, Bitboard]) =>
m.nonEmpty ==> {
val str = stringFromPossibleMoves(m)
val totalSquares = m.values.foldLeft(0)(_ + _.count) + m.size
// str length = total squares * 2 + spaces in between
assertEquals(str.length, totalSquares * 2 + m.size - 1)
}

private def stringFromPossibleMoves(moves: Map[Square, Bitboard]): String =
Event.PossibleMoves.json(moves) match
case JsString(str) => str
case _ => failSuite("Expected JsString")