Skip to content

Commit

Permalink
Add some StringIn1 laws
Browse files Browse the repository at this point in the history
  • Loading branch information
satabin committed Dec 17, 2020
1 parent f10a5a9 commit 039c285
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 1 deletion.
2 changes: 1 addition & 1 deletion core/shared/src/main/scala/cats/parse/Parser.scala
Original file line number Diff line number Diff line change
Expand Up @@ -858,7 +858,7 @@ object Parser extends ParserInstances {
* If no string matches, this parser results in an epsilon failure.
*/
def stringIn1(strings: List[String]): Parser1[Unit] =
strings match {
strings.distinct match {
case Nil => fail
case s :: Nil => string1(s)
case two => Impl.StringIn1(two)
Expand Down
33 changes: 33 additions & 0 deletions core/shared/src/test/scala/cats/parse/ParserTest.scala
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import org.scalacheck.Prop.forAll
import org.scalacheck.{Arbitrary, Gen, Cogen}

import cats.implicits._
import scala.util.Random

sealed abstract class GenT[F[_]] { self =>
type A
Expand Down Expand Up @@ -1799,4 +1800,36 @@ class ParserTest extends munit.ScalaCheckSuite {
assertEquals(left.parse(str), right.parse(str))
}
}

property("oneOf(string1(s)*) success => stringIn1(s*) success") {
forAll { (ss0: List[String], toParse: String) =>
val ss = ss0.filterNot(_.isEmpty)
val oneOfs = Parser.oneOf1(ss.map(Parser.string1))
val stringIn = Parser.stringIn1(ss)
if (oneOfs.parse(toParse).isRight) assert(stringIn.parse(toParse).isRight)
}
}

property("stringIn1(List(s)) == string1(s)") {
forAll { (s: String) =>
if (s.nonEmpty)
assertEquals(Parser.stringIn1(List(s)), Parser.string1(s))
}
}

property("stringIn1(List(s, s)) == string1(s)") {
forAll { (s: String) =>
if (s.nonEmpty)
assertEquals(Parser.stringIn1(List(s, s)), Parser.string1(s))
}
}

property("string1(s) matches => stringIn1(ss) matches if s in ss") {
forAll { (s: String, ss0: List[String], toParse: String) =>
val ss = ss0.filterNot(_.isEmpty)
val ss1 = Random.shuffle(s :: ss)
if (s.nonEmpty && Parser.string1(s).parse(toParse).isRight)
assert(Parser.stringIn1(ss1).parse(toParse).isRight)
}
}
}

0 comments on commit 039c285

Please sign in to comment.