Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: For steps with example have column names for parameters #48

Merged
merged 2 commits into from
Dec 4, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 28 additions & 18 deletions lib/src/step/generic_step.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import 'package:bdd_widget_test/src/regex.dart';
import 'package:bdd_widget_test/src/step/bdd_step.dart';
import 'package:bdd_widget_test/src/step_generator.dart';
import 'package:collection/collection.dart';

class GenericStep implements BddStep {
GenericStep(
Expand All @@ -19,31 +20,40 @@ class GenericStep implements BddStep {
String get content => '''
import 'package:flutter_test/flutter_test.dart';

${getStepSignature(rawLine, testerType, customTesterName)} {
Future<void> $methodName($testerType $customTesterName${_getMethodParameters(rawLine)}) async {
throw UnimplementedError();
}
''';

String getStepSignature(
String stepLine,
String testerType,
String testerName,
) {
String _getMethodParameters(String stepLine) {
final params = parseRawStepLine(stepLine).skip(1);
if (params.isEmpty) {
final examples = examplesRegExp.allMatches(stepLine);
if (examples.isEmpty) {
return 'Future<void> $methodName($testerType $testerName) async';
} else {
return _generateSignature(examples.length, testerName);
}
if (params.isNotEmpty) {
return params
.mapIndexed(
(index, p) => ', ${_getGenericParameterType(p)} param${index + 1}',
)
.join();
}
return _generateSignature(params.length, testerName);

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

return '';
}

String _generateSignature(int paramsCount, String testerName) {
final p = List.generate(paramsCount, (index) => index + 1);
final methodParameters = p.map((p) => 'dynamic param$p').join(', ');
return 'Future<void> $methodName($testerType $testerName, $methodParameters) async';
String _getGenericParameterType(String parameter) {
if (parameter == 'true' || parameter == 'false') {
return 'bool';
}
if (num.tryParse(parameter) != null) {
return 'num';
}
if ((parameter.startsWith('"') && parameter.endsWith('"')) ||
(parameter.startsWith("'") && parameter.endsWith("'"))) {
return 'String';
}
return 'dynamic';
}
}
2 changes: 1 addition & 1 deletion test/scenario_outline_step_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ Feature: Testing feature
const expectedStep = '''
import 'package:flutter_test/flutter_test.dart';

Future<void> thereAreCucumbers(WidgetTester tester, dynamic param1) async {
Future<void> thereAreCucumbers(WidgetTester tester, dynamic start) async {
throw UnimplementedError();
}
''';
Expand Down
4 changes: 2 additions & 2 deletions test/step/generic_step_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,13 @@ Future<void> iInvokeTest(WidgetTester tester) async {
const featureFile = '''
Feature: Testing feature
Scenario: Testing scenario
When I invoke {0} test with {Some} parameter
When I invoke {0} test with {Some} parameter and {true} parameter and {'value'} parameter
''';

const expectedSteps = '''
import 'package:flutter_test/flutter_test.dart';

Future<void> iInvokeTestWithParameter(WidgetTester tester, dynamic param1, dynamic param2) async {
Future<void> iInvokeTestWithParameterAndParameterAndParameter(WidgetTester tester, num param1, dynamic param2, bool param3, String param4) async {
throw UnimplementedError();
}
''';
Expand Down
Loading