Skip to content

Commit 4e2f14f

Browse files
committed
[#17] Support constant expressions in step definitions
1 parent e90ce57 commit 4e2f14f

File tree

3 files changed

+38
-17
lines changed

3 files changed

+38
-17
lines changed

cucumber-scala/src/main/scala/com/github/danielwegener/intellij/cucumber/scala/steps/ScalaStepDefinition.scala

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
11
package com.github.danielwegener.intellij.cucumber.scala.steps
22

3-
import org.jetbrains.plugins.cucumber.steps.AbstractStepDefinition
4-
import org.jetbrains.plugins.scala.lang.psi.api.expr.ScMethodCall
5-
import org.jetbrains.annotations.Nullable
6-
import com.intellij.psi.PsiElement
7-
import org.jetbrains.plugins.scala.lang.psi.api.base.ScLiteral
83
import java.util
9-
import com.intellij.openapi.diagnostic.Logger
104
import java.util.Collections
115

6+
import com.intellij.openapi.diagnostic.Logger
7+
import com.intellij.psi.PsiElement
8+
import org.jetbrains.annotations.Nullable
9+
import org.jetbrains.plugins.cucumber.steps.AbstractStepDefinition
10+
import org.jetbrains.plugins.scala.lang.psi.api.expr.ScMethodCall
11+
import org.jetbrains.plugins.scala.lang.psi.util.ScalaConstantExpressionEvaluator
12+
1213
object ScalaStepDefinition {
1314
val LOG: Logger = Logger.getInstance(classOf[ScalaStepDefinition])
1415

@@ -18,6 +19,8 @@ object ScalaStepDefinition {
1819
class ScalaStepDefinition(scMethod: ScMethodCall) extends AbstractStepDefinition(scMethod) {
1920
import ScalaStepDefinition._
2021

22+
private final val evaluator = new ScalaConstantExpressionEvaluator()
23+
2124
override def getVariableNames: util.List[String] = {
2225
val r = for {
2326
// WHEN("""regexp""") { (arg0:Int, arg1:String) <-- we want to match these
@@ -32,15 +35,15 @@ class ScalaStepDefinition(scMethod: ScMethodCall) extends AbstractStepDefinition
3235

3336
@Nullable
3437
override def getCucumberRegexFromElement(element: PsiElement): String = {
35-
3638
element match {
3739
case mc: ScMethodCall =>
38-
val x = for {
40+
val literals = for {
3941
innerMethodCall <- Some(mc.getEffectiveInvokedExpr).toSeq.collect { case some: ScMethodCall => some }
40-
literalParameter @ (someOther: ScLiteral) <- innerMethodCall.args.exprs
41-
if literalParameter.isString
42-
} yield literalParameter.getValue.toString
43-
x.headOption.orNull
42+
expression <- innerMethodCall.args.exprs
43+
literal <- Option(evaluator.computeConstantExpression(expression, throwExceptionOnOverflow = false)).toSeq
44+
} yield literal.toString
45+
46+
literals.headOption.orNull
4447
case _ => null
4548
}
4649
}

example/src/test/resources/cucumber/examples/scalacalculator/basic_arithmetic.feature

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,14 @@
22
Feature: Basic Arithmetic
33

44
Scenario: Adding
5-
# Try to change one of the values below to provoke a failure
5+
# Try to change one of the values below to provoke a failure
66
When I add 4 and 5
77
Then the result is 9
88

9+
Scenario: Subtracting
10+
When I sub 4 and 5
11+
Then the result is -1
12+
13+
Scenario: Dividing
14+
When I div 10 by 2
15+
Then the result is 5

example/src/test/scala/RpnCalculatorStepDefinitions.scala

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
1-
2-
31
import cucumber.api.Scenario
4-
import cucumber.api.scala.{ScalaDsl, EN}
2+
import cucumber.api.scala.EN
53
import org.junit.Assert._
64

75
class RpnCalculatorStepDefinitions extends ScalaDslIndirection with EN {
@@ -14,7 +12,20 @@ class RpnCalculatorStepDefinitions extends ScalaDslIndirection with EN {
1412
calc push "+"
1513
}
1614

17-
Then("^the result is (\\d+)$") { expected: Double =>
15+
When("I sub (\\d+)" + " and " + "(\\d+)") {
16+
(arg1: Double, arg2: Double) =>
17+
calc push arg1
18+
calc push arg2
19+
calc push "-"
20+
}
21+
22+
When("I div " + (5 + 5) + " by " + (10 - 8)) {
23+
calc push 10.0
24+
calc push 2.0
25+
calc push "/"
26+
}
27+
28+
Then("^the result is ([+-]?\\d+)$") { expected: Double =>
1829
assertEquals(expected, calc.value, 0.001)
1930
}
2031

0 commit comments

Comments
 (0)