Html程序  |  67行  |  1.84 KB

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<meta http-equiv="content-type" content="text/html;charset=utf-8" />
<title>t015calc</title>

<!-- ANTLR includes -->
<script type="text/javascript" src="../../lib/antlr3-all.js"></script>
<script type="text/javascript" src="t015calcLexer.js"></script>
<script type="text/javascript" src="t015calcParser.js"></script>


<!-- JsUnit include -->
<script type="text/javascript" src="../jsunit/app/jsUnitCore.js"></script>

<!-- Test Code -->
<script type="text/javascript">
    function _evaluate(expr, expected, errors) {
        var cstream = new org.antlr.runtime.ANTLRStringStream(expr),
            lexer = new t015calcLexer(cstream),
            tstream = new org.antlr.runtime.CommonTokenStream(lexer),
            parser = new t015calcParser(tstream);

        var result = parser.evaluate();
        assertEquals(result, expected);
        if (!errors) {
            assertUndefined(parser.reportedErrors);
        } else {
            assertEquals(parser.reportedErrors.length, errors.length);
        }
    }

    function testValid01() {
        _evaluate("1 + 2", 3);
    }


    function testValid02() {
        _evaluate("1 + 2 * 3", 7);
    }


    function testValid03() {
        _evaluate("10 / 2", 5);
    }


    function testValid04() {
        _evaluate("6 + 2*(3+1) - 4", 10);
    }


    function testMalformedInput() {
        _evaluate("6 - (2*1", 4, ["mismatched token at pos 8"]);
    }
        
    // FIXME: most parse errors result in TypeErrors in action code, because
    // rules return None, which is then added/multiplied... to integers.
    // evaluate("6 - foo 2", 4, ["some error"])
</script>

</head>
<body>
    <h1>t015calc</h1>
</body>
</html>