| 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'; |
| (...skipping 26 matching lines...) Expand all Loading... |
| 37 /// | 37 /// |
| 38 /// This will throw a [SourceSpanFormatException] if the selector is | 38 /// This will throw a [SourceSpanFormatException] if the selector is |
| 39 /// malformed or if it uses an undefined variable. | 39 /// malformed or if it uses an undefined variable. |
| 40 factory PlatformSelector.parse(String selector) => | 40 factory PlatformSelector.parse(String selector) => |
| 41 new _PlatformSelector.parse(selector); | 41 new _PlatformSelector.parse(selector); |
| 42 | 42 |
| 43 /// Returns whether the selector matches the given [platform] and [os]. | 43 /// Returns whether the selector matches the given [platform] and [os]. |
| 44 /// | 44 /// |
| 45 /// [os] defaults to [OperatingSystem.none]. | 45 /// [os] defaults to [OperatingSystem.none]. |
| 46 bool evaluate(TestPlatform platform, {OperatingSystem os}); | 46 bool evaluate(TestPlatform platform, {OperatingSystem os}); |
| 47 |
| 48 /// Returns a new [PlatformSelector] that matches only platforms matched by |
| 49 /// both [this] and [other]. |
| 50 PlatformSelector intersect(PlatformSelector other); |
| 47 } | 51 } |
| 48 | 52 |
| 49 /// The concrete implementation of a [PlatformSelector] parsed from a string. | 53 /// The concrete implementation of a [PlatformSelector] parsed from a string. |
| 50 /// | 54 /// |
| 51 /// This is separate from [PlatformSelector] so that [_AllPlatforms] can | 55 /// This is separate from [PlatformSelector] so that [_AllPlatforms] can |
| 52 /// implement [PlatformSelector] without having to implement private members. | 56 /// implement [PlatformSelector] without having to implement private members. |
| 53 class _PlatformSelector implements PlatformSelector{ | 57 class _PlatformSelector implements PlatformSelector{ |
| 54 /// The parsed AST. | 58 /// The parsed AST. |
| 55 final Node _selector; | 59 final Node _selector; |
| 56 | 60 |
| 57 _PlatformSelector.parse(String selector) | 61 _PlatformSelector.parse(String selector) |
| 58 : _selector = new Parser(selector).parse() { | 62 : _selector = new Parser(selector).parse() { |
| 59 _selector.accept(const _VariableValidator()); | 63 _selector.accept(const _VariableValidator()); |
| 60 } | 64 } |
| 61 | 65 |
| 62 _PlatformSelector(this._selector); | 66 _PlatformSelector(this._selector); |
| 63 | 67 |
| 64 bool evaluate(TestPlatform platform, {OperatingSystem os}) => | 68 bool evaluate(TestPlatform platform, {OperatingSystem os}) => |
| 65 _selector.accept(new Evaluator(platform, os: os)); | 69 _selector.accept(new Evaluator(platform, os: os)); |
| 66 | 70 |
| 71 PlatformSelector intersect(PlatformSelector other) { |
| 72 if (other == PlatformSelector.all) return this; |
| 73 return new _PlatformSelector(new AndNode( |
| 74 _selector, (other as _PlatformSelector)._selector)); |
| 75 } |
| 76 |
| 67 String toString() => _selector.toString(); | 77 String toString() => _selector.toString(); |
| 68 } | 78 } |
| 69 | 79 |
| 70 /// A selector that matches all platforms. | 80 /// A selector that matches all platforms. |
| 71 class _AllPlatforms implements PlatformSelector { | 81 class _AllPlatforms implements PlatformSelector { |
| 72 const _AllPlatforms(); | 82 const _AllPlatforms(); |
| 73 | 83 |
| 74 bool evaluate(TestPlatform platform, {OperatingSystem os}) => true; | 84 bool evaluate(TestPlatform platform, {OperatingSystem os}) => true; |
| 75 | 85 |
| 86 PlatformSelector intersect(PlatformSelector other) => other; |
| 87 |
| 76 String toString() => "*"; | 88 String toString() => "*"; |
| 77 } | 89 } |
| 78 | 90 |
| 79 /// An AST visitor that ensures that all variables are valid. | 91 /// An AST visitor that ensures that all variables are valid. |
| 80 /// | 92 /// |
| 81 /// This isn't done when evaluating to ensure that errors are eagerly detected, | 93 /// This isn't done when evaluating to ensure that errors are eagerly detected, |
| 82 /// and it isn't done when parsing to avoid coupling the syntax too tightly to | 94 /// and it isn't done when parsing to avoid coupling the syntax too tightly to |
| 83 /// the semantics. | 95 /// the semantics. |
| 84 class _VariableValidator extends RecursiveVisitor { | 96 class _VariableValidator extends RecursiveVisitor { |
| 85 const _VariableValidator(); | 97 const _VariableValidator(); |
| 86 | 98 |
| 87 void visitVariable(VariableNode node) { | 99 void visitVariable(VariableNode node) { |
| 88 if (_validVariables.contains(node.name)) return; | 100 if (_validVariables.contains(node.name)) return; |
| 89 throw new SourceSpanFormatException("Undefined variable.", node.span); | 101 throw new SourceSpanFormatException("Undefined variable.", node.span); |
| 90 } | 102 } |
| 91 } | 103 } |
| OLD | NEW |