有没有办法生成单元测试来测试我的语法 [英] Is there a way to generate unit test to test my grammar

查看:25
本文介绍了有没有办法生成单元测试来测试我的语法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用 antlr4 创建了我的语法,但我想测试鲁棒性
是否有自动工具或快速完成此操作的好方法

I created my grammar using antlr4 but I want to test robustess
is there an automatic tool or a good way to do that fast

谢谢:)

推荐答案

我发现为语法创建单元测试的唯一方法是根据给定语言的书面规范创建许多示例.这既不快也不完整,但我看不出其他方法.

The only way I found to create unit tests for a grammar is to create a number of examples from a written spec of the given language. This is neither fast, nor complete, but I see no other way.

您可能会想直接从语法中创建测试用例(为此编写一个工具并不难).但是想一想.那你要测试什么?您的单元测试总是会成功的,除非您使用从早期版本的语法生成的测试用例.

You could be tempted to create test cases directly from the grammar (writing a tool for that isn't that hard). But think a moment about this. What would you test then? Your unit tests would always succeed, unless you use generated test cases from an earlier version of the grammar.

一种特殊情况是,当您为一种语言编写语法时,该语言已经具有另一个解析器生成工具的语法.在这种情况下,您可以使用原始语法生成测试用例,然后您可以使用这些用例来测试新语法的一致性.

A special case is when you write a grammar for a language that has already a grammar for another parser generation tool. In that case you could use the original grammar to generate test cases which you then can use to test your new grammar for conformity.

但是,我不知道有什么工具可以为您生成测试用例.

However, I don't know any tool that can generate the test cases for you.

与此同时,我有了另一个可以进行更好测试的想法:有一个句子生成器,可以根据您的语法生成随机句子(我目前正在我的 Visual Studio Code ANTLR4 扩展).然后可以使用启发式方法检查生成的句子的有效性:

Meanwhile I got another idea that would allow for better testing: have a sentence generator that generates random sentences from your grammar (I'm currently working on one in my Visual Studio Code ANTLR4 extension). The produced sentences can then be examined using a heuristic approach, for their validity:

  • 确认基础结构.
  • 检查必填关键字及其正确顺序.
  • 检查标识符和字符串是否有效.
  • 注意根据语言无效的异常结构.
  • ...

这已经涵盖了语言的很大一部分,但有局限性.匹配代码和生成它不是 1:1 的操作.匹配某些(有效)输入的语法规则可能会产生更多(因此可能会产生无效输入).

This would already cover a good part of the language, but has limits. Matching code and generating it are not 1:1 operations. A grammar rule that matches certain (valid) input might generate much more than that (and can so produce invalid input).

这篇关于有没有办法生成单元测试来测试我的语法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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