Skip to content

Commit

Permalink
more Scalacheck less Scalatest
Browse files Browse the repository at this point in the history
  • Loading branch information
Arnaud Gourlay committed Oct 16, 2019
1 parent 0d894b8 commit 3d75cfa
Show file tree
Hide file tree
Showing 3 changed files with 90 additions and 92 deletions.
@@ -0,0 +1,88 @@
package com.github.agourlay.cornichon.json

import com.github.agourlay.cornichon.core.{ Scenario, ScenarioRunner, Session, SessionKey }
import com.github.agourlay.cornichon.json.JsonSteps.JsonStepBuilder
import com.github.agourlay.cornichon.steps.StepUtilSpec
import io.circe.{ Json, JsonObject }
import io.circe.testing.ArbitraryInstances
import org.scalacheck.Properties
import org.scalacheck.Prop._
import org.typelevel.claimant.Claim

class JsonStepsProperties extends Properties("JsonSteps") with ArbitraryInstances with StepUtilSpec {

private val testKey = "test-key"
private val jsonStepBuilder = JsonStepBuilder(SessionKey(testKey), Some("test body"))

property("JsonStepBuild is value") =
forAll { input: String
val session = Session.newEmpty.addValuesUnsafe(testKey -> input)
val step = jsonStepBuilder.is(input)
val s = Scenario("scenario with JsonSteps", step :: Nil)
val t = awaitFuture(ScenarioRunner.runScenario(session)(s))
Claim(t.isSuccess)
}

property("JsonStepBuild is value fail") =
forAll { input: String
val session = Session.newEmpty.addValuesUnsafe(testKey -> input)
val step = jsonStepBuilder.is(input + "42")
val s = Scenario("scenario with JsonSteps", step :: Nil)
val t = awaitFuture(ScenarioRunner.runScenario(session)(s))
Claim(!t.isSuccess)
}

property("JsonStepBuild isNot value") =
forAll { input: String
val session = Session.newEmpty.addValuesUnsafe(testKey -> input)
val step = jsonStepBuilder.isNot(input + "42")
val s = Scenario("scenario with JsonSteps", step :: Nil)
val t = awaitFuture(ScenarioRunner.runScenario(session)(s))
Claim(t.isSuccess)
}

property("JsonStepBuild isNot value fail") =
forAll { input: String
val session = Session.newEmpty.addValuesUnsafe(testKey -> input)
val step = jsonStepBuilder.isNot(input)
val s = Scenario("scenario with JsonSteps", step :: Nil)
val t = awaitFuture(ScenarioRunner.runScenario(session)(s))
Claim(!t.isSuccess)
}

property("JsonStepBuilder is any Circe jsonObject") =
forAll { jsonOb: JsonObject
val json = Json.fromJsonObject(jsonOb)
val session = Session.newEmpty.addValuesUnsafe(testKey -> json.spaces2)
val step = jsonStepBuilder.is(json)
val s = Scenario("scenario with JsonSteps", step :: Nil)
val t = awaitFuture(ScenarioRunner.runScenario(session)(s))
Claim(t.isSuccess)
}

property("JsonStepBuilder is any Circe jsonObject with placeholder") =
forAll { jsonOb: JsonObject
val fullJsonObj = jsonOb.add("myKeyOther", Json.fromString("myOtherValue"))
val session = Session.newEmpty.addValuesUnsafe(
testKey -> Json.fromJsonObject(fullJsonObj).spaces2,
"a-placeholder" -> "myOtherValue"
)
val fullPlaceholderJsonObj = jsonOb.add("myKeyOther", Json.fromString("<a-placeholder>"))
val step = jsonStepBuilder.is(Json.fromJsonObject(fullPlaceholderJsonObj))
val s = Scenario("scenario with JsonSteps", step :: Nil)
val t = awaitFuture(ScenarioRunner.runScenario(session)(s))
Claim(t.isSuccess)
}

property("JsonStepBuilder is Circe jsonObject with absent placeholder") =
forAll { jsonOb: JsonObject
val fullJsonObj = jsonOb.add("myKeyOther", Json.fromString("myOtherValue"))
val session = Session.newEmpty.addValuesUnsafe(testKey -> Json.fromJsonObject(fullJsonObj).spaces2)
val fullPlaceholderJsonObj = jsonOb.add("myKeyOther", Json.fromString("<a-placeholder>"))
val step = jsonStepBuilder.is(Json.fromJsonObject(fullPlaceholderJsonObj))
val s = Scenario("scenario with JsonSteps", step :: Nil)
val t = awaitFuture(ScenarioRunner.runScenario(session)(s))
Claim(!t.isSuccess)
}
}

Expand Up @@ -3,17 +3,9 @@ package com.github.agourlay.cornichon.json
import com.github.agourlay.cornichon.core._
import com.github.agourlay.cornichon.json.JsonSteps.JsonStepBuilder
import com.github.agourlay.cornichon.steps.StepUtilSpec
import io.circe.{ Json, JsonObject }
import io.circe.testing.ArbitraryInstances
import org.scalatest.{ AsyncWordSpec, Matchers, OptionValues }
import org.scalatestplus.scalacheck.ScalaCheckPropertyChecks

