Java и сопоставление по выражению огурца - PullRequest
1 голос
/ 19 апреля 2019

Я использую Cucumber-JVM во внешних рамках

Мне нужен код для совпадения шагов огурца и предложения корнишона. В cucumber-jvm, как соответствовать хорошему выражению. Мне это нужно, потому что в V2.x.x это регулярное выражение (не выражение огурца).

огурчик: I wait 3 seconds. совпадение с @Then("I wait {int} second(s)\\.")

огурчик: I wait 1 second. совпадение с @Then("I wait {int} second(s)\\.")

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

EDIT:

Я нахожу это в модульном тесте cucumber-jvm, если вам поможет:

@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());
}

1 Ответ

0 голосов
/ 06 мая 2019

Решение:

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;
    }
}

Вы можете проверить это решение с помощью:

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);
}

результат:

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