proef-core 0.9.0

Engine-agnostic core of proef: parsing, binding, lowering, IR, emit, dispatch, World, events, errors
Documentation
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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
//! Feature front end (TECH-SPEC §4.2, §4.4, §7): gherkin parse, tag
//! accumulation, Background prepending, Rule pass-through, Scenario Outline
//! expansion, and data-table capture.
//!
//! Span discipline (TECH-SPEC §9): the gherkin crate's `Span` is 0-based byte
//! offsets (end-exclusive) into the **normalized** source (a trailing newline
//! is appended when missing) — this module normalizes identically, attaches the
//! normalized text to every diagnostic, and clamps. `LineCol` is char-counted
//! and is never used in byte math; parse-error positions are converted by
//! walking the line's `char_indices`.

use std::collections::{BTreeMap, BTreeSet};
use std::sync::Arc;

use gherkin::GherkinEnv;

use crate::diag::{Diag, Span};

/// A parsed, fully-expanded feature file: outlines are concrete scenarios,
/// Background steps are prepended, Rule scenarios are inlined.
#[derive(Debug, Clone)]
pub struct FeatureFile {
    /// Feature name as authored.
    pub name: String,
    /// Path as authored (diagnostics + step anchors).
    pub path: String,
    /// Normalized source text (trailing newline guaranteed).
    pub source: Arc<str>,
    /// Feature-level tags (without `@`).
    pub tags: Vec<String>,
    /// All concrete scenarios, in authored order.
    pub scenarios: Vec<ScenarioDef>,
}

/// One concrete (post-expansion) scenario.
#[derive(Debug, Clone)]
pub struct ScenarioDef {
    /// Scenario name (outline placeholders substituted; `#N` suffix added only
    /// when expansion would produce duplicate names).
    pub name: String,
    /// Accumulated tags: feature + rule + scenario + examples (without `@`).
    pub tags: Vec<String>,
    /// Steps, Background-first, in authored order.
    pub steps: Vec<StepDefn>,
    /// 1-based line of the scenario header (display).
    pub line: usize,
}

/// One authored step, ready for binding.
#[derive(Debug, Clone)]
pub struct StepDefn {
    /// Step text (keyword stripped, outline placeholders substituted).
    pub text: String,
    /// Data-table rows (outline placeholders substituted), when present.
    pub table: Option<Vec<Vec<String>>>,
    /// Docstring, when present (raw request bodies; outline placeholders
    /// substituted, exactly as in `text` and `table`). Naming the substitution
    /// on the two fields above and not this one read as a deliberate exception:
    /// a data-driven request body is the reason to reach for it.
    pub docstring: Option<String>,
    /// 1-based line of the step (anchors + events).
    pub line: usize,
    /// Byte span in the normalized source.
    pub span: Span,
}

/// Parse one feature file into concrete scenarios. All diagnostics carry the
/// normalized source and byte spans.
pub fn parse(path: &str, text: &str) -> Result<FeatureFile, Vec<Diag>> {
    // A UTF-8 BOM would shift every byte span (and confuse the gherkin
    // parser's first line) — normalization strips it.
    let mut normalized = text.strip_prefix('\u{feff}').unwrap_or(text).to_owned();
    if !normalized.ends_with('\n') {
        normalized.push('\n');
    }
    let source: Arc<str> = Arc::from(normalized.as_str());
    if normalized.trim().is_empty() {
        return Err(vec![
            Diag::error(
                "proef::feature::empty_file",
                "the feature file is empty — a `Feature:` header and at least one scenario are required",
            )
            .with_source(path.to_owned(), Arc::clone(&source)),
        ]);
    }

    let feature = match gherkin::Feature::parse(&*source, GherkinEnv::default()) {
        Ok(feature) => feature,
        Err(err) => {
            let mut diag = Diag::error(
                "proef::feature::parse",
                format!("the feature file does not parse: {err}"),
            )
            .with_source(path.to_owned(), Arc::clone(&source));
            if let Some(span) = parse_error_span(&err.to_string(), &source) {
                diag = diag.with_span(span);
            }
            return Err(vec![diag]);
        }
    };

    let mut diags: Vec<Diag> = Vec::new();
    let mut scenarios: Vec<ScenarioDef> = Vec::new();

    let feature_background = feature.background.as_ref();
    for scenario in &feature.scenarios {
        expand_scenario(
            scenario,
            &feature.tags,
            &[feature_background],
            path,
            &source,
            &mut scenarios,
            &mut diags,
        );
    }
    for rule in &feature.rules {
        let mut rule_tags = feature.tags.clone();
        rule_tags.extend(rule.tags.iter().cloned());
        for scenario in &rule.scenarios {
            expand_scenario(
                scenario,
                &rule_tags,
                &[feature_background, rule.background.as_ref()],
                path,
                &source,
                &mut scenarios,
                &mut diags,
            );
        }
    }

    if diags
        .iter()
        .any(|d| d.severity == crate::diag::Severity::Error)
    {
        return Err(diags);
    }
    dedup_names(&mut scenarios);
    Ok(FeatureFile {
        name: feature.name.clone(),
        path: path.to_owned(),
        source,
        tags: strip_tag_markers(&feature.tags),
        scenarios,
    })
}

