OLD | NEW |
| (Empty) |
1 // Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file | |
2 // for details. All rights reserved. Use of this source code is governed by a | |
3 // BSD-style license that can be found in the LICENSE file. | |
4 | |
5 #library('EvaluatorTest'); | |
6 | |
7 #import('node_config.dart'); | |
8 #import('../../../../lib/unittest/unittest.dart'); | |
9 #import('../../../evaluator.dart'); | |
10 // TODO(nweiz): don't depend on Node for these tests | |
11 #import('../../../lib/node/node.dart'); | |
12 #import('../../../file_system_node.dart'); | |
13 #import('../../../js_evaluator_node.dart'); | |
14 | |
15 main() { | |
16 useNodeConfiguration(); | |
17 // TODO(nweiz): This won't work if we aren't running through frogsh. | |
18 var homedir = path.dirname(fs.realpathSync(process.argv[1])); | |
19 Evaluator.initWorld(homedir, [], new NodeFileSystem()); | |
20 | |
21 evaluator() => new Evaluator(new NodeJsEvaluator()); | |
22 eval(String dart) => evaluator().eval(dart); | |
23 | |
24 group('Evaluation works correctly for', () { | |
25 test('simple expressions', () { | |
26 Expect.equals(3, eval('1 + 2')); | |
27 Expect.equals('foobar', eval('"foo" + "bar"')); | |
28 }); | |
29 | |
30 test('built-in methods', () { | |
31 Expect.equals(2, eval('(2.3).round()')); | |
32 Expect.isTrue(eval('"foobar".contains("oba")')); | |
33 }); | |
34 | |
35 test('user-defined top-level functions', () { | |
36 var ev = evaluator(); | |
37 ev.eval('foo(a) => a + 2'); | |
38 Expect.equals(3, ev.eval('foo(1)')); | |
39 | |
40 ev.eval('bar(b) => "foo" + foo(b)'); | |
41 Expect.equals("foo3", ev.eval('bar(1)')); | |
42 }); | |
43 | |
44 test('user-defined variables', () { | |
45 var ev = evaluator(); | |
46 ev.eval('a = 3'); | |
47 Expect.equals(5, ev.eval('2 + a')); | |
48 | |
49 ev.eval('b = a + 4'); | |
50 Expect.equals(21, ev.eval('b * 3')); | |
51 }); | |
52 | |
53 test('redefining variables', () { | |
54 var ev = evaluator(); | |
55 ev.eval('a = 3'); | |
56 Expect.equals(3, ev.eval('a')); | |
57 ev.eval('a = 4'); | |
58 Expect.equals(4, ev.eval('a')); | |
59 }); | |
60 | |
61 test('redefining functions', () { | |
62 var ev = evaluator(); | |
63 ev.eval('int foo() => 3'); | |
64 Expect.equals(3, ev.eval('foo()')); | |
65 ev.eval('String foo(int a) => "foo" + a'); | |
66 Expect.equals("foo12", ev.eval('foo(12)')); | |
67 }); | |
68 | |
69 test('user-defined classes', () { | |
70 var ev = evaluator(); | |
71 ev.eval('class Foo { int a; Foo(this.a); foo(b) => a + b; }'); | |
72 ev.eval('f = new Foo(5)'); | |
73 Expect.equals(5, ev.eval('f.a')); | |
74 Expect.equals(9, ev.eval('f.foo(4)')); | |
75 }); | |
76 | |
77 test('list literals', () { | |
78 var ev = evaluator(); | |
79 // Coerce to a string because the evaluation context can define methods | |
80 // differently than the primary context. | |
81 Expect.equals('1,2,3', ev.eval('[1, 2, 3].toString()')); | |
82 }); | |
83 | |
84 test('map literals', () { | |
85 var ev = evaluator(); | |
86 // Coerce to a string because the evaluation context can define methods | |
87 // differently than the primary context. | |
88 Expect.equals('a,b', ev.eval("({'a': 1, 'b': 2}).getKeys().toString()")); | |
89 Expect.equals('1,2', ev.eval("({'a': 1, 'b': 2}).getValues().toString()"))
; | |
90 }); | |
91 }); | |
92 | |
93 group('The parser is flexible enough to', () { | |
94 test('allow semicolons or not', () { | |
95 Expect.equals(2, eval('1 + 1;')); | |
96 Expect.equals(2, eval('1 + 1')); | |
97 }); | |
98 | |
99 test('allow "var" or not', () { | |
100 var ev = evaluator(); | |
101 ev.eval('var a = 1'); | |
102 Expect.equals(1, ev.eval('a')); | |
103 ev.eval('b = 2'); | |
104 Expect.equals(2, ev.eval('b')); | |
105 }); | |
106 | |
107 // TODO(nweiz): make this work | |
108 // test('parse maps with or without parentheses', () { | |
109 // var ev = evaluator(); | |
110 // // Coerce to a string because the evaluation context can define methods | |
111 // // differently than the primary context. | |
112 // Expect.equals('a,b', ev.eval("{'a': 1, 'b': 2}.getKeys().toString()")); | |
113 // Expect.equals('a,b', ev.eval("({'a': 1, 'b': 2}).getKeys().toString()")
); | |
114 // }); | |
115 }); | |
116 } | |
OLD | NEW |