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