/// Expand one (possibly outlined) scenario into concrete [`ScenarioDef`]s.
// One cohesive listing of the expansion rules; splitting hides the order.
#[allow(clippy::too_many_lines)]
fn expand_scenario(
    scenario: &gherkin::Scenario,
    inherited_tags: &[String],
    backgrounds: &[Option<&gherkin::Background>],
    path: &str,
    source: &Arc<str>,
    out: &mut Vec<ScenarioDef>,
    diags: &mut Vec<Diag>,
) {
    let mut tags = inherited_tags.to_vec();
    tags.extend(scenario.tags.iter().cloned());
    let base_steps: Vec<&gherkin::Step> = backgrounds
        .iter()
        .flatten()
        .flat_map(|b| b.steps.iter())
        .chain(scenario.steps.iter())
        .collect();

    // A scenario is an outline when it carries `Examples` — the gherkin crate
    // attaches those only to outlines, in any language, so it is the reliable,
    // dialect-independent signal and is what makes a localized outline expand.
    // The keyword check is a fallback so an English `Scenario Outline`/`Template`
    // whose `Examples` block is omitted still gets the crisp `no_examples` error
    // instead of being mistaken for a plain scenario. A *localized* outline
    // missing its `Examples` cannot be distinguished from a plain scenario here
    // (gherkin 0.16 keeps its dialect keywords private), so it degrades to an
    // unbound-step error on the leftover `<placeholder>` steps — a worse message,
    // never a silent pass.
    let is_outline = !scenario.examples.is_empty()
        || scenario.keyword.contains("Outline")
        || scenario.keyword.contains("Template");
    if !is_outline && scenario.examples.is_empty() {
        out.push(concrete_scenario(
            scenario,
            &tags,
            &base_steps,
            None,
            path,
            source,
            diags,
        ));
        return;
    }

    if scenario.examples.is_empty() || scenario.examples.iter().all(|e| e.table.is_none()) {
        diags.push(
            Diag::error(
                "proef::feature::no_examples",
                format!("scenario outline `{}` has no Examples rows", scenario.name),
            )
            .with_source(path.to_owned(), Arc::clone(source))
            .with_span(clamp(scenario.span, source)),
        );
        return;
    }

    // Checked here, before expansion, like every other outline-level defect
    // above (`no_examples`) — `base_steps` is the same for every Examples row,
    // so checking it once per row inside `concrete_scenario` would emit one
    // identical `empty_scenario` diagnostic per row instead of once.
    if base_steps.is_empty() {
        diags.push(empty_scenario_diag(
            &scenario.name,
            scenario.span,
            path,
            source,
        ));
        return;
    }

    let mut expanded: Vec<ScenarioDef> = Vec::new();
    for examples in &scenario.examples {
        let Some(table) = &examples.table else {
            continue;
        };
        let Some((header, rows)) = table.rows.split_first() else {
            continue;
        };
        if rows.is_empty() {
            diags.push(
                Diag::error(
                    "proef::feature::no_examples",
                    format!(
                        "scenario outline `{}` has an Examples table with a header but no rows",
                        scenario.name
                    ),
                )
                .with_source(path.to_owned(), Arc::clone(source))
                .with_span(clamp(examples.span, source)),
            );
            continue;
        }
        // Duplicate or empty header names would silently drop columns (the
        // substitution map keeps only the last) — reject them loudly.
        let mut seen = std::collections::BTreeSet::new();
        let mut header_broken = false;
        for name in header {
            let name = name.trim();
            if name.is_empty() || !seen.insert(name) {
                let what = if name.is_empty() {
                    "an empty column name".to_owned()
                } else {
                    format!("duplicate column `{name}`")
                };
                diags.push(
                    Diag::error(
                        "proef::feature::bad_examples_header",
                        format!(
                            "scenario outline `{}`: the Examples header has {what} — every column needs a unique, non-empty name",
                            scenario.name
                        ),
                    )
                    .with_source(path.to_owned(), Arc::clone(source))
                    .with_span(clamp(examples.span, source)),
                );
                header_broken = true;
            }
        }
        if header_broken {
            continue;
        }
        let mut example_tags = tags.clone();
        example_tags.extend(examples.tags.iter().cloned());
        for (row_index, row) in rows.iter().enumerate() {
            if row.len() != header.len() {
                diags.push(
                    Diag::error(
                        "proef::feature::ragged_examples",
                        format!(
                            "scenario outline `{}`: Examples row {} has {} cells, the header has {}",
                            scenario.name,
                            row_index + 1,
                            row.len(),
                            header.len()
                        ),
                    )
                    .with_source(path.to_owned(), Arc::clone(source))
                    .with_span(clamp(examples.span, source)),
                );
                continue;
            }
            let substitutions: BTreeMap<&str, &str> = header
                .iter()
                .map(String::as_str)
                .zip(row.iter().map(String::as_str))
                .collect();
            expanded.push(concrete_scenario(
                scenario,
                &example_tags,
                &base_steps,
                Some(&substitutions),
                path,
                source,
                diags,
            ));
        }
    }

    out.extend(expanded);
}

