t015calc.html revision 324c4644fee44b9898524c09511bd33c3f12e2df
1<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
2<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
3<head>
4<meta http-equiv="content-type" content="text/html;charset=utf-8" />
5<title>t015calc</title>
6
7<!-- ANTLR includes -->
8<script type="text/javascript" src="/lib/antlr3-all.js"></script>
9<script type="text/javascript" src="t015calcLexer.js"></script>
10<script type="text/javascript" src="t015calcParser.js"></script>
11
12
13<!-- JsUnit include -->
14<script type="text/javascript" src="/jsunit/app/jsUnitCore.js"></script>
15
16<!-- Test Code -->
17<script type="text/javascript">
18    function _evaluate(expr, expected, errors) {
19        var cstream = new org.antlr.runtime.ANTLRStringStream(expr),
20            lexer = new t015calcLexer(cstream),
21            tstream = new org.antlr.runtime.CommonTokenStream(lexer),
22            parser = new t015calcParser(tstream);
23
24        var result = parser.evaluate();
25        assertEquals(result, expected);
26        if (!errors) {
27            assertUndefined(parser.reportedErrors);
28        } else {
29            assertEquals(parser.reportedErrors.length, errors.length);
30        }
31    }
32
33    function testValid01() {
34        _evaluate("1 + 2", 3);
35    }
36
37
38    function testValid02() {
39        _evaluate("1 + 2 * 3", 7);
40    }
41
42
43    function testValid03() {
44        _evaluate("10 / 2", 5);
45    }
46
47
48    function testValid04() {
49        _evaluate("6 + 2*(3+1) - 4", 10);
50    }
51
52
53    function testMalformedInput() {
54        _evaluate("6 - (2*1", 4, ["mismatched token at pos 8"]);
55    }
56        
57    // FIXME: most parse errors result in TypeErrors in action code, because
58    // rules return None, which is then added/multiplied... to integers.
59    // evaluate("6 - foo 2", 4, ["some error"])
60</script>
61
62</head>
63<body>
64    <h1>t015calc</h1>
65</body>
66</html>
67