Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2015, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2015, 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 unittest.backend.platform_selector; | 5 library unittest.backend.platform_selector; |
| 6 | 6 |
| 7 import 'package:source_span/source_span.dart'; | 7 import 'package:source_span/source_span.dart'; |
| 8 | 8 |
| 9 import 'operating_system.dart'; | 9 import 'operating_system.dart'; |
| 10 import 'platform_selector/ast.dart'; | 10 import 'platform_selector/ast.dart'; |
| 11 import 'platform_selector/evaluator.dart'; | 11 import 'platform_selector/evaluator.dart'; |
| 12 import 'platform_selector/parser.dart'; | 12 import 'platform_selector/parser.dart'; |
| 13 import 'platform_selector/visitor.dart'; | 13 import 'platform_selector/visitor.dart'; |
| 14 import 'test_platform.dart'; | 14 import 'test_platform.dart'; |
| 15 | 15 |
| 16 /// The set of all valid variable names. | 16 /// The set of all valid variable names. |
| 17 final _validVariables = | 17 final _validVariables = |
| 18 new Set<String>.from(["posix", "dart-vm", "browser", "js", "blink"]) | 18 new Set<String>.from(["posix", "dart-vm", "browser", "js", "blink"]) |
| 19 ..addAll(TestPlatform.all.map((platform) => platform.identifier)) | 19 ..addAll(TestPlatform.all.map((platform) => platform.identifier)) |
| 20 ..addAll(OperatingSystem.all.map((os) => os.name)); | 20 ..addAll(OperatingSystem.all.map((os) => os.name)); |
| 21 | 21 |
| 22 /// An expression for selecting certain platforms, including operating systems | 22 /// An expression for selecting certain platforms, including operating systems |
| 23 /// and browsers. | 23 /// and browsers. |
| 24 /// | 24 /// |
| 25 /// The syntax is mostly Dart's expression syntax restricted to boolean | 25 /// The syntax is mostly Dart's expression syntax restricted to boolean |
| 26 /// operations. See the README for full details. | 26 /// operations. See [the README][] for full details. |
| 27 class PlatformSelector { | 27 /// |
| 28 /// The parsed AST. | 28 /// [the README]: https://github.com/dart-lang/unittest/#platform-selector-synta x |
|
kevmoo
2015/03/25 08:06:55
nit: provide a shortname for the URL so it doesn't
nweiz
2015/03/25 22:40:55
I don't think there's any reason to try to shorten
| |
| 29 final Node _selector; | 29 abstract class PlatformSelector { |
| 30 /// A selector that declares that a test can be run on all platforms. | |
| 31 /// | |
| 32 /// This isn't representable in the platform selector syntax but it is the | |
| 33 /// default selector. | |
| 34 static const all = const _AllPlatforms(); | |
| 30 | 35 |
| 31 /// Parses [selector]. | 36 /// Parses [selector]. |
| 32 /// | 37 /// |
| 33 /// This will throw a [SourceSpanFormatException] if the selector is | 38 /// This will throw a [SourceSpanFormatException] if the selector is |
| 34 /// malformed or if it uses an undefined variable. | 39 /// malformed or if it uses an undefined variable. |
| 35 PlatformSelector.parse(String selector) | 40 factory PlatformSelector.parse(String selector) => |
| 41 new _PlatformSelector.parse(selector); | |
| 42 | |
| 43 /// Returns whether the selector matches the given [platform] and [os]. | |
| 44 /// | |
| 45 /// [os] defaults to [OperatingSystem.none]. | |
| 46 bool evaluate(TestPlatform platform, {OperatingSystem os}); | |
| 47 } | |
| 48 | |
| 49 /// The concrete implementation of a [PlatformSelector] parsed from a string. | |
| 50 /// | |
| 51 /// This is separate from [PlatformSelector] so that [_AllPlatforms] can | |
| 52 /// implement [PlatformSelector] without having to implement private members. | |
| 53 class _PlatformSelector implements PlatformSelector{ | |
| 54 /// The parsed AST. | |
| 55 final Node _selector; | |
| 56 | |
| 57 _PlatformSelector.parse(String selector) | |
| 36 : _selector = new Parser(selector).parse() { | 58 : _selector = new Parser(selector).parse() { |
| 37 _selector.accept(const _VariableValidator()); | 59 _selector.accept(const _VariableValidator()); |
| 38 } | 60 } |
| 39 | 61 |
| 40 /// Returns whether the selector matches the given [platform] and [os]. | 62 _PlatformSelector(this._selector); |
| 41 /// | 63 |
| 42 /// [os] defaults to [OperatingSystem.none]. | |
| 43 bool evaluate(TestPlatform platform, {OperatingSystem os}) => | 64 bool evaluate(TestPlatform platform, {OperatingSystem os}) => |
| 44 _selector.accept(new Evaluator(platform, os: os)); | 65 _selector.accept(new Evaluator(platform, os: os)); |
| 66 | |
| 67 String toString() => _selector.toString(); | |
| 68 } | |
| 69 | |
| 70 /// A selector that matches all platforms. | |
| 71 class _AllPlatforms implements PlatformSelector { | |
| 72 const _AllPlatforms(); | |
| 73 | |
| 74 bool evaluate(TestPlatform platform, {OperatingSystem os}) => true; | |
| 75 | |
| 76 String toString() => "*"; | |
| 45 } | 77 } |
| 46 | 78 |
| 47 /// An AST visitor that ensures that all variables are valid. | 79 /// An AST visitor that ensures that all variables are valid. |
| 48 /// | 80 /// |
| 49 /// This isn't done when evaluating to ensure that errors are eagerly detected, | 81 /// This isn't done when evaluating to ensure that errors are eagerly detected, |
| 50 /// and it isn't done when parsing to avoid coupling the syntax too tightly to | 82 /// and it isn't done when parsing to avoid coupling the syntax too tightly to |
| 51 /// the semantics. | 83 /// the semantics. |
| 52 class _VariableValidator extends RecursiveVisitor { | 84 class _VariableValidator extends RecursiveVisitor { |
| 53 const _VariableValidator(); | 85 const _VariableValidator(); |
| 54 | 86 |
| 55 void visitVariable(VariableNode node) { | 87 void visitVariable(VariableNode node) { |
| 56 if (_validVariables.contains(node.name)) return; | 88 if (_validVariables.contains(node.name)) return; |
| 57 throw new SourceSpanFormatException("Undefined variable.", node.span); | 89 throw new SourceSpanFormatException("Undefined variable.", node.span); |
| 58 } | 90 } |
| 59 } | 91 } |
| OLD | NEW |