Skip to content

Commit

Permalink
Make sure void is idempotent (#297)
Browse files Browse the repository at this point in the history
  • Loading branch information
johnynek committed Nov 10, 2021
1 parent eea17fa commit 714a9df
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 2 deletions.
10 changes: 8 additions & 2 deletions core/shared/src/main/scala/cats/parse/Parser.scala
Original file line number Diff line number Diff line change
Expand Up @@ -1968,7 +1968,10 @@ object Parser {
else SoftProd0(u1, u2)
}
case Defer0(fn) =>
Defer0(UnmapDefer0(fn))
fn match {
case UnmapDefer0(_) => pa // already unmapped
case _ => Defer0(UnmapDefer0(fn))
}
case Rep0(p, max, _) => Rep0(unmap(p), max, Accumulator0.unitAccumulator0)
case WithContextP0(ctx, p0) => WithContextP0(ctx, unmap0(p0))
case StartParser | EndParser | TailRecM0(_, _) | FlatMap0(_, _) =>
Expand Down Expand Up @@ -2049,7 +2052,10 @@ object Parser {
else SoftProd(u1, u2)
}
case Defer(fn) =>
Defer(UnmapDefer(fn))
fn match {
case UnmapDefer(_) => pa // already unmapped
case _ => Defer(UnmapDefer(fn))
}
case Rep(p, min, max, _) => Rep(unmap(p), min, max, Accumulator0.unitAccumulator0)
case WithContextP(ctx, p) =>
WithContextP(ctx, unmap(p))
Expand Down
14 changes: 14 additions & 0 deletions core/shared/src/test/scala/cats/parse/ParserTest.scala
Original file line number Diff line number Diff line change
Expand Up @@ -2446,4 +2446,18 @@ class ParserTest extends munit.ScalaCheckSuite {
assertEquals(Parser.charWhere(chars).parse(input), Parser.charIn(chars).parse(input))
}
}

property("P0.void is idempotent") {
forAll(ParserGen.gen0) { p =>
val v1 = p.fa.void
assertEquals(v1.void, v1)
}
}

property("P.void is idempotent") {
forAll(ParserGen.gen) { p =>
val v1 = p.fa.void
assertEquals(v1.void, v1)
}
}
}

0 comments on commit 714a9df

Please sign in to comment.