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

Make sure void is idempotent #297

Merged
merged 1 commit into from
Nov 10, 2021
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
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)
}
}
}