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, |
12 MirrorSystem.getName(new Symbol(string)), | 12 MirrorSystem.getName(new Symbol(string)), |
13 'Valid symbol "$string" should be invertable'); | 13 'Valid symbol "$string" should be invertable'); |
14 return; /// 01: ok | |
14 Expect.equals(string, | 15 Expect.equals(string, |
15 MirrorSystem.getName(MirrorSystem.getSymbol(string)), | 16 MirrorSystem.getName(MirrorSystem.getSymbol(string)), |
16 'Valid symbol "$string" should be invertable'); | 17 'Valid symbol "$string" should be invertable'); |
17 } | 18 } |
18 | 19 |
19 invalidSymbol(String string) { | 20 invalidSymbol(String string) { |
20 Expect.throws(() => new Symbol(string), | 21 Expect.throws(() => new Symbol(string), |
21 (e) => e is ArgumentError, | 22 (e) => e is ArgumentError, |
22 'Invalid symbol "$string" should be rejected'); | 23 'Invalid symbol "$string" should be rejected'); |
24 return; /// 01: continued | |
23 Expect.throws(() => MirrorSystem.getSymbol(string), | 25 Expect.throws(() => MirrorSystem.getSymbol(string), |
24 (e) => e is ArgumentError, | 26 (e) => e is ArgumentError, |
25 'Invalid symbol "$string" should be rejected'); | 27 'Invalid symbol "$string" should be rejected'); |
26 } | 28 } |
27 | 29 |
28 main() { | 30 main() { |
29 ['%', '&', '*', '+', '-', '/', '<', '<<', '<=', '==', '>', | 31 // Operators that can be declared as class member operators. |
30 '>=', '>>', '[]', '[]=', '^', 'unary-', '|', '~', '~/'] | 32 // These are all valid as symbols. |
31 .forEach(validSymbol); | 33 var operators = [ |
34 '%', '&', '*', '+', '-', '/', '<', '<<', '<=', '==', '>', | |
35 '>=', '>>', '[]', '[]=', '^', 'unary-', '|', '~', '~/' | |
36 ]; | |
37 operators.expand((op) => [op, "x.$op"]).forEach(validSymbol); | |
38 operators.expand((op) => [".$op", "$op.x", "x$op", "_x.$op"]) | |
39 .forEach(invalidSymbol); | |
40 operators.expand((op) => operators.contains("$op=") ? [] : ["x.$op=", "$op="]) | |
41 .forEach(invalidSymbol); | |
32 | 42 |
33 ['foo', '_bar', 'baz.quz', 'fisk1', 'hest2fisk', 'a.b.c.d.e', | 43 var simpleSymbols = [ |
34 '\$', 'foo\$', 'bar\$bar'] | 44 'foo', 'bar_', 'baz.quz', 'fisk1', 'hest2fisk', 'a.b.c.d.e', |
35 .forEach(validSymbol); | 45 r'$', r'foo$', r'bar$bar', r'$.$', r'x6$_', r'$6_', r'x.$$6_', |
46 'x_', 'x_.x_', | |
47 ]; | |
48 simpleSymbols.expand((s) => [s, "s="]).forEach(validSymbol); | |
36 | 49 |
37 ['6', '0foo', ',', 'S with M', '_invalid&private'] | 50 var nonSymbols = [ |
38 .forEach(invalidSymbol); | 51 // Non-identifiers. |
52 '6', '0foo', ',', 'S with M', '_invalid&private', "#foo", " foo", "foo ", | |
53 // Operator variants. | |
54 '+=', '()', 'operator+', 'unary+', '>>>', "&&", "||", "!", "@", "#", "[", | |
55 // Privat symbols. | |
ahe
2014/02/21 12:52:09
Privat*e*
| |
56 '_', '_x', 'x._y', 'x._', | |
57 // Empty parts of "qualified" symbols. | |
58 '.', 'x.', '.x', 'x..y' | |
59 ]; | |
60 nonSymbols.forEach(invalidSymbol); | |
61 | |
62 // Reserved words are not valid identifiers and therefore not valid symbols. | |
63 var reservedWords = [ | |
64 "assert", | |
65 "break", | |
66 "case", | |
67 "catch", | |
68 "class", | |
69 "const", | |
70 "continue", | |
71 "default", | |
72 "do", | |
73 "else", | |
74 "enum", | |
75 "extends", | |
76 "false", | |
77 "final", | |
78 "finally", | |
79 "for", | |
80 "if", | |
81 "in", | |
82 "is", | |
83 "new", | |
84 "null", | |
85 "rethrow", | |
86 "return", | |
87 "super", | |
88 "switch", | |
89 "this", | |
90 "throw", | |
91 "true", | |
92 "try", | |
93 "var", | |
94 "void", | |
95 "while", | |
96 "with" | |
97 ]; | |
98 reservedWords.expand((w) => [w, "$w=", "x.$w" , "$w.x", "x.$w.x"]) | |
99 .forEach(invalidSymbol); | |
100 reservedWords.expand((w) => ["${w}_", "${w}\$", "${w}q"]) | |
101 .forEach(validSymbol); | |
102 | |
103 // Built-in identifiers are valid identifiers that are restricted from being | |
104 // used in some cases, but they are all valid symbols. | |
105 var builtInIdentifiers = [ | |
106 "abstract", | |
107 "as", | |
108 "dynamic", | |
109 "export", | |
110 "external", | |
111 "factory", | |
112 "get", | |
113 "implements", | |
114 "import", | |
115 "library", | |
116 "operator", | |
117 "part", | |
118 "set", | |
119 "static", | |
120 "typedef" | |
121 ]; | |
122 builtInIdentifiers.expand((w) => [w, "$w=", "x.$w" , "$w.x", "x.$w.x", | |
123 "$w=", "x.$w="]) | |
124 .forEach(validSymbol); | |
39 } | 125 } |
OLD | NEW |