Skip to content

Commit

Permalink
nicer DSL for examples
Browse files Browse the repository at this point in the history
  • Loading branch information
Arnaud Gourlay committed Apr 18, 2016
1 parent ebb3f6d commit eb9c549
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ class MathScenario extends CornichonFeature with MathSteps {

}

Scenario("Random draw should eventually converge") {
Scenario("Random draw should eventually be identical") {

When I generate_random_int("random-1")

Expand All @@ -33,7 +33,7 @@ class MathScenario extends CornichonFeature with MathSteps {

Scenario("MonteCarlo approximation of PI") {

Repeat(1000) {
Repeat(5000) {

Given I generate_random_double("x")

Expand All @@ -45,7 +45,9 @@ class MathScenario extends CornichonFeature with MathSteps {

And I estimate_pi_from_ratio("inside", "pi")

Then assert double_value_between("pi", 3.10, 3.16)
Then assert double_value("pi").isBetween(3.1, 3.2)

And I show_session("pi")

}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,30 +20,32 @@ trait MathSteps {

def generate_random_int(target: String, max: Int = 100) =
EffectStep(
title = s"Generate random Int into $target (max=$max)",
title = s"Generate random Int into '$target' (max=$max)",
effect = s {
s.addValue(target, Random.nextInt(max).toString)
}
)

def generate_random_double(target: String) =
EffectStep(
title = s"Generate random Double into $target",
title = s"Generate random Double into '$target'",
effect = s {
s.addValue(target, Random.nextDouble().toString)
}
)

def double_value_between(source: String, low: Double, high: Double) =
AssertStep(
title = s"Double value of $source is between $low and $high",
action = s {
val v = s.get(source).toDouble
DetailedStepAssertion(true, v > low && v < high, ratioError(v, low, high))
}
)
case class double_value(source: String) {
def isBetween(low: Double, high: Double) =
AssertStep(
title = s"Double value of '$source' is between '$low' and '$high'",
action = s {
val v = s.get(source).toDouble
DetailedStepAssertion(true, v > low && v < high, ratioError(v, low, high))
}
)
}

def ratioError(v: Double, low: Double, high: Double): Boolean String = b s"$v is not between $low and $high"
private def ratioError(v: Double, low: Double, high: Double): Boolean String = b s"$v is not between $low and $high"

def calculate_point_in_circle(target: String) = EffectStep(
title = s"Calculate points inside circle",
Expand All @@ -57,7 +59,7 @@ trait MathSteps {

def estimate_pi_from_ratio(inside: String, target: String) =
EffectStep(
title = s"Estimate PI from ration into $target",
title = s"Estimate PI from ration into key '$target'",
effect = s {
val insides = s.getHistory(inside)
val trial = insides.size
Expand Down

0 comments on commit eb9c549

Please sign in to comment.