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