From f9497ee193477d57e089ff133596ae38e16f2b44 Mon Sep 17 00:00:00 2001 From: Thanh Le Date: Mon, 28 Oct 2024 08:58:14 +0100 Subject: [PATCH 1/3] Rename pgn to macros --- test-kit/src/main/scala/chess/{pgn.scala => macros.scala} | 2 +- test-kit/src/test/scala/format/pgn/MacrosTest.scala | 1 - 2 files changed, 1 insertion(+), 2 deletions(-) rename test-kit/src/main/scala/chess/{pgn.scala => macros.scala} (94%) diff --git a/test-kit/src/main/scala/chess/pgn.scala b/test-kit/src/main/scala/chess/macros.scala similarity index 94% rename from test-kit/src/main/scala/chess/pgn.scala rename to test-kit/src/main/scala/chess/macros.scala index 1bb45349..452f197a 100644 --- a/test-kit/src/main/scala/chess/pgn.scala +++ b/test-kit/src/main/scala/chess/macros.scala @@ -1,8 +1,8 @@ package chess -package format.pgn import cats.syntax.all.* import org.typelevel.literally.Literally +import chess.format.pgn.* object macros: extension (inline ctx: StringContext) diff --git a/test-kit/src/test/scala/format/pgn/MacrosTest.scala b/test-kit/src/test/scala/format/pgn/MacrosTest.scala index c9bc4e0f..428b58fc 100644 --- a/test-kit/src/test/scala/format/pgn/MacrosTest.scala +++ b/test-kit/src/test/scala/format/pgn/MacrosTest.scala @@ -1,5 +1,4 @@ package chess -package format.pgn class MacrosTest extends munit.FunSuite: From 634af22bada520687d095438bfc7f1e609709849 Mon Sep 17 00:00:00 2001 From: Thanh Le Date: Mon, 28 Oct 2024 09:01:33 +0100 Subject: [PATCH 2/3] Add literal uci --- test-kit/src/main/scala/chess/macros.scala | 13 ++++++++++++- test-kit/src/test/scala/format/pgn/MacrosTest.scala | 8 +++++++- 2 files changed, 19 insertions(+), 2 deletions(-) diff --git a/test-kit/src/main/scala/chess/macros.scala b/test-kit/src/main/scala/chess/macros.scala index 452f197a..a30ee3eb 100644 --- a/test-kit/src/main/scala/chess/macros.scala +++ b/test-kit/src/main/scala/chess/macros.scala @@ -1,16 +1,27 @@ package chess import cats.syntax.all.* -import org.typelevel.literally.Literally +import chess.format.Uci import chess.format.pgn.* +import org.typelevel.literally.Literally object macros: extension (inline ctx: StringContext) + inline def pgn(inline args: Any*): ParsedPgn = ${ PgnLiteral('ctx, 'args) } + inline def uci(inline args: Any*): Uci = + ${ UciLiteral('ctx, 'args) } + object PgnLiteral extends Literally[ParsedPgn]: def validate(s: String)(using Quotes) = Parser.full(PgnStr(s)) match case Right(parsed) => Right('{ Parser.full(PgnStr(${ Expr(s) })).toOption.get }) case Left(err) => Left(err.toString) + + object UciLiteral extends Literally[Uci]: + def validate(s: String)(using Quotes) = + Uci(s) match + case Some(_) => Right('{ Uci(${ Expr(s) }).get }) + case _ => Left(s"Invalid UCI: $s") diff --git a/test-kit/src/test/scala/format/pgn/MacrosTest.scala b/test-kit/src/test/scala/format/pgn/MacrosTest.scala index 428b58fc..6d55534c 100644 --- a/test-kit/src/test/scala/format/pgn/MacrosTest.scala +++ b/test-kit/src/test/scala/format/pgn/MacrosTest.scala @@ -1,9 +1,15 @@ package chess +import macros.* + class MacrosTest extends munit.FunSuite: - import macros.* test("pgn macro"): val pgn = pgn"1. e4 e5 2. Nf3 Nc6" assert(pgn.tree.isDefined) assertEquals(pgn.toPgn.toString, "1. e4 e5 2. Nf3 Nc6") + + test("uci macro"): + val uci = uci"d2d4" + assert(uci.isInstanceOf[chess.format.Uci.Move]) + assertEquals(uci.uci, "d2d4") From a0cdf7941f02b67f8e6216981602630261682260 Mon Sep 17 00:00:00 2001 From: Thanh Le Date: Mon, 28 Oct 2024 09:16:51 +0100 Subject: [PATCH 3/3] Apply literal uci --- test-kit/src/test/scala/ReplayTest.scala | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/test-kit/src/test/scala/ReplayTest.scala b/test-kit/src/test/scala/ReplayTest.scala index 7a214ac4..b8709291 100644 --- a/test-kit/src/test/scala/ReplayTest.scala +++ b/test-kit/src/test/scala/ReplayTest.scala @@ -5,6 +5,7 @@ import chess.format.pgn.{ Fixtures, SanStr } import chess.variant.Chess960 import format.{ FullFen, Fen, Uci } +import macros.uci class ReplayTest extends ChessTest: @@ -27,7 +28,7 @@ class ReplayTest extends ChessTest: test("bongcloud attack"): Replay .situationsFromUci( - moves = List(Uci("e2e4"), Uci("e7e5"), Uci("e1e2")).flatten, + moves = List(uci"e2e4", uci"e7e5", uci"e1e2"), initialFen = None, variant = variant.Standard )