Skip to content

Commit 8a753d1

Browse files
committed
Support multiple imports
1 parent acf284c commit 8a753d1

2 files changed

Lines changed: 79 additions & 30 deletions

File tree

lib/src/generator_options.dart

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -20,25 +20,32 @@ class GeneratorOptions {
2020
testMethodName: json['testMethodName'] as String?,
2121
externalSteps: (json['externalSteps'] as List?)?.cast<String>(),
2222
stepFolderName: json['stepFolderName'] as String?,
23-
include: json['include'] as String?,
23+
include: json['include'] is String
24+
? [(json['include'] as String)]
25+
: (json['include'] as List?)?.cast<String>(),
2426
);
2527

2628
final String stepFolder;
2729
final String testMethodName;
28-
final String? include;
30+
final List<String>? include;
2931
final List<String> externalSteps;
3032
}
3133

3234
Future<GeneratorOptions> flattenOptions(GeneratorOptions options) async {
33-
if (options.include == null) {
35+
if (options.include?.isEmpty ?? true) {
3436
return options;
3537
}
36-
final includedOptions = await readFromPackage(options.include!);
37-
final newOptions = merge(options, includedOptions);
38-
return flattenOptions(newOptions);
38+
var resultOptions = options;
39+
for (final include in resultOptions.include!) {
40+
final includedOptions = await _readFromPackage(include);
41+
final newOptions = merge(resultOptions, includedOptions);
42+
resultOptions = await flattenOptions(newOptions);
43+
}
44+
45+
return resultOptions;
3946
}
4047

41-
Future<GeneratorOptions> readFromPackage(String packageUri) async {
48+
Future<GeneratorOptions> _readFromPackage(String packageUri) async {
4249
final uri = await resolvePackageUri(
4350
Uri.parse(packageUri),
4451
);
@@ -55,7 +62,9 @@ GeneratorOptions readFromUri(Uri uri) {
5562
testMethodName: doc['testMethodName'] as String?,
5663
externalSteps: (doc['externalSteps'] as List?)?.cast<String>(),
5764
stepFolderName: doc['stepFolderName'] as String?,
58-
include: doc['include'] as String?,
65+
include: doc['include'] is String
66+
? [(doc['include'] as String)]
67+
: (doc['include'] as YamlList?)?.value.cast<String>(),
5968
);
6069
}
6170

test/feature_generator_test.dart

Lines changed: 62 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ import 'package:file/memory.dart';
77
import 'package:flutter_test/flutter_test.dart';
88
import 'package:path/path.dart' as p;
99

10+
import 'util/testing_data.dart';
11+
1012
void main() {
1113
setUp(() {
1214
resolvePackageUriFactory = (uri) {
@@ -116,15 +118,6 @@ testMethodName: customName
116118
stepFolderName: scenarios
117119
''');
118120

119-
// resolvePackageUriFactory = (uri) {
120-
// if (uri.path == externalYaml) {
121-
// return Future.value(
122-
// Uri.parse(externalYaml),
123-
// );
124-
// }
125-
// throw Exception();
126-
// };
127-
128121
const expected = '// GENERATED CODE - DO NOT MODIFY BY HAND\n'
129122
'// ignore_for_file: unused_import, directives_ordering\n'
130123
'\n'
@@ -145,25 +138,78 @@ stepFolderName: scenarios
145138
final content = await generate(scenario);
146139
expect(content, expected);
147140
});
141+
142+
test('nested includes', () async {
143+
const includeYaml = 'bdd_options.yaml';
144+
const externalYaml1 = 'external_options_1.yaml';
145+
const externalYaml2 = 'external_options_2.yaml';
146+
const externalYaml3 = 'external_options_3.yaml';
147+
148+
fs.file(includeYaml)
149+
..createSync()
150+
..writeAsStringSync('''
151+
include:
152+
- $externalYaml1
153+
- $externalYaml2
154+
''');
155+
156+
fs.file(externalYaml1)
157+
..createSync()
158+
..writeAsStringSync('''
159+
testMethodName: customName
160+
''');
161+
162+
fs.file(externalYaml2)
163+
..createSync()
164+
..writeAsStringSync('''
165+
include: $externalYaml3
166+
''');
167+
168+
fs.file(externalYaml3)
169+
..createSync()
170+
..writeAsStringSync('''
171+
stepFolderName: scenarios
172+
''');
173+
174+
const expected = '// GENERATED CODE - DO NOT MODIFY BY HAND\n'
175+
'// ignore_for_file: unused_import, directives_ordering\n'
176+
'\n'
177+
'import \'package:flutter/material.dart\';\n'
178+
'import \'package:flutter_test/flutter_test.dart\';\n'
179+
'\n'
180+
'import \'./scenarios/the_app_is_running.dart\';\n'
181+
'\n'
182+
'void main() {\n'
183+
' group(\'\'\'Testing feature\'\'\', () {\n'
184+
' customName(\'\'\'Testing scenario\'\'\', (tester) async {\n'
185+
' await theAppIsRunning(tester);\n'
186+
' });\n'
187+
' });\n'
188+
'}\n';
189+
190+
const scenario = 'options';
191+
final content = await generate(
192+
scenario,
193+
const BuilderOptions(<String, dynamic>{
194+
'include': externalYaml3,
195+
}));
196+
expect(content, expected);
197+
});
148198
}
149199

150200
// ----------------------------------------------------------------------------
151201
const pkgName = 'pkg';
152202

153-
final builder = featureBuilder(const BuilderOptions(
154-
<String, dynamic>{},
155-
));
156-
157-
Future<String> generate(String scenario) async {
203+
Future<String> generate(String scenario, [BuilderOptions? options]) async {
158204
final path = 'test/builder_scenarios/$scenario';
159205

160206
final srcs = <String, String>{
161-
'$pkgName|$path/sample.feature': builtValueSource,
207+
'$pkgName|$path/sample.feature': minimalFeatureFile,
162208
};
163209

164210
final writer = InMemoryAssetWriter();
165211
await testBuilder(
166-
builder,
212+
featureBuilder(options ?? const BuilderOptions(<String, dynamic>{})),
167213
srcs,
168214
rootPackage: pkgName,
169215
writer: writer,
@@ -173,12 +219,6 @@ Future<String> generate(String scenario) async {
173219
);
174220
}
175221

176-
const String builtValueSource = r'''
177-
Feature: Testing feature
178-
Scenario: Testing scenario
179-
Given the app is running
180-
''';
181-
182222
String getStepFolderName(String scenario) => p.joinAll([
183223
fs.currentDirectory.path,
184224
'test',

0 commit comments

Comments
 (0)