Search code examples
javacucumber-jvm

Java and matching by cucumber expression


I use cucumber-jvm in external framework

I need code for match cucumber steps and Gherkin sentence. In cucumber-jvm, how to match the good expression. I need this, because in V2.x.x is it regep (not cucumber expression).

Gherkin: I wait 3 seconds. match with @Then("I wait {int} second(s)\\.")

Gherkin: I wait 1 second. match with @Then("I wait {int} second(s)\\.")

public boolean match(String cucumberExp, String gerkinSentence) {
   boolean result = false;
    ????
    return result;
}

EDIT:

I find this in unit test of cucumber-jvm if help you:

@Test
public void unknown_target_type_does_no_transform_data_table() {
    StepExpression expression = new StepExpressionFactory(registry).createExpression("Given some stuff:", UNKNOWN_TYPE);
    List<Argument> match = expression.match("Given some stuff:", table);
    assertEquals(DataTable.create(table), match.get(0).getValue());
}

Solution

  • Solution is:

    private static List<?> match(String expressionString, String text) {
        Expression expression;
        ParameterTypeRegistry parameterTypeRegistry = new ParameterTypeRegistry(Locale.ENGLISH);
        expression = new CucumberExpression(expressionString, parameterTypeRegistry);
        List<Argument<?>> args = expression.match(text);
        if (args == null) {
            return null;
        } else {
            List<Object> list = new ArrayList<>();
            for (Argument<?> arg : args) {
                Object value = arg.getValue();
                list.add(value);
            }
            return list;
        }
    }
    

    You can valid this solution with:

    public static void main(String[] args) {
        String res = new Gson().toJson(match("I expect to have {string} with the text {string}(\\?)", "I expect to have 'foo' with the text 'foo'?"));
        System.out.println(res);
        res = new Gson().toJson(match("I expect to have {string} with the text {string}(\\?)", "I expect to have 'foo' with the text 'foo'"));
        System.out.println(res);
        res = new Gson().toJson(match("I wait {int} second(s)(\\?)", "I wait 1 second?"));
        System.out.println(res);
        res = new Gson().toJson(match("I wait {int} second(s)(\\?)", "I wait 1 second"));
        System.out.println(res);
        res = new Gson().toJson(match("I wait {int} second(s)(\\?)", "I wait 2 seconds?"));
        System.out.println(res);
        res = new Gson().toJson(match("I wait {int} second(s)(\\?)", "I wait 2 seconds"));
        System.out.println(res);
        res = new Gson().toJson(match("I wait {int} second(s)(\\?)", "I wait 2 seconds!")); //false
        System.out.println(res);
        res = new Gson().toJson(match("I poc", "I poc"));
        System.out.println(res);
        res = new Gson().toJson(match("If {string} matches {string}, While {string} respects {string} I do with {int} max tries:", "If 'foo' matches '.+', While 'demo.DemoPage-big_title' respects 'This is a demo for NORAUI.*' I do with 3 max tries:"));
        System.out.println(res);
    }
    

    result is:

    ["foo","foo"]
    ["foo","foo"]
    [1]
    [1]
    [2]
    [2]
    null
    []
    ["foo",".+","demo.DemoPage-big_title","This is a demo for NORAUI.*",3]