OLD | NEW |
1 // Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file | 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 | 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. | 3 // BSD-style license that can be found in the LICENSE file. |
4 | 4 |
5 library StatusExpressionTest; | 5 import "package:expect/expect.dart"; |
6 | 6 |
7 import "package:expect/expect.dart"; | 7 import "../../tools/testing/dart/environment.dart"; |
8 import "../../tools/testing/dart/status_expression.dart"; | 8 import "../../tools/testing/dart/status_expression.dart"; |
9 | 9 |
| 10 class TestEnvironment implements Environment { |
| 11 final Map<String, String> _values; |
| 12 |
| 13 TestEnvironment(this._values); |
| 14 |
| 15 /// Looks up the value of the variable with [name]. |
| 16 String lookUp(String name) => _values[name]; |
| 17 |
| 18 operator []=(String key, String value) => _values[key] = value; |
| 19 } |
| 20 |
10 main() { | 21 main() { |
11 testExpression(); | 22 testExpression(); |
12 testSyntaxError(); | 23 testSyntaxError(); |
13 testBoolean(); | 24 testBoolean(); |
14 testNotEqual(); | 25 testNotEqual(); |
15 } | 26 } |
16 | 27 |
17 void testExpression() { | 28 void testExpression() { |
18 var expression = Expression | 29 var expression = Expression |
19 .parse(r" $mode == debug && ($arch == chromium || $arch == dartc) "); | 30 .parse(r" $mode == debug && ($arch == chromium || $arch == dartc) "); |
20 Expect.equals( | 31 Expect.equals( |
21 r"(($mode == debug) && (($arch == chromium) || ($arch == dartc)))", | 32 r"(($mode == debug) && (($arch == chromium) || ($arch == dartc)))", |
22 expression.toString()); | 33 expression.toString()); |
23 | 34 |
24 // Test BooleanExpression.evaluate(). | 35 // Test BooleanExpression.evaluate(). |
25 var environment = <String, dynamic>{"arch": "dartc", "mode": "debug"}; | 36 var environment = new TestEnvironment({"arch": "dartc", "mode": "debug"}); |
26 | 37 |
27 Expect.isTrue(expression.evaluate(environment)); | 38 Expect.isTrue(expression.evaluate(environment)); |
28 environment["mode"] = "release"; | 39 environment["mode"] = "release"; |
29 Expect.isFalse(expression.evaluate(environment)); | 40 Expect.isFalse(expression.evaluate(environment)); |
30 environment["arch"] = "ia32"; | 41 environment["arch"] = "ia32"; |
31 Expect.isFalse(expression.evaluate(environment)); | 42 Expect.isFalse(expression.evaluate(environment)); |
32 environment["mode"] = "debug"; | 43 environment["mode"] = "debug"; |
33 Expect.isFalse(expression.evaluate(environment)); | 44 Expect.isFalse(expression.evaluate(environment)); |
34 environment["arch"] = "chromium"; | 45 environment["arch"] = "chromium"; |
35 Expect.isTrue(expression.evaluate(environment)); | 46 Expect.isTrue(expression.evaluate(environment)); |
36 } | 47 } |
37 | 48 |
38 void testSyntaxError() { | 49 void testSyntaxError() { |
39 var input = r"($arch == (-dartc || $arch == chromium) && $mode == release"; | 50 var input = r"($arch == (-dartc || $arch == chromium) && $mode == release"; |
40 Expect.throws(() { | 51 Expect.throws(() { |
41 Expression.parse(input); | 52 Expression.parse(input); |
42 }, (e) => e.toString() == "FormatException: Syntax error in '$input'"); | 53 }, (e) => e.toString() == "FormatException: Syntax error in '$input'"); |
43 } | 54 } |
44 | 55 |
45 void testBoolean() { | 56 void testBoolean() { |
46 var expression = | 57 var expression = |
47 Expression.parse(r" $arch == ia32 && $checked || $mode == release "); | 58 Expression.parse(r" $arch == ia32 && $checked || $mode == release "); |
48 Expect.equals(r"((($arch == ia32) && (bool $checked)) || ($mode == release))", | 59 Expect.equals(r"((($arch == ia32) && (bool $checked)) || ($mode == release))", |
49 expression.toString()); | 60 expression.toString()); |
50 | 61 |
51 // Test BooleanExpression.evaluate(). | 62 // Test BooleanExpression.evaluate(). |
52 var environment = <String, dynamic>{ | 63 var environment = |
53 "arch": "ia32", | 64 new TestEnvironment({"arch": "ia32", "checked": "true", "mode": "debug"}); |
54 "checked": true, | |
55 "mode": "debug" | |
56 }; | |
57 | 65 |
58 Expect.isTrue(expression.evaluate(environment)); | 66 Expect.isTrue(expression.evaluate(environment)); |
59 environment["mode"] = "release"; | 67 environment["mode"] = "release"; |
60 Expect.isTrue(expression.evaluate(environment)); | 68 Expect.isTrue(expression.evaluate(environment)); |
61 environment["checked"] = false; | 69 environment["checked"] = false; |
62 Expect.isTrue(expression.evaluate(environment)); | 70 Expect.isTrue(expression.evaluate(environment)); |
63 environment["mode"] = "debug"; | 71 environment["mode"] = "debug"; |
64 Expect.isFalse(expression.evaluate(environment)); | 72 Expect.isFalse(expression.evaluate(environment)); |
65 environment["arch"] = "arm"; | 73 environment["arch"] = "arm"; |
66 Expect.isFalse(expression.evaluate(environment)); | 74 Expect.isFalse(expression.evaluate(environment)); |
67 environment["checked"] = true; | 75 environment["checked"] = true; |
68 Expect.isFalse(expression.evaluate(environment)); | 76 Expect.isFalse(expression.evaluate(environment)); |
69 } | 77 } |
70 | 78 |
71 void testNotEqual() { | 79 void testNotEqual() { |
72 // Test the != operator. | 80 // Test the != operator. |
73 var expression = Expression.parse(r"$compiler == dart2js && $runtime != ie9"); | 81 var expression = Expression.parse(r"$compiler == dart2js && $runtime != ie9"); |
74 Expect.equals( | 82 Expect.equals( |
75 r"(($compiler == dart2js) && ($runtime != ie9))", expression.toString()); | 83 r"(($compiler == dart2js) && ($runtime != ie9))", expression.toString()); |
76 | 84 |
77 // Test BooleanExpression.evaluate(). | 85 // Test BooleanExpression.evaluate(). |
78 var environment = <String, dynamic>{ | 86 var environment = new TestEnvironment({ |
79 "compiler": "none", | 87 "compiler": "none", |
80 "runtime": "ie9", | 88 "runtime": "ie9", |
81 }; | 89 }); |
82 | 90 |
83 Expect.isFalse(expression.evaluate(environment)); | 91 Expect.isFalse(expression.evaluate(environment)); |
84 environment["runtime"] = "chrome"; | 92 environment["runtime"] = "chrome"; |
85 Expect.isFalse(expression.evaluate(environment)); | 93 Expect.isFalse(expression.evaluate(environment)); |
86 | 94 |
87 environment["compiler"] = "dart2js"; | 95 environment["compiler"] = "dart2js"; |
88 environment["runtime"] = "ie9"; | 96 environment["runtime"] = "ie9"; |
89 Expect.isFalse(expression.evaluate(environment)); | 97 Expect.isFalse(expression.evaluate(environment)); |
90 environment["runtime"] = "chrome"; | 98 environment["runtime"] = "chrome"; |
91 Expect.isTrue(expression.evaluate(environment)); | 99 Expect.isTrue(expression.evaluate(environment)); |
92 } | 100 } |
OLD | NEW |