Skip to content

Commit 372e24f

Browse files
authored
Merge pull request #84 from olexale/bugfix/combine-params-and-datatable
Refactor generic step generator
2 parents 5c7e8a7 + 2d438bc commit 372e24f

5 files changed

Lines changed: 62 additions & 19 deletions

File tree

example/test/songs.feature

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
Feature: Songs
22

33
Scenario: Available songs
4-
Given the following songs
4+
Given the following {'Good'} songs
55
| 'artist' | 'title' |
66
| 'The Beatles' | 'Let It Be' |
77
| 'Camel' | 'Slow yourself down' |

example/test/songs_test.dart

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
import 'package:bdd_widget_test/data_table.dart' as bdd;
22
import 'package:flutter_test/flutter_test.dart';
33

4-
/// Usage: the following songs
5-
Future<void> theFollowingSongs(WidgetTester tester, bdd.DataTable dataTable) async {
4+
/// Usage: the following {'Good'} songs
5+
Future<void> theFollowingSongs(
6+
WidgetTester tester, String param1, bdd.DataTable dataTable) async {
67
throw UnimplementedError();
78
}

lib/src/step/generic_step.dart

Lines changed: 7 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -31,25 +31,16 @@ Future<void> $methodName($testerType $customTesterName${_getMethodParameters(raw
3131
''';
3232

3333
String _getMethodParameters(String stepLine, bool hadDataTable) {
34-
if (hadDataTable) {
35-
return ', bdd.DataTable dataTable';
36-
}
37-
3834
final params = parseRawStepLine(stepLine).skip(1);
39-
if (params.isNotEmpty) {
40-
return params
41-
.mapIndexed(
42-
(index, p) => ', ${_getGenericParameterType(p)} param${index + 1}',
43-
)
44-
.join();
45-
}
46-
4735
final examples = examplesRegExp.allMatches(stepLine);
48-
if (examples.isNotEmpty) {
49-
return examples.map((p) => ', dynamic ${p.group(1)}').join();
50-
}
5136

52-
return '';
37+
return [
38+
...params.mapIndexed(
39+
(index, p) => ', ${_getGenericParameterType(p)} param${index + 1}',
40+
),
41+
...examples.map((p) => ', dynamic ${p.group(1)}'),
42+
if (hasDataTable) ', bdd.DataTable dataTable',
43+
].join();
5344
}
5445

5546
String _getGenericParameterType(String parameter) {

test/data_tables_test.dart

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import 'package:bdd_widget_test/src/feature_file.dart';
2+
import 'package:bdd_widget_test/src/step_file.dart';
23
import 'package:flutter_test/flutter_test.dart';
34

45
void main() {
@@ -435,4 +436,53 @@ void main() {
435436

436437
expect(feature.dartContent, expectedFeatureDart);
437438
});
439+
test('Data table with parameters', () {
440+
const featureFile = '''
441+
Feature: Testing feature
442+
Scenario: Testing scenario
443+
Given the following {'Good'} songs
444+
| artist | title |
445+
| 'The Beatles' | 'Let It Be' |
446+
| 'Camel' | 'Slow yourself down' |
447+
''';
448+
449+
const expectedFeatureDart = '''
450+
// GENERATED CODE - DO NOT MODIFY BY HAND
451+
// ignore_for_file: unused_import, directives_ordering
452+
453+
import 'package:bdd_widget_test/data_table.dart' as bdd;
454+
import 'package:flutter/material.dart';
455+
import 'package:flutter_test/flutter_test.dart';
456+
457+
import './step/the_following_songs.dart';
458+
459+
void main() {
460+
group(\'\'\'Testing feature\'\'\', () {
461+
testWidgets(\'\'\'Testing scenario\'\'\', (tester) async {
462+
await theFollowingSongs(tester, 'Good', const bdd.DataTable([[artist, title], ['The Beatles', 'Let It Be'], ['Camel', 'Slow yourself down']]));
463+
});
464+
});
465+
}
466+
''';
467+
const expectedStep = '''
468+
import 'package:bdd_widget_test/data_table.dart' as bdd;
469+
import 'package:flutter_test/flutter_test.dart';
470+
471+
/// Usage: the following {'Good'} songs
472+
Future<void> theFollowingSongs(WidgetTester tester, String param1, bdd.DataTable dataTable) async {
473+
throw UnimplementedError();
474+
}
475+
''';
476+
477+
final feature = FeatureFile(
478+
featureDir: 'test.feature',
479+
package: 'test',
480+
input: featureFile,
481+
);
482+
expect(feature.dartContent, expectedFeatureDart);
483+
expect(
484+
(feature.getStepFiles().first as NewStepFile).dartContent,
485+
expectedStep,
486+
);
487+
});
438488
}

0 commit comments

Comments
 (0)