-
Notifications
You must be signed in to change notification settings - Fork 39
Expand file tree
/
Copy pathscenario_generator.dart
More file actions
210 lines (194 loc) · 5.66 KB
/
scenario_generator.dart
File metadata and controls
210 lines (194 loc) · 5.66 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
import 'package:bdd_widget_test/src/bdd_line.dart';
import 'package:bdd_widget_test/src/step_generator.dart';
import 'package:bdd_widget_test/src/util/constants.dart';
void parseScenario(
StringBuffer sb,
String scenarioTitle,
List<BddLine> scenario,
bool hasSetUp,
bool hasTearDown,
bool hasHooks,
String testMethodName,
String testerName,
List<String> tags,
String scenarioParams,
) {
sb.writeln(
" $testMethodName('''$scenarioTitle''', ($testerName) async {",
);
if (hasHooks) {
sb.writeln(' var $testSuccessVariableName = true;');
}
if (hasTearDown || hasHooks) {
sb.writeln(' try {');
}
final spaces = hasTearDown ? ' ' : ' ';
if (hasHooks) {
sb.writeln(
"${spaces}await $setUpHookName('''$scenarioTitle''' ${tags.isNotEmpty ? ', ${tagsToString(tags)}' : ''});",
);
}
if (hasSetUp) {
sb.writeln('${spaces}await $setUpMethodName($testerName);');
}
for (final step in scenario) {
sb.writeln('${spaces}await ${getStepMethodCall(step.value, testerName)};');
}
if (hasHooks) {
sb.writeln(' } catch (_) {');
sb.writeln(' $testSuccessVariableName = false;');
sb.writeln(' rethrow;');
}
if (hasTearDown | hasHooks) {
sb.writeln(' } finally {');
if (hasTearDown) {
sb.writeln(' await $tearDownMethodName($testerName);');
}
if (hasHooks) {
sb.writeln(' await $tearDownHookName(');
sb.writeln(" '''$scenarioTitle''',");
sb.writeln(' $testSuccessVariableName,');
if (tags.isNotEmpty) {
sb.writeln(' ${tagsToString(tags)},');
}
sb.writeln(' );');
}
sb.writeln(' }');
}
sb.writeln(
' }${tags.isNotEmpty ? ", tags: ${tagsToString(tags)}" : ''}${scenarioParams.isNotEmpty ? ',' : ');'}',
);
if (scenarioParams.isNotEmpty) {
for (final param in scenarioParams.split(', ')) {
sb.writeln(' $param,');
}
sb.writeln(
' );',
);
}
}
String tagsToString(List<String> tags) {
return "['${tags.join("', '")}']";
}
List<List<BddLine>> generateScenariosFromScenarioOutline(
List<BddLine> scenario,
) {
final examples = _getExamples(scenario);
return examples
.map((e) => _processScenarioLines(scenario, e).toList())
.toList();
}
List<Map<String, String>> _getExamples(
List<BddLine> scenario,
) {
final exampleLines = scenario
.skipWhile((l) => l.type != LineType.exampleTitle)
.where((l) => l.type == LineType.examples)
.map(
(e) => e.rawLine.substring(
// Remove the first and the last '|' separator
1,
e.rawLine.length - 1,
),
)
.map(_parseExampleLine);
final names = exampleLines.first;
return exampleLines.skip(1).map((e) => Map.fromIterables(names, e)).toList();
}
List<String> _parseExampleLine(String line) =>
line.split('|').map((e) => e.trim()).toList();
Iterable<BddLine> _processScenarioLines(
List<BddLine> lines,
Map<String, String> examples,
) sync* {
final name = lines.first;
yield BddLine.fromValue(
name.type,
'${name.value} (${examples.values.join(', ')})',
);
for (final line in lines.skip(1)) {
yield BddLine.fromValue(
line.type,
_replacePlaceholders(
line.value,
line.type == LineType.dataTableStep,
examples,
),
);
}
}
String _replacePlaceholders(
String line,
bool isDataTableStep,
Map<String, String> example,
) {
// For data table steps, we want placeholders in the step text
// to become parameters (wrapped with {}), but placeholders inside the
// DataTable argument should be inlined as raw values.
if (isDataTableStep) {
const marker = '{const bdd.DataTable(';
final dataTableIndex = line.indexOf(marker);
if (dataTableIndex != -1) {
final head = line.substring(0, dataTableIndex);
final tail = line.substring(dataTableIndex);
var headReplaced = head;
var tailReplaced = tail;
for (final e in example.keys) {
headReplaced = headReplaced.replaceAll('<$e>', '{${example[e]}}');
tailReplaced = tailReplaced.replaceAll('<$e>', '${example[e]}');
}
return headReplaced + tailReplaced;
}
}
return _replacePlaceholdersWithContext(line, example);
}
// Placeholders inside {} blocks become raw values,
// Placeholders outside {} blocks become parameters (wrapped with {})
String _replacePlaceholdersWithContext(
String line,
Map<String, String> example,
) {
final result = StringBuffer();
var i = 0;
var braceDepth = 0;
while (i < line.length) {
// Track brace depth to know if we're inside a parameter block
if (line[i] == '{') {
braceDepth++;
result.write('{');
i++;
} else if (line[i] == '}') {
braceDepth--;
result.write('}');
i++;
} else if (line[i] == '<') {
// Check if this is a placeholder
var foundPlaceholder = false;
for (final key in example.keys) {
final placeholder = '<$key>';
if (i + placeholder.length <= line.length &&
line.substring(i, i + placeholder.length) == placeholder) {
// Found a placeholder
if (braceDepth > 0) {
// Inside a parameter block - use raw value
result.write(example[key]);
} else {
// Outside parameter blocks - wrap with {}
result.write('{${example[key]}}');
}
i += placeholder.length;
foundPlaceholder = true;
break;
}
}
if (!foundPlaceholder) {
result.write(line[i]);
i++;
}
} else {
result.write(line[i]);
i++;
}
}
return result.toString();
}