-
Couldn't load subscription status.
- Fork 40
Description
When using Scenario Outline with variables that should be substituted as raw values (not parameters), the generated test code incorrectly wraps the substituted values in curly braces {}, creating unexpected Dart syntax.
Expected Behavior
Variables in scenario outlines should be substituted as raw values when they appear within parameter structures like arrays or named parameters.
Array Parameter Example
Expected generated code:
testWidgets('''Outline: Variables in array (1, 2)''', (tester) async {
await iTapTimes(tester, [1, 2]);
});
testWidgets('''Outline: Variables in array (42, -84)''', (tester) async {
await iTapTimes(tester, [42, -84]);
});Named Parameter Example
Expected generated code:
testWidgets('''Outline: Variables in named parameter variable (1, 2)''',
(tester) async {
await iTapTimes(tester, plus: 1, minus: 2);
});
testWidgets('''Outline: Variables in named parameter variable (42, -84)''',
(tester) async {
await iTapTimes(tester, plus: 42, minus: -84);
});Actual Behavior
Variables are wrapped in curly braces, creating invalid Dart syntax.
Array Parameter Example
Actual generated code:
testWidgets('''Outline: Variables in array (1, 2)''', (tester) async {
await iTapTimes(tester, [{1}, {2}]);
});
testWidgets('''Outline: Variables in array (42, -84)''', (tester) async {
await iTapTimes(tester, [{42}, {-84}]);
});Named Parameter Example
Actual generated code:
testWidgets('''Outline: Variables in named parameter variable (1, 2)''',
(tester) async {
await iTapTimes(tester, plus: {1}, minus: {2});
});
testWidgets('''Outline: Variables in named parameter variable (42, -84)''',
(tester) async {
await iTapTimes(tester, plus: {42}, minus: {-84});
});Steps to Reproduce
- Create a feature file with scenario outlines containing variables within arrays or named parameters:
Scenario Outline: Variables in array
When I tap {[<plus>, <minus>]} times
Examples:
| plus | minus |
| 1 | 2 |
| 42 | -84 |
Scenario Outline: Variables in named parameter variable
When I tap {plus: <plus>, minus: <minus>} times
Examples:
| plus | minus |
| 1 | 2 |
| 42 | -84 |- Generate the test code using the BDD widget test framework
- Observe that variables are wrapped in
{}instead of being substituted as raw values
Root Cause
The issue is in the _replacePlaceholders function in lib/src/scenario_generator.dart at line 158:
replaced = replaced.replaceAll('<$e>', '{${example[e]}}');This line always wraps substituted variables with curly braces, but when variables appear within existing parameter structures (like arrays [<var>] or named parameters {key: <var>}), they should be substituted as raw values without additional wrapping.
Impact
- Generated test code contains unexpected Dart sets
- Developers need to use test steps with Dart sets and place additional logic in test step to extract the variable
Environment
- BDD Widget Test version: 2.1.2
- Dart/Flutter version: 3.9.2/3.35.4
Suggested Fix
The _replacePlaceholders function needs to be enhanced to detect when variables are already within parameter contexts and substitute them as raw values instead of wrapping them in additional curly braces.
A potential approach would be to:
- Parse the context around each placeholder
- If the placeholder is already within a parameter structure (between
{}braces), substitute as raw value - Otherwise, maintain the current behavior of wrapping with
{}