From e267353573f1bb31754ac3601f650d5fef2cebc7 Mon Sep 17 00:00:00 2001 From: Flavian Alexandru Date: Mon, 26 Nov 2018 01:01:33 +0400 Subject: [PATCH 01/11] Adding tut plugin --- project/plugins.sbt | 1 + 1 file changed, 1 insertion(+) diff --git a/project/plugins.sbt b/project/plugins.sbt index 381d999..e1568fc 100644 --- a/project/plugins.sbt +++ b/project/plugins.sbt @@ -22,6 +22,7 @@ addSbtPlugin("com.timushev.sbt" % "sbt-updates" % "0.3.0") addSbtPlugin("com.jsuereth" % "sbt-pgp" % "1.0.0") +addSbtPlugin("org.tpolecat" % "tut-plugin" % "0.5.6") addSbtPlugin("me.lessis" % "bintray-sbt" % "0.3.0") From 574e677de45f2f8503d927d5d9f4678c7e60a477 Mon Sep 17 00:00:00 2001 From: Flavian Alexandru Date: Mon, 26 Nov 2018 01:54:17 +0400 Subject: [PATCH 02/11] Add in readme for the project --- build.sbt | 20 +++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/build.sbt b/build.sbt index 4fa9ed8..eae728c 100644 --- a/build.sbt +++ b/build.sbt @@ -179,7 +179,8 @@ lazy val baseProjectList: Seq[ProjectReference] = Seq( testing, testingTwitter, macros, - tags + tags, + readme ) lazy val util = (project in file(".")) @@ -414,3 +415,20 @@ lazy val validators = (project in file("util-validators")) parsers, testing % Test ) + +lazy val readme = (project in file("readme")) + .settings(sharedSettings: _*) + .dependsOn( + domain, + lift, + liftCats, + parsers, + parsersCats, + validatorsCats, + validators, + samplers, + testing, + testingTwitter, + macros, + tags + ).enablePlugins(TutPlugin) \ No newline at end of file From e54ed87d49ec664adf86280cf8389401fc898afb Mon Sep 17 00:00:00 2001 From: Flavian Alexandru Date: Mon, 26 Nov 2018 03:35:31 +0400 Subject: [PATCH 03/11] Updating the documentation to use tut --- build.sbt | 4 + readme/src/main/tut/README.md | 186 ++++++++++-------- .../util/play/PlayAugmenterTests.scala | 2 +- .../outworkers/util/samplers/Generators.scala | 2 - .../com/outworkers/util/samplers/Optins.scala | 15 +- .../util/samplers/SamplerMacro.scala | 4 +- .../com/outworkers/util/testing/package.scala | 61 +----- .../util/testing/AssertionsTest.scala | 19 ++ 8 files changed, 146 insertions(+), 147 deletions(-) create mode 100644 util-testing/src/test/scala/com/outworkers/util/testing/AssertionsTest.scala diff --git a/build.sbt b/build.sbt index eae728c..e2581e5 100644 --- a/build.sbt +++ b/build.sbt @@ -431,4 +431,8 @@ lazy val readme = (project in file("readme")) testingTwitter, macros, tags + ).settings( + libraryDependencies ++= Seq( + "org.scalatest" %% "scalatest" % Versions.scalatest % "tut" + ) ).enablePlugins(TutPlugin) \ No newline at end of file diff --git a/readme/src/main/tut/README.md b/readme/src/main/tut/README.md index c6c03b4..3c5b388 100644 --- a/readme/src/main/tut/README.md +++ b/readme/src/main/tut/README.md @@ -86,63 +86,6 @@ heavily throughout the Outworkers ecosystem of projects, from internal to DSL mo performance gain in code. -### Async assertions ### -Back to top - - -The async assertions module features a dual API, so you can call the same methods on both ```scala.concurrent.Future``` and ```com.twitter.util.Future```. -The underlying mechanism will create an async ```Waiter```, that will wait for the future to complete within the given ```PatienceConfiguration```. The -awaiting is done asynchronously and the assertions are invoked and evaluated once the future in question has returned a result. - -```tut:silent -import com.outworkers.util.testing._ - -class MyTests extends FlatSuite with Matchers { - - "The async computation" should "return 0 on completion" in { - val f: Future[Int] = .. // Pretend this is a Future just like any other future. - f.successful { - res => { - res shouldEqual 0 - } - } - } - - "This async computation" should "fail by design" in { - val f: Future[Unit] = .. - - // You don't even need to do anything more than failure at this stage. - // If the Future fails, the test will succeed, as this method is used when you "expect a failure". - // You can however perform assertions on the error returned. - f.failing { - err => { - } - } - } - - "This async computation" should "fail with a specific error" in { - val f: Future[Unit] = .. - f.failingWith[NumberFormatException] { - err => { - } - } - } - -} -``` - - -You can directly customise the ```timeout``` of all ```Waiters``` using the ScalaTest specific time span implementations and interval configurations. - - -```scala -import org.scalatest.concurrent.PatienceConfiguration -import org.scalatest.time.SpanSugar._ - -implicit val timeout: PatienceConfiguration.Timeout = timeout(20 seconds) - -``` - Summary: - The dependency you need is ```"com.outworkers" %% "util-testing" % UtilVersion```. @@ -161,11 +104,15 @@ After you define such a one-time sampling type class instance, you have access t It's useful to define such typeclass instances inside package objects, as they will be "invisibly" imported in to the scope you need them to. This is often really neat, albeit potentially confusing for novice Scala users. -```tut:passthrough +```tut:silent import com.outworkers.util.testing._ -@sample case class MyAwesomeClass(name: String, age: Int, email: String) +case class MyAwesomeClass( + name: String, + age: Int, + email: String +) ``` You may notice this pattern is already available in better libraries such as ScalaMock and we are not trying to provide an alternative to ScalaMock or compete with it in any way. Our typeclass generator approach only becomes very useful where you really care about very specific properties of the data. @@ -179,13 +126,15 @@ It's also useful when you want to define specific ways in which hierarchies of c Back to top One interesting thing that happens when using the `@sample` annotation is that using `gen` immediately after it will basically -give you an instance of your `case class` with the fields appropiately pre-filled, and some of the basic scenarios are also name aware. +give you an instance of your `case class` with the fields appropriately pre-filled, and some of the basic scenarios are also name aware. What this means is that we try to make the data feel "real" with respect to what it should be. Let's take the below example: -```tut:passthrough +```tut:silent + +import java.util.UUID -@sample case class User( +case class User( id: UUID, firstName: String, lastName: String, @@ -194,20 +143,23 @@ What this means is that we try to make the data feel "real" with respect to what ``` This is interesting and common enough. What's more interesting is the output of `gen`. -```tut:passthrough - -val user = gen[User] - -user.trace() +```tut:silent +import com.outworkers.util.samplers._ -/** -User( - id = 6be8914c-4274-40ee-83f5-334131246fd8 - firstName = Lindsey - lastName = Craft - email = rparker@hotma1l.us -) -*/ +object Examplers { + val user = gen[User] + + user.trace() + + /** + User( + id = 6be8914c-4274-40ee-83f5-334131246fd8 + firstName = Lindsey + lastName = Craft + email = rparker@hotma1l.us + ) + */ +} ``` @@ -219,6 +171,78 @@ During the macro expansion phase, we check the annotation targets and try to inf if your field name is either "email" or "emailAddress" or anything similar enough, you will get an "email" back. +It is also possible to generate deeply nested case classes. + +```tut:silent + +case class Address( + postcode: String, + firstLine: String, + secondLine: String, + thirdLine: String, + city: String, + country: String +) + +case class GeoLocation( + longitude: BigDecimal, + latitude: BigDecimal +) + +case class LocatedUser( + geo: GeoLocation, + address: Address, + user: User +) + +object GenerationExamples { + val deeplyNested = gen[LocatedUser] +} + +``` + +#### Extending the sampling capability. + +The automated sampling capability is a fairly simple but useful party trick. It relies +on the framework knowing how to generate basic things, such as `Int`, `Boolean`, `String`, +and so on, and the framework can then compose from these samplers to build them up +into any hierarchy of case classes you need. + +But sometimes it will come short when it doesn't know how to generate a specific type. For example, +let's look at how we could deal with `java.sql.Date`, which has no implicit sample available by default. + +```tut:silent + +case class ExpansionExample( + id: UUID, + date: java.sql.Date +) +``` + +Let's try to write some tests around the sampler. All we need to do is create a sampler for `java.sql.Date`. + +```tut:silent + +import org.scalatest.{ FlatSpec, Matchers } +import org.joda.time.DateTime + +class MyAwesomeSpec extends FlatSpec with Matchers { + + implicit val sqlDateSampler: Sample[java.sql.Date] = Sample.iso[DateTime] + + "The samplers lib" should "automatically sample an instance of ExpansionExample" in { + val instance = gen[ExpansionExample] + } +} + +``` + + + +#### Working with options. + + + ### Generating data There are multiple methods available, allowing you to generate more than just the type: @@ -330,14 +354,16 @@ import scalaz._ import scalaz.Scalaz._ import com.outworkers.util.parsers._ +case class UserToRegister( + email: String, + age: Int +) + object Test { - def registerUser(str: String, age: String): Unit = { + def registerUser(str: String, age: String): Validation[NonEmptyList[String], UserToRegister] = { (parse[EmailAddress](str) |@| parse[Int](age)) { - (validEmail, validAge) => { - } - }.fold { - // .. + (validEmail, validAge) => UserToRegister(validEmail.value, validAge) } } diff --git a/util-play/src/test/scala/com/outworkers/util/play/PlayAugmenterTests.scala b/util-play/src/test/scala/com/outworkers/util/play/PlayAugmenterTests.scala index 097be10..d151afb 100644 --- a/util-play/src/test/scala/com/outworkers/util/play/PlayAugmenterTests.scala +++ b/util-play/src/test/scala/com/outworkers/util/play/PlayAugmenterTests.scala @@ -51,7 +51,7 @@ class PlayAugmenterTests extends FlatSpec implicit val defaultTimeout: PatienceConfiguration.Timeout = timeout(defaultTimeoutSpan) - override implicit val patienceConfig = PatienceConfig( + override implicit val patienceConfig: PatienceConfig = PatienceConfig( timeout = defaultTimeoutSpan, interval = Span(defaultScalaInterval, Millis) ) diff --git a/util-samplers/src/main/scala/com/outworkers/util/samplers/Generators.scala b/util-samplers/src/main/scala/com/outworkers/util/samplers/Generators.scala index 685597a..e91c8cd 100644 --- a/util-samplers/src/main/scala/com/outworkers/util/samplers/Generators.scala +++ b/util-samplers/src/main/scala/com/outworkers/util/samplers/Generators.scala @@ -53,8 +53,6 @@ trait Generators { builder.result() } - - def genList[T : Sample](size: Int = defaultGeneration): List[T] = gen[List, T](size) def genSet[T : Sample](size: Int = defaultGeneration): Set[T] = gen[Set, T](size) diff --git a/util-samplers/src/main/scala/com/outworkers/util/samplers/Optins.scala b/util-samplers/src/main/scala/com/outworkers/util/samplers/Optins.scala index 15ef4a0..7c6967b 100644 --- a/util-samplers/src/main/scala/com/outworkers/util/samplers/Optins.scala +++ b/util-samplers/src/main/scala/com/outworkers/util/samplers/Optins.scala @@ -1,7 +1,18 @@ package com.outworkers.util.samplers -trait FillOptions +trait FillOptions { + def apply[T : Sample](opt: Option[T]): Option[T] +} object Options { - implicit val alwaysFillOptions: FillOptions = new FillOptions {} + implicit val alwaysFillOptions: FillOptions = new FillOptions { + override def apply[T: Sample](opt: Option[T]): Option[T] = { + opt.orElse(Some(gen[T])) + } + } + implicit val neverFillOptions: FillOptions = new FillOptions { + override def apply[T: Sample](opt: Option[T]): Option[T] = { + Option.empty[T] + } + } } diff --git a/util-samplers/src/main/scala/com/outworkers/util/samplers/SamplerMacro.scala b/util-samplers/src/main/scala/com/outworkers/util/samplers/SamplerMacro.scala index 6e48d50..29b02ad 100644 --- a/util-samplers/src/main/scala/com/outworkers/util/samplers/SamplerMacro.scala +++ b/util-samplers/src/main/scala/com/outworkers/util/samplers/SamplerMacro.scala @@ -207,7 +207,7 @@ class SamplerMacro(val c: blackbox.Context) extends AnnotationToolkit with Black applier = applied => TypeName(s"scala.Option[..$applied]"), generator = t => { if (fillOptionsImp.nonEmpty) { - q"""$prefix.getConstOpt[..$t]""" + q"""$fillOptionsImp($prefix.genOpt[..$t])""" } else { q"""$prefix.genOpt[..$t]""" } @@ -235,7 +235,7 @@ class SamplerMacro(val c: blackbox.Context) extends AnnotationToolkit with Black val fillOptionsImp = c.inferImplicitValue(fillOptions, silent = true) if (fillOptionsImp.nonEmpty) { - q"""$prefix.getConstOpt[$derived].map(_.value)""" + q"""$fillOptionsImp($prefix.genOpt[$derived]).map(_.value)""" } else { q"""$prefix.genOpt[$derived].map(_.value)""" } diff --git a/util-testing/src/main/scala/com/outworkers/util/testing/package.scala b/util-testing/src/main/scala/com/outworkers/util/testing/package.scala index 797e301..78457fe 100644 --- a/util-testing/src/main/scala/com/outworkers/util/testing/package.scala +++ b/util-testing/src/main/scala/com/outworkers/util/testing/package.scala @@ -20,7 +20,7 @@ import com.outworkers.util.samplers.Generators import com.outworkers.util.tags.DefaultTaggedTypes import org.joda.time.{DateTime, DateTimeZone, LocalDate} import org.scalacheck.Gen -import org.scalatest.Assertions +import org.scalatest.{Assertion, Assertions} import org.scalatest.concurrent.{PatienceConfiguration, ScalaFutures, Waiters} import org.scalatest.exceptions.TestFailedException @@ -74,63 +74,4 @@ package object testing extends ScalaFutures ScalaAwait.result(future, duration) } } - - /** - * Augmentation to allow asynchronous assertions of a @code {scala.concurrent.Future}. - * @param f The future to augment. - * @tparam A The underlying type of the computation. - */ - implicit class ScalaFutureAssertions[A](val f: ScalaFuture[A]) extends Assertions with Waiters { - - /** - * Use this to assert an expected asynchronous failure of a @code {com.twitter.util.Future} - * The computation and waiting are both performed asynchronously. - * @param mf The class Manifest to extract class information from. - * @param timeout The timeout of the asynchronous Waiter. - * @tparam T The error returned by the failing computation. Used to assert error messages. - */ - def failing[T <: Throwable]( - implicit mf: Manifest[T], - timeout: PatienceConfiguration.Timeout, - ec: ExecutionContext - ): Unit = { - val w = new Waiter - - f onComplete { - case Success(_) => w.dismiss() - case Failure(e) => w(throw e); w.dismiss() - } - - intercept[T] { - w.await(timeout, dismissals(1)) - } - } - - def failingWith[T <: Throwable](fs: ScalaFuture[_]*)(implicit mf: Manifest[T], ec: ExecutionContext) { - val w = new Waiter - fs foreach (_ onComplete { - case Failure(er) => - w(intercept[T](er)) - w.dismiss() - case Success(_) => w.dismiss() - }) - w.await() - } - - /** - * Use this to assert a successful future computation of a @code {com.twitter.util.Future} - * @param x The computation inside the future to await. This waiting is asynchronous. - * @param timeout The timeout of the future. - */ - @deprecated("Use ScalaTest AsyncAssertions trait instead", "0.31.0") - def successful(x: A => Unit)(implicit timeout: PatienceConfiguration.Timeout, ec: ExecutionContext) : Unit = { - val w = new Waiter - - f onComplete { - case Success(res) => w{x(res)}; w.dismiss() - case Failure(e) => w(throw e); w.dismiss() - } - w.await(timeout, dismissals(1)) - } - } } diff --git a/util-testing/src/test/scala/com/outworkers/util/testing/AssertionsTest.scala b/util-testing/src/test/scala/com/outworkers/util/testing/AssertionsTest.scala new file mode 100644 index 0000000..b2092ed --- /dev/null +++ b/util-testing/src/test/scala/com/outworkers/util/testing/AssertionsTest.scala @@ -0,0 +1,19 @@ +package com.outworkers.util.testing + +import org.scalatest.{FlatSpec, Matchers} + +import scala.concurrent.Future +import scala.concurrent.ExecutionContext.Implicits.global + +class AssertionsTest extends FlatSpec with Matchers { + + it should "correctly assert a failure in a failing test" in { + val msg = gen[ShortString].value + val f = Future { throw new Exception(msg)} + + f.failing { err => + + } + } + +} From 5ee79d8de6d556870f0f8c2ca1c95f8c20be9216 Mon Sep 17 00:00:00 2001 From: Flavian Alexandru Date: Mon, 26 Nov 2018 03:38:50 +0400 Subject: [PATCH 04/11] Adding examples of how to derive new types. --- readme/src/main/tut/README.md | 11 ++++++++--- .../com/outworkers/util/samplers/TracerTests.scala | 3 +++ 2 files changed, 11 insertions(+), 3 deletions(-) diff --git a/readme/src/main/tut/README.md b/readme/src/main/tut/README.md index 3c5b388..e9f4e7c 100644 --- a/readme/src/main/tut/README.md +++ b/readme/src/main/tut/README.md @@ -201,7 +201,7 @@ object GenerationExamples { ``` -#### Extending the sampling capability. +#### Creating custom samplers and adding new types The automated sampling capability is a fairly simple but useful party trick. It relies on the framework knowing how to generate basic things, such as `Int`, `Boolean`, `String`, @@ -224,11 +224,13 @@ Let's try to write some tests around the sampler. All we need to do is create a ```tut:silent import org.scalatest.{ FlatSpec, Matchers } -import org.joda.time.DateTime +import java.time.{LocalDate, ZoneId} class MyAwesomeSpec extends FlatSpec with Matchers { - implicit val sqlDateSampler: Sample[java.sql.Date] = Sample.iso[DateTime] + implicit val sqlDateSampler = new Sample[java.sql.Date] { + override def sample: java.sql.Date = java.sql.Date.valueOf(LocalDate.now(ZoneId.of("UTC"))) + } "The samplers lib" should "automatically sample an instance of ExpansionExample" in { val instance = gen[ExpansionExample] @@ -237,6 +239,9 @@ class MyAwesomeSpec extends FlatSpec with Matchers { ``` +Now, no matter how deeply nested in a case class structure the `java.sql.Date` is located inside a case class, +the framework is capable of finding it as long as it's available in the implicit scope where the `gen` method is called. + #### Working with options. diff --git a/util-samplers/src/test/scala/com/outworkers/util/samplers/TracerTests.scala b/util-samplers/src/test/scala/com/outworkers/util/samplers/TracerTests.scala index 26f2d22..0e0cd21 100644 --- a/util-samplers/src/test/scala/com/outworkers/util/samplers/TracerTests.scala +++ b/util-samplers/src/test/scala/com/outworkers/util/samplers/TracerTests.scala @@ -15,6 +15,9 @@ */ package com.outworkers.util.samplers +import java.sql.Date +import java.time.{LocalDate, ZoneId} + import org.outworkers.domain.test._ import org.scalatest.{FlatSpec, Matchers} From d122d41e182cc055cfd0504506d1645b11b1b92f Mon Sep 17 00:00:00 2001 From: Flavian Alexandru Date: Mon, 26 Nov 2018 03:43:16 +0400 Subject: [PATCH 05/11] Simplifying the logic with env variables --- .travis.yml | 6 +++++- build/publish_develop.sh | 4 ++-- build/run_tests.sh | 36 ++++++++++++++++++++++++++---------- 3 files changed, 33 insertions(+), 13 deletions(-) diff --git a/.travis.yml b/.travis.yml index 487aaf9..6c725e2 100644 --- a/.travis.yml +++ b/.travis.yml @@ -17,11 +17,15 @@ branches: - develop matrix: include: - - scala: 2.12.6 + - scala: 2.12.7 jdk: oraclejdk8 + RUN_WITH_COVERAGE: "true" + PUBLISH_ARTIFACT: "true" env: global: + - RUN_WITH_COVERAGE: "false" + - PUBLISH_ARTIFACT: "false" - GH_REF: github.com/outworkers/util.git - secure: Onl6jQhDgCHVhsxIhC2FltwTlvTWI5815lI9wsb79OvE+Xl/hh8XcafOBzUJ/LtKmt021oieOsR53RAdIJDKhNrKo3AQYoyp3rAX48zCInE0Y29slKVCwj51w5Mns+aYlPbJcHJvRNWkFIpaQ1AmBvkHfc0A0rxfoSB1lOIrtHs= - secure: k/DGy5KkvzmQNJEfazsEoD6biwkIoYC9DyjhDTMCxhXLz/mURsCtfhdWo3Uz4nhuX3qDK0N/6C6BTwl0ktVEA7eH8XZQY/dW1lLTY3jglD2U/FhAfCngbcI+ToL5kdK77Zy4LKvu4XBKXfSusI20E1gbK+Tjp1uCkNvVaAyyVv0= diff --git a/build/publish_develop.sh b/build/publish_develop.sh index 28b1b88..8b390b8 100755 --- a/build/publish_develop.sh +++ b/build/publish_develop.sh @@ -3,7 +3,7 @@ echo "Pull request: ${TRAVIS_PULL_REQUEST}; Branch: ${TRAVIS_BRANCH}" if [ "$TRAVIS_PULL_REQUEST" == "false" ] && [ "$TRAVIS_BRANCH" == "develop" ]; then - if [ "${TRAVIS_SCALA_VERSION}" == "2.12.4" ] && [ "${TRAVIS_JDK_VERSION}" == "oraclejdk8" ]; + if [ "${PUBLISH_ARTIFACT}" == "true" ]; then echo "Setting git user email to ci@outworkers.com" @@ -75,7 +75,7 @@ then fi else - echo "Only publishing version for Scala 2.12.4 and Oracle JDK 8 to prevent multiple artifacts" + echo "Only publishing version for Scala 2.12.7 and Oracle JDK 8 to prevent multiple artifacts" fi else echo "This is either a pull request or the branch is not develop, deployment not necessary" diff --git a/build/run_tests.sh b/build/run_tests.sh index 74c9325..b28bd87 100755 --- a/build/run_tests.sh +++ b/build/run_tests.sh @@ -1,11 +1,27 @@ #!/usr/bin/env bash -if [ "${TRAVIS_SCALA_VERSION}" == "2.11.8" ] && [ "${TRAVIS_JDK_VERSION}" == "oraclejdk8" ]; -then - echo "Running tests with coverage and report submission" - sbt ++$TRAVIS_SCALA_VERSION coverage test coverageReport coverageAggregate - exit $? -else - echo "Running tests without attempting to submit coverage reports" - sbt "plz $TRAVIS_SCALA_VERSION test" - exit $? -fi \ No newline at end of file +function run_test_suite { + if [ "${RUN_WITH_COVERAGE}" == "true" ]; + then + echo "Running tests with coverage and report submission" + sbt ++$TRAVIS_SCALA_VERSION coverage test coverageReport coverageAggregate coveralls + test_exit_code=$? + + if [ ${test_exit_code} -eq "0" ]; + then + echo "Running tut compilation" + sbt ++$TRAVIS_SCALA_VERSION "project readme" "tut" + local tut_exit_code=$? + echo "Tut compilation exited with status $tut_exit_code" + exit ${tut_exit_code} + else + echo "Unable to run tut compilation, test suite failed" + exit ${test_exit_code} + fi + else + echo "Running tests without attempting to submit coverage reports" + sbt "plz $TRAVIS_SCALA_VERSION test" + exit $? + fi +} + +run_test_suite \ No newline at end of file From e0ae34da7f864d3726cf7632c4b0d5d12f4db8fb Mon Sep 17 00:00:00 2001 From: Flavian Alexandru Date: Mon, 26 Nov 2018 03:52:41 +0400 Subject: [PATCH 06/11] Removing assertions test as assertions have been removed. --- .../util/testing/AssertionsTest.scala | 19 ------------------- 1 file changed, 19 deletions(-) delete mode 100644 util-testing/src/test/scala/com/outworkers/util/testing/AssertionsTest.scala diff --git a/util-testing/src/test/scala/com/outworkers/util/testing/AssertionsTest.scala b/util-testing/src/test/scala/com/outworkers/util/testing/AssertionsTest.scala deleted file mode 100644 index b2092ed..0000000 --- a/util-testing/src/test/scala/com/outworkers/util/testing/AssertionsTest.scala +++ /dev/null @@ -1,19 +0,0 @@ -package com.outworkers.util.testing - -import org.scalatest.{FlatSpec, Matchers} - -import scala.concurrent.Future -import scala.concurrent.ExecutionContext.Implicits.global - -class AssertionsTest extends FlatSpec with Matchers { - - it should "correctly assert a failure in a failing test" in { - val msg = gen[ShortString].value - val f = Future { throw new Exception(msg)} - - f.failing { err => - - } - } - -} From e044acdc770984645a32336a44064d73216529ae Mon Sep 17 00:00:00 2001 From: Flavian Alexandru Date: Mon, 26 Nov 2018 03:53:28 +0400 Subject: [PATCH 07/11] Updating patience configuration --- .../scala/com/outworkers/util/testing/twitter/package.scala | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/util-testing-twitter/src/main/scala/com/outworkers/util/testing/twitter/package.scala b/util-testing-twitter/src/main/scala/com/outworkers/util/testing/twitter/package.scala index 6d7aa4f..94a243f 100644 --- a/util-testing-twitter/src/main/scala/com/outworkers/util/testing/twitter/package.scala +++ b/util-testing-twitter/src/main/scala/com/outworkers/util/testing/twitter/package.scala @@ -66,7 +66,10 @@ package object twitter { * @param timeout The timeout of the asynchronous Waiter. * @tparam T The error returned by the failing computation. Used to assert error messages. */ - def failing[T <: Throwable]()(implicit mf: Manifest[T], timeout: PatienceConfiguration.Timeout): Unit = { + def failing[T <: Throwable]()( + implicit mf: Manifest[T], + timeout: PatienceConfiguration.Timeout + ): Unit = { val w = new Waiter f onSuccess { _ => w.dismiss()} From a7c8b1de95fc77a123f2a52efdc3b1870c3f4b82 Mon Sep 17 00:00:00 2001 From: Flavian Alexandru Date: Mon, 26 Nov 2018 04:05:00 +0400 Subject: [PATCH 08/11] Adding environment exports --- .travis.yml | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/.travis.yml b/.travis.yml index 6c725e2..2ec0fb3 100644 --- a/.travis.yml +++ b/.travis.yml @@ -19,8 +19,7 @@ matrix: include: - scala: 2.12.7 jdk: oraclejdk8 - RUN_WITH_COVERAGE: "true" - PUBLISH_ARTIFACT: "true" + env: RUN_WITH_COVERAGE=true, PUBLISH_ARTIFACT=true env: global: From 028d7f098a8afdd7a92423a26313ac7e6886e8a5 Mon Sep 17 00:00:00 2001 From: Flavian Alexandru Date: Mon, 26 Nov 2018 04:12:11 +0400 Subject: [PATCH 09/11] Trying to fix Travis logic --- .travis.yml | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/.travis.yml b/.travis.yml index 2ec0fb3..a79ad25 100644 --- a/.travis.yml +++ b/.travis.yml @@ -19,12 +19,12 @@ matrix: include: - scala: 2.12.7 jdk: oraclejdk8 - env: RUN_WITH_COVERAGE=true, PUBLISH_ARTIFACT=true + env: RUN_WITH_COVERAGE=true PUBLISH_ARTIFACT=true env: global: - - RUN_WITH_COVERAGE: "false" - - PUBLISH_ARTIFACT: "false" + - RUN_WITH_COVERAGE: false + - PUBLISH_ARTIFACT: false - GH_REF: github.com/outworkers/util.git - secure: Onl6jQhDgCHVhsxIhC2FltwTlvTWI5815lI9wsb79OvE+Xl/hh8XcafOBzUJ/LtKmt021oieOsR53RAdIJDKhNrKo3AQYoyp3rAX48zCInE0Y29slKVCwj51w5Mns+aYlPbJcHJvRNWkFIpaQ1AmBvkHfc0A0rxfoSB1lOIrtHs= - secure: k/DGy5KkvzmQNJEfazsEoD6biwkIoYC9DyjhDTMCxhXLz/mURsCtfhdWo3Uz4nhuX3qDK0N/6C6BTwl0ktVEA7eH8XZQY/dW1lLTY3jglD2U/FhAfCngbcI+ToL5kdK77Zy4LKvu4XBKXfSusI20E1gbK+Tjp1uCkNvVaAyyVv0= From 2f6a41be0ca9a188755ef0500a588a9f91467df7 Mon Sep 17 00:00:00 2001 From: Flavian Alexandru Date: Mon, 26 Nov 2018 04:22:42 +0400 Subject: [PATCH 10/11] Adding docs folder output --- build.sbt | 2 + docs/README.md | 390 +++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 392 insertions(+) create mode 100644 docs/README.md diff --git a/build.sbt b/build.sbt index e2581e5..fb82cd1 100644 --- a/build.sbt +++ b/build.sbt @@ -432,6 +432,8 @@ lazy val readme = (project in file("readme")) macros, tags ).settings( + tutSourceDirectory := sourceDirectory.value / "main" / "tut", + tutTargetDirectory := util.base / "docs", libraryDependencies ++= Seq( "org.scalatest" %% "scalatest" % Versions.scalatest % "tut" ) diff --git a/docs/README.md b/docs/README.md new file mode 100644 index 0000000..20afd0e --- /dev/null +++ b/docs/README.md @@ -0,0 +1,390 @@ +# util[![Build Status](https://travis-ci.org/outworkers/util.svg?branch=develop)](https://travis-ci.org/outworkers/util) [![Coverage Status](https://coveralls.io/repos/github/outworkers/util/badge.svg?branch=develop)](https://coveralls.io/github/outworkers/util?branch=develop) [![Maven Central](https://maven-badges.herokuapp.com/maven-central/com.outworkers/util-lift_2.11/badge.svg)](https://maven-badges.herokuapp.com/maven-central/com.outworkers/util-lift_2.11) [ ![Bintray](https://api.bintray.com/packages/outworkers/oss-releases/util/images/download.svg) ](https://bintray.com/outworkers/oss-releases/util-lift/_latestVersion) [![ScalaDoc](http://javadoc-badge.appspot.com/com.outworkers/util_2.11.svg?label=scaladoc)](http://javadoc-badge.appspot.com/com.outworkers/util-lift_2.11) [![Gitter](https://badges.gitter.im/Join%20Chat.svg)](https://gitter.im/outworkers/util?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge) + +This library is available on Maven Central and on our public Bintray repository, found at: `https://dl.bintray.com/outworkers/oss-releases/`. + +It is publicly available, for both Scala 2.10.x and Scala 2.11.x. Check the badges at the top of this README for the +latest version of `util` available. The badges are automatically updated when a new version is out, this readme is not. + +![Util](https://s3-eu-west-1.amazonaws.com/websudos/oss/logos/util.png "Outworkers Util") + +### Table of contents ### + +
    +
  1. Integrating the util library
  2. + +
  3. +

    util-parsers

    + +
  4. + +
  5. +

    util-parsers-cats

    + +
  6. + +
  7. +

    util-testing

    + +
  8. + +
  9. +

    util-zookeeper

    +
+ + +### Integrating the util library ### +Back to top + + +The util library is designed to wrap common functionality in all our frameworks and offer it at the convenience of a dependency. Anything that will be useful + long term to a great number of people belongs in these modules, to avoid duplication and help make our devs aware they can simply use what already exists. + +The full list of available modules is: + +```scala + +libraryDependencies ++= Seq( + "com.outworkers" %% "util-lift" % Versions.util, + "com.outworkers" %% "util-domain" % Versions.util, + "com.outworkers" %% "util-parsers" % Versions.util, + "com.outworkers" %% "util-parsers-cats" % Versions.util, + "com.outworkers" %% "util-validators" % Versions.util, + "com.outworkers" %% "util-validators-cats" % Versions.util, + "com.outworkers" %% "util-play" % Versions.util, + "com.outworkers" %% "util-urls" % Versions.util, + "com.outworkers" %% "util-testing" % Versions.util +) +``` + + +### util-testing ### +Back to top + +The testing module features the ```AsyncAssertionsHelper```, which builds on top of ScalaTest to offer simple asynchronous assertions. We use this pattern +heavily throughout the Outworkers ecosystem of projects, from internal to DSL modules and so forth. Asynchronous testing generally offers a considerable +performance gain in code. + + +Summary: + +- The dependency you need is ```"com.outworkers" %% "util-testing" % UtilVersion```. +- You have to import ```com.outworkers.util.testing._```. +- You have three main assertion methods, ```successful```, ```failing```, and ```failingWith```. +- You can configure the timeout of waiters with ```implicit val timeout: PatienceConfiguration.Timeout = timeout(20 seconds)```. +- The default timeout value is ```1 second```. + + +### Data sampling ### +Back to top + +This is a very common pattern we use in our testing and it's very easy to interchange this generation with something like ScalaCheck. The idea is very simple, you use type classes to define ways to sample a given type. +After you define such a one-time sampling type class instance, you have access to several methods that will allow you to generate test data. + +It's useful to define such typeclass instances inside package objects, as they will be "invisibly" imported in to the scope you need them to. This is often really neat, albeit potentially confusing for novice Scala users. + + +```scala + +import com.outworkers.util.testing._ + +case class MyAwesomeClass( + name: String, + age: Int, + email: String +) +``` + +You may notice this pattern is already available in better libraries such as ScalaMock and we are not trying to provide an alternative to ScalaMock or compete with it in any way. Our typeclass generator approach only becomes very useful where you really care about very specific properties of the data. +For instance, you may want to get a user with a valid email address, or you may use the underlying factories to get a name that reassembles the name of a real person, and so on. + +It's also useful when you want to define specific ways in which hierarchies of classes are composed together into a sample. If generation for the sake of generation is all you care about, then ScalaMock is probably more robust. + + +### Automated sampling + +Back to top + +One interesting thing that happens when using the `@sample` annotation is that using `gen` immediately after it will basically +give you an instance of your `case class` with the fields appropriately pre-filled, and some of the basic scenarios are also name aware. + +What this means is that we try to make the data feel "real" with respect to what it should be. Let's take the below example: + +```scala + +import java.util.UUID + +case class User( + id: UUID, + firstName: String, + lastName: String, + email: String +) +``` +This is interesting and common enough. What's more interesting is the output of `gen`. + +```scala +import com.outworkers.util.samplers._ + +object Examplers { + val user = gen[User] + + user.trace() + + /** + User( + id = 6be8914c-4274-40ee-83f5-334131246fd8 + firstName = Lindsey + lastName = Craft + email = rparker@hotma1l.us + ) + */ +} + +``` + +So as you can see, the fields have been appropriately pre-filled. The email is a valid email, and the first and last name look like first and last names. For +anything that's in the default generation domain, including dates and country codes and much more, we have the ability to produce automated +appropriate values. + +During the macro expansion phase, we check the annotation targets and try to infer the "natural" value based on the field name and type. So +if your field name is either "email" or "emailAddress" or anything similar enough, you will get an "email" back. + + +It is also possible to generate deeply nested case classes. + +```scala + +case class Address( + postcode: String, + firstLine: String, + secondLine: String, + thirdLine: String, + city: String, + country: String +) + +case class GeoLocation( + longitude: BigDecimal, + latitude: BigDecimal +) + +case class LocatedUser( + geo: GeoLocation, + address: Address, + user: User +) + +object GenerationExamples { + val deeplyNested = gen[LocatedUser] +} + +``` + +#### Creating custom samplers and adding new types + +The automated sampling capability is a fairly simple but useful party trick. It relies +on the framework knowing how to generate basic things, such as `Int`, `Boolean`, `String`, +and so on, and the framework can then compose from these samplers to build them up +into any hierarchy of case classes you need. + +But sometimes it will come short when it doesn't know how to generate a specific type. For example, +let's look at how we could deal with `java.sql.Date`, which has no implicit sample available by default. + +```scala + +case class ExpansionExample( + id: UUID, + date: java.sql.Date +) +``` + +Let's try to write some tests around the sampler. All we need to do is create a sampler for `java.sql.Date`. + +```scala + +import org.scalatest.{ FlatSpec, Matchers } +import java.time.{LocalDate, ZoneId} + +class MyAwesomeSpec extends FlatSpec with Matchers { + + implicit val sqlDateSampler = new Sample[java.sql.Date] { + override def sample: java.sql.Date = java.sql.Date.valueOf(LocalDate.now(ZoneId.of("UTC"))) + } + + "The samplers lib" should "automatically sample an instance of ExpansionExample" in { + val instance = gen[ExpansionExample] + } +} + +``` + +Now, no matter how deeply nested in a case class structure the `java.sql.Date` is located inside a case class, +the framework is capable of finding it as long as it's available in the implicit scope where the `gen` method is called. + + + +#### Working with options. + + + +### Generating data + +There are multiple methods available, allowing you to generate more than just the type: + +- ```gen[T]```, used to generate a single instance of T. +- ```gen[X, Y]```, used to generate a tuple based on two samples. +- ```genOpt[T]```, convenience method that will give you back a ```Some[T](..)```. +- ```genList[T](limit)```, convenience method that will give you back a ```List[T]```. The numbers of items in the list is equal to the ```limit``` and has a default value of 5 if not specified. +- ```genMap[T]()```, convenience method that will give you back a ```Map[String, T]```. + + +There is also a default list of available generators for some default types, and to get to their value simply use the `value` method if the type is not a primitive. For things like ```EmailAddress```, the point of the extra class is obviously to distinguish the type during implicit resolution, but you don't need to use our abstraction at all, there will always be an easy way to get to the underlying generated primitives. + +In the case of email addresses, you can use ```gen[EmailAddress].value```, which will correctly generate a valid ```EmailAddress``` but you can work directly with a ```String```. + +- ```scala.Int``` +- ```scala.Double``` +- ```scala.Float``` +- ```scala.Long``` +- ```scala.String``` +- ```scala.math.BigDecimal``` +- ```scala.math.BigInt``` +- ```java.util.Date``` +- ```java.util.UUID``` +- ```org.joda.time.DateTime``` +- ```org.joda.time.LocalDate``` +- ```com.outworkers.util.domain.Definitions.EmailAddress(value)``` +- ```com.outworkers.util.domain.Definitions.FirstName(value)``` +- ```com.outworkers.util.domain.Definitions.LastName(value)``` +- ```com.outworkers.util.domain.Definitions.FullName(value)``` +- ```com.outworkers.util.domain.Definitions.CountryCode(value)``` +- ```com.outworkers.util.domain.Definitions.Country(value)``` +- ```com.outworkers.util.domain.Definitions.City(value)``` +- ```com.outworkers.util.domain.Definitions.ProgrammingLanguage(value)``` +- ```com.outworkers.util.domain.Definitions.LoremIpsum(value)``` + + +### util-parsers ### +Back to top + +The parser module features an easy to use and integrate set of ScalaZ Applicative based parsers, with an ```Option``` based parser variant. It allows us to +seamlessly deal with validation chains at REST API level or whenever validation is involved. Whether it's monadic composition of options or chaining of +applicative functors to obtain a "correct" chain, the parser module is designed to offer an all-you-can-eat buffet of mini parsers that can be easily +composed to suit any validation needs. + +Each parser comes in three distinct flavours, a ```ValidationNel``` parser that parsers the end type from a ```String``` and returns the type itself, +a parser that parses an end result from an ```Option[String]``` and parserOpt variant that returns an ```Option[T]``` instead of a ```ValidationNel[String, +T]```, which allows for Monadic composition, where you need to "short-circuit" evaluation and validation, instead of computing the full chain by chaining +applicatives. + +### Option parsers ### +Back to top + +The full list of optional parsers is: + +| Type | Input type | Parser Output type | +| --------------- |---------------------------| --------------------------------- | +| Int | String\|Option[String] | ValidationNel[String, Int] | +| Long | String\|Option[String] | ValidationNel[String, Long] | +| Double | String\|Option[String] | ValidationNel[String, Double] | +| Float | String\|Option[String] | ValidationNel[String, Float] | +| UUID | String\|Option[String] | ValidationNel[String, UUID] | +| Email | String\|Option[String] | ValidationNel[String, String] | +| DateTime | String\|Option[String] | ValidationNel[String, org.joda.time.DateTime] | + +Option parsers are designed for chains where you want to short-circuit and exit to result as soon a parser fails. This short-circuit behaviour is the default + ```flatMap``` behaviour of an ```Option```, as soon as an ```Option``` is ```None``` the chain breaks. Unlike applicatives, + the evaluation sequence of options will be escaped and you cannot for instance return an error for every parser that couldn't validate. Instead, + you will only get the first error in the sequence. + +An example of how to use ```Option``` parsers might be: + +```scala + +import com.outworkers.util.parsers._ + +object Test { + def optionalParsing(email: String, age: String): Option[String] = { + for { + validEmail <- parseOpt[EmailAddress](email) + validAge <- parseOpt[Int](age) + } yield s"This person can be reached at $validEmail and is $validAge years old" + } +} + +``` + + +### Applicative parsers ### +Back to top + +The full list of ScalaZ Validation based applicative parsers is: + +| Type | Input type | Parser Output type | +| --------------- |---------------------------| --------------------------------- | +| Int | String\|Option[String] | ValidationNel[String, Int] | +| Long | String\|Option[String] | ValidationNel[String, Long] | +| Double | String\|Option[String] | ValidationNel[String, Double] | +| Float | String\|Option[String] | ValidationNel[String, Float] | +| UUID | String\|Option[String] | ValidationNel[String, UUID] | +| Email | String\|Option[String] | ValidationNel[String, String] | +| DateTime | String\|Option[String] | ValidationNel[String, org.joda.time.DateTime] | + +To illustrate the basic usage of applicative parsers and how to chain them, have a look below. + +```scala + +import scalaz._ +import scalaz.Scalaz._ +import com.outworkers.util.parsers._ + +case class UserToRegister( + email: String, + age: Int +) + +object Test { + + def registerUser(str: String, age: String): Validation[NonEmptyList[String], UserToRegister] = { + (parse[EmailAddress](str) |@| parse[Int](age)) { + (validEmail, validAge) => UserToRegister(validEmail.value, validAge) + } + } + +} +``` + +### Contributors +Back to top + +- Flavian Alexandru @alexflav23 +- Jens Halm @jenshalm +- Bartosz Jankiewicz @bjankie1 + + +Copyright +=============================== +Back to top + +Copyright (c) 2014 - 2016 outworkers. From e45ab62877cd008f6af19b40328827f94726df0f Mon Sep 17 00:00:00 2001 From: Flavian Alexandru Date: Mon, 26 Nov 2018 04:23:43 +0400 Subject: [PATCH 11/11] Changing entrypoint into readme --- README.md | 335 +----------------------------------------------------- 1 file changed, 2 insertions(+), 333 deletions(-) diff --git a/README.md b/README.md index 74df96a..8e521f0 100644 --- a/README.md +++ b/README.md @@ -7,341 +7,10 @@ latest version of `util` available. The badges are automatically updated when a ![Util](https://s3-eu-west-1.amazonaws.com/websudos/oss/logos/util.png "Outworkers Util") -### Table of contents ### +### Documentation -
    -
  1. Integrating the util library
  2. - -
  3. -

    util-parsers

    - -
  4. - -
  5. -

    util-parsers-cats

    - -
  6. - -
  7. -

    util-testing

    - -
  8. +The official documentation is found [here](./docs/README.md) -
  9. -

    util-zookeeper

    -
