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