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