Skip to content

Commit fc85a56

Browse files
authored
Simplify the TestFileArgs constructors (#9005)
1 parent 6b3efe2 commit fc85a56

2 files changed

Lines changed: 48 additions & 64 deletions

File tree

packages/devtools_app/integration_test/test_infra/run/_in_file_args.dart

Lines changed: 32 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,8 @@ import 'dart:io';
77

88
import '_test_app_driver.dart';
99

10-
enum TestFileArgItems { experimentsOn, appPath }
11-
12-
const _defaultFlutterAppPath = 'test/test_infra/fixtures/flutter_app';
13-
const _defaultDartCliAppPath = 'test/test_infra/fixtures/empty_app.dart';
10+
const defaultFlutterAppPath = 'test/test_infra/fixtures/flutter_app';
11+
const defaultDartCliAppPath = 'test/test_infra/fixtures/empty_app.dart';
1412

1513
/// Test arguments, defined inside the test file as a comment.
1614
class TestFileArgs {
@@ -22,56 +20,46 @@ class TestFileArgs {
2220
return TestFileArgs.fromFileContent(content, testAppDevice: testAppDevice);
2321
}
2422

23+
/// Returns a [TestFileArgs] parsed from [fileContent].
24+
///
25+
/// Separate from [TestFileArgs.new] for easier testing.
2526
factory TestFileArgs.fromFileContent(
2627
String fileContent, {
2728
required TestAppDevice testAppDevice,
2829
}) {
29-
final testFileArgItems = _parseFileContent(fileContent);
30-
31-
for (final arg in testFileArgItems.keys) {
32-
testFileArgItems.putIfAbsent(arg, () => null);
33-
}
34-
35-
return TestFileArgs.parse(testFileArgItems, testAppDevice: testAppDevice);
30+
final args = _parseFileContent(fileContent);
31+
final appPath =
32+
args[_TestFileArgItems.appPath] ??
33+
(testAppDevice == TestAppDevice.cli
34+
? defaultDartCliAppPath
35+
: defaultFlutterAppPath);
36+
return TestFileArgs._parse(args, appPath: appPath);
3637
}
3738

38-
TestFileArgs.parse(
39-
Map<TestFileArgItems, dynamic> map, {
40-
required TestAppDevice testAppDevice,
41-
}) : experimentsOn = map[TestFileArgItems.experimentsOn] ?? false,
42-
appPath =
43-
map[TestFileArgItems.appPath] ??
44-
(testAppDevice == TestAppDevice.cli
45-
? _defaultDartCliAppPath
46-
: _defaultFlutterAppPath);
39+
TestFileArgs._parse(
40+
Map<_TestFileArgItems, dynamic> args, {
41+
required this.appPath,
42+
}) : experimentsOn = args[_TestFileArgItems.experimentsOn] ?? false;
4743

48-
/// If true, experiments will be enabled in the test.
44+
/// Whether experiments are enabled in the test.
4945
final bool experimentsOn;
5046

51-
/// Path to the application to connect to.
47+
/// The path to the application to connect to.
5248
final String appPath;
53-
}
5449

55-
final _argRegex = RegExp(
56-
r'^\/\/\s*test-argument\s*:\s*(\w*)\s*=\s*(\S*)\s*$',
57-
multiLine: true,
58-
);
59-
60-
Map<TestFileArgItems, dynamic> _parseFileContent(String fileContent) {
61-
final matches = _argRegex.allMatches(fileContent);
62-
63-
final entries = matches.map<MapEntry<TestFileArgItems, dynamic>>((
64-
RegExpMatch m,
65-
) {
66-
final name = m.group(1) ?? '';
67-
if (name.isEmpty) {
68-
throw ArgumentError(
69-
'Name of test argument should be provided: [${m.group(0)}].',
70-
);
71-
}
72-
final value = m.group(2) ?? '';
73-
return MapEntry(TestFileArgItems.values.byName(name), jsonDecode(value));
74-
});
50+
/// Parses 'test-argument' comments in [fileContent].
51+
static Map<_TestFileArgItems, dynamic> _parseFileContent(String fileContent) {
52+
return {
53+
for (final m in _argRegex.allMatches(fileContent))
54+
_TestFileArgItems.values.byName(m.group(1)!): jsonDecode(m.group(2)!),
55+
};
56+
}
7557

76-
return Map.fromEntries(entries);
58+
static final _argRegex = RegExp(
59+
r'^\/\/\s*test-argument\s*:\s*(\w+)\s*=\s*(\S*)\s*$',
60+
multiLine: true,
61+
);
7762
}
63+
64+
/// The different arguments accepted as "file args."
65+
enum _TestFileArgItems { experimentsOn, appPath }

packages/devtools_app/test/integration_test_infra/in_file_args_test.dart

Lines changed: 16 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,13 @@ import '../../integration_test/test_infra/run/_test_app_driver.dart';
99

1010
const _testAppPath = 'test/test_infra/fixtures/memory_app';
1111

12-
final _defaultArgs = TestFileArgs.parse(
13-
{},
12+
final _defaultArgs = TestFileArgs.fromFileContent(
13+
'',
1414
testAppDevice: TestAppDevice.flutterTester,
1515
);
1616

17-
final _defaultArgsForCliDevice = TestFileArgs.parse(
18-
{},
17+
final _defaultArgsForCliDevice = TestFileArgs.fromFileContent(
18+
'',
1919
testAppDevice: TestAppDevice.cli,
2020
);
2121

@@ -24,19 +24,15 @@ final tests = [
2424
name: 'empty',
2525
input: '',
2626
testAppDevice: TestAppDevice.flutterTester,
27-
output: TestFileArgs.parse({
28-
TestFileArgItems.experimentsOn: _defaultArgs.experimentsOn,
29-
TestFileArgItems.appPath: _defaultArgs.appPath,
30-
}, testAppDevice: TestAppDevice.flutterTester),
27+
expectedExperimentsOn: _defaultArgs.experimentsOn,
28+
expectedAppPath: defaultFlutterAppPath,
3129
),
3230
_InFileTestArgsTest(
3331
name: 'empty',
3432
input: '',
3533
testAppDevice: TestAppDevice.cli,
36-
output: TestFileArgs.parse({
37-
TestFileArgItems.experimentsOn: _defaultArgsForCliDevice.experimentsOn,
38-
TestFileArgItems.appPath: _defaultArgsForCliDevice.appPath,
39-
}, testAppDevice: TestAppDevice.cli),
34+
expectedExperimentsOn: _defaultArgsForCliDevice.experimentsOn,
35+
expectedAppPath: defaultDartCliAppPath,
4036
),
4137
_InFileTestArgsTest(
4238
name: 'non-empty',
@@ -53,10 +49,8 @@ final tests = [
5349
import 'dart:ui' as ui;
5450
''',
5551
testAppDevice: TestAppDevice.flutterTester,
56-
output: TestFileArgs.parse({
57-
TestFileArgItems.experimentsOn: true,
58-
TestFileArgItems.appPath: _testAppPath,
59-
}, testAppDevice: TestAppDevice.flutterTester),
52+
expectedExperimentsOn: true,
53+
expectedAppPath: _testAppPath,
6054
),
6155
];
6256

@@ -67,8 +61,8 @@ void main() {
6761
t.input,
6862
testAppDevice: t.testAppDevice,
6963
);
70-
expect(args.experimentsOn, t.output.experimentsOn);
71-
expect(args.appPath, t.output.appPath);
64+
expect(args.experimentsOn, t.expectedExperimentsOn);
65+
expect(args.appPath, t.expectedAppPath);
7266
});
7367
}
7468
}
@@ -78,11 +72,13 @@ class _InFileTestArgsTest {
7872
required this.name,
7973
required this.input,
8074
required this.testAppDevice,
81-
required this.output,
75+
required this.expectedExperimentsOn,
76+
required this.expectedAppPath,
8277
});
8378

8479
final String name;
8580
final String input;
8681
final TestAppDevice testAppDevice;
87-
final TestFileArgs output;
82+
final bool expectedExperimentsOn;
83+
final String expectedAppPath;
8884
}

0 commit comments

Comments
 (0)