Skip to content

Commit

Permalink
Merge pull request #84 from olexale/bugfix/combine-params-and-datatable
Browse files Browse the repository at this point in the history
Refactor generic step generator
  • Loading branch information
olexale authored Dec 26, 2024
2 parents 5c7e8a7 + 2d438bc commit 372e24f
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 19 deletions.
2 changes: 1 addition & 1 deletion example/test/songs.feature
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Feature: Songs

Scenario: Available songs
Given the following songs
Given the following {'Good'} songs
| 'artist' | 'title' |
| 'The Beatles' | 'Let It Be' |
| 'Camel' | 'Slow yourself down' |
1 change: 1 addition & 0 deletions example/test/songs_test.dart

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 3 additions & 2 deletions example/test/step/the_following_songs.dart
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import 'package:bdd_widget_test/data_table.dart' as bdd;
import 'package:flutter_test/flutter_test.dart';

/// Usage: the following songs
Future<void> theFollowingSongs(WidgetTester tester, bdd.DataTable dataTable) async {
/// Usage: the following {'Good'} songs
Future<void> theFollowingSongs(
WidgetTester tester, String param1, bdd.DataTable dataTable) async {
throw UnimplementedError();
}
23 changes: 7 additions & 16 deletions lib/src/step/generic_step.dart
Original file line number Diff line number Diff line change
Expand Up @@ -31,25 +31,16 @@ Future<void> $methodName($testerType $customTesterName${_getMethodParameters(raw
''';

String _getMethodParameters(String stepLine, bool hadDataTable) {
if (hadDataTable) {
return ', bdd.DataTable dataTable';
}

final params = parseRawStepLine(stepLine).skip(1);
if (params.isNotEmpty) {
return params
.mapIndexed(
(index, p) => ', ${_getGenericParameterType(p)} param${index + 1}',
)
.join();
}

final examples = examplesRegExp.allMatches(stepLine);
if (examples.isNotEmpty) {
return examples.map((p) => ', dynamic ${p.group(1)}').join();
}

return '';
return [
...params.mapIndexed(
(index, p) => ', ${_getGenericParameterType(p)} param${index + 1}',
),
...examples.map((p) => ', dynamic ${p.group(1)}'),
if (hasDataTable) ', bdd.DataTable dataTable',
].join();
}

String _getGenericParameterType(String parameter) {
Expand Down
50 changes: 50 additions & 0 deletions test/data_tables_test.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import 'package:bdd_widget_test/src/feature_file.dart';
import 'package:bdd_widget_test/src/step_file.dart';
import 'package:flutter_test/flutter_test.dart';

void main() {
Expand Down Expand Up @@ -435,4 +436,53 @@ void main() {

expect(feature.dartContent, expectedFeatureDart);
});
test('Data table with parameters', () {
const featureFile = '''
Feature: Testing feature
Scenario: Testing scenario
Given the following {'Good'} songs
| artist | title |
| 'The Beatles' | 'Let It Be' |
| 'Camel' | 'Slow yourself down' |
''';

const expectedFeatureDart = '''
// GENERATED CODE - DO NOT MODIFY BY HAND
// ignore_for_file: unused_import, directives_ordering
import 'package:bdd_widget_test/data_table.dart' as bdd;
import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart';
import './step/the_following_songs.dart';
void main() {
group(\'\'\'Testing feature\'\'\', () {
testWidgets(\'\'\'Testing scenario\'\'\', (tester) async {
await theFollowingSongs(tester, 'Good', const bdd.DataTable([[artist, title], ['The Beatles', 'Let It Be'], ['Camel', 'Slow yourself down']]));
});
});
}
''';
const expectedStep = '''
import 'package:bdd_widget_test/data_table.dart' as bdd;
import 'package:flutter_test/flutter_test.dart';
/// Usage: the following {'Good'} songs
Future<void> theFollowingSongs(WidgetTester tester, String param1, bdd.DataTable dataTable) async {
throw UnimplementedError();
}
''';

final feature = FeatureFile(
featureDir: 'test.feature',
package: 'test',
input: featureFile,
);
expect(feature.dartContent, expectedFeatureDart);
expect(
(feature.getStepFiles().first as NewStepFile).dartContent,
expectedStep,
);
});
}

0 comments on commit 372e24f

Please sign in to comment.