Line data Source code
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
3 : // BSD-style license that can be found in the LICENSE file.
4 :
5 : import 'package:boolean_selector/boolean_selector.dart';
6 : import 'package:source_span/source_span.dart';
7 :
8 : import 'operating_system.dart';
9 : import 'test_platform.dart';
10 :
11 : /// The set of all valid variable names.
12 : final _validVariables =
13 : new Set<String>.from(["posix", "dart-vm", "browser", "js", "blink"])
14 : ..addAll(TestPlatform.all.map((platform) => platform.identifier))
15 : ..addAll(OperatingSystem.all.map((os) => os.identifier));
16 :
17 : /// An expression for selecting certain platforms, including operating systems
18 : /// and browsers.
19 : ///
20 : /// This uses the [boolean selector][] syntax.
21 : ///
22 : /// [boolean selector]: https://pub.dartlang.org/packages/boolean_selector
23 : class PlatformSelector {
24 : /// A selector that declares that a test can be run on all platforms.
25 : static const all = const PlatformSelector._(BooleanSelector.all);
26 :
27 : /// The boolean selector used to implement this selector.
28 : final BooleanSelector _inner;
29 :
30 : /// Parses [selector].
31 : ///
32 : /// This will throw a [SourceSpanFormatException] if the selector is
33 : /// malformed or if it uses an undefined variable.
34 : PlatformSelector.parse(String selector)
35 0 : : _inner = new BooleanSelector.parse(selector) {
36 0 : _inner.validate(_validVariables.contains);
37 : }
38 :
39 5 : const PlatformSelector._(this._inner);
40 :
41 : /// Returns whether the selector matches the given [platform] and [os].
42 : ///
43 : /// [os] defaults to [OperatingSystem.none].
44 : bool evaluate(TestPlatform platform, {OperatingSystem os}) {
45 : os ??= OperatingSystem.none;
46 :
47 10 : return _inner.evaluate((variable) {
48 0 : if (variable == platform.identifier) return true;
49 0 : if (variable == os.identifier) return true;
50 : switch (variable) {
51 0 : case "dart-vm":
52 0 : return platform.isDartVM;
53 0 : case "browser":
54 0 : return platform.isBrowser;
55 0 : case "js":
56 0 : return platform.isJS;
57 0 : case "blink":
58 0 : return platform.isBlink;
59 0 : case "posix":
60 0 : return os.isPosix;
61 : default:
62 : return false;
63 : }
64 : });
65 : }
66 :
67 : /// Returns a new [PlatformSelector] that matches only platforms matched by
68 : /// both [this] and [other].
69 : PlatformSelector intersection(PlatformSelector other) {
70 5 : if (other == PlatformSelector.all) return this;
71 0 : return new PlatformSelector._(_inner.intersection(other._inner));
72 : }
73 :
74 0 : String toString() => _inner.toString();
75 :
76 : bool operator ==(other) =>
77 20 : other is PlatformSelector && _inner == other._inner;
78 :
79 0 : int get hashCode => _inner.hashCode;
80 : }
|