Skip to content

Commit c9905fc

Browse files
committed
Add Data Tables
1 parent edb1108 commit c9905fc

11 files changed

Lines changed: 118 additions & 27 deletions

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
## [1.2.1] - Data Tables
2+
3+
* Add data tables support
4+
15
## [1.2.0] - Scenario Outline
26

37
* Add scenario outline support

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ flutter test
5151

5252
Feature file sample:
5353
```ruby
54-
// comment here
54+
# comment here
5555

5656
Feature: Counter
5757

example/pubspec.lock

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ packages:
4242
path: ".."
4343
relative: true
4444
source: path
45-
version: "1.2.0"
45+
version: "1.2.1"
4646
boolean_selector:
4747
dependency: transitive
4848
description:

lib/src/bdd_line.dart

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ enum LineType {
1919
step,
2020
after,
2121
examples,
22+
exampleTitle,
2223
unknown,
2324
}
2425

@@ -44,6 +45,9 @@ LineType _lineTypeFromString(String line) {
4445
if (examplesMarkers.any((marker) => line.startsWith(marker))) {
4546
return LineType.examples;
4647
}
48+
if (examplesTitleMarkers.any((marker) => line.startsWith(marker))) {
49+
return LineType.exampleTitle;
50+
}
4751
if (tagMarkers.any((marker) => line.startsWith(marker))) {
4852
return LineType.tag;
4953
}
@@ -57,7 +61,8 @@ const tagMarkers = ['@'];
5761
const scenarioMarkers = ['Scenario:', 'Example:'];
5862
const scenarioOutlineMarkers = ['Scenario Outline:'];
5963
const stepMarkers = ['Given', 'When', 'Then', 'And', 'But'];
60-
const examplesMarkers = ['Examples:', 'Scenarios', '|'];
64+
const examplesMarkers = ['|'];
65+
const examplesTitleMarkers = ['Examples:', 'Scenarios'];
6166

6267
String _removeLinePrefix(String rawLine) {
6368
final lines = rawLine.split(' ');

lib/src/data_table_parser.dart

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
import 'package:bdd_widget_test/src/bdd_line.dart';
2+
import 'package:bdd_widget_test/src/scenario_generator.dart';
3+
4+
Iterable<BddLine> replaceDataTables(List<BddLine> lines) sync* {
5+
var reachedExamples = false;
6+
var i = 0;
7+
while (i < lines.length) {
8+
if (lines[i].type == LineType.exampleTitle) {
9+
reachedExamples = true;
10+
}
11+
if (reachedExamples) {
12+
yield lines[i++];
13+
continue;
14+
}
15+
if (i + 1 < lines.length && _foundDataTable(lines, i)) {
16+
final dataTable = [
17+
lines[i],
18+
...lines.skip(i + 1).takeWhile((l) => l.type == LineType.examples)
19+
];
20+
final data = generateScenariosFromScenaioOutline([
21+
// pretend to be an Example section to re-use some logic
22+
BddLine.fromValue(LineType.exampleTitle, ''),
23+
...dataTable,
24+
]);
25+
for (final item in data) {
26+
yield item[1];
27+
}
28+
i += dataTable.length;
29+
continue;
30+
}
31+
yield lines[i++];
32+
}
33+
}
34+
35+
bool _foundDataTable(List<BddLine> lines, int index) =>
36+
lines[index].type == LineType.step &&
37+
lines[index + 1].type == LineType.examples;

lib/src/feature_generator.dart

Lines changed: 10 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import 'package:bdd_widget_test/src/bdd_line.dart';
2+
import 'package:bdd_widget_test/src/data_table_parser.dart';
23
import 'package:bdd_widget_test/src/scenario_generator.dart';
34
import 'package:bdd_widget_test/src/step_file.dart';
45
import 'package:bdd_widget_test/src/step_generator.dart';
@@ -103,27 +104,21 @@ void _parseFeature(
103104
for (final scenario in scenarios) {
104105
final scenarioTestMethodName =
105106
_parseScenaioTags(feature, scenario.first, testMethodName);
106-
if (scenario.first.type == LineType.scenario) {
107+
108+
final flattenDataTables = replaceDataTables(scenario).toList();
109+
final scenariosToParse = flattenDataTables.first.type == LineType.scenario
110+
? [flattenDataTables]
111+
: generateScenariosFromScenaioOutline(flattenDataTables);
112+
113+
for (final s in scenariosToParse) {
107114
parseScenario(
108115
sb,
109-
scenario.first.value,
110-
scenario.where((e) => e.type == LineType.step).toList(),
116+
s.first.value,
117+
s.where((e) => e.type == LineType.step).toList(),
111118
hasSetUp,
112119
hasTearDown,
113120
scenarioTestMethodName,
114121
);
115-
} else {
116-
final generatedScenarios = generateScenariosFromScenaioOutline(scenario);
117-
for (final g in generatedScenarios) {
118-
parseScenario(
119-
sb,
120-
g.first.value,
121-
g.where((e) => e.type == LineType.step).toList(),
122-
hasSetUp,
123-
hasTearDown,
124-
scenarioTestMethodName,
125-
);
126-
}
127122
}
128123
}
129124
sb.writeln(' });');

lib/src/scenario_generator.dart

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,12 +40,12 @@ List<Map<String, String>> _getExamples(
4040
List<BddLine> scenario,
4141
) {
4242
final exampleLines = scenario
43+
.skipWhile((l) => l.type != LineType.exampleTitle)
4344
.where((l) => l.type == LineType.examples)
44-
.skip(1) // Skip 'Examples:` line
4545
.map((e) => e.rawLine.substring(
4646
// Remove the first and the last '|' separator
4747
1,
48-
e.rawLine.length - 2,
48+
e.rawLine.length - 1,
4949
))
5050
.map(_parseExampleLine);
5151
final names = exampleLines.first;
@@ -61,7 +61,7 @@ Iterable<BddLine> _processScenarioLines(
6161
yield BddLine.fromValue(
6262
name.type, '${name.value} (${examples.values.join(', ')})');
6363

64-
for (final line in lines.skip(1).where((l) => l.type == LineType.step)) {
64+
for (final line in lines.skip(1)) {
6565
yield BddLine.fromValue(
6666
line.type, _replacePlaceholders(line.value, examples));
6767
}
@@ -70,7 +70,7 @@ Iterable<BddLine> _processScenarioLines(
7070
String _replacePlaceholders(String line, Map<String, String> example) {
7171
var replaced = line;
7272
for (final e in example.keys) {
73-
replaced = replaced.replaceAll(' <$e> ', ' {${example[e]}} ');
73+
replaced = replaced.replaceAll('<$e>', '{${example[e]}}');
7474
}
7575
return replaced;
7676
}

lib/src/step/generic_step.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ ${getStepSignature(rawLine)} {
1717
''';
1818

1919
String getStepSignature(String stepLine) {
20-
final params = parametersValueRegExp.allMatches(stepLine);
20+
final params = parametersRegExp.allMatches(stepLine);
2121
if (params.isEmpty) {
2222
final examples = examplesRegExp.allMatches(stepLine);
2323
if (examples.isEmpty) {

lib/src/step_generator.dart

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,15 +38,16 @@ String getStepMethodName(String stepText) {
3838
return camelize(text);
3939
}
4040

41-
String getStepMethodCall(String stepLine) {
41+
String getStepMethodCall(String stepLine, {List<String>? forceParams}) {
4242
final name = getStepMethodName(stepLine);
4343

4444
final params = parametersValueRegExp.allMatches(stepLine);
45-
if (params.isEmpty) {
45+
if (params.isEmpty && forceParams == null) {
4646
return '$name(tester)';
4747
}
4848

49-
final methodParameters = params.map((p) => p.group(0)).join(', ');
49+
final methodParameters =
50+
forceParams ?? params.map((p) => p.group(0)).join(', ');
5051
return '$name(tester, $methodParameters)';
5152
}
5253

pubspec.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
name: bdd_widget_test
22
description: A BDD-style widget testing library. Generates Flutter widget tests from *.feature files.
3-
version: 1.2.0
3+
version: 1.2.1
44
author: Oleksandr Leushchenko <olexa.le@gmail.com>
55
homepage: https://github.com/olexale/bdd_widget_test
66

0 commit comments

Comments
 (0)