OLD | NEW |
1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2013, 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 symbol_validation_test; | 5 library symbol_validation_test; |
6 | 6 |
7 import 'dart:mirrors'; | 7 import 'dart:mirrors'; |
8 import 'package:expect/expect.dart'; | 8 import 'package:expect/expect.dart'; |
9 | 9 |
10 validSymbol(String string) { | 10 validSymbol(String string) { |
11 Expect.equals(string, | 11 Expect.equals(string, MirrorSystem.getName(new Symbol(string)), |
12 MirrorSystem.getName(new Symbol(string)), | 12 'Valid symbol "$string" should be invertable'); |
13 'Valid symbol "$string" should be invertable'); | 13 Expect.equals(string, MirrorSystem.getName(MirrorSystem.getSymbol(string)), |
14 Expect.equals(string, | 14 'Valid symbol "$string" should be invertable'); |
15 MirrorSystem.getName(MirrorSystem.getSymbol(string)), | |
16 'Valid symbol "$string" should be invertable'); | |
17 } | 15 } |
18 | 16 |
19 invalidSymbol(String string) { | 17 invalidSymbol(String string) { |
20 Expect.throws(() => new Symbol(string), | 18 Expect.throws(() => new Symbol(string), (e) => e is ArgumentError, |
21 (e) => e is ArgumentError, | 19 'Invalid symbol "$string" should be rejected'); |
22 'Invalid symbol "$string" should be rejected'); | 20 Expect.throws(() => MirrorSystem.getSymbol(string), (e) => e is ArgumentError, |
23 Expect.throws(() => MirrorSystem.getSymbol(string), | 21 'Invalid symbol "$string" should be rejected'); |
24 (e) => e is ArgumentError, | |
25 'Invalid symbol "$string" should be rejected'); | |
26 } | 22 } |
27 | 23 |
28 validPrivateSymbol(String string) { | 24 validPrivateSymbol(String string) { |
29 ClosureMirror closure = reflect(main); | 25 ClosureMirror closure = reflect(main); |
30 LibraryMirror library = closure.function.owner; | 26 LibraryMirror library = closure.function.owner; |
31 Expect.equals(string, | 27 Expect.equals( |
32 MirrorSystem.getName(MirrorSystem.getSymbol(string, library)), | 28 string, |
33 'Valid private symbol "$string" should be invertable'); | 29 MirrorSystem.getName(MirrorSystem.getSymbol(string, library)), |
| 30 'Valid private symbol "$string" should be invertable'); |
34 } | 31 } |
35 | 32 |
36 main() { | 33 main() { |
37 // Operators that can be declared as class member operators. | 34 // Operators that can be declared as class member operators. |
38 // These are all valid as symbols. | 35 // These are all valid as symbols. |
39 var operators = [ | 36 var operators = [ |
40 '%', '&', '*', '+', '-', '/', '<', '<<', '<=', '==', '>', | 37 '%', |
41 '>=', '>>', '[]', '[]=', '^', 'unary-', '|', '~', '~/' | 38 '&', |
| 39 '*', |
| 40 '+', |
| 41 '-', |
| 42 '/', |
| 43 '<', |
| 44 '<<', |
| 45 '<=', |
| 46 '==', |
| 47 '>', |
| 48 '>=', |
| 49 '>>', |
| 50 '[]', |
| 51 '[]=', |
| 52 '^', |
| 53 'unary-', |
| 54 '|', |
| 55 '~', |
| 56 '~/' |
42 ]; | 57 ]; |
43 operators.expand((op) => [op, "x.$op"]).forEach(validSymbol); | 58 operators.expand((op) => [op, "x.$op"]).forEach(validSymbol); |
44 operators.expand((op) => [".$op", "$op.x", "x$op", "_x.$op"]) | 59 operators |
45 .forEach(invalidSymbol); | 60 .expand((op) => [".$op", "$op.x", "x$op", "_x.$op"]) |
46 operators.expand((op) => operators.contains("$op=") ? [] : ["x.$op=", "$op="]) | 61 .forEach(invalidSymbol); |
47 .forEach(invalidSymbol); | 62 operators |
| 63 .expand((op) => operators.contains("$op=") ? [] : ["x.$op=", "$op="]) |
| 64 .forEach(invalidSymbol); |
48 | 65 |
49 var simpleSymbols = [ | 66 var simpleSymbols = [ |
50 'foo', 'bar_', 'baz.quz', 'fisk1', 'hest2fisk', 'a.b.c.d.e', | 67 'foo', |
51 r'$', r'foo$', r'bar$bar', r'$.$', r'x6$_', r'$6_', r'x.$$6_', | 68 'bar_', |
52 'x_', 'x_.x_', 'unary', 'x.unary' | 69 'baz.quz', |
| 70 'fisk1', |
| 71 'hest2fisk', |
| 72 'a.b.c.d.e', |
| 73 r'$', |
| 74 r'foo$', |
| 75 r'bar$bar', |
| 76 r'$.$', |
| 77 r'x6$_', |
| 78 r'$6_', |
| 79 r'x.$$6_', |
| 80 'x_', |
| 81 'x_.x_', |
| 82 'unary', |
| 83 'x.unary' |
53 ]; | 84 ]; |
54 simpleSymbols.expand((s) => [s, "s="]).forEach(validSymbol); | 85 simpleSymbols.expand((s) => [s, "s="]).forEach(validSymbol); |
55 | 86 |
56 var nonSymbols = [ | 87 var nonSymbols = [ |
57 // Non-identifiers. | 88 // Non-identifiers. |
58 '6', '0foo', ',', 'S with M', '_invalid&private', "#foo", " foo", "foo ", | 89 '6', '0foo', ',', 'S with M', '_invalid&private', "#foo", " foo", "foo ", |
59 // Operator variants. | 90 // Operator variants. |
60 '+=', '()', 'operator+', 'unary+', '>>>', "&&", "||", "!", "@", "#", "[", | 91 '+=', '()', 'operator+', 'unary+', '>>>', "&&", "||", "!", "@", "#", "[", |
61 // Private symbols. | 92 // Private symbols. |
62 '_', '_x', 'x._y', 'x._', | 93 '_', '_x', 'x._y', 'x._', |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
94 "switch", | 125 "switch", |
95 "this", | 126 "this", |
96 "throw", | 127 "throw", |
97 "true", | 128 "true", |
98 "try", | 129 "try", |
99 "var", | 130 "var", |
100 "void", | 131 "void", |
101 "while", | 132 "while", |
102 "with" | 133 "with" |
103 ]; | 134 ]; |
104 reservedWords.expand((w) => [w, "$w=", "x.$w" , "$w.x", "x.$w.x"]) | 135 reservedWords |
105 .forEach(invalidSymbol); | 136 .expand((w) => [w, "$w=", "x.$w", "$w.x", "x.$w.x"]) |
106 reservedWords.expand((w) => ["${w}_", "${w}\$", "${w}q"]) | 137 .forEach(invalidSymbol); |
107 .forEach(validSymbol); | 138 reservedWords |
| 139 .expand((w) => ["${w}_", "${w}\$", "${w}q"]) |
| 140 .forEach(validSymbol); |
108 | 141 |
109 // Built-in identifiers are valid identifiers that are restricted from being | 142 // Built-in identifiers are valid identifiers that are restricted from being |
110 // used in some cases, but they are all valid symbols. | 143 // used in some cases, but they are all valid symbols. |
111 var builtInIdentifiers = [ | 144 var builtInIdentifiers = [ |
112 "abstract", | 145 "abstract", |
113 "as", | 146 "as", |
114 "dynamic", | 147 "dynamic", |
115 "export", | 148 "export", |
116 "external", | 149 "external", |
117 "factory", | 150 "factory", |
118 "get", | 151 "get", |
119 "implements", | 152 "implements", |
120 "import", | 153 "import", |
121 "library", | 154 "library", |
122 "operator", | 155 "operator", |
123 "part", | 156 "part", |
124 "set", | 157 "set", |
125 "static", | 158 "static", |
126 "typedef" | 159 "typedef" |
127 ]; | 160 ]; |
128 builtInIdentifiers.expand((w) => [w, "$w=", "x.$w" , "$w.x", "x.$w.x", | 161 builtInIdentifiers |
129 "$w=", "x.$w="]) | 162 .expand((w) => [w, "$w=", "x.$w", "$w.x", "x.$w.x", "$w=", "x.$w="]) |
130 .forEach(validSymbol); | 163 .forEach(validSymbol); |
131 | 164 |
132 var privateSymbols = [ | 165 var privateSymbols = ['_', '_x', 'x._y', 'x._', 'x.y._', 'x._.y', '_true']; |
133 '_', '_x', 'x._y', 'x._', 'x.y._', 'x._.y', '_true' | |
134 ]; | |
135 privateSymbols.forEach(invalidSymbol); | 166 privateSymbols.forEach(invalidSymbol); |
136 privateSymbols.forEach(validPrivateSymbol); // //# 01: ok | 167 privateSymbols.forEach(validPrivateSymbol); // //# 01: ok |
137 } | 168 } |
OLD | NEW |