/// Disambiguate duplicate scenario names feature-wide with `#N` suffixes —
/// names key artifact slugs, console buffers, and events, so two scenarios
/// sharing a name would silently overwrite each other's artifact and drain
/// each other's console output. It is also the sole guarantee behind the
/// worker free-list key `(scenario, file)` (`proef-cli`'s
/// `exec::stamp_scenario_timing`) and behind `Record::scenarios`'s
/// `(file, scenario)` key (`proef-cli::record`), on which `explain`'s totals
/// and `--rerun`'s identity depend.
fn dedup_names(scenarios: &mut [ScenarioDef]) {
    let mut seen: BTreeMap<String, usize> = BTreeMap::new();
    for scenario_def in scenarios.iter() {
        *seen.entry(scenario_def.name.clone()).or_default() += 1;
    }
    // Every name in play — authored and assigned — so a rename can never
    // recreate the collision this function exists to prevent (an authored
    // `Name #1` next to a renamed duplicate of `Name`).
    let mut taken: BTreeSet<String> = scenarios.iter().map(|s| s.name.clone()).collect();
    let mut counters: BTreeMap<String, usize> = BTreeMap::new();
    for scenario_def in scenarios.iter_mut() {
        if seen.get(&scenario_def.name).copied().unwrap_or(0) > 1 {
            let n = counters.entry(scenario_def.name.clone()).or_default();
            let renamed = loop {
                *n += 1;
                let candidate = format!("{} #{n}", scenario_def.name);
                if !taken.contains(&candidate) {
                    break candidate;
                }
            };
            taken.insert(renamed.clone());
            scenario_def.name = renamed;
        }
    }
}

