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

Tests for Parser.SpecialChar() and extension options #168

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Changes from 1 commit
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
Prev Previous commit
#168 - refactored: formOptions is done using Seq.combinations instead…
… of recursive functions as suggested by @Deraen.
  • Loading branch information
kevin-lee committed Jul 13, 2015
commit f4f24afd5c1296d749e3b80362284a7548f97a08
48 changes: 15 additions & 33 deletions src/test/scala/org/pegdown/ParserSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import org.parboiled.support.Characters
import org.pegdown.plugins.PegDownPlugins
import org.specs2.mutable._

import scala.annotation.tailrec
import scala.util.Random

/**
Expand All @@ -23,7 +22,7 @@ class ParserSpec extends Specification with ParserHelper {
s" specialChar() === $expected" in {
val actual = parser.SpecialChar()
val actualChars = extractSpecialChars(actual)
actualChars must not be (None)
actualChars must not be None
actualChars.get === expected
}
}
Expand All @@ -35,7 +34,7 @@ class ParserSpec extends Specification with ParserHelper {
s" specialChar() === $expected" in {
val actual = parser.SpecialChar()
val actualChars = extractSpecialChars(actual)
actualChars must not be (None)
actualChars must not be None
actualChars.get === expected
}
}
Expand All @@ -47,7 +46,7 @@ class ParserSpec extends Specification with ParserHelper {
s" specialChar() === $expected" in {
val actual = parser.SpecialChar()
val actualChars = extractSpecialChars(actual)
actualChars must not be (None)
actualChars must not be None
actualChars.get === expected
}
}
Expand All @@ -60,7 +59,7 @@ class ParserSpec extends Specification with ParserHelper {
s" specialChar() === $expected" in {
val actual = parser.SpecialChar()
val actualChars = extractSpecialChars(actual)
actualChars must not be (None)
actualChars must not be None
actualChars.get === expected
}
}
Expand All @@ -71,7 +70,7 @@ class ParserSpec extends Specification with ParserHelper {
s" specialChar() === $expected" in {
val actual = parser.SpecialChar()
val actualChars = extractSpecialChars(actual)
actualChars must not be (None)
actualChars must not be None
actualChars.get === expected
}
}
Expand Down Expand Up @@ -185,50 +184,33 @@ trait ParserHelper {
Extension("SUPPRESS_INLINE_HTML", Extensions.SUPPRESS_INLINE_HTML),
Extension( "TABLES", Extensions.TABLES),
Extension( "WIKILINKS", Extensions.WIKILINKS)
)
).toVector
/* @formatter:on */

/* This has to be split into smaller sets. Otherwise it takes too long to test due to too many possible option combinations. */
protected val groupedOptions = Random.shuffle(options).grouped(5).toList

/**
* helper method to create list of option sets.
* helper method to create Seq of options.
*
* {{{
* // examples,
*
* input: Set("a")
* output: List(Set(a))
* input: Seq("a")
* output: Seq(Seq(a))
*
* input: Set("a", "b")
* output: List(Set(a), Set(b), Set(a, b))
* input: Seq("a", "b")
* output: Seq(Seq(a), Seq(b), Seq(a, b))
*
* input: Set("a", "b", "c")
* output: List(Set(b), Set(c), Set(a), Set(a, b), Set(b, c), Set(a, c), Set(a, b, c))
* input: Seq("a", "b", "c")
* output: Seq(Seq(b), Seq(c), Seq(a), Seq(a, b), Seq(b, c), Seq(a, c), Seq(a, b, c))
* }}}
*
* @param options the given options
* @tparam T the element type
* @return List of option Set containing all possible combinations of the given options.
* @return Seq of option Seq containing all possible combinations of the given options.
*/
def formOptions[T](options: Set[T]): List[Set[T]] = {

@tailrec
def eachNumberOfOptions(options: Set[T], howMany: Int, acc: Set[Set[T]]): Set[Set[T]] = howMany match {
case 0 =>
acc
case _ =>
@tailrec
def collectOptions(options: Set[T], howMany: Int, acc: Set[Set[T]]): Set[Set[T]] = howMany match {
case 0 =>
acc
case _ =>
collectOptions(options, howMany - 1, acc.flatMap(found => (options diff found).map(found + _)))
}
eachNumberOfOptions(options, howMany - 1, collectOptions(options, howMany, Set(Set())) ++ acc)
}
eachNumberOfOptions(options, options.size, Set()).toList.sortBy(_.size)
}
def formOptions[T](options: Seq[T]): Seq[Seq[T]] = (1 to options.length).flatMap(options.combinations(_))

protected def collectPairs[T](base: Set[T])(options: Set[T]): Set[(T, T)] = base.flatMap(option => (options - option).map((option, _)))

Expand Down