Skip to content

Conversation

@tide-khushal
Copy link
Contributor

🐛 Problem

When using BDD scenario outlines with data tables containing variables from the Examples table, the generated Dart code incorrectly wrapped variable values in extra curly braces {}, causing invalid Dart syntax as reported in #110.

Before (Broken):

const bdd.DataTable([
  ['counter', 'color'],
  [
    {'5'},    // ❌ not an intuitive syntax - extra curly braces
    {'blue'}  // ❌ not an intuitive syntax - extra curly braces
  ]
])

After (Fixed):

const bdd.DataTable([
  ['counter', 'color'],
  [
    '5',      // ✅ Improved syntax
    'blue'    // ✅ Improved syntax
  ]
])

🔧 Root Cause & Solution

Issue Analysis

The bug was in lib/src/scenario_generator.dart in the _replacePlaceholders() function at line 131:

replaced = replaced.replaceAll('<$e>', '{${example[e]}}');

This function indiscriminately wrapped all placeholder substitutions in curly braces, including data table variables where they shouldn't be wrapped.

Core Fix

  • Refactored: _replacePlaceholders()_replacePlaceholdersInLine()
  • Added: Logic to detect data table steps (LineType.dataTableStep)
  • Conditional replacement:
    • Data table variables: Substitute directly without curly braces
    • Regular step variables: Maintain existing curly brace wrapping behavior
// New implementation
BddLine _replacePlaceholdersInLine(BddLine line, Map<String, String> example) {
  var replaced = line.value;
  final isDataTable = line.type == LineType.dataTableStep;
  for (final e in example.keys) {
    final replaceWith = isDataTable ? '${example[e]}' : '{${example[e]}}';
    replaced = replaced.replaceAll('<$e>', replaceWith);
  }
  return BddLine.fromValue(line.type, replaced);
}

🧪 Testing & Validation

Comprehensive Test Coverage

Real-World Example

  • Enhanced: Example app with working scenario outline + data table
  • Demonstrates: The fix in action with a complete working example
  • Includes: Proper step implementation for data table validation

📋 Files Changed

File Change Type Description
lib/src/scenario_generator.dart 🔧 Fix Core logic fix for data table variable substitution
test/data_tables_test.dart Test Regression test covering issue #110
example/* 📝 Example Working demonstration of the fix

✅ Verification

🎯 Impact

Resolves: Issue #110 - Data table variables incorrectly wrapped in curly braces
Enables: Proper BDD testing with scenario outlines using data tables
Maintains: Full backward compatibility with existing features

This fix ensures that generated Dart code is syntactically correct and tests can execute properly when using scenario outlines with data table variables.

Copy link
Owner

@olexale olexale left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey, thanks for the PR! 🙌 That’s definitely a bug, so I really appreciate you fixing it. Could you please revert the changes to the example app, though?

I feel that data tables should be used with caution: they don’t allow compile-time type validation and can break at runtime if someone accidentally changes a type. On top of that, I find them harder to read. Even in your artificial example, something like:

Then I see <counter> counter value on <color> background color

would be much clearer.

Since I’d prefer not to promote this functionality, I think it’s best to exclude it from the example.

@olexale olexale merged commit de94c7a into olexale:master Sep 22, 2025
1 check passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants