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

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

问题描述

我使用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.

与此同时,我有另一个想法可以进行更好的测试:拥有一个可以从您的语法生成随机句子的句子生成器(我目前正在我的

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天全站免登陆