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 'runtime.dart'; 10 : import 'suite_platform.dart'; 11 : 12 : /// The set of variable names that are valid for all platform selectors. 13 0 : final _universalValidVariables = { 14 : 'posix', 15 : 'dart-vm', 16 : 'browser', 17 : 'js', 18 : 'blink', 19 : 'google', 20 : for (var runtime in Runtime.builtIn) runtime.identifier, 21 : for (var os in OperatingSystem.all) os.identifier, 22 : }; 23 : 24 : /// An expression for selecting certain platforms, including operating systems 25 : /// and browsers. 26 : /// 27 : /// This uses the [boolean selector][] syntax. 28 : /// 29 : /// [boolean selector]: https://pub.dev/packages/boolean_selector 30 : class PlatformSelector { 31 : /// A selector that declares that a test can be run on all platforms. 32 : static const all = PlatformSelector._(BooleanSelector.all); 33 : 34 : /// The boolean selector used to implement this selector. 35 : final BooleanSelector _inner; 36 : 37 : /// The source span from which this selector was parsed. 38 : final SourceSpan? _span; 39 : 40 : /// Parses [selector]. 41 : /// 42 : /// If [span] is passed, it indicates the location of the text for [selector] 43 : /// in a larger document. It's used for error reporting. 44 5 : PlatformSelector.parse(String selector, [SourceSpan? span]) 45 : : _inner = 46 15 : _wrapFormatException(() => BooleanSelector.parse(selector), span), 47 : _span = span; 48 : 49 11 : const PlatformSelector._(this._inner) : _span = null; 50 : 51 : /// Runs [body] and wraps any [FormatException] it throws in a 52 : /// [SourceSpanFormatException] using [span]. 53 : /// 54 : /// If [span] is `null`, runs [body] as-is. 55 5 : static T _wrapFormatException<T>(T Function() body, [SourceSpan? span]) { 56 : if (span == null) return body(); 57 : 58 : try { 59 : return body(); 60 0 : } on FormatException catch (error) { 61 0 : throw SourceSpanFormatException(error.message, span); 62 : } 63 : } 64 : 65 : /// Throws a [FormatException] if this selector uses any variables that don't 66 : /// appear either in [validVariables] or in the set of variables that are 67 : /// known to be valid for all selectors. 68 11 : void validate(Set<String> validVariables) { 69 : if (identical(this, all)) return; 70 : 71 0 : _wrapFormatException( 72 0 : () => _inner.validate((name) => 73 0 : _universalValidVariables.contains(name) || 74 0 : validVariables.contains(name)), 75 0 : _span); 76 : } 77 : 78 : /// Returns whether the selector matches the given [platform]. 79 : /// 80 : /// [os] defaults to [OperatingSystem.none]. 81 11 : bool evaluate(SuitePlatform platform) { 82 27 : return _inner.evaluate((String variable) { 83 15 : if (variable == platform.runtime.identifier) return true; 84 0 : if (variable == platform.runtime.parent?.identifier) return true; 85 0 : if (variable == platform.os.identifier) return true; 86 : switch (variable) { 87 0 : case 'dart-vm': 88 0 : return platform.runtime.isDartVM; 89 0 : case 'browser': 90 0 : return platform.runtime.isBrowser; 91 0 : case 'js': 92 0 : return platform.runtime.isJS; 93 0 : case 'blink': 94 0 : return platform.runtime.isBlink; 95 0 : case 'posix': 96 0 : return platform.os.isPosix; 97 0 : case 'google': 98 0 : return platform.inGoogle; 99 : default: 100 : return false; 101 : } 102 : }); 103 : } 104 : 105 : /// Returns a new [PlatformSelector] that matches only platforms matched by 106 : /// both [this] and [other]. 107 11 : PlatformSelector intersection(PlatformSelector other) { 108 11 : if (other == PlatformSelector.all) return this; 109 0 : return PlatformSelector._(_inner.intersection(other._inner)); 110 : } 111 : 112 5 : @override 113 10 : String toString() => _inner.toString(); 114 : 115 11 : @override 116 : bool operator ==(other) => 117 44 : other is PlatformSelector && _inner == other._inner; 118 : 119 0 : @override 120 0 : int get hashCode => _inner.hashCode; 121 : }