1
我想将Java方法名称与正则表达式匹配,但我不确定如何在Rascal中执行此操作。我想匹配名称以test
(例如JUnit 3测试用例)开头的方法,并将其转换为JUnit 4测试用例,并使用@Test
注释和删除前缀test
。我的代码如下所示:将声明的方法名称与正则表达式匹配
public tuple[int, CompilationUnit] refactorJUnitTestCaseDeclaration(CompilationUnit cu) {
int total = 0;
println(cu);
CompilationUnit unit = visit(cu) {
case(MethodDeclaration)`public void <Identifier name>() <MethodBody body>`: {
if(/test[A-Z]*/ := name) {
total += 1;
newName = name[4..];
insert (MethodDeclaration)`@Test public void <Identifier newName>() <MethodBody body>`;
};
}
};
return <total, unit>;
}
该代码产生以下错误:
Expected str, but got Identifier
有什么办法来访问name
方法标识符为字符串,所以我可以尝试搭配呢?如果没有,完成这个任务的最好方法是什么?
它的工作太棒了!谢谢! – urielSilva