Skip to content
Permalink
Browse files

final conversion from specs2 to utest

  • Loading branch information...
francisdb committed Feb 28, 2019
1 parent 407b8c0 commit 4cfcba49544e352e414dc0c6cd44e6ecf456ef7d
@@ -1,5 +1,5 @@
import com.typesafe.sbt.SbtScalariform.ScalariformKeys
import sbt.Keys.libraryDependencies

import scalariform.formatter.preferences._

import scala.xml.transform._
@@ -75,6 +75,9 @@ val noPublishingSettings = Seq(
publishArtifact := false,
publishTo := Some(Resolver.file("Unused transient repository", file("target/unusedrepo"))))

val utestSettings = Seq(
testFrameworks := Seq(new TestFramework("utest.runner.Framework")))

/////////////////////// DEPENDENCIES /////////////////////////

def scalaReflect(v: String) = "org.scala-lang" % "scala-reflect" % v % "provided"
@@ -84,8 +87,6 @@ val scalaCheck = Def.setting("org.scalacheck" %%% "scalacheck"
// since ScalaCheck native is not available from the original authors @lolgab made a release
// see https://github.com/rickynils/scalacheck/issues/396#issuecomment-467782592
val scalaCheckNative = Def.setting("com.github.lolgab" %%% "scalacheck" % "1.14.1" % Test)
val specs2Core = Def.setting("org.specs2" %%% "specs2-core" % "4.4.1" % Test)
val specs2ScalaCheck = Def.setting("org.specs2" %%% "specs2-scalacheck" % "4.4.1" % Test)

/////////////////////// PROJECTS /////////////////////////

@@ -100,7 +101,8 @@ lazy val examples = project
.dependsOn(parboiledJVM)
.settings(commonSettings)
.settings(noPublishingSettings)
.settings(libraryDependencies ++= Seq(specs2Core.value, "io.spray" %% "spray-json" % "1.3.5"))
.settings(libraryDependencies ++= Seq(utest.value, "io.spray" %% "spray-json" % "1.3.5"))
.settings(utestSettings)

lazy val bench = inputKey[Unit]("Runs the JSON parser benchmark with a simple standard config")

@@ -119,7 +121,8 @@ lazy val scalaParser = project
.dependsOn(parboiledJVM)
.settings(commonSettings)
.settings(noPublishingSettings)
.settings(libraryDependencies ++= Seq(shapeless.value, specs2Core.value))
.settings(libraryDependencies ++= Seq(shapeless.value, utest.value))
.settings(utestSettings)

lazy val parboiledOsgiSettings = osgiSettings ++ Seq(
OsgiKeys.exportPackage := Seq("org.parboiled2.*;version=${Bundle-Version}"),
@@ -136,13 +139,11 @@ lazy val parboiled = crossProject(JSPlatform, JVMPlatform, NativePlatform)
.settings(formattingSettings)
.settings(publishingSettings)
.jvmSettings(
libraryDependencies ++= Seq(specs2Core.value),
mappings in (Compile, packageBin) ++= (mappings in (parboiledCoreJVM.project, Compile, packageBin)).value,
mappings in (Compile, packageSrc) ++= (mappings in (parboiledCoreJVM.project, Compile, packageSrc)).value,
mappings in (Compile, packageDoc) ++= (mappings in (parboiledCoreJVM.project, Compile, packageDoc)).value
)
.jsSettings(
libraryDependencies ++= Seq(specs2Core.value),
mappings in (Compile, packageBin) ++= (mappings in (parboiledCoreJS.project, Compile, packageBin)).value,
mappings in (Compile, packageSrc) ++= (mappings in (parboiledCoreJS.project, Compile, packageSrc)).value,
mappings in (Compile, packageDoc) ++= (mappings in (parboiledCoreJS.project, Compile, packageDoc)).value
@@ -152,6 +153,7 @@ lazy val parboiled = crossProject(JSPlatform, JVMPlatform, NativePlatform)
mappings in (Compile, packageSrc) ++= (mappings in (parboiledCoreNative.project, Compile, packageSrc)).value,
mappings in (Compile, packageDoc) ++= (mappings in (parboiledCoreNative.project, Compile, packageDoc)).value,
nativeLinkStubs := true,
scalaVersion := "2.11.12",
crossScalaVersions := Seq("2.11.12")
)
.settings(
@@ -163,22 +165,22 @@ lazy val parboiled = crossProject(JSPlatform, JVMPlatform, NativePlatform)
override def transform(n: XNode) = if ((n \ "artifactId").text.startsWith("parboiledcore")) NodeSeq.Empty else n
}
new RuleTransformer(filter).transform(_).head
},
testFrameworks := Seq(new TestFramework("utest.runner.Framework"))
).
enablePlugins(SbtOsgi).settings(parboiledOsgiSettings:_*)
}
)
.settings(utestSettings)
.enablePlugins(SbtOsgi).settings(parboiledOsgiSettings:_*)