- - -### Integrating the util library ### -Back to top - - -The util library is designed to wrap common functionality in all our frameworks and offer it at the convenience of a dependency. Anything that will be useful - long term to a great number of people belongs in these modules, to avoid duplication and help make our devs aware they can simply use what already exists. - -The full list of available modules is: - -```scala - -libraryDependencies ++= Seq( - "com.outworkers" %% "util-lift" % Versions.util, - "com.outworkers" %% "util-domain" % Versions.util, - "com.outworkers" %% "util-parsers" % Versions.util, - "com.outworkers" %% "util-parsers-cats" % Versions.util, - "com.outworkers" %% "util-validators" % Versions.util, - "com.outworkers" %% "util-validators-cats" % Versions.util, - "com.outworkers" %% "util-play" % Versions.util, - "com.outworkers" %% "util-urls" % Versions.util, - "com.outworkers" %% "util-testing" % Versions.util -) -``` - - -### util-testing ### -Back to top - -The testing module features the ```AsyncAssertionsHelper```, which builds on top of ScalaTest to offer simple asynchronous assertions. We use this pattern -heavily throughout the Outworkers ecosystem of projects, from internal to DSL modules and so forth. Asynchronous testing generally offers a considerable -performance gain in code. - - -### Async assertions ### -Back to top - - -The async assertions module features a dual API, so you can call the same methods on both ```scala.concurrent.Future``` and ```com.twitter.util.Future```. -The underlying mechanism will create an async ```Waiter```, that will wait for the future to complete within the given ```PatienceConfiguration```. The -awaiting is done asynchronously and the assertions are invoked and evaluated once the future in question has returned a result. - -```scala -import com.outworkers.util.testing._ - -class MyTests extends FlatSuite with Matchers { - - "The async computation" should "return 0 on completion" in { - val f: Future[Int] = .. // Pretend this is a Future just like any other future. - f.successful { - res => { - res shouldEqual 0 - } - } - } - - "This async computation" should "fail by design" in { - val f: Future[Unit] = .. - - // You don't even need to do anything more than failure at this stage. - // If the Future fails, the test will succeed, as this method is used when you "expect a failure". - // You can however perform assertions on the error returned. - f.failing { - err => { - } - } - } - - "This async computation" should "fail with a specific error" in { - val f: Future[Unit] = .. - f.failingWith[NumberFormatException] { - err => { - } - } - } - -} -``` - - -You can directly customise the ```timeout``` of all ```Waiters``` using the ScalaTest specific time span implementations and interval configurations. - - -```scala -import org.scalatest.concurrent.PatienceConfiguration -import org.scalatest.time.SpanSugar._ - -implicit val timeout: PatienceConfiguration.Timeout = timeout(20 seconds) - -``` - -Summary: - -- The dependency you need is ```"com.outworkers" %% "util-testing" % UtilVersion```. -- You have to import ```com.outworkers.util.testing._```. -- You have three main assertion methods, ```successful```, ```failing```, and ```failingWith```. -- You can configure the timeout of waiters with ```implicit val timeout: PatienceConfiguration.Timeout = timeout(20 seconds)```. -- The default timeout value is ```1 second```. - - -### Data sampling ### -Back to top - -This is a very common pattern we use in our testing and it's very easy to interchange this generation with something like ScalaCheck. The idea is very simple, you use type classes to define ways to sample a given type. -After you define such a one-time sampling type class instance, you have access to several methods that will allow you to generate test data. - -It's useful to define such typeclass instances inside package objects, as they will be "invisibly" imported in to the scope you need them to. This is often really neat, albeit potentially confusing for novice Scala users. - - -```scala - -import com.outworkers.util.testing._ - -@sample case class MyAwesomeClass(name: String, age: Int, email: String) -``` - -You may notice this pattern is already available in better libraries such as ScalaMock and we are not trying to provide an alternative to ScalaMock or compete with it in any way. Our typeclass generator approach only becomes very useful where you really care about very specific properties of the data. -For instance, you may want to get a user with a valid email address, or you may use the underlying factories to get a name that reassembles the name of a real person, and so on. - -It's also useful when you want to define specific ways in which hierarchies of classes are composed together into a sample. If generation for the sake of generation is all you care about, then ScalaMock is probably more robust. - - -### Automated sampling - -Back to top - -One interesting thing that happens when using the `@sample` annotation is that using `gen` immediately after it will basically -give you an instance of your `case class` with the fields appropiately pre-filled, and some of the basic scenarios are also name aware. - -What this means is that we try to make the data feel "real" with respect to what it should be. Let's take the below example: - -```scala -@sample case class User( - id: UUID, - firstName: String, - lastName: String, - email: String -) -``` -This is interesting and common enough. What's more interesting is the output of `gen`. - -```scala - -val user = gen[User] - -Console.println(user.trace()) - -/** -User( - id = 6be8914c-4274-40ee-83f5-334131246fd8 - firstName = Lindsey - lastName = Craft - email = rparker@hotma1l.us -) -*/ - -``` - -So as you can see, the fields have been appropriately pre-filled. The email is a valid email, and the first and last name look like first and last names. For -anything that's in the default generation domain, including dates and country codes and much more, we have the ability to produce automated -appropriate values. - -During the macro expansion phase, we check the annotation targets and try to infer the "natural" value based on the field name and type. So -if your field name is either "email" or "emailAddress" or anything similar enough, you will get an "email" back. - - -### Generating data - -There are multiple methods available, allowing you to generate more than just the type: - -- ```gen[T]```, used to generate a single instance of T. -- ```gen[X, Y]```, used to generate a tuple based on two samples. -- ```genOpt[T]```, convenience method that will give you back a ```Some[T](..)```. -- ```genList[T](limit)```, convenience method that will give you back a ```List[T]```. The numbers of items in the list is equal to the ```limit``` and has a default value of 5 if not specified. -- ```genMap[T]()```, convenience method that will give you back a ```Map[String, T]```. - - -There is also a default list of available generators for some default types, and to get to their value simply use the `value` method if the type is not a primitive. For things like ```EmailAddress```, the point of the extra class is obviously to distinguish the type during implicit resolution, but you don't need to use our abstraction at all, there will always be an easy way to get to the underlying generated primitives. - -In the case of email addresses, you can use ```gen[EmailAddress].value```, which will correctly generate a valid ```EmailAddress``` but you can work directly with a ```String```. - -- ```scala.Int``` -- ```scala.Double``` -- ```scala.Float``` -- ```scala.Long``` -- ```scala.String``` -- ```scala.math.BigDecimal``` -- ```scala.math.BigInt``` -- ```java.util.Date``` -- ```java.util.UUID``` -- ```org.joda.time.DateTime``` -- ```org.joda.time.LocalDate``` -- ```com.outworkers.util.domain.Definitions.EmailAddress(value)``` -- ```com.outworkers.util.domain.Definitions.FirstName(value)``` -- ```com.outworkers.util.domain.Definitions.LastName(value)``` -- ```com.outworkers.util.domain.Definitions.FullName(value)``` -- ```com.outworkers.util.domain.Definitions.CountryCode(value)``` -- ```com.outworkers.util.domain.Definitions.Country(value)``` -- ```com.outworkers.util.domain.Definitions.City(value)``` -- ```com.outworkers.util.domain.Definitions.ProgrammingLanguage(value)``` -- ```com.outworkers.util.domain.Definitions.LoremIpsum(value)``` - - -### util-parsers ### -Back to top - -The parser module features an easy to use and integrate set of ScalaZ Applicative based parsers, with an ```Option``` based parser variant. It allows us to -seamlessly deal with validation chains at REST API level or whenever validation is involved. Whether it's monadic composition of options or chaining of -applicative functors to obtain a "correct" chain, the parser module is designed to offer an all-you-can-eat buffet of mini parsers that can be easily -composed to suit any validation needs. - -Each parser comes in three distinct flavours, a ```ValidationNel``` parser that parsers the end type from a ```String``` and returns the type itself, -a parser that parses an end result from an ```Option[String]``` and parserOpt variant that returns an ```Option[T]``` instead of a ```ValidationNel[String, -T]```, which allows for Monadic composition, where you need to "short-circuit" evaluation and validation, instead of computing the full chain by chaining -applicatives. - -### Option parsers ### -Back to top - -The full list of optional parsers is: - -| Type | Input type | Parser Output type | -| --------------- |---------------------------| --------------------------------- | -| Int | String\|Option[String] | ValidationNel[String, Int] | -| Long | String\|Option[String] | ValidationNel[String, Long] | -| Double | String\|Option[String] | ValidationNel[String, Double] | -| Float | String\|Option[String] | ValidationNel[String, Float] | -| UUID | String\|Option[String] | ValidationNel[String, UUID] | -| Email | String\|Option[String] | ValidationNel[String, String] | -| DateTime | String\|Option[String] | ValidationNel[String, org.joda.time.DateTime] | - -Option parsers are designed for chains where you want to short-circuit and exit to result as soon a parser fails. This short-circuit behaviour is the default - ```flatMap``` behaviour of an ```Option```, as soon as an ```Option``` is ```None``` the chain breaks. Unlike applicatives, - the evaluation sequence of options will be escaped and you cannot for instance return an error for every parser that couldn't validate. Instead, - you will only get the first error in the sequence. - -An example of how to use ```Option``` parsers might be: - -```scala - -import com.outworkers.util.parsers._ - -object Test { - def optionalParsing(email: String, age: String): Unit = { - for { - validEmail <- parseOpt[EmailAddress](email) - validAge <- parseOpt[Int](age) - } yield s"This person can be reached at $validEmail and is $validAge years old" - } -} - -``` - - -### Applicative parsers ### -Back to top - -The full list of ScalaZ Validation based applicative parsers is: - -| Type | Input type | Parser Output type | -| --------------- |---------------------------| --------------------------------- | -| Int | String\|Option[String] | ValidationNel[String, Int] | -| Long | String\|Option[String] | ValidationNel[String, Long] | -| Double | String\|Option[String] | ValidationNel[String, Double] | -| Float | String\|Option[String] | ValidationNel[String, Float] | -| UUID | String\|Option[String] | ValidationNel[String, UUID] | -| Email | String\|Option[String] | ValidationNel[String, String] | -| DateTime | String\|Option[String] | ValidationNel[String, org.joda.time.DateTime] | - -To illustrate the basic usage of applicative parsers and how to chain them, have a look below. - -```scala - -import scalaz._ -import scalaz.Scalaz._ -import com.outworkers.util.parsers._ - -object Test { - - def registerUser(str: String, age: String): Unit = { - (parse[EmailAddress](str) |@| parse[Int](age)) { - (validEmail, validAge) => { - } - }.fold { - // .. - } - } - -} -``` ### Contributors Back to top