Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(293)

Unified Diff: lib/src/backend/platform_selector.dart

Issue 1004013002: Add support for evaluating platform selectors. (Closed) Base URL: git@github.com:dart-lang/unittest@master
Patch Set: Code review changes Created 5 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « lib/src/backend/operating_system.dart ('k') | lib/src/backend/platform_selector/ast.dart » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: lib/src/backend/platform_selector.dart
diff --git a/lib/src/backend/platform_selector.dart b/lib/src/backend/platform_selector.dart
new file mode 100644
index 0000000000000000000000000000000000000000..46e4db27aabc796340893b94f8db261585d2ac9e
--- /dev/null
+++ b/lib/src/backend/platform_selector.dart
@@ -0,0 +1,59 @@
+// Copyright (c) 2015, the Dart project authors. Please see the AUTHORS file
+// for details. All rights reserved. Use of this source code is governed by a
+// BSD-style license that can be found in the LICENSE file.
+
+library unittest.backend.platform_selector;
+
+import 'package:source_span/source_span.dart';
+
+import 'operating_system.dart';
+import 'platform_selector/ast.dart';
+import 'platform_selector/evaluator.dart';
+import 'platform_selector/parser.dart';
+import 'platform_selector/visitor.dart';
+import 'test_platform.dart';
+
+/// The set of all valid variable names.
+final _validVariables =
+ new Set<String>.from(["posix", "dart-vm", "browser", "js", "blink"])
+ ..addAll(TestPlatform.all.map((platform) => platform.identifier))
+ ..addAll(OperatingSystem.all.map((os) => os.name));
+
+/// An expression for selecting certain platforms, including operating systems
+/// and browsers.
+///
+/// The syntax is mostly Dart's expression syntax restricted to boolean
+/// operations. See the README for full details.
+class PlatformSelector {
+ /// The parsed AST.
+ final Node _selector;
+
+ /// Parses [selector].
+ ///
+ /// This will throw a [SourceSpanFormatException] if the selector is
+ /// malformed or if it uses an undefined variable.
+ PlatformSelector.parse(String selector)
+ : _selector = new Parser(selector).parse() {
+ _selector.accept(const _VariableValidator());
+ }
+
+ /// Returns whether the selector matches the given [platform] and [os].
+ ///
+ /// [os] defaults to [OperatingSystem.none].
+ bool evaluate(TestPlatform platform, {OperatingSystem os}) =>
+ _selector.accept(new Evaluator(platform, os: os));
+}
+
+/// An AST visitor that ensures that all variables are valid.
+///
+/// This isn't done when evaluating to ensure that errors are eagerly detected,
+/// and it isn't done when parsing to avoid coupling the syntax too tightly to
+/// the semantics.
+class _VariableValidator extends RecursiveVisitor {
+ const _VariableValidator();
+
+ void visitVariable(VariableNode node) {
+ if (_validVariables.contains(node.name)) return;
+ throw new SourceSpanFormatException("Undefined variable.", node.span);
+ }
+}
« no previous file with comments | « lib/src/backend/operating_system.dart ('k') | lib/src/backend/platform_selector/ast.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698