在我的编译器编写单元测试(产生IL) [英] Writing unit tests in my compiler (which generates IL)

查看:141
本文介绍了在我的编译器编写单元测试(产生IL)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我写在 C# A 编译器和我要翻译代码到 IL



在实施每一个节点的语义检查在我的AST,我创造了很多的单元测试这一点。这是非常简单的,因为我的 CheckSemantic 方法是这样的:

 公共覆盖无效CheckSemantics(适用范围适用范围,IList的<错误>错误){

}

所以,如果我想要写一些节点的语义检查一些单元测试,我所要做的就是建立一个AST,并调用该方法。然后,我可以做这样的事情:

  Assert.That(errors.Count == 0); 

  Assert.That(errors.Count == 1); 
Assert.That(误差[0]是UnexpectedTypeError);
Assert.That(scope.ExistsType(some_declared_type));



但我开始在这一刻代码生成,我不知道这可能是编写该阶段的单元测试时,一个很好的做法。



我使用了的ILGenerator 类。我想到了以下内容:




  • 生成我想测试

  • 样本程序代码<李>保存该可执行文件
  • 执行该文件,并输出存储在一个文件

  • 断言针对该文件



但我不知道是否有这样做呢?


解决方案

这正是我们要做的C#编译器团队来测试我们的IL发生器。



我们也跑了通过ILDASM可执行文件生成和验证IL是生产符合市场预期,并通过PEVERIFY运行它,以确保我们生成可验证代码。 (当然除了在那些情况下,我们都刻意生成无法验证的代码。)


I'm writing a Tiger compiler in C# and I'm going to translate the Tiger code into IL.

While implementing the semantic check of every node in my AST, I created lots of unit tests for this. That is pretty simple, because my CheckSemantic method looks like this:

public override void CheckSemantics(Scope scope, IList<Error> errors) {
...
}

so, if I want to write some unit test for the semantic check of some node, all I have to do is build an AST, and call that method. Then I can do something like:

Assert.That(errors.Count == 0);

or

Assert.That(errors.Count == 1);
Assert.That(errors[0] is UnexpectedTypeError);
Assert.That(scope.ExistsType("some_declared_type"));

but I'm starting the code generation in this moment, and I don't know what could be a good practice when writing unit tests for that phase.

I'm using the ILGenerator class. I've thought about the following:

  • Generate the code of the sample program I want to test
  • Save that executable
  • Execute that file, and store the output in a file
  • Assert against that file

but I'm wondering if there is a better way of doing it?

解决方案

That's exactly what we do on the C# compiler team to test our IL generator.

We also run the generated executable through ILDASM and verify that the IL is produced as expected, and run it through PEVERIFY to ensure that we're generating verifiable code. (Except of course in those cases where we are deliberately generating unverifiable code.)

这篇关于在我的编译器编写单元测试(产生IL)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