基于抽象语法树遍历生成JUnit的代码

发布于 2024-10-25 12:44:58 字数 827 浏览 3 评论 0原文

假设我有以下类和方法:

package generation;

class HelloWorld {
  public boolean isEven(int val) {
    if ( (val % 2) == 0)
      return true;
    else
      return false;
  }
}

假设我想生成以下 JUnit 测试:

包生成;

import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;

import org.junit.Test;

public class HelloWorldTest {

    @Test
    public void testIsEven() {
        HelloWorld h = new HelloWorld();
        assertTrue(h.isEven(2));
        assertFalse(h.isEven(1));
    }
} 

给出以下遍历 Java 语法树的方法: 如何我在 Eclipse 之外的项目中使用 java Eclipse 抽象语法树? (即不是 Eclipse 插件)

在给定顶部的类示例的情况下,您将如何编码生成单元测试用例?

Assuming I have the following class and method:

package generation;

class HelloWorld {
  public boolean isEven(int val) {
    if ( (val % 2) == 0)
      return true;
    else
      return false;
  }
}

Assume I want to generated the following JUnit test:

package generation;

import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;

import org.junit.Test;

public class HelloWorldTest {

    @Test
    public void testIsEven() {
        HelloWorld h = new HelloWorld();
        assertTrue(h.isEven(2));
        assertFalse(h.isEven(1));
    }
} 

Given the following method of tree walking a Java Syntax Tree:
How can I use the java Eclipse Abstract Syntax Tree in a project outside Eclipse? (ie not an eclipse plugin)

How would you code generate the Unit test case given the class example up the top?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

蓬勃野心 2024-11-01 12:44:58

您需要的不仅仅是解析树。 (几乎所有没有构建过认真的程序分析工具的人都会重复这个错误;你需要更多的机器来完成任何真正有趣的事情。这就是为什么编译器并不简单)。

“最简单”的情况需要将要测试的方法的代码解析为 AST,名称/类型解析所有内容,以便您知道所有符号的含义(您必须知道整数中的 val),并确定控制流代码以及控制它们的谓词。

有了这些信息,您基本上可以枚举有效的控制流路径,获取有关每个路径沿线的谓词的信息,本质上形成沿该路径的所有条件的结合。 (在您的示例中,if .. val%2 ... return true;是一条路径,由val%2==true控制)。您需要担心路径中的副作用如何影响各种谓词的建模。您想要对整数信息(以及字符串和数组的大小等)进行范围分析。

然后,对于每个路径,您需要生成一组输入参数,使路径谓词为真;鉴于此谓词可能非常复杂,您可能需要某种 SAT 求解器。有了路径谓词的解决方案,您现在需要生成与测试相对应的 AST(例如,设置变量以使方法参数能够满足谓词;对于简单的整数方程,您可能只需为参数生成表达式,如您的例子)。最后,将测试调用组装到方法的 AST 中,插入到表示单元测试用例方法的 AST 中,并漂亮地打印结果。

嗯,这并不难 :-}

我们的 DMS 软件重新工程工具包 具有Java 前端,它将解析 Java、生成 AST、枚举控制流路径一种方法[这不是那么容易:考虑异常],计算整数变量的范围约束,并为您提供绕过 AST 来提取/构造您想要的内容的一般能力。尚不包括 SAT 求解器,但我们已经考虑过了。

You need a lot more than just a parse tree. (This mistake is repeated by practically everybody that hasn't built a serious program analysis tool; you need a lot more machinery to do anything really interesting. It is why compilers aren't trivial).

The "simplest" case requires parsing the code of the method to be tested into ASTs, name/type resolving everything so you know the meaning of all the symbols (you have to know that val in an integer), and determining the control flows through the code, and the predicates that control them.

With that information, you can essentially enumerate valid control-flow paths, picking up information about the predicates along the path for each one, forming in essence a conjunction of all the conditions along that path. (In your example, if .. val%2 ... return true; is one path, controlled by val%2==true). You get to worry about modelling how side effects in the path affect the various predicates. And you'd like to range information on integers (and sizes of strings and arrays, etc.).

Then for each path, you need to generate a set of input arguments that makes the path predicate true; given that this predicate could be pretty complicated, you'll likely need some kind of SAT solver. With solutuions to the path predicate, you now need to generate ASTs corresponding to to tests (e.g., set up variables to enable the method arguments to satisy the predicate; for simple integer equations, you can likely just generate expressions for the arguments as in your example). Finally, assemble the test calls into an AST for a method, insert into an AST representing a unit test case method, and prettyprint the result.

Well, that wasn't so hard :-}

Our DMS Software Reengineering Toolkit has a Java front end that will parse Java, produce ASTs, enumerate control flow paths through a method [this isn't so easy: consider exceptions], compute range constraints on integer variables, and give you the general ability to climb around the ASTs to extract/construct what you want. Doesn't include a SAT solver yet, but we've thought about it.

荒人说梦 2024-11-01 12:44:58

您可能想跳过问题中尴尬的、难以明确定义的部分,并研究基于属性的测试

You might want to skip the awkward, hardly well-defined part of your questions and look into property based testing.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文