Javacc 的单元测试

发布于 2024-10-28 22:15:17 字数 1436 浏览 0 评论 0原文

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

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

发布评论

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

评论(1

奈何桥上唱咆哮 2024-11-04 22:15:17

虽然没有框架,但我的书的示例代码中有一些 JavaCC 和 JJTree 语法的测试示例。


Tom Copeland 是经典著作《使用 JavaCC 生成解析器》 的作者。所引用的示例代码是免费提供的。

下面是一个简单 Robot 语法的示例测试用例:

public class RobotTest {

    @Test
    public void tokenizeMoveCommand() {
        String cmd = "STEP 10";
        SimpleCharStream cs = new SimpleCharStream(new StringReader(cmd));
        RobotTokenManager ltm = new RobotTokenManager(cs);
        Token t = ltm.getNextToken();
        assertEquals(RobotConstants.STEP, t.kind);
        t = ltm.getNextToken();
        assertEquals(RobotConstants.NUM, t.kind);
    }

    @Test(expected = TokenMgrError.class)
    public void tokenizeFailure() {
        String cmd = "STOP 10";
        SimpleCharStream cs = new SimpleCharStream(new StringReader(cmd));
        RobotTokenManager ltm = new RobotTokenManager(cs);
        ltm.getNextToken();
    }
}

语法本身:

options {
    BUILD_PARSER=false;
    STATIC=false;
}
PARSER_BEGIN(Robot)

public class Robot {}
PARSER_END(Robot)
TOKEN_MGR_DECLS: {
  public static void main(String[] args) throws Exception {
    java.io.Reader r = new java.io.FileReader(args[0]);
    SimpleCharStream scs = new SimpleCharStream(r);
    RobotTokenManager mgr = new RobotTokenManager(scs);
    for (Token t = mgr.getNextToken(); t.kind != EOF;
      t = mgr.getNextToken()) {
        System.out.println("Found a " + RobotConstants.tokenImage[t.kind] + ": " + t.image);
    }
  }
}    
SKIP :
{
    " "
|   "\n"
|   "\r"
|   "\r\n"
}
TOKEN :
{
    <STEP   :   "STEP">
|   <RIGHT  :   "RIGHT">
|   <LEFT   :   "LEFT">
|   <NUM    :   (["1"-"9"])+ (["0"-"9"])*>
}

There's not a framework, but there are some examples of tests for both JavaCC and JJTree grammars in my book's example code.


Tom Copeland is the author of the classic Generating Parsers with JavaCC. The example code which is referred to is available for free.

Below is a sample test case for a simple Robot grammar:

public class RobotTest {

    @Test
    public void tokenizeMoveCommand() {
        String cmd = "STEP 10";
        SimpleCharStream cs = new SimpleCharStream(new StringReader(cmd));
        RobotTokenManager ltm = new RobotTokenManager(cs);
        Token t = ltm.getNextToken();
        assertEquals(RobotConstants.STEP, t.kind);
        t = ltm.getNextToken();
        assertEquals(RobotConstants.NUM, t.kind);
    }

    @Test(expected = TokenMgrError.class)
    public void tokenizeFailure() {
        String cmd = "STOP 10";
        SimpleCharStream cs = new SimpleCharStream(new StringReader(cmd));
        RobotTokenManager ltm = new RobotTokenManager(cs);
        ltm.getNextToken();
    }
}

The grammar itself:

options {
    BUILD_PARSER=false;
    STATIC=false;
}
PARSER_BEGIN(Robot)

public class Robot {}
PARSER_END(Robot)
TOKEN_MGR_DECLS: {
  public static void main(String[] args) throws Exception {
    java.io.Reader r = new java.io.FileReader(args[0]);
    SimpleCharStream scs = new SimpleCharStream(r);
    RobotTokenManager mgr = new RobotTokenManager(scs);
    for (Token t = mgr.getNextToken(); t.kind != EOF;
      t = mgr.getNextToken()) {
        System.out.println("Found a " + RobotConstants.tokenImage[t.kind] + ": " + t.image);
    }
  }
}    
SKIP :
{
    " "
|   "\n"
|   "\r"
|   "\r\n"
}
TOKEN :
{
    <STEP   :   "STEP">
|   <RIGHT  :   "RIGHT">
|   <LEFT   :   "LEFT">
|   <NUM    :   (["1"-"9"])+ (["0"-"9"])*>
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文