/// Build one concrete scenario, substituting outline placeholders when given.
fn concrete_scenario(
    scenario: &gherkin::Scenario,
    tags: &[String],
    steps: &[&gherkin::Step],
    substitutions: Option<&BTreeMap<&str, &str>>,
    path: &str,
    source: &Arc<str>,
    diags: &mut Vec<Diag>,
) -> ScenarioDef {
    let mut check = |text: &str, span: gherkin::Span, what: &str| -> String {
        match substitutions {
            None => text.to_owned(),
            Some(map) => {
                let (result, unknown) = substitute_placeholders(text, map);
                if let Some(name) = unknown {
                    diags.push(
                        Diag::error(
                            "proef::feature::unknown_placeholder",
                            format!(
                                "{what} references `<{name}>`, which is not an Examples column"
                            ),
                        )
                        .with_source(path.to_owned(), Arc::clone(source))
                        .with_span(clamp(span, source)),
                    );
                }
                result
            }
        }
    };

    let name = check(&scenario.name, scenario.span, "the scenario name");
    let steps: Vec<StepDefn> = steps
        .iter()
        .map(|step| {
            let text = check(&step.value, step.span, "a step");
            let docstring = step
                .docstring
                .as_ref()
                .map(|d| check(d, step.span, "a docstring"));
            let table = step.table.as_ref().map(|t| {
                t.rows
                    .iter()
                    .map(|row| {
                        row.iter()
                            .map(|cell| check(cell, t.span, "a table cell"))
                            .collect()
                    })
                    .collect()
            });
            StepDefn {
                text,
                table,
                docstring,
                line: step.position.line,
                span: clamp(step.span, source),
            }
        })
        .collect();

    // gherkin makes steps optional, so a header with a commented-out or
    // never-written body parses clean, binds to nothing, lowers to zero
    // batches, and folds to Passed — silently green forever. Catch it here,
    // where every other structural feature-file defect is caught. (The
    // outline path checks this pre-expansion instead — see the call site
    // above `concrete_scenario`'s per-row loop — so this arm only ever fires
    // for the plain-scenario path, once.)
    if steps.is_empty() {
        diags.push(empty_scenario_diag(&name, scenario.span, path, source));
    }

    ScenarioDef {
        name,
        tags: strip_tag_markers(tags),
        steps,
        line: scenario.position.line,
    }
}

/// The "scenario has no steps" diagnostic, shared by the plain-scenario path
/// (`concrete_scenario`, called once) and the outline pre-expansion check
/// (`expand_scenario`, checked once before any row is expanded) — one
/// diagnostic per empty scenario body, never one per Examples row.
fn empty_scenario_diag(name: &str, span: gherkin::Span, path: &str, source: &Arc<str>) -> Diag {
    Diag::error(
        "proef::feature::empty_scenario",
        format!("scenario `{name}` has no steps"),
    )
    .with_source(path.to_owned(), Arc::clone(source))
    .with_span(clamp(span, source))
    .with_help("a scenario must have at least one step — a commented-out body is the usual cause")
}

/// Substitute `<col>` placeholders; returns the text and the first unknown
/// placeholder name, if any.
fn substitute_placeholders(
    text: &str,
    substitutions: &BTreeMap<&str, &str>,
) -> (String, Option<String>) {
    let mut out = String::with_capacity(text.len());
    let mut unknown = None;
    let mut rest = text;
    while let Some(open) = rest.find('<') {
        out.push_str(&rest[..open]);
        let after = &rest[open + 1..];
        match after.find('>') {
            Some(close) if !after[..close].contains('<') => {
                let name = &after[..close];
                if let Some(value) = substitutions.get(name.trim()) {
                    out.push_str(value);
                } else {
                    if unknown.is_none() {
                        unknown = Some(name.trim().to_owned());
                    }
                    out.push('<');
                    out.push_str(&after[..=close]);
                }
                rest = &after[close + 1..];
            }
            _ => {
                out.push('<');
                rest = after;
            }
        }
    }
    out.push_str(rest);
    (out, unknown)
}

/// Tags without their `@` marker.
fn strip_tag_markers(tags: &[String]) -> Vec<String> {
    tags.iter()
        .map(|t| t.strip_prefix('@').unwrap_or(t).to_owned())
        .collect()
}

