Skip to content

Commit

Permalink
Use switch expressions for switch/return pattern (#2027)
Browse files Browse the repository at this point in the history
Find switch statements which had all cases either immediately `return`
or `throw` and refactor them to returned switch expressions.
  • Loading branch information
natebosch committed Jun 1, 2023
1 parent 06bdbb6 commit 23bd415
Show file tree
Hide file tree
Showing 8 changed files with 72 additions and 120 deletions.
28 changes: 11 additions & 17 deletions pkgs/test/lib/src/runner/browser/browser_manager.dart
Original file line number Diff line number Diff line change
Expand Up @@ -152,23 +152,17 @@ class BrowserManager {
///
/// If [debug] is true, starts the browser in debug mode.
static Browser _newBrowser(Uri url, Runtime browser,
ExecutableSettings settings, Configuration configuration) {
switch (browser.root) {
case Runtime.chrome:
case Runtime.experimentalChromeWasm:
return Chrome(url, configuration, settings: settings);
case Runtime.firefox:
return Firefox(url, settings: settings);
case Runtime.safari:
return Safari(url, settings: settings);
case Runtime.internetExplorer:
return InternetExplorer(url, settings: settings);
case Runtime.edge:
return MicrosoftEdge(url, configuration, settings: settings);
default:
throw ArgumentError('$browser is not a browser.');
}
}
ExecutableSettings settings, Configuration configuration) =>
switch (browser.root) {
Runtime.chrome ||
Runtime.experimentalChromeWasm =>
Chrome(url, configuration, settings: settings),
Runtime.firefox => Firefox(url, settings: settings),
Runtime.safari => Safari(url, settings: settings),
Runtime.internetExplorer => InternetExplorer(url, settings: settings),
Runtime.edge => MicrosoftEdge(url, configuration, settings: settings),
_ => throw ArgumentError('$browser is not a browser.'),
};

/// Creates a new BrowserManager that communicates with [browser] over
/// [webSocket].
Expand Down
27 changes: 9 additions & 18 deletions pkgs/test_api/lib/src/backend/configuration/timeout.dart
Original file line number Diff line number Diff line change
Expand Up @@ -106,24 +106,15 @@ final class Timeout {
}

/// Returns the number of microseconds in [number] [unit]s.
static double _microsecondsFor(double number, String unit) {
switch (unit) {
case 'd':
return number * 24 * 60 * 60 * 1000000;
case 'h':
return number * 60 * 60 * 1000000;
case 'm':
return number * 60 * 1000000;
case 's':
return number * 1000000;
case 'ms':
return number * 1000;
case 'us':
return number;
default:
throw ArgumentError('Unknown unit $unit.');
}
}
static double _microsecondsFor(double number, String unit) => switch (unit) {
'd' => number * 24 * 60 * 60 * 1000000,
'h' => number * 60 * 60 * 1000000,
'm' => number * 60 * 1000000,
's' => number * 1000000,
'ms' => number * 1000,
'us' => number,
_ => throw ArgumentError('Unknown unit $unit.'),
};

/// Returns a new [Timeout] that merges [this] with [other].
///
Expand Down
15 changes: 5 additions & 10 deletions pkgs/test_api/lib/src/backend/message.dart
Original file line number Diff line number Diff line change
Expand Up @@ -28,16 +28,11 @@ class MessageType {
/// The name of the message type.
final String name;

factory MessageType.parse(String name) {
switch (name) {
case 'print':
return MessageType.print;
case 'skip':
return MessageType.skip;
default:
throw ArgumentError('Invalid message type "$name".');
}
}
factory MessageType.parse(String name) => switch (name) {
'print' => MessageType.print,
'skip' => MessageType.skip,
_ => throw ArgumentError('Invalid message type "$name".'),
};

const MessageType._(this.name);

Expand Down
24 changes: 8 additions & 16 deletions pkgs/test_api/lib/src/backend/operating_system.dart
Original file line number Diff line number Diff line change
Expand Up @@ -49,22 +49,14 @@ class OperatingSystem {
/// `Platform.operatingSystem`.
///
/// If no operating system is found, returns [none].
static OperatingSystem findByIoName(String name) {
switch (name) {
case 'windows':
return windows;
case 'macos':
return macOS;
case 'linux':
return linux;
case 'android':
return android;
case 'ios':
return iOS;
default:
return none;
}
}
static OperatingSystem findByIoName(String name) => switch (name) {
'windows' => windows,
'macos' => macOS,
'linux' => linux,
'android' => android,
'ios' => iOS,
_ => none,
};

/// The human-friendly of the operating system.
final String name;
Expand Down
43 changes: 17 additions & 26 deletions pkgs/test_api/lib/src/backend/platform_selector.dart
Original file line number Diff line number Diff line change
Expand Up @@ -79,32 +79,23 @@ final class PlatformSelector {
}

/// Returns whether the selector matches the given [platform].
bool evaluate(SuitePlatform platform) {
return _inner.evaluate((String variable) {
if (variable == platform.runtime.identifier) return true;
if (variable == platform.runtime.parent?.identifier) return true;
if (variable == platform.os.identifier) return true;
if (variable == platform.compiler.identifier) return true;
switch (variable) {
case 'dart-vm':
return platform.runtime.isDartVM;
case 'browser':
return platform.runtime.isBrowser;
case 'js':
return platform.runtime.isJS;
case 'blink':
return platform.runtime.isBlink;
case 'posix':
return platform.os.isPosix;
case 'google':
return platform.inGoogle;
case 'wasm':
return platform.runtime.isWasm;
default:
return false;
}
});
}
bool evaluate(SuitePlatform platform) =>
_inner.evaluate((String variable) => switch (variable) {
_
when variable == platform.runtime.identifier ||
variable == platform.runtime.parent?.identifier ||
variable == platform.os.identifier ||
variable == platform.compiler.identifier =>
true,
'dart-vm' => platform.runtime.isDartVM,
'browser' => platform.runtime.isBrowser,
'js' => platform.runtime.isJS,
'blink' => platform.runtime.isBlink,
'posix' => platform.os.isPosix,
'google' => platform.inGoogle,
'wasm' => platform.runtime.isWasm,
_ => false,
});

/// Returns a new [PlatformSelector] that matches only platforms matched by
/// both [this] and [other].
Expand Down
10 changes: 4 additions & 6 deletions pkgs/test_api/lib/src/backend/remote_exception.dart
Original file line number Diff line number Diff line change
Expand Up @@ -67,12 +67,10 @@ final class RemoteException implements Exception {
final type = serialized['type'] as String;
final toString = serialized['toString'] as String;

switch (serialized['supertype'] as String?) {
case 'TestFailure':
return _RemoteTestFailure(message, type, toString);
default:
return RemoteException._(message, type, toString);
}
return switch (serialized['supertype'] as String?) {
'TestFailure' => _RemoteTestFailure(message, type, toString),
_ => RemoteException._(message, type, toString),
};
}

RemoteException._(this.message, this.type, this._toString);
Expand Down
18 changes: 6 additions & 12 deletions pkgs/test_core/lib/src/runner/spawn_hybrid.dart
Original file line number Diff line number Diff line change
Expand Up @@ -159,15 +159,9 @@ Future<String> _languageVersionCommentFor(String url) async {
return '';
}

Future<String> _readUri(Uri uri) async {
switch (uri.scheme) {
case '':
case 'file':
return File.fromUri(uri).readAsString();
case 'data':
return uri.data!.contentAsString();
default:
throw ArgumentError.value(uri, 'uri',
'Only data and file uris (as well as relative paths) are supported');
}
}
Future<String> _readUri(Uri uri) async => switch (uri.scheme) {
'' || 'file' => await File.fromUri(uri).readAsString(),
'data' => uri.data!.contentAsString(),
_ => throw ArgumentError.value(uri, 'uri',
'Only data and file uris (as well as relative paths) are supported'),
};
27 changes: 12 additions & 15 deletions pkgs/test_core/lib/src/runner/vm/platform.dart
Original file line number Diff line number Diff line change
Expand Up @@ -226,21 +226,18 @@ stderr: ${processResult.stderr}''');
return _spawnPubServeIsolate(
path, message, _config.pubServeUrl!, compiler);
}
switch (compiler) {
case Compiler.kernel:
return _spawnIsolateWithUri(
await _compileToKernel(path, suiteMetadata), message);
case Compiler.source:
return _spawnIsolateWithUri(
_bootstrapIsolateTestFile(
path,
suiteMetadata.languageVersionComment ??
await rootPackageLanguageVersionComment),
message);
default:
throw StateError(
'Unsupported compiler $compiler for the VM platform');
}
return switch (compiler) {
Compiler.kernel => _spawnIsolateWithUri(
await _compileToKernel(path, suiteMetadata), message),
Compiler.source => _spawnIsolateWithUri(
_bootstrapIsolateTestFile(
path,
suiteMetadata.languageVersionComment ??
await rootPackageLanguageVersionComment),
message),
_ => throw StateError(
'Unsupported compiler $compiler for the VM platform'),
};
} catch (_) {
if (_closeMemo.hasRun) return null;
rethrow;
Expand Down

0 comments on commit 23bd415

Please sign in to comment.