import scala.concurrent.Await
import scala.concurrent.duration.Duration

class JsonStepsSpec extends AsyncWordSpec
with ScalaCheckPropertyChecks
with ArbitraryInstances
with Matchers
with OptionValues
with StepUtilSpec {
Expand All @@ -23,23 +15,6 @@ class JsonStepsSpec extends AsyncWordSpec

"JsonStep" when {
"JsonStepBuilder" must {
"is value" in {
val session = Session.newEmpty.addValuesUnsafe(testKey -> "test")
val step = jsonStepBuilder.is("test")
val s = Scenario("scenario with JsonSteps", step :: Nil)
ScenarioRunner.runScenario(session)(s).map { r
r.isSuccess should be(true)
}
}

"is value fail" in {
val session = Session.newEmpty.addValuesUnsafe(testKey -> "test")
val step = jsonStepBuilder.isNot("test")
val s = Scenario("scenario with JsonSteps", step :: Nil)
ScenarioRunner.runScenario(session)(s).map { r
r.isSuccess should be(false)
}
}

"is malformed Json fail" in {
val session = Session.newEmpty.addValuesUnsafe(testKey -> "test")
Expand All @@ -49,23 +24,6 @@ class JsonStepsSpec extends AsyncWordSpec
r.isSuccess should be(false)
}
}
"isNot value" in {
val session = Session.newEmpty.addValuesUnsafe(testKey -> "test")
val step = jsonStepBuilder.isNot("test1")
val s = Scenario("scenario with JsonSteps", step :: Nil)
ScenarioRunner.runScenario(session)(s).map { r
r.isSuccess should be(true)
}
}

"isNot value fail" in {
val session = Session.newEmpty.addValuesUnsafe(testKey -> "test")
val step = jsonStepBuilder.isNot("test")
val s = Scenario("scenario with JsonSteps", step :: Nil)
ScenarioRunner.runScenario(session)(s).map { r
r.isSuccess should be(false)
}
}

"is path to json key" in {
val session = Session.newEmpty.addValuesUnsafe(testKey -> """{ "myKey" : "myValue" }""")
Expand Down Expand Up @@ -252,56 +210,6 @@ class JsonStepsSpec extends AsyncWordSpec
}
}

"is any Circe jsonObject" in {
forAll { jsonOb: JsonObject
val json = Json.fromJsonObject(jsonOb)
val session = Session.newEmpty.addValuesUnsafe(testKey -> json.spaces2)
val step = jsonStepBuilder.is(json)
val s = Scenario("scenario with JsonSteps", step :: Nil)
val t = ScenarioRunner.runScenario(session)(s).map { r
withClue(LogInstruction.renderLogs(r.logs)) {
r.isSuccess should be(true)
}
}
Await.result(t, Duration.Inf)
}
}

"is any Circe jsonObject with placeholder" in {
forAll { jsonOb: JsonObject
val fullJsonObj = jsonOb.add("myKeyOther", Json.fromString("myOtherValue"))
val session = Session.newEmpty.addValuesUnsafe(
testKey -> Json.fromJsonObject(fullJsonObj).spaces2,
"a-placeholder" -> "myOtherValue"
)
val fullPlaceholderJsonObj = jsonOb.add("myKeyOther", Json.fromString("<a-placeholder>"))
val step = jsonStepBuilder.is(Json.fromJsonObject(fullPlaceholderJsonObj))
val s = Scenario("scenario with JsonSteps", step :: Nil)
val t = ScenarioRunner.runScenario(session)(s).map { r
withClue(LogInstruction.renderLogs(r.logs)) {
r.isSuccess should be(true)
}
}
Await.result(t, Duration.Inf)
}
}

"is Circe jsonObject with absent placeholder" in {
forAll { jsonOb: JsonObject
val fullJsonObj = jsonOb.add("myKeyOther", Json.fromString("myOtherValue"))
val session = Session.newEmpty.addValuesUnsafe(testKey -> Json.fromJsonObject(fullJsonObj).spaces2)
val fullPlaceholderJsonObj = jsonOb.add("myKeyOther", Json.fromString("<a-placeholder>"))
val step = jsonStepBuilder.is(Json.fromJsonObject(fullPlaceholderJsonObj))
val s = Scenario("scenario with JsonSteps", step :: Nil)
val t = ScenarioRunner.runScenario(session)(s).map { r
withClue(LogInstruction.renderLogs(r.logs)) {
r.isSuccess should be(false)
}
}
Await.result(t, Duration.Inf)
}
}

"is case class asJson with placeholder" in {
import io.circe.generic.auto._
import io.circe.syntax._
Expand Down
Expand Up @@ -3,9 +3,11 @@ package com.github.agourlay.cornichon.util
import monix.eval.Task
import monix.execution.Scheduler

import scala.concurrent.duration.Duration
import scala.concurrent.Future

trait TaskSpec {
implicit val scheduler: Scheduler = Scheduler.Implicits.global
implicit def taskToFuture[A](t: Task[A]): Future[A] = t.runToFuture(scheduler)
def awaitFuture[A](t: Task[A]): A = t.runSyncUnsafe(Duration.Inf)
}

0 comments on commit 3d75cfa

Please sign in to comment.