/// Clamp a gherkin span into the normalized source (TECH-SPEC §9).
fn clamp(span: gherkin::Span, source: &str) -> Span {
    Span::clamped(span.start, span.end, source.len())
}

/// Best-effort byte span for a gherkin parse error, extracted from its
/// rendered `Error at {line}:{col}` position (the struct fields are private;
/// col is char-counted, so the byte offset walks `char_indices`).
fn parse_error_span(message: &str, source: &str) -> Option<Span> {
    let at = message.strip_prefix("Error at ")?;
    let (line, rest) = at.split_once(':')?;
    let (col, _) = rest.split_once(':')?;
    let (line, col) = (line.parse::<usize>().ok()?, col.parse::<usize>().ok()?);
    let line_start: usize = source
        .split_inclusive('\n')
        .take(line.saturating_sub(1))
        .map(str::len)
        .sum();
    let line_text = source[line_start..].lines().next().unwrap_or("");
    let byte_in_line = line_text
        .char_indices()
        .nth(col.saturating_sub(1))
        .map_or(line_text.len(), |(idx, _)| idx);
    Some(Span::clamped(
        line_start + byte_in_line,
        line_start + byte_in_line + 1,
        source.len(),
    ))
}

#[cfg(test)]
mod tests {
    #![allow(clippy::unwrap_used)]

    use super::*;

    const FEATURE: &str = "@e2e @api\nFeature: Search\n\n  Background:\n    Given the api is available\n\n  @search\n  Scenario: Find a record\n    When I search for \"Jansen\"\n    Then the response status is 200\n\n  Scenario Outline: Statuses\n    When I check <path>\n    Then the response status is <status>\n\n    Examples:\n      | path | status |\n      | /a   | 200    |\n      | /b   | 404    |\n";

    #[test]
    fn tags_background_and_outline_expand() {
        let feature = parse("search.feature", FEATURE).unwrap();
        assert_eq!(feature.tags, vec!["e2e", "api"]);
        assert_eq!(feature.scenarios.len(), 3);

        let first = &feature.scenarios[0];
        assert_eq!(first.tags, vec!["e2e", "api", "search"]);
        assert_eq!(first.steps.len(), 3, "background prepended");
        assert_eq!(first.steps[0].text, "the api is available");

        let expanded = &feature.scenarios[1];
        assert_eq!(expanded.steps[1].text, "I check /a");
        assert_eq!(expanded.steps[2].text, "the response status is 200");
        assert_eq!(feature.scenarios[2].steps[1].text, "I check /b");
    }

    // A localized (`# language:`) feature: the gherkin crate strips the dialect
    // keywords, proef consumes the stripped step text, and a localized outline
    // with `Examples` expands like any other. Accented keywords also exercise
    // the non-ASCII byte-offset path (spans stay byte-correct).
    const FEATURE_FR: &str = "# language: fr\nFonctionnalité: Recherche\n\n  Contexte:\n    \
        Soit l'api est disponible\n\n  Scénario: Trouver un enregistrement\n    \
        Quand je cherche \"Jansen\"\n    Alors le statut est 200\n\n  \
        Plan du scénario: Statuts\n    Quand je vérifie <chemin>\n    \
        Alors le statut est <statut>\n\n    Exemples:\n      | chemin | statut |\n      \
        | /a     | 200    |\n      | /b     | 404    |\n";

    #[test]
    fn localized_gherkin_parses_and_outline_expands() {
        let feature = parse("recherche.feature", FEATURE_FR).unwrap();
        // 1 plain scenario + 2 expanded from the localized outline.
        assert_eq!(feature.scenarios.len(), 3);
        // The localized `Contexte`/`Soit` background prepends, keyword-stripped.
        assert_eq!(feature.scenarios[0].steps[0].text, "l'api est disponible");
        // The localized `Plan du scénario` expanded with `<chemin>` substituted
        // and the `Quand` keyword stripped.
        assert_eq!(feature.scenarios[1].steps[1].text, "je vérifie /a");
        assert_eq!(feature.scenarios[2].steps[1].text, "je vérifie /b");
    }

