1

我在 ANTLR 中有一个带有参数的解析器规则。

tincture [Tinctures tinctures] returns [Tincture tincture]   
    :   { String tinctureName = ""; }
    (   COLOUR { tinctureName = $COLOUR.text; }
    |   METAL  { tinctureName = $METAL.text; }
    |   FUR    { tinctureName = $FUR.text; }
    )
    {
        try {
            $tincture = tinctures.getTincture(tinctureName);
        } catch (UnknownTinctureException e) {
            throw new MyRecognitionException("Unknown tincture found.", e);
        }
    }
    ;

如何在 GUnit 中为此编写测试?

我已经成功地为我的 Lexer 规则编写了测试,因为它们没有任何参数。如果我写以下内容,我会得到“java.lang.NoSuchMethodException:mypackage.BlazonParser.tincture()”

tincture
    :   "gules"             OK
        "sable"             OK
        "blah"              FAIL

除了这个页面,我几乎找不到关于 GUnit 的文档,但它没有涵盖这一点。

4

1 回答 1

1

查看官方文档,您似乎无法为您测试的规则提供参数,从而导致异常:

java.lang.NoSuchMethodException: mypackage.BlazonParser.tincture()

因为tincture需要一个Tinctures作为参数。

您可以继续使用 gUnit,并为那些包含参数的规则使用 JUnit。但是混合 gUnit 和 JUnit 有点混乱,IMO,所以就个人而言,我只是将 JUnit 用于我所有的 ANTLR 测试。

You could also have a look at aunit, a 3rd party ANTLR unit test suite. I haven't looked at it in detail, and it seems to be lacking in documentation, but still, it could be what you're looking for.

于 2011-07-20T09:17:39.597 回答