Skip to content

Commit

Permalink
Merge pull request #590 from lenguyenthanh/literal-pgn
Browse files Browse the repository at this point in the history
Implement literal pgn
  • Loading branch information
ornicar authored Oct 27, 2024
2 parents ffbd182 + 8d6596c commit 99e0243
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 0 deletions.
1 change: 1 addition & 0 deletions build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ lazy val testKit = project
name := "scalachess-test-kit",
libraryDependencies ++= List(
"org.scalacheck" %% "scalacheck" % "1.18.1",
"org.typelevel" %% "literally" % "1.2.0",
"org.scalameta" %% "munit" % "1.0.2" % Test,
"org.scalameta" %% "munit-scalacheck" % "1.0.0" % Test,
"com.disneystreaming" %% "weaver-cats" % "0.8.4" % Test,
Expand Down
16 changes: 16 additions & 0 deletions test-kit/src/main/scala/chess/pgn.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package chess
package format.pgn

import cats.syntax.all.*
import org.typelevel.literally.Literally

object macros:
extension (inline ctx: StringContext)
inline def pgn(inline args: Any*): ParsedPgn =
${ PgnLiteral('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)
10 changes: 10 additions & 0 deletions test-kit/src/test/scala/format/pgn/MacrosTest.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package chess
package format.pgn

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")

0 comments on commit 99e0243

Please sign in to comment.