gpt4 book ai didi

java - 如何在java或groovy中解析Cucumber特征文件?

转载 作者:行者123 更新时间:2023-12-02 12:10:34 25 4
gpt4 key购买 nike

我需要解析.feature文件并生成Json数据。我目前可以使用正则表达式读取步骤,但我想解析整个功能文件。

Path path= Paths.get("path to feature file").toAbsolutePath();
try {
File file = new File(path.toString())
try {
List<String> data = Files.readAllLines(file);

data.forEach(line -> {
Matcher stepMatcher = Pattern.compile("\\b(Given|When|Then|And|But)(.*)").matcher(line);
if (stepMatcher.find()) {
System.out.println(line);
}
});
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

最佳答案

我在谷歌表单中得到了答案。答案是

public void readFeatureFile() {
Path fpath= Paths.get("path to file").toAbsolutePath();
String path = fpath.toString();
String gherkin;
try {

gherkin = FixJava.readReader(new InputStreamReader(new FileInputStream(path), "UTF-8"));
System.out.println("gherkin...\n" + gherkin);
StringBuilder json = new StringBuilder();
System.out.println("json: '" + json + "'");
JSONFormatter formatter = new JSONFormatter(json);
System.out.println("formatter: " + formatter.toString());
Parser parser = new Parser(formatter);
System.out.println("parser: " + parser.toString());
parser.parse(gherkin, path, 0);
System.out.println("json: '" + json + "'");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (RuntimeException e) {
e.printStackTrace();
}
}

查看此 url 进行讨论 discussion link

关于java - 如何在java或groovy中解析Cucumber特征文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46582658/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com