lazy val generateActionOps = taskKey[Seq[File]]("Generates the ActionOps boilerplate source file")

lazy val parboiledCore = crossProject(JSPlatform, JVMPlatform, NativePlatform).crossType(CrossType.Pure).in(file("parboiled-core"))
.settings(commonSettings)
.settings(formattingSettings)
.settings(noPublishingSettings)
.settings(utestSettings)
.settings(
libraryDependencies ++= Seq(scalaReflect(scalaVersion.value), shapeless.value, utest.value),
generateActionOps := ActionOpsBoilerplate((sourceManaged in Compile).value, streams.value),
(sourceGenerators in Compile) += generateActionOps.taskValue,
testFrameworks := Seq(new TestFramework("utest.runner.Framework"))
(sourceGenerators in Compile) += generateActionOps.taskValue
)
.jvmSettings(
libraryDependencies ++= Seq(scalaCheck.value)
@@ -17,25 +17,28 @@
package org.parboiled2.examples

import org.parboiled2.examples.CsvParser.{Record, CsvFile}
import org.specs2.mutable.Specification
import utest._

class CsvParserSpec extends Specification {
object CsvParserSpec extends TestSuite {

"The CsvParser" should {
"correctly parse simple CSV test input" in {
CsvParser {
""""first_name","last_name","company_name","address","city","county","state","zip","phone1","phone2","email","web"
|"James","Butt", "Benton, John B Jr","6649 N Blue Gum St","New Orleans","Orleans","LA",70116,"504-621-8927","504-845-1427","jbutt@gmail.com","http://www.bentonjohnbjr.com"
|"Josephine","Darakjy","Chanay, Jeffrey A Esq","4 B Blue Ridge Blvd","Brighton","Livingston","MI",48116,"810-292-9388","810-374-9840","josephine_darakjy@darakjy.org","http://www.chanayjeffreyaesq.com"
|Art,"Venere","Chemel, James L Cpa","8 W Cerritos Ave #54","Bridgeport","Gloucester","NJ",08014 ,"856-636-8749","856-264-4130","art@venere.org","http://www.chemeljameslcpa.com"
|"Lenna","Paprocki","Feltz ""Printing"" Service", 639 Main St,"Anchorage","Anchorage","AK",99501,"907-385-4412","907-921-2010","lpaprocki@hotmail.com","http://www.feltzprintingservice.com"
|""".stripMargin
} === file(
record("first_name","last_name","company_name","address","city","county","state","zip","phone1","phone2","email","web"),
record("James","Butt", "Benton, John B Jr","6649 N Blue Gum St","New Orleans","Orleans","LA","70116","504-621-8927","504-845-1427","jbutt@gmail.com","http://www.bentonjohnbjr.com"),
record("Josephine","Darakjy","Chanay, Jeffrey A Esq","4 B Blue Ridge Blvd","Brighton","Livingston","MI","48116","810-292-9388","810-374-9840","josephine_darakjy@darakjy.org","http://www.chanayjeffreyaesq.com"),
record("Art","Venere","Chemel, James L Cpa","8 W Cerritos Ave #54","Bridgeport","Gloucester","NJ","08014 ","856-636-8749","856-264-4130","art@venere.org","http://www.chemeljameslcpa.com"),
record("Lenna","Paprocki","Feltz \"Printing\" Service"," 639 Main St","Anchorage","Anchorage","AK","99501","907-385-4412","907-921-2010","lpaprocki@hotmail.com","http://www.feltzprintingservice.com"))
val tests = Tests{

"The CsvParser" - {
"correctly parse simple CSV test input" - {
assert(CsvParser {
""""first_name","last_name","company_name","address","city","county","state","zip","phone1","phone2","email","web"
|"James","Butt", "Benton, John B Jr","6649 N Blue Gum St","New Orleans","Orleans","LA",70116,"504-621-8927","504-845-1427","jbutt@gmail.com","http://www.bentonjohnbjr.com"
|"Josephine","Darakjy","Chanay, Jeffrey A Esq","4 B Blue Ridge Blvd","Brighton","Livingston","MI",48116,"810-292-9388","810-374-9840","josephine_darakjy@darakjy.org","http://www.chanayjeffreyaesq.com"
|Art,"Venere","Chemel, James L Cpa","8 W Cerritos Ave #54","Bridgeport","Gloucester","NJ",08014 ,"856-636-8749","856-264-4130","art@venere.org","http://www.chemeljameslcpa.com"
|"Lenna","Paprocki","Feltz ""Printing"" Service", 639 Main St,"Anchorage","Anchorage","AK",99501,"907-385-4412","907-921-2010","lpaprocki@hotmail.com","http://www.feltzprintingservice.com"
|""".stripMargin
} == file(
record("first_name","last_name","company_name","address","city","county","state","zip","phone1","phone2","email","web"),
record("James","Butt", "Benton, John B Jr","6649 N Blue Gum St","New Orleans","Orleans","LA","70116","504-621-8927","504-845-1427","jbutt@gmail.com","http://www.bentonjohnbjr.com"),
record("Josephine","Darakjy","Chanay, Jeffrey A Esq","4 B Blue Ridge Blvd","Brighton","Livingston","MI","48116","810-292-9388","810-374-9840","josephine_darakjy@darakjy.org","http://www.chanayjeffreyaesq.com"),
record("Art","Venere","Chemel, James L Cpa","8 W Cerritos Ave #54","Bridgeport","Gloucester","NJ","08014 ","856-636-8749","856-264-4130","art@venere.org","http://www.chemeljameslcpa.com"),
record("Lenna","Paprocki","Feltz \"Printing\" Service"," 639 Main St","Anchorage","Anchorage","AK","99501","907-385-4412","907-921-2010","lpaprocki@hotmail.com","http://www.feltzprintingservice.com")))
}
}
}

@@ -17,49 +17,52 @@
package org.parboiled2.examples

import scala.util.{Failure, Success}
import org.specs2.mutable.Specification
import utest._
import spray.json.{JsonParser => _, _}
import org.parboiled2._

class JsonParserSpec extends Specification {
object JsonParserSpec extends TestSuite {

"The JsonParser" should {
"parse 'null' to JsNull" in {
parse("null") === JsNull
}
"parse 'true' to JsTrue" in {
parse("true") === JsTrue
}
"parse 'false' to JsFalse" in {
parse("false") === JsFalse
}
"parse '0' to JsNumber" in {
parse("0") === JsNumber(0)
}
"parse '1.23' to JsNumber" in {
parse("1.23") === JsNumber(1.23)
}
"parse '-1E10' to JsNumber" in {
parse("-1E10") === JsNumber("-1E+10")
}
"parse '12.34e-10' to JsNumber" in {
parse("12.34e-10") === JsNumber("1.234E-9")
}
"parse \"xyz\" to JsString" in {
parse("\"xyz\"") === JsString("xyz")
}
"parse escapes in a JsString" in {
parse(""""\"\\/\b\f\n\r\t"""") === JsString("\"\\/\b\f\n\r\t")
parse("\"L\\" + "u00e4nder\"") === JsString("Länder")
val tests = Tests{

"The JsonParser" - {
"parse 'null' to JsNull" - {
assert(parse("null") == JsNull)
}
"parse 'true' to JsTrue" - {
assert(parse("true") == JsTrue)
}
"parse 'false' to JsFalse" - {
assert(parse("false") == JsFalse)
}
"parse '0' to JsNumber" - {
assert(parse("0") == JsNumber(0))
}
"parse '1.23' to JsNumber" - {
assert(parse("1.23") == JsNumber(1.23))
}
"parse '-1E10' to JsNumber" - {
assert(parse("-1E10") == JsNumber("-1E+10"))
}
"parse '12.34e-10' to JsNumber" - {
assert(parse("12.34e-10") == JsNumber("1.234E-9"))
}
"parse \"xyz\" to JsString" - {
assert(parse("\"xyz\"") == JsString("xyz"))
}
"parse escapes in a JsString" - {
assert(parse(""""\"\\/\b\f\n\r\t"""") == JsString("\"\\/\b\f\n\r\t"))
assert(parse("\"L\\" + "u00e4nder\"") == JsString("Länder"))
}
"properly parse a simple JsObject" - (
assert(parse(""" { "key" :42, "key2": "value" }""") ==
JsObject("key" -> JsNumber(42), "key2" -> JsString("value"))
))
"properly parse a simple JsArray" - (
assert(parse("""[null, 1.23 ,{"key":true } ] """) ==
JsArray(JsNull, JsNumber(1.23), JsObject("key" -> JsTrue))
))
}
"properly parse a simple JsObject" in (
parse(""" { "key" :42, "key2": "value" }""") ===
JsObject("key" -> JsNumber(42), "key2" -> JsString("value"))
)
"properly parse a simple JsArray" in (
parse("""[null, 1.23 ,{"key":true } ] """) ===
JsArray(JsNull, JsNumber(1.23), JsObject("key" -> JsTrue))
)
}

def parse(s: String): JsValue = {
@@ -16,12 +16,12 @@

package org.parboiled2

import org.specs2.mutable.Specification
import scala.util.Random
import org.parboiled2.util.Base64
import shapeless._
import utest._

class Base64ParsingSpec extends Specification {
object Base64ParsingSpec extends TestSuite {

class TestParser(val input: ParserInput) extends Parser with Base64Parsing

@@ -30,20 +30,24 @@ class Base64ParsingSpec extends Specification {
Stream.continually(random.nextPrintableChar())
}

"Base64Parsing" should {
"enable parsing of RFC2045 Strings" in test("rfc2045String", Base64.rfc2045())
"enable parsing of RFC2045 Blocks" in test("rfc2045Block", Base64.rfc2045())
"enable parsing of custom-Base64 Strings" in test("base64CustomString", Base64.rfc2045())
"enable parsing of custom-Base64 Blocks" in test("base64CustomBlock", Base64.rfc2045())
val tests = Tests{

"Base64Parsing" - {
"enable parsing of RFC2045 Strings" - test("rfc2045String", Base64.rfc2045())
"enable parsing of RFC2045 Blocks" - test("rfc2045Block", Base64.rfc2045())
"enable parsing of custom-Base64 Strings" - test("base64CustomString", Base64.rfc2045())
"enable parsing of custom-Base64 Blocks" - test("base64CustomBlock", Base64.rfc2045())
}

}

val (dispatch, rules) = DynamicRuleDispatch[TestParser, Array[Byte] :: HNil](
"rfc2045String", "rfc2045Block", "base64CustomString", "base64CustomBlock")

def test(ruleName: String, base64: Base64) =
(1 to 100).map { x =>
def test(ruleName: String, base64: Base64): Unit =
(1 to 100).foreach { x =>
val string = randomChars.take(x).toString()
val encoded = base64.encodeToString(string getBytes UTF8, false)
val encoded = base64.encodeToString(string getBytes UTF8, lineSep = false)
val parser = new TestParser(encoded) with DynamicRuleHandler[TestParser, Array[Byte]:: HNil] {
type Result = String
def parser: TestParser = this
@@ -52,6 +56,6 @@ class Base64ParsingSpec extends Specification {
def parseError(error: ParseError): Result = sys.error("unexpected parse error")
def failure(error: Throwable): Result = sys.error("unexpected parser exception")
}
dispatch(parser, ruleName) === string
}.reduceLeft(_ and _)
assert(dispatch(parser, ruleName) == string)
}
}
Oops, something went wrong.

0 comments on commit 4cfcba4

Please sign in to comment.
You can’t perform that action at this time.