Skip to content

Commit f52afbf

Browse files
authored
Utilize switch expressions (#327)
1 parent ee311e0 commit f52afbf

10 files changed

+94
-122
lines changed

analysis_options.yaml

+2
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ linter:
2525
- prefer_const_declarations
2626
- prefer_expression_function_bodies
2727
- prefer_final_locals
28+
- unnecessary_breaks
29+
- use_enums
2830
- use_if_null_to_convert_nulls_to_bools
2931
- use_raw_strings
3032
- use_string_buffers

lib/src/common/by.dart

+8-18
Original file line numberDiff line numberDiff line change
@@ -35,24 +35,14 @@ class By {
3535

3636
@override
3737
String toString() {
38-
var constructor = using;
39-
switch (using) {
40-
case 'link text':
41-
constructor = 'linkText';
42-
break;
43-
case 'partial link text':
44-
constructor = 'partialLinkText';
45-
break;
46-
case 'tag name':
47-
constructor = 'tagName';
48-
break;
49-
case 'class name':
50-
constructor = 'className';
51-
break;
52-
case 'css selector':
53-
constructor = 'cssSelector';
54-
break;
55-
}
38+
final constructor = switch (using) {
39+
'link text' => 'linkText',
40+
'partial link text' => 'partialLinkText',
41+
'tag name' => 'tagName',
42+
'class name' => 'className',
43+
'css selector' => 'cssSelector',
44+
_ => using
45+
};
5646
return 'By.$constructor($value)';
5747
}
5848

lib/src/common/exception.dart

+8-11
Original file line numberDiff line numberDiff line change
@@ -255,25 +255,22 @@ WebDriverException getExceptionFromJsonWireResponse(
255255
WebDriverException getExceptionFromW3cResponse({
256256
int? httpStatusCode,
257257
String? httpReasonPhrase,
258-
dynamic jsonResp,
258+
Object? jsonResp,
259259
}) {
260260
if (jsonResp is Map && jsonResp.keys.contains('value')) {
261261
final value = jsonResp['value'] as Map<String, Object?>;
262262

263-
switch (value['error']) {
264-
case 'invalid argument':
265-
return InvalidArgumentException(
263+
return switch (value['error']) {
264+
'invalid argument' => InvalidArgumentException(
266265
httpStatusCode,
267266
value['message'] as String?,
268-
);
269-
case 'no such element':
270-
return NoSuchElementException(
267+
),
268+
'no such element' => NoSuchElementException(
271269
httpStatusCode,
272270
value['message'] as String?,
273-
);
274-
default:
275-
return WebDriverException(httpStatusCode, value['message'] as String?);
276-
}
271+
),
272+
_ => WebDriverException(httpStatusCode, value['message'] as String?),
273+
};
277274
}
278275

279276
return InvalidResponseException(httpStatusCode, jsonResp.toString());

lib/src/common/request.dart

+20-6
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,30 @@
1+
// Copyright 2019 Google Inc. All Rights Reserved.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
115
import 'dart:convert';
216

3-
class HttpMethod {
17+
enum HttpMethod {
18+
httpGet('GET'),
19+
httpPost('POST'),
20+
httpDelete('DELETE');
21+
422
final String name;
523

6-
const HttpMethod._(this.name);
24+
const HttpMethod(this.name);
725

826
@override
927
String toString() => 'HttpMethod.$name';
10-
11-
static const httpGet = HttpMethod._('GET');
12-
static const httpPost = HttpMethod._('POST');
13-
static const httpDelete = HttpMethod._('DELETE');
1428
}
1529

1630
/// Request data to send to WebDriver.

lib/src/common/utils.dart

+5-10
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,8 @@ import '../handler/w3c_handler.dart';
44
import 'spec.dart';
55
import 'webdriver_handler.dart';
66

7-
WebDriverHandler getHandler(WebDriverSpec spec) {
8-
switch (spec) {
9-
case WebDriverSpec.JsonWire:
10-
return JsonWireWebDriverHandler();
11-
case WebDriverSpec.W3c:
12-
return W3cWebDriverHandler();
13-
case WebDriverSpec.Auto:
14-
return InferWebDriverHandler();
15-
}
16-
}
7+
WebDriverHandler getHandler(WebDriverSpec spec) => switch (spec) {
8+
WebDriverSpec.JsonWire => JsonWireWebDriverHandler(),
9+
WebDriverSpec.W3c => W3cWebDriverHandler(),
10+
WebDriverSpec.Auto => InferWebDriverHandler(),
11+
};

lib/src/handler/w3c/element_finder.dart

+14-23
Original file line numberDiff line numberDiff line change
@@ -9,31 +9,22 @@ class W3cElementFinder extends ElementFinder {
99
/// In principle, W3C spec implementations should be nearly the same as
1010
/// the existing JSON wire spec. In practice compliance is uneven.
1111
Map<String, String> _byToJson(By by) {
12-
String using;
13-
String value;
12+
final (using, value) = switch (by.using) {
13+
// This doesn't exist in the W3C spec.
14+
'id' => ('css selector', '#${by.value}'),
15+
16+
// This doesn't exist in the W3C spec.
17+
'name' => ('css selector', '[name=${by.value}]'),
18+
19+
// This is in the W3C spec, but not in geckodriver.
20+
'tag name' => ('css selector', by.value),
21+
22+
// This doesn't exist in the W3C spec.
23+
'class name' => ('css selector', '.${by.value}'),
1424

15-
switch (by.using) {
16-
case 'id': // This doesn't exist in the W3C spec.
17-
using = 'css selector';
18-
value = '#${by.value}';
19-
break;
20-
case 'name': // This doesn't exist in the W3C spec.
21-
using = 'css selector';
22-
value = '[name=${by.value}]';
23-
break;
24-
case 'tag name': // This is in the W3C spec, but not in geckodriver.
25-
using = 'css selector';
26-
value = by.value;
27-
break;
28-
case 'class name': // This doesn't exist in the W3C spec.
29-
using = 'css selector';
30-
value = '.${by.value}';
31-
break;
3225
// xpath, css selector, link text, partial link text, seem fine.
33-
default:
34-
using = by.using;
35-
value = by.value;
36-
}
26+
_ => (by.using, by.value),
27+
};
3728

3829
return {'using': using, 'value': value};
3930
}

lib/src/request/async_io_request_client.dart

+7-13
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import 'dart:async';
22
import 'dart:convert';
3-
import 'dart:io' show ContentType, HttpClient, HttpClientRequest, HttpHeaders;
3+
import 'dart:io' show ContentType, HttpClient, HttpHeaders;
44

55
import '../../support/async.dart';
66
import '../common/request.dart';
@@ -19,19 +19,13 @@ class AsyncIoRequestClient extends AsyncRequestClient {
1919
@override
2020
Future<WebDriverResponse> sendRaw(WebDriverRequest request) async {
2121
await _lock.acquire();
22-
late HttpClientRequest httpRequest;
2322

24-
switch (request.method) {
25-
case HttpMethod.httpGet:
26-
httpRequest = await client.getUrl(resolve(request.uri!));
27-
break;
28-
case HttpMethod.httpPost:
29-
httpRequest = await client.postUrl(resolve(request.uri!));
30-
break;
31-
case HttpMethod.httpDelete:
32-
httpRequest = await client.deleteUrl(resolve(request.uri!));
33-
break;
34-
}
23+
final requestUri = resolve(request.uri!);
24+
final httpRequest = switch (request.method!) {
25+
HttpMethod.httpGet => await client.getUrl(requestUri),
26+
HttpMethod.httpPost => await client.postUrl(requestUri),
27+
HttpMethod.httpDelete => await client.deleteUrl(requestUri)
28+
};
3529

3630
httpRequest.followRedirects = true;
3731
_headers.forEach(httpRequest.headers.add);

lib/src/request/sync_http_request_client.dart

+6-8
Original file line numberDiff line numberDiff line change
@@ -13,20 +13,18 @@ class SyncHttpRequestClient extends SyncRequestClient {
1313

1414
@override
1515
WebDriverResponse sendRaw(WebDriverRequest request) {
16-
late SyncHttpClientRequest httpRequest;
17-
switch (request.method) {
16+
final requestUri = resolve(request.uri!);
17+
final SyncHttpClientRequest httpRequest;
18+
switch (request.method!) {
1819
case HttpMethod.httpGet:
19-
httpRequest = SyncHttpClient.getUrl(resolve(request.uri!));
20-
break;
20+
httpRequest = SyncHttpClient.getUrl(requestUri);
2121
case HttpMethod.httpPost:
22-
httpRequest = SyncHttpClient.postUrl(resolve(request.uri!));
22+
httpRequest = SyncHttpClient.postUrl(requestUri);
2323
httpRequest.headers.contentType =
2424
ContentType('application', 'json', charset: 'utf-8');
2525
httpRequest.write(request.body);
26-
break;
2726
case HttpMethod.httpDelete:
28-
httpRequest = SyncHttpClient.deleteUrl(resolve(request.uri!));
29-
break;
27+
httpRequest = SyncHttpClient.deleteUrl(requestUri);
3028
}
3129

3230
_headers.forEach(httpRequest.headers.add);

lib/support/stdio_stepper.dart

-1
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,6 @@ class StdioStepper implements Stepper {
5151
case 'help':
5252
case 'h':
5353
_printUsage();
54-
break;
5554
case 'disable':
5655
case 'd':
5756
enabled = false;

test/configs/common_config.dart

+24-32
Original file line numberDiff line numberDiff line change
@@ -35,40 +35,32 @@ String get testHostname => '127.0.0.1';
3535

3636
String get testHomePath => path.absolute('test');
3737

38-
Uri? getWebDriverUri(WebDriverSpec spec) {
39-
switch (spec) {
40-
case WebDriverSpec.W3c:
41-
return _defaultFirefoxUri;
42-
case WebDriverSpec.JsonWire:
43-
return _defaultChromeUri;
44-
default:
45-
return null;
46-
}
47-
}
38+
Uri? getWebDriverUri(WebDriverSpec spec) => switch (spec) {
39+
WebDriverSpec.W3c => _defaultFirefoxUri,
40+
WebDriverSpec.JsonWire => _defaultChromeUri,
41+
_ => null,
42+
};
4843

49-
Map<String, dynamic> getCapabilities(WebDriverSpec spec) {
50-
switch (spec) {
51-
case WebDriverSpec.W3c:
52-
return Capabilities.firefox;
53-
case WebDriverSpec.JsonWire:
54-
final capabilities = Capabilities.chrome;
55-
final env = Platform.environment;
44+
Map<String, Object?> getCapabilities(WebDriverSpec spec) => switch (spec) {
45+
WebDriverSpec.W3c => Capabilities.firefox,
46+
WebDriverSpec.JsonWire => () {
47+
final capabilities = Capabilities.chrome;
48+
final env = Platform.environment;
5649

57-
final chromeOptions = <String, dynamic>{};
50+
final chromeOptions = <String, Object?>{};
5851

59-
if (env['CHROMEDRIVER_BINARY'] != null) {
60-
chromeOptions['binary'] = env['CHROMEDRIVER_BINARY'];
61-
}
52+
if (env['CHROMEDRIVER_BINARY'] != null) {
53+
chromeOptions['binary'] = env['CHROMEDRIVER_BINARY'];
54+
}
6255

63-
if (env['CHROMEDRIVER_ARGS'] != null) {
64-
chromeOptions['args'] = env['CHROMEDRIVER_ARGS']!.split(' ');
65-
}
56+
if (env['CHROMEDRIVER_ARGS'] != null) {
57+
chromeOptions['args'] = env['CHROMEDRIVER_ARGS']!.split(' ');
58+
}
6659

67-
if (chromeOptions.isNotEmpty) {
68-
capabilities[Capabilities.chromeOptions] = chromeOptions;
69-
}
70-
return capabilities;
71-
default:
72-
return <String, dynamic>{};
73-
}
74-
}
60+
if (chromeOptions.isNotEmpty) {
61+
capabilities[Capabilities.chromeOptions] = chromeOptions;
62+
}
63+
return capabilities;
64+
}(),
65+
_ => <String, Object?>{}
66+
};

0 commit comments

Comments
 (0)