Skip to content

Commit

Permalink
master: scalacheck refinements
Browse files Browse the repository at this point in the history
  • Loading branch information
lperry committed May 27, 2017
1 parent 2dcfa26 commit f0106e1
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 45 deletions.
63 changes: 27 additions & 36 deletions src/test/scala/lp/template/testsupport/TestProperties.scala
@@ -1,34 +1,30 @@
package lp.template.testsupport

import org.scalacheck.Arbitrary.arbitrary
import org.scalacheck.{Arbitrary, Gen}
import org.scalacheck.Gen
import org.scalacheck.Gen._
import org.scalatest.FunSpec
import org.scalatest.prop.Checkers

object TestProperties {

object ArbitraryImplicits {
//implicit val arbNonEmptyAlphaPair: Arbitrary[(String, String)] = Arbitrary(genNonEmptyAlphaPair)
}

def genWord: Gen[String] = {
for {
c <- alphaUpperChar
cs <- resize(4, listOf(alphaLowerStr))
} yield (c :: cs).mkString
private def genAlphaLowerStrN(size: Int) = {
resize(size, listOf(alphaLowerStr))
}

def genTwoTermWord: Gen[String] = {
def genTerm: Gen[String] = {
for {
w0 <- genWord
w1 <- genWord
} yield w0 + w1
c <- alphaUpperChar
cs <- genAlphaLowerStrN(10)
} yield s"$c${cs.mkString}"
}

def genWordPlusLower: Gen[(String, String)] = {
def genTwoTerm: Gen[String] = {
for {
s <- genWord
} yield (s, s.toLowerCase)
w0 <- genTerm
w1 <- genTerm
} yield s"$w0$w1"
}

def genNonEmptyAlpha = {
Expand All @@ -37,37 +33,32 @@ object TestProperties {

def genNonEmptyAlphaPair: Gen[(String, String)] = {
for {
from <- "from" |: genNonEmptyAlpha
to <- "to" |: genNonEmptyAlpha
from <- genNonEmptyAlpha :| "from"
to <- genNonEmptyAlpha :| "to"
} yield (from, to)
}

def genTwoOrMore: Gen[Int] = {
choose(2, 10)
}

def genTwoOrMoreStringPairs: Gen[List[(String, String)]] = {
for {
count <- "count" |: genTwoOrMore
count <- choose(2, 10) :| "count"
tuples <- listOfN(count, genNonEmptyAlphaPair)
} yield tuples
}

def genPicks[A, B](implicit aa: Arbitrary[A], ab: Arbitrary[B]): Gen[(Map[A, B], List[A])] = {
for {
pairs <- arbitrary[Map[A, B]]
validKeys = pairs.keySet
anotherList <- listOf(arbitrary[A])
invalidPicks = anotherList.filterNot((i: A) => validKeys.contains(i))
} yield (pairs, invalidPicks)
}
// def genPicks[A, B](implicit aa: Arbitrary[A], ab: Arbitrary[B]): Gen[(Map[A, B], List[A])] = {
// for {
// pairs <- arbitrary[Map[A, B]]
// validKeys = pairs.keySet
// anotherList <- listOf(arbitrary[A])
// invalidPicks = anotherList.filterNot((i: A) => validKeys.contains(i))
// } yield (pairs, invalidPicks)
// }

def genPicksAlpha: Gen[(Map[String, String], List[String])] = {
for {
pairs <- nonEmptyMap[String, String](genNonEmptyAlphaPair)
validKeys = pairs.keySet
anotherList <- listOf(genNonEmptyAlpha)
invalidPicks = anotherList.filterNot(i => validKeys.contains(i))
} yield (pairs, invalidPicks)
validKeyValues <- nonEmptyMap[String, String](genNonEmptyAlphaPair)
validKeys = validKeyValues.keySet
invalidPicks <- listOf(genNonEmptyAlpha.suchThat(!validKeys.contains(_)))
} yield (validKeyValues, invalidPicks)
}
}
4 changes: 2 additions & 2 deletions src/test/scala/lp/template/wizard/AppMainSpec.scala
Expand Up @@ -50,7 +50,7 @@ class AppMainSpec extends FunSpec with Checkers {
describe("String variants") {
it("should include full set for multiple terms") {
check {
forAll(genTwoTermWord :| "from", genTwoTermWord :| "to") {
forAll(genTwoTerm :| "from", genTwoTerm :| "to") {
(from, to) =>
val variants = AppMain.variantsOf(Array((from, to)))
variants.deep ?= AppMain.rawVariants(from, to).deep
Expand All @@ -60,7 +60,7 @@ class AppMainSpec extends FunSpec with Checkers {

it("should include reduced set for single terms of two or more chars") {
check {
forAll(genWord.suchThat(_.length > 1) :| "term 1", genWord.suchThat(_.length > 1) :| "term 2") {
forAll(genTerm.suchThat(_.length > 1) :| "term 1", genTerm.suchThat(_.length > 1) :| "term 2") {
(from, to) =>
val variants = AppMain.variantsOf(Array((from, to)))
val lowerCase = (from.toLowerCase, to.toLowerCase)
Expand Down
14 changes: 7 additions & 7 deletions src/test/scala/lp/template/wizard/AsciiSpec.scala
Expand Up @@ -27,7 +27,7 @@ class AsciiSpec extends FunSpec with Checkers {
check {
forAll {
for {
w <- nonEmptyListOf(genWord)
w <- nonEmptyListOf(genTerm)
c <- alphaLowerChar
} yield (
w.map(_ + c).mkString, // WordxClassx
Expand All @@ -44,9 +44,9 @@ class AsciiSpec extends FunSpec with Checkers {
check {
forAll {
for {
w <- nonEmptyListOf(genWordPlusLower)
w <- nonEmptyListOf(genTerm)
c <- alphaLowerChar
} yield (w.map(_._1 + c).mkString, w.map(_._2 + c).mkString("-"))
} yield (w.map(_ + c).mkString, w.map(_.toLowerCase + c).mkString("-"))
} {
case (classCase, snakeCase) =>
Ascii.classToMinusSnakeCase(classCase) ?= snakeCase
Expand All @@ -58,9 +58,9 @@ class AsciiSpec extends FunSpec with Checkers {
check {
forAll {
for {
w <- nonEmptyListOf(genWordPlusLower)
w <- nonEmptyListOf(genTerm)
c <- alphaLowerChar
} yield (w.map(_._1 + c).mkString, w.map(_._2 + c).mkString("_").toUpperCase())
} yield (w.map(_ + c).mkString, w.map(_.toLowerCase + c).mkString("_").toUpperCase())
} {
case (classCase, snakeCase) =>
Ascii.classToSnakeUpperCase(classCase) ?= snakeCase
Expand All @@ -72,9 +72,9 @@ class AsciiSpec extends FunSpec with Checkers {
check {
forAll {
for {
w <- nonEmptyListOf(genWordPlusLower)
w <- nonEmptyListOf(genTerm)
c <- alphaLowerChar
} yield w.map(_._1 + c).mkString
} yield w.map(_ + c).mkString
} {
(classCase: String) =>
val method = Ascii.classToMethodCase(classCase)
Expand Down

0 comments on commit f0106e1

Please sign in to comment.