2
  Given Java is a language supporting Double.NaN
  Then cucumber's double expression should support 1.234 
  And cucumber's double expression should support NaN <<< FAIL

and

  @Given("cucumber's double expression should support {double}")
  public void check(double expected) {}

Is there a proper method to make this work with the builtins?

drekbour
  • 2,895
  • 18
  • 28

1 Answers1

0

The easiest way is to introduce custom parameter like this:

public class StepDef {

    @ParameterType(".*")
    public Double doubleNaNable(String val){
        try{
            return Double.parseDouble(val);
        }catch (NumberFormatException e){
            return Double.NaN;
        }
    }

    @Given("Java is a language supporting Double.NaN")
    public void givenStep() {
        System.out.println("Java is a language supporting Double.NaN");
    }
    @Then("cucumber's double expression should support {doubleNaNable}")
    public void thenStep(double value) {
        System.out.println(value);
    }

}

More nice way is to add your text representation of NaN to regexp that captures double value from gherkin.

Alexey R.
  • 8,057
  • 2
  • 11
  • 27
  • Indeed. That `.*` loses the nice snippet-generation unless you try to write a much more complete (and less greedy) regex (which _none_ of the examples here do: https://stackoverflow.com/questions/14563106/java-regex-on-doubles) – drekbour Feb 25 '22 at 09:18
  • Returning `NaN` on exception is definitely wrong logic. Would take ages to debug that one :) – drekbour Feb 25 '22 at 09:19