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

Cucumber data table #56

Merged
merged 21 commits into from
Jan 23, 2024
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
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -74,4 +74,4 @@ pubspec.lock
!**/ios/**/default.pbxuser
!**/ios/**/default.perspectivev3
!/packages/flutter_tools/test/data/dart_dependencies_test/**/.packages
coverage/lcov.info
/coverage/
7 changes: 7 additions & 0 deletions example/test/songs.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
Feature: Songs

Scenario: Available songs
Given the following songs
| 'artist' | 'title' |
| 'The Beatles' | 'Let It Be' |
| 'Camel' | 'Slow yourself down' |
16 changes: 16 additions & 0 deletions example/test/songs_test.dart

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

7 changes: 7 additions & 0 deletions example/test/step/the_following_songs.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
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 {
throw UnimplementedError();
}
20 changes: 20 additions & 0 deletions lib/data_table.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import 'package:collection/collection.dart';

class DataTable {
const DataTable(this._data);

final List<List<dynamic>> _data;

List<List<dynamic>> asLists() => _data;

List<Map<dynamic, dynamic>> asMaps() {
final headers = _data.first;
return _data.skip(1).map((row) {
final map = <dynamic, dynamic>{};
headers.forEachIndexed((index, header) {
map[header] = row[index];
});
return map;
}).toList();
}
}
3 changes: 3 additions & 0 deletions lib/src/bdd_line.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ class BddLine {
value = _removeLinePrefix(rawLine);

BddLine.fromValue(this.type, this.value) : rawLine = '';
BddLine.fromRawValue(this.type, this.rawLine)
: value = _removeLinePrefix(rawLine);

final String rawLine;
final String value;
Expand All @@ -17,6 +19,7 @@ enum LineType {
scenario,
scenarioOutline,
step,
dataTableStep,
after,
examples,
exampleTitle,
Expand Down
105 changes: 77 additions & 28 deletions lib/src/data_table_parser.dart
Original file line number Diff line number Diff line change
@@ -1,37 +1,86 @@
import 'package:bdd_widget_test/src/bdd_line.dart';
import 'package:bdd_widget_test/src/regex.dart';
import 'package:bdd_widget_test/src/scenario_generator.dart';

Iterable<BddLine> replaceDataTables(List<BddLine> lines) sync* {
var reachedExamples = false;
var i = 0;
while (i < lines.length) {
if (lines[i].type == LineType.exampleTitle) {
reachedExamples = true;
}
if (reachedExamples) {
yield lines[i++];
continue;
bool hasBddDataTable(List<BddLine> lines) {
for (var index = 0; index < lines.length; index++) {
final isStep = lines[index].type == LineType.step ||
lines[index].type == LineType.dataTableStep;
final isNextLineTable = isTable(lines: lines, index: index + 1);
final isExamplesFormatted = hasExamplesFormat(bddLine: lines[index]);
if (isStep && isNextLineTable && !isExamplesFormatted) {
return true;
}
if (i + 1 < lines.length && _foundDataTable(lines, i)) {
final dataTable = [
lines[i],
...lines.skip(i + 1).takeWhile((l) => l.type == LineType.examples),
];
final data = generateScenariosFromScenaioOutline([
// pretend to be an Example section to re-use some logic
BddLine.fromValue(LineType.exampleTitle, ''),
...dataTable,
]);
for (final item in data) {
yield item[1];
}
return false;
}

Iterable<BddLine> replaceDataTables(List<BddLine> lines) sync* {
for (var index = 0; index < lines.length; index++) {
final isStep = lines[index].type == LineType.step ||
lines[index].type == LineType.dataTableStep;
final isNextLineTable = isTable(lines: lines, index: index + 1);
if (isStep && isNextLineTable) {
if (!hasExamplesFormat(bddLine: lines[index])) {
yield* _createCucumberDataTable(lines: lines, index: index);
} else {
yield* _createDataTableFromExamples(lines: lines, index: index);
}
i += dataTable.length;
continue;
} else {
yield lines[index];
}
yield lines[i++];
}
}

bool _foundDataTable(List<BddLine> lines, int index) =>
lines[index].type == LineType.step &&
lines[index + 1].type == LineType.examples;
bool isTable({
required List<BddLine> lines,
required int index,
}) =>
index < lines.length && lines[index].type == LineType.examples;

bool hasExamplesFormat({required BddLine bddLine}) =>
examplesRegExp.firstMatch(bddLine.rawLine) != null;

List<String> _createRow({
required BddLine bddLine,
}) =>
bddLine.value
.split('|')
.map((example) => example.trim())
.takeWhile((value) => value.isNotEmpty)
.toList();

Iterable<BddLine> _createCucumberDataTable({
required List<BddLine> lines,
required int index,
}) sync* {
final text = lines[index].value;
final table = <List<String>>[];
do {
table.add(_createRow(bddLine: lines[++index]));
} while (isTable(lines: lines, index: index + 1));
yield BddLine.fromValue(
LineType.dataTableStep,
'$text {const bdd.DataTable($table)}',
);
}

Iterable<BddLine> _createDataTableFromExamples({
required List<BddLine> lines,
required int index,
}) sync* {
final dataTable = [lines[index]];
do {
dataTable.add(lines[++index]);
} while (
index + 1 < lines.length && lines[index + 1].type == LineType.examples);
final data = generateScenariosFromScenaioOutline([
// pretend to be an Example section to re-use some logic
BddLine.fromValue(LineType.exampleTitle, ''),
...dataTable,
]);

for (final item in data) {
yield item[1];
}
}
36 changes: 30 additions & 6 deletions lib/src/feature_file.dart
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
import 'package:bdd_widget_test/src/bdd_line.dart';
import 'package:bdd_widget_test/src/data_table_parser.dart'
as data_table_parser;
import 'package:bdd_widget_test/src/feature_generator.dart';
import 'package:bdd_widget_test/src/generator_options.dart';
import 'package:bdd_widget_test/src/step_file.dart';
import 'package:bdd_widget_test/src/util/common.dart';
import 'package:bdd_widget_test/src/util/constants.dart';
import 'package:collection/collection.dart';

class FeatureFile {
FeatureFile({
Expand All @@ -29,12 +32,15 @@ class FeatureFile {
);

_stepFiles = _lines
.where((line) => line.type == LineType.step)
.where(
(line) =>
line.type == LineType.step || line.type == LineType.dataTableStep,
)
.map(
(e) => StepFile.create(
(bddLine) => StepFile.create(
featureDir,
package,
e.value,
bddLine,
existingSteps,
generatorOptions,
_testerType,
Expand Down Expand Up @@ -67,10 +73,28 @@ class FeatureFile {
List<StepFile> getStepFiles() => _stepFiles;

static List<BddLine> _prepareLines(Iterable<BddLine> input) {
final headers = input.takeWhile((value) => value.type == LineType.unknown);
final lines = input
final lines = input.mapIndexed(
(index, bddLine) {
final isStep = bddLine.type == LineType.step;
final hasExamplesFormat = data_table_parser.hasExamplesFormat(
bddLine: bddLine,
);
final isNextTable = data_table_parser.isTable(
lines: input.toList(),
index: index + 1,
);
if (isStep && !hasExamplesFormat && isNextTable) {
return BddLine.fromRawValue(LineType.dataTableStep, bddLine.rawLine);
} else {
return bddLine;
}
},
);

final headers = lines.takeWhile((value) => value.type == LineType.unknown);
final steps = lines
.skip(headers.length)
.where((value) => value.type != LineType.unknown);
return [...headers, ...lines];
return [...headers, ...steps];
}
}
15 changes: 12 additions & 3 deletions lib/src/feature_generator.dart
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,9 @@ String generateFeatureDart(
if (tags.isNotEmpty) {
sb.writeln("@Tags(['${tags.join("', '")}'])");
}
if (hasBddDataTable(lines)) {
sb.writeln("import 'package:bdd_widget_test/data_table.dart' as bdd;");
}
sb.writeln("import 'package:flutter/material.dart';");
sb.writeln("import 'package:flutter_test/flutter_test.dart';");
if (isIntegrationTest) {
Expand Down Expand Up @@ -138,7 +141,8 @@ bool _parseSetup(
if (offset != -1) {
sb.writeln(' Future<void> $title($testerType $testerName) async {');
offset++;
while (lines[offset].type == LineType.step) {
while (lines[offset].type == LineType.step ||
lines[offset].type == LineType.dataTableStep) {
sb.writeln(
' await ${getStepMethodCall(lines[offset].value, testerName)};',
);
Expand Down Expand Up @@ -186,7 +190,12 @@ void _parseFeature(
parseScenario(
sb,
s.first.value,
s.where((e) => e.type == LineType.step).toList(),
s
.where(
(e) =>
e.type == LineType.step || e.type == LineType.dataTableStep,
)
.toList(),
hasSetUp,
hasTearDown,
scenarioTestMethodName,
Expand Down Expand Up @@ -236,7 +245,7 @@ Iterable<List<BddLine>> _splitScenarios(List<BddLine> lines) sync* {
Iterable<BddLine> _parseScenario(List<BddLine> lines) sync* {
var isNewScenario = true;
for (final line in lines) {
if (line.type == LineType.step) {
if (line.type == LineType.step || line.type == LineType.dataTableStep) {
isNewScenario = false;
}
if (!isNewScenario && _isNewScenario(line.type)) {
Expand Down
14 changes: 11 additions & 3 deletions lib/src/step/generic_step.dart
Original file line number Diff line number Diff line change
Expand Up @@ -9,24 +9,32 @@ class GenericStep implements BddStep {
this.rawLine,
this.testerType,
this.customTesterName,
this.hasDataTable,
);

final String rawLine;
final String methodName;
final String testerType;
final String customTesterName;
final bool hasDataTable;

@override
String get content => '''
String get content =>
'${hasDataTable ? "import 'package:bdd_widget_test/data_table.dart' as bdd;\n" : ''}'
'''
import 'package:flutter_test/flutter_test.dart';

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

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

final params = parseRawStepLine(stepLine).skip(1);
if (params.isNotEmpty) {
return params
Expand Down
Loading
Loading