Java 并通过 cucumber 表达式匹配
Java and matching by cucumber expression
我在外部框架中使用 cucumber-jvm
我需要匹配黄瓜步骤和小黄瓜句子的代码。 cucumber-jvm中,如何匹配好的表达式。我需要这个,因为在 V2.x.x 中是 regep(不是黄瓜表达式)。
小黄瓜: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;
}
编辑:
我在 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());
}
解决方法是:
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]
我在外部框架中使用 cucumber-jvm
我需要匹配黄瓜步骤和小黄瓜句子的代码。 cucumber-jvm中,如何匹配好的表达式。我需要这个,因为在 V2.x.x 中是 regep(不是黄瓜表达式)。
小黄瓜: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;
}
编辑:
我在 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());
}
解决方法是:
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]