    #[test]
    fn and_but_steps_parse_as_plain_steps() {
        let text = "Feature: F\n  Scenario: S\n    When I do a thing\n    And I do another\n    Then it worked\n    But not too much\n";
        let feature = parse("f.feature", text).unwrap();
        let steps = &feature.scenarios[0].steps;
        assert_eq!(steps.len(), 4, "And/But bind by text like any step");
        assert_eq!(steps[1].text, "I do another");
    }

    #[test]
    fn scenario_with_no_steps_is_an_error() {
        // gherkin makes steps optional, so a header with a commented-out or
        // never-written body must not parse clean — it would bind to
        // nothing, lower to zero batches, and fold to Passed.
        let text = "Feature: F\n  Scenario: todo later\n";
        let errs = parse("f.feature", text).unwrap_err();
        assert_eq!(errs[0].code, "proef::feature::empty_scenario");
        assert!(
            errs[0].message.contains("todo later"),
            "{}",
            errs[0].message
        );
    }

    #[test]
    fn empty_scenario_outline_reports_once_not_once_per_row() {
        // A 3-row Examples table with an empty outline body must not emit
        // three identical `empty_scenario` diagnostics at the same span —
        // every sibling outline-level defect (`no_examples`,
        // `bad_examples_header`) reports once, and this must match.
        let text = "Feature: F\n  Scenario Outline: todo later\n\n    Examples:\n      \
            | n |\n      | 1 |\n      | 2 |\n      | 3 |\n";
        let errs = parse("f.feature", text).unwrap_err();
        let empty_scenario_errs: Vec<_> = errs
            .iter()
            .filter(|e| e.code == "proef::feature::empty_scenario")
            .collect();
        assert_eq!(
            empty_scenario_errs.len(),
            1,
            "expected exactly one empty_scenario diagnostic, got {}: {errs:?}",
            empty_scenario_errs.len()
        );
    }

    #[test]
    fn scenario_with_only_background_steps_is_not_empty() {
        // A Background contributes real steps, so a scenario with no steps of
        // its own still runs something and must not be flagged.
        let text = "Feature: F\n  Background:\n    Given the api is available\n\n  Scenario: S\n";
        let feature = parse("f.feature", text).unwrap();
        assert_eq!(feature.scenarios[0].steps.len(), 1);
    }

    /// An outline substitutes into the docstring as well as the step text —
    /// the way a request body gets data-driven. Specified in TECH-SPEC §4.4 and
    /// implemented since, but pinned by nothing until now: every other outline
    /// test asserts on step text, so a regression here would have emitted the
    /// literal `<label>` into an artifact with the suite still green.
    #[test]
    fn outline_placeholders_substitute_into_a_docstring() {
        let text = "Feature: F\n  Scenario Outline: Posting <label>\n    \
            When a record is posted\n      \"\"\"\n      \
            {\"label\": \"<label>\", \"priority\": \"<priority>\"}\n      \"\"\"\n\n    \
            Examples:\n      | label | priority |\n      | alpha | high     |\n      \
            | beta  | low      |\n";
        let feature = parse("f.feature", text).unwrap();
        assert_eq!(feature.scenarios.len(), 2);
        // Both columns land, and the scenario name substitutes alongside them.
        // The delimiting newlines are kept: a pack interpolating `${docstring}`
        // straight after its headers relies on the leading one to separate
        // headers from body in the emitted hurl.
        assert_eq!(feature.scenarios[0].name, "Posting alpha");
        assert_eq!(
            feature.scenarios[0].steps[0].docstring.as_deref(),
            Some("\n{\"label\": \"alpha\", \"priority\": \"high\"}\n")
        );
        assert_eq!(
            feature.scenarios[1].steps[0].docstring.as_deref(),
            Some("\n{\"label\": \"beta\", \"priority\": \"low\"}\n")
        );
    }

    /// The error covers docstrings too, so an author who typos a column inside
    /// a body is told at parse time rather than shipping the literal.
    #[test]
    fn unknown_placeholder_in_a_docstring_is_an_error() {
        let text = "Feature: F\n  Scenario Outline: S\n    When a record is posted\n      \
            \"\"\"\n      {\"label\": \"<wrong>\"}\n      \"\"\"\n\n    \
            Examples:\n      | label |\n      | alpha |\n";
        let errs = parse("f.feature", text).unwrap_err();
        assert_eq!(errs[0].code, "proef::feature::unknown_placeholder");
        assert!(
            errs[0].message.contains("docstring"),
            "the message must name where it looked: {}",
            errs[0].message
        );
    }

    #[test]
    fn unknown_placeholder_is_an_error() {
        let text = "Feature: F\n  Scenario Outline: S\n    When I check <wrong>\n\n    Examples:\n      | path |\n      | /a   |\n";
        let errs = parse("f.feature", text).unwrap_err();
        assert_eq!(errs[0].code, "proef::feature::unknown_placeholder");
    }

    #[test]
    fn outline_without_examples_is_an_error() {
        let text = "Feature: F\n  Scenario Outline: S\n    When I check things\n";
        let errs = parse("f.feature", text).unwrap_err();
        assert_eq!(errs[0].code, "proef::feature::no_examples");
    }

    #[test]
    fn duplicate_examples_header_column_is_an_error() {
        // Without the check the substitution map keeps only the last column
        // and the first silently vanishes.
        let text = "Feature: F\n  Scenario Outline: S\n    When I check <path>\n\n    Examples:\n      | path | path |\n      | /a   | /b   |\n";
        let errs = parse("f.feature", text).unwrap_err();
        assert!(
            errs.iter()
                .any(|d| d.code == "proef::feature::bad_examples_header"),
            "{errs:?}"
        );
    }

    #[test]
    fn empty_feature_file_gets_a_named_error() {
        let errs = parse("f.feature", "  \n\n").unwrap_err();
        assert_eq!(errs[0].code, "proef::feature::empty_file");
    }

    #[test]
    fn utf8_bom_is_stripped_before_parsing_and_spans() {
        let text = "\u{feff}Feature: F\n  Scenario: S\n    When I do a thing\n";
        let feature = parse("f.feature", text).unwrap();
        assert_eq!(feature.name, "F");
        assert!(
            !feature.source.starts_with('\u{feff}'),
            "normalized source must not carry the BOM (it would shift spans)"
        );
    }

    #[test]
    fn ragged_examples_row_is_an_error() {
        let text = "Feature: F\n  Scenario Outline: S\n    When I check <path>\n\n    Examples:\n      | path | status |\n      | /a   |\n";
        let errs = parse("f.feature", text).unwrap_err();
        // The gherkin crate itself rejects ragged tables at parse time; our
        // expansion-time check (`ragged_examples`) backstops pad-behavior
        // changes. Either way it must be a parse-time error.
        assert!(
            errs.iter()
                .any(|d| d.code == "proef::feature::ragged_examples"
                    || d.code == "proef::feature::parse")
        );
    }

    #[test]
    fn malformed_gherkin_reports_a_located_parse_error() {
        let errs = parse("f.feature", "Feature broken\nScenario: S\n").unwrap_err();
        assert_eq!(errs[0].code, "proef::feature::parse");
        assert!(errs[0].source_text.is_some());
    }

    #[test]
    fn duplicate_expanded_names_get_disambiguated() {
        let text = "Feature: F\n  Scenario Outline: Same name\n    When I check <path>\n\n    Examples:\n      | path |\n      | /a   |\n      | /b   |\n";
        let feature = parse("f.feature", text).unwrap();
        assert_eq!(feature.scenarios[0].name, "Same name #1");
        assert_eq!(feature.scenarios[1].name, "Same name #2");
    }

    #[test]
    fn rules_pass_through_with_tag_accumulation() {
        let text =
            "@f\nFeature: F\n  @r\n  Rule: R\n    @s\n    Scenario: S\n      When I do a thing\n";
        let feature = parse("f.feature", text).unwrap();
        assert_eq!(feature.scenarios[0].tags, vec!["f", "r", "s"]);
    }
}