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
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
// Copyright (c) 2018-2022  Brendan Molloy <brendan@bbqsrc.net>,
//                          Ilya Solovyiov <ilya.solovyiov@gmail.com>,
//                          Kai Ren <tyranron@gmail.com>
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.

//! [Rust `libtest`][1] compatible [`Writer`] implementation.
//!
//! [1]: https://doc.rust-lang.org/rustc/tests/index.html

use std::{
    fmt::Debug,
    io, iter, mem,
    str::FromStr,
    time::{Duration, SystemTime},
};

use async_trait::async_trait;
use derive_more::From;
use either::Either;
use itertools::Itertools as _;
use serde::Serialize;

use crate::{
    cli,
    event::{self, Retries},
    parser,
    writer::{
        self,
        basic::{coerce_error, trim_path},
        out::WriteStrExt as _,
        Arbitrary, Normalize, Summarize,
    },
    Event, World, Writer, WriterExt as _,
};

/// CLI options of a [`Libtest`] [`Writer`].
#[derive(clap::Args, Clone, Debug, Default)]
#[group(skip)]
pub struct Cli {
    /// Formatting of the output.
    #[arg(long, value_name = "json")]
    pub format: Option<Format>,

    /// Show captured stdout of successful tests. Currently, outputs only step
    /// function location.
    #[arg(long)]
    pub show_output: bool,

    /// Enable nightly-only flags.
    #[arg(short = 'Z')]
    pub nightly: Option<String>,
}

/// Output formats.
///
/// Currently supports only JSON.
#[derive(Clone, Copy, Debug)]
pub enum Format {
    /// [`libtest`][1]'s JSON format.
    ///
    /// [1]: https://doc.rust-lang.org/rustc/tests/index.html
    Json,
}

impl FromStr for Format {
    type Err = String;

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        match s.to_lowercase().as_str() {
            "json" => Ok(Self::Json),
            s @ ("pretty" | "terse" | "junit") => {
                Err(format!("`{s}` option is not supported yet"))
            }
            s => Err(format!(
                "Unknown option `{s}`, expected `pretty` or `json`",
            )),
        }
    }
}

/// [`libtest`][1] compatible [`Writer`].
///
/// Currently used only to support `--format=json` option.
///
/// # Ordering
///
/// This [`Writer`] isn't [`Normalized`] by itself, so should be wrapped into a
/// [`writer::Normalize`], otherwise will produce output [`Event`]s in a broken
/// order.
///
/// Ideally, we shouldn't wrap this into a [`writer::Normalize`] and leave this
/// to tools, parsing JSON output. Unfortunately, not all tools can do that (ex.
/// [`IntelliJ Rust`][2]), so it's still recommended to wrap this into
/// [`writer::Normalize`] even if it can mess up timing reports.
///
/// [`Normalized`]: writer::Normalized
/// [1]: https://doc.rust-lang.org/rustc/tests/index.html
/// [2]: https://github.com/intellij-rust/intellij-rust/issues/9041
#[derive(Debug)]
pub struct Libtest<W, Out: io::Write = io::Stdout> {
    /// [`io::Write`] implementor to output into.
    output: Out,

    /// Collection of events before [`ParsingFinished`] is received.
    ///
    /// Until a [`ParsingFinished`] is received, all the events are stored
    /// inside [`Libtest::events`] and outputted only after that event is
    /// received. This is done, because [`libtest`][1]'s first event must
    /// contain number of executed test cases.
    ///
    /// [`ParsingFinished`]: event::Cucumber::ParsingFinished
    /// [1]: https://doc.rust-lang.org/rustc/tests/index.html
    events: Vec<parser::Result<Event<event::Cucumber<W>>>>,

    /// Indicates whether a [`ParsingFinished`] event was received.
    ///
    /// [`ParsingFinished`]: event::Cucumber::ParsingFinished
    parsed_all: bool,

    /// Number of passed [`Step`]s.
    ///
    /// [`Step`]: gherkin::Step
    passed: usize,

    /// Number of failed [`Step`]s.
    ///
    /// [`Step`]: gherkin::Step
    failed: usize,

    /// Number of retried [`Step`]s.
    ///
    /// [`Step`]: gherkin::Step
    retried: usize,

    /// Number of skipped [`Step`]s.
    ///
    /// [`Step`]: gherkin::Step
    ignored: usize,

    /// Number of [`Parser`] errors.
    ///
    /// [`Parser`]: crate::Parser
    parsing_errors: usize,

    /// Number of [`Hook`] errors.
    ///
    /// [`Hook`]: event::Hook
    hook_errors: usize,

    /// Number of [`Feature`]s with [`path`] set to [`None`].
    ///
    /// This value is used to generate a unique name for each [`Feature`] to
    /// avoid name collisions.
    ///
    /// [`Feature`]: gherkin::Feature
    /// [`path`]: gherkin::Feature::path
    features_without_path: usize,

    /// [`SystemTime`] when the [`Started`] event was received.
    ///
    /// [`Started`]: event::Cucumber::Started
    started_at: Option<SystemTime>,
}

// Implemented manually to omit redundant `World: Clone` trait bound, imposed by
// `#[derive(Clone)]`.
impl<World, Out: Clone + io::Write> Clone for Libtest<World, Out> {
    fn clone(&self) -> Self {
        Self {
            output: self.output.clone(),
            events: self.events.clone(),
            parsed_all: self.parsed_all,
            passed: self.passed,
            failed: self.failed,
            retried: self.retried,
            ignored: self.ignored,
            parsing_errors: self.parsing_errors,
            hook_errors: self.hook_errors,
            features_without_path: self.features_without_path,
            started_at: self.started_at,
        }
    }
}

#[async_trait(?Send)]
impl<W: World + Debug, Out: io::Write> Writer<W> for Libtest<W, Out> {
    type Cli = Cli;

    async fn handle_event(
        &mut self,
        event: parser::Result<Event<event::Cucumber<W>>>,
        cli: &Self::Cli,
    ) {
        self.handle_cucumber_event(event, cli);
    }
}

/// Shortcut of a [`Libtest::or()`] return type.
pub type Or<W, Wr> = writer::Or<
    Wr,
    Normalize<W, Libtest<W, io::Stdout>>,
    fn(
        &parser::Result<Event<event::Cucumber<W>>>,
        &cli::Compose<<Wr as Writer<W>>::Cli, Cli>,
    ) -> bool,
>;

/// Shortcut of a [`Libtest::or_basic()`] return type.
pub type OrBasic<W> = Or<W, Summarize<Normalize<W, writer::Basic>>>;

impl<W: Debug + World> Libtest<W, io::Stdout> {
    /// Creates a new [`Normalized`] [`Libtest`] [`Writer`] outputting into the
    /// [`io::Stdout`].
    ///
    /// [`Normalized`]: writer::Normalized
    #[must_use]
    pub fn stdout() -> Normalize<W, Self> {
        Self::new(io::stdout())
    }

    /// Creates a new [`Writer`] which uses a [`Normalized`] [`Libtest`] in case
    /// [`Cli::format`] is set to [`Json`], or provided the `writer` otherwise.
    ///
    /// [`Json`]: Format::Json
    /// [`Normalized`]: writer::Normalized
    #[must_use]
    pub fn or<AnotherWriter: Writer<W>>(
        writer: AnotherWriter,
    ) -> Or<W, AnotherWriter> {
        writer::Or::new(writer, Self::stdout(), |_, cli| {
            !matches!(cli.right.format, Some(writer::libtest::Format::Json))
        })
    }

    /// Creates a new [`Writer`] which uses a [`Normalized`] [`Libtest`] in case
    /// [`Cli::format`] is set to [`Json`], or a [`Normalized`]
    /// [`writer::Basic`] otherwise.
    ///
    /// [`Json`]: Format::Json
    /// [`Normalized`]: writer::Normalized
    #[must_use]
    pub fn or_basic() -> OrBasic<W> {
        Self::or(writer::Basic::stdout().summarized())
    }
}

impl<W: Debug + World, Out: io::Write> Libtest<W, Out> {
    /// Creates a new [`Normalized`] [`Libtest`] [`Writer`] outputting into the
    /// provided `output`.
    ///
    /// Theoretically, normalization should be done by the tool that's consuming
    /// the output og this [`Writer`]. But lack of clear specification of the
    /// [`libtest`][1]'s JSON output leads to some tools [struggling][2] to
    /// interpret it. So, we recommend using a [`Normalized`] [`Libtest::new()`]
    /// rather than a non-[`Normalized`] [`Libtest::raw()`].
    ///
    /// [`Normalized`]: writer::Normalized
    /// [1]: https://doc.rust-lang.org/rustc/tests/index.html
    /// [2]: https://github.com/intellij-rust/intellij-rust/issues/9041
    #[must_use]
    pub fn new(output: Out) -> Normalize<W, Self> {
        Self::raw(output).normalized()
    }

    /// Creates a new non-[`Normalized`] [`Libtest`] [`Writer`] outputting into
    /// the provided `output`.
    ///
    /// Theoretically, normalization should be done by the tool that's consuming
    /// the output og this [`Writer`]. But lack of clear specification of the
    /// [`libtest`][1]'s JSON output leads to some tools [struggling][2] to
    /// interpret it. So, we recommend using a [`Normalized`] [`Libtest::new()`]
    /// rather than a non-[`Normalized`] [`Libtest::raw()`].
    ///
    /// [`Normalized`]: writer::Normalized
    /// [1]: https://doc.rust-lang.org/rustc/tests/index.html
    /// [2]: https://github.com/intellij-rust/intellij-rust/issues/9041
    #[must_use]
    pub const fn raw(output: Out) -> Self {
        Self {
            output,
            events: Vec::new(),
            parsed_all: false,
            passed: 0,
            failed: 0,
            retried: 0,
            parsing_errors: 0,
            hook_errors: 0,
            ignored: 0,
            started_at: None,
            features_without_path: 0,
        }
    }

    /// Handles the provided [`event::Cucumber`].
    ///
    /// Until [`ParsingFinished`] is received, all the events are stored inside
    /// [`Libtest::events`] and outputted only after that event is received.
    /// This is done, because [`libtest`][1]'s first event must contain number
    /// of executed test cases.
    ///
    /// [1]: https://doc.rust-lang.org/rustc/tests/index.html
    /// [`ParsingFinished`]: event::Cucumber::ParsingFinished
    fn handle_cucumber_event(
        &mut self,
        event: parser::Result<Event<event::Cucumber<W>>>,
        cli: &Cli,
    ) {
        use event::{Cucumber, Metadata};

        let unite = |ev: Result<(Cucumber<W>, Metadata), _>| {
            ev.map(|(e, m)| m.insert(e))
        };

        match (event.map(Event::split), self.parsed_all) {
            (event @ Ok((Cucumber::ParsingFinished { .. }, _)), false) => {
                self.parsed_all = true;

                let all_events =
                    iter::once(unite(event)).chain(mem::take(&mut self.events));
                for ev in all_events {
                    self.output_event(ev, cli);
                }
            }
            (event, false) => self.events.push(unite(event)),
            (event, true) => self.output_event(unite(event), cli),
        }
    }

    /// Outputs the provided [`event::Cucumber`].
    fn output_event(
        &mut self,
        event: parser::Result<Event<event::Cucumber<W>>>,
        cli: &Cli,
    ) {
        for ev in self.expand_cucumber_event(event, cli) {
            self.output
                .write_line(serde_json::to_string(&ev).unwrap_or_else(|e| {
                    panic!("Failed to serialize `LibTestJsonEvent`: {e}")
                }))
                .unwrap_or_else(|e| panic!("Failed to write: {e}"));
        }
    }

    /// Converts the provided [`event::Cucumber`] into [`LibTestJsonEvent`]s.
    fn expand_cucumber_event(
        &mut self,
        event: parser::Result<Event<event::Cucumber<W>>>,
        cli: &Cli,
    ) -> Vec<LibTestJsonEvent> {
        use event::Cucumber;

        match event.map(Event::split) {
            Ok((Cucumber::Started, meta)) => {
                self.started_at = Some(meta.at);
                Vec::new()
            }
            Ok((
                Cucumber::ParsingFinished {
                    steps,
                    parser_errors,
                    ..
                },
                _,
            )) => {
                vec![SuiteEvent::Started {
                    test_count: steps + parser_errors,
                }
                .into()]
            }
            Ok((Cucumber::Finished, _)) => {
                let exec_time = self
                    .started_at
                    .and_then(|started| {
                        SystemTime::now().duration_since(started).ok()
                    })
                    .as_ref()
                    .map(Duration::as_secs_f64);

                let failed =
                    self.failed + self.parsing_errors + self.hook_errors;
                let results = SuiteResults {
                    passed: self.passed,
                    failed,
                    ignored: self.ignored,
                    measured: 0,
                    filtered_out: 0,
                    exec_time,
                };
                let ev = if failed == 0 {
                    SuiteEvent::Ok { results }
                } else {
                    SuiteEvent::Failed { results }
                }
                .into();

                vec![ev]
            }
            Ok((Cucumber::Feature(feature, ev), _)) => {
                self.expand_feature_event(&feature, ev, cli)
            }
            Err(e) => {
                self.parsing_errors += 1;

                let path = match &e {
                    parser::Error::Parsing(e) => match &**e {
                        gherkin::ParseFileError::Parsing { path, .. }
                        | gherkin::ParseFileError::Reading { path, .. } => {
                            Some(path)
                        }
                    },
                    parser::Error::ExampleExpansion(e) => e.path.as_ref(),
                };
                let name = path.and_then(|p| p.to_str()).map_or_else(
                    || self.parsing_errors.to_string(),
                    |p| p.escape_default().to_string(),
                );
                let name = format!("Feature: Parsing {name}");

                vec![
                    TestEvent::started(name.clone()).into(),
                    TestEvent::failed(name).with_stdout(e.to_string()).into(),
                ]
            }
        }
    }

    /// Converts the provided [`event::Feature`] into [`LibTestJsonEvent`]s.
    fn expand_feature_event(
        &mut self,
        feature: &gherkin::Feature,
        ev: event::Feature<W>,
        cli: &Cli,
    ) -> Vec<LibTestJsonEvent> {
        use event::{Feature, Rule};

        match ev {
            Feature::Started
            | Feature::Finished
            | Feature::Rule(_, Rule::Started | Rule::Finished) => Vec::new(),
            Feature::Rule(rule, Rule::Scenario(scenario, ev)) => self
                .expand_scenario_event(
                    feature,
                    Some(&rule),
                    &scenario,
                    ev,
                    cli,
                ),
            Feature::Scenario(scenario, ev) => {
                self.expand_scenario_event(feature, None, &scenario, ev, cli)
            }
        }
    }

    /// Converts the provided [`event::Scenario`] into [`LibTestJsonEvent`]s.
    fn expand_scenario_event(
        &mut self,
        feature: &gherkin::Feature,
        rule: Option<&gherkin::Rule>,
        scenario: &gherkin::Scenario,
        ev: event::RetryableScenario<W>,
        cli: &Cli,
    ) -> Vec<LibTestJsonEvent> {
        use event::Scenario;

        let retries = ev.retries;
        match ev.event {
            Scenario::Started | Scenario::Finished => Vec::new(),
            Scenario::Hook(ty, ev) => {
                self.expand_hook_event(feature, rule, scenario, ty, ev, retries)
            }
            Scenario::Background(step, ev) => self.expand_step_event(
                feature, rule, scenario, &step, ev, retries, true, cli,
            ),
            Scenario::Step(step, ev) => self.expand_step_event(
                feature, rule, scenario, &step, ev, retries, false, cli,
            ),
        }
    }

    /// Converts the provided [`event::Hook`] into [`LibTestJsonEvent`]s.
    fn expand_hook_event(
        &mut self,
        feature: &gherkin::Feature,
        rule: Option<&gherkin::Rule>,
        scenario: &gherkin::Scenario,
        hook: event::HookType,
        ev: event::Hook<W>,
        retries: Option<Retries>,
    ) -> Vec<LibTestJsonEvent> {
        match ev {
            event::Hook::Started | event::Hook::Passed => Vec::new(),
            event::Hook::Failed(world, info) => {
                self.hook_errors += 1;

                let name = self.test_case_name(
                    feature,
                    rule,
                    scenario,
                    Either::Left(hook),
                    retries,
                );

                vec![
                    TestEvent::started(name.clone()).into(),
                    TestEvent::failed(name)
                        .with_stdout(format!(
                            "{}{}",
                            coerce_error(&info),
                            world
                                .map(|w| format!("\n{w:#?}"))
                                .unwrap_or_default(),
                        ))
                        .into(),
                ]
            }
        }
    }

    /// Converts the provided [`event::Step`] into [`LibTestJsonEvent`]s.
    #[allow(clippy::too_many_arguments)]
    fn expand_step_event(
        &mut self,
        feature: &gherkin::Feature,
        rule: Option<&gherkin::Rule>,
        scenario: &gherkin::Scenario,
        step: &gherkin::Step,
        ev: event::Step<W>,
        retries: Option<Retries>,
        is_background: bool,
        cli: &Cli,
    ) -> Vec<LibTestJsonEvent> {
        use event::Step;

        let name = self.test_case_name(
            feature,
            rule,
            scenario,
            Either::Right((step, is_background)),
            retries,
        );

        let ev = match ev {
            Step::Started => TestEvent::started(name),
            Step::Passed(_, loc) => {
                self.passed += 1;

                let event = TestEvent::ok(name);
                if cli.show_output {
                    event.with_stdout(format!(
                        "{}:{}:{} (defined){}",
                        feature
                            .path
                            .as_ref()
                            .and_then(|p| p.to_str().map(trim_path))
                            .unwrap_or(&feature.name),
                        step.position.line,
                        step.position.col,
                        loc.map(|l| format!(
                            "\n{}:{}:{} (matched)",
                            l.path, l.line, l.column,
                        ))
                        .unwrap_or_default()
                    ))
                } else {
                    event
                }
            }
            Step::Skipped => {
                self.ignored += 1;

                let event = TestEvent::ignored(name);
                if cli.show_output {
                    event.with_stdout(format!(
                        "{}:{}:{} (defined)",
                        feature
                            .path
                            .as_ref()
                            .and_then(|p| p.to_str().map(trim_path))
                            .unwrap_or(&feature.name),
                        step.position.line,
                        step.position.col,
                    ))
                } else {
                    event
                }
            }
            Step::Failed(_, loc, world, err) => {
                if retries
                    .map(|r| {
                        r.left > 0 && !matches!(err, event::StepError::NotFound)
                    })
                    .unwrap_or_default()
                {
                    self.retried += 1;
                } else {
                    self.failed += 1;
                }

                TestEvent::failed(name).with_stdout(format!(
                    "{}:{}:{} (defined){}\n{err}{}",
                    feature
                        .path
                        .as_ref()
                        .and_then(|p| p.to_str().map(trim_path))
                        .unwrap_or(&feature.name),
                    step.position.line,
                    step.position.col,
                    loc.map(|l| format!(
                        "\n{}:{}:{} (matched)",
                        l.path, l.line, l.column,
                    ))
                    .unwrap_or_default(),
                    world.map(|w| format!("\n{w:#?}")).unwrap_or_default(),
                ))
            }
        };

        vec![ev.into()]
    }

    /// Generates test case name.
    fn test_case_name(
        &mut self,
        feature: &gherkin::Feature,
        rule: Option<&gherkin::Rule>,
        scenario: &gherkin::Scenario,
        step: Either<event::HookType, (&gherkin::Step, IsBackground)>,
        retries: Option<Retries>,
    ) -> String {
        let feature_name = format!(
            "{}: {} {}",
            feature.keyword,
            feature.name,
            feature
                .path
                .as_ref()
                .and_then(|p| p.to_str().map(trim_path))
                .map_or_else(
                    || {
                        self.features_without_path += 1;
                        self.features_without_path.to_string()
                    },
                    |s| s.escape_default().to_string()
                ),
        );
        let rule_name = rule
            .as_ref()
            .map(|r| format!("{}: {}: {}", r.position.line, r.keyword, r.name));
        let scenario_name = format!(
            "{}: {}: {}{}",
            scenario.position.line,
            scenario.keyword,
            scenario.name,
            retries
                .filter(|r| r.current > 0)
                .map(|r| format!(
                    " | Retry attempt {}/{}",
                    r.current,
                    r.current + r.left,
                ))
                .unwrap_or_default(),
        );
        let step_name = match step {
            Either::Left(hook) => format!("{hook} hook"),
            Either::Right((step, is_bg)) => format!(
                "{}: {} {}{}",
                step.position.line,
                is_bg
                    .then(|| feature
                        .background
                        .as_ref()
                        .map_or("Background", |bg| bg.keyword.as_str()))
                    .unwrap_or_default(),
                step.keyword,
                step.value,
            ),
        };

        [
            Some(feature_name),
            rule_name,
            Some(scenario_name),
            Some(step_name),
        ]
        .into_iter()
        .flatten()
        .join("::")
    }
}

/// Indicator, whether a [`Step`] is [`Background`] or not.
///
/// [`Background`]: event::Scenario::Background
/// [`Step`]: gherkin::Step
type IsBackground = bool;

impl<W, O: io::Write> writer::NonTransforming for Libtest<W, O> {}

impl<W, O> writer::Stats<W> for Libtest<W, O>
where
    O: io::Write,
    Self: Writer<W>,
{
    fn passed_steps(&self) -> usize {
        self.passed
    }

    fn skipped_steps(&self) -> usize {
        self.ignored
    }

    fn failed_steps(&self) -> usize {
        self.failed
    }

    fn retried_steps(&self) -> usize {
        self.retried
    }

    fn parsing_errors(&self) -> usize {
        self.parsing_errors
    }

    fn hook_errors(&self) -> usize {
        self.hook_errors
    }
}

#[async_trait(?Send)]
impl<'val, W, Val, Out> Arbitrary<'val, W, Val> for Libtest<W, Out>
where
    W: World + Debug,
    Val: AsRef<str> + 'val,
    Out: io::Write,
{
    async fn write(&mut self, val: Val)
    where
        'val: 'async_trait,
    {
        self.output
            .write_line(val.as_ref())
            .unwrap_or_else(|e| panic!("Failed to write: {e}"));
    }
}

/// [`libtest`][1]'s JSON event.
///
/// This format isn't stable, so this implementation uses [implementation][1] as
/// a reference point.
///
/// [1]: https://bit.ly/3PrLtKC
#[derive(Clone, Debug, From, Serialize)]
#[serde(tag = "type", rename_all = "snake_case")]
enum LibTestJsonEvent {
    /// Event of test suite.
    Suite {
        /// [`SuiteEvent`]
        #[serde(flatten)]
        event: SuiteEvent,
    },

    /// Event of the test case.
    Test {
        /// [`TestEvent`]
        #[serde(flatten)]
        event: TestEvent,
    },
}

/// Test suite event.
#[derive(Clone, Debug, Serialize)]
#[serde(tag = "event", rename_all = "snake_case")]
enum SuiteEvent {
    /// Test suite started.
    Started {
        /// Number of test cases. In our case, this is number of parsed
        /// [`Step`]s and [`Parser`] errors.
        ///
        /// [`Parser`]: crate::Parser
        /// [`Step`]: gherkin::Step
        test_count: usize,
    },

    /// Test suite finished without errors.
    Ok {
        /// Execution results.
        #[serde(flatten)]
        results: SuiteResults,
    },

    /// Test suite encountered errors during the execution.
    Failed {
        /// Execution results.
        #[serde(flatten)]
        results: SuiteResults,
    },
}

/// Test suite execution results.
#[derive(Clone, Copy, Debug, Serialize)]
struct SuiteResults {
    /// Number of passed test cases.
    passed: usize,

    /// Number of failed test cases.
    failed: usize,

    /// Number of ignored test cases.
    ignored: usize,

    /// Number of measured benches.
    measured: usize,

    // TODO: Figure out a way to actually report this.
    /// Number of filtered out test cases.
    filtered_out: usize,

    /// Test suite execution time.
    #[serde(skip_serializing_if = "Option::is_none")]
    exec_time: Option<f64>,
}

/// Test case event.
#[derive(Clone, Debug, Serialize)]
#[serde(tag = "event", rename_all = "snake_case")]
enum TestEvent {
    /// Test case started.
    Started(TestEventInner),

    /// Test case finished successfully.
    Ok(TestEventInner),

    /// Test case failed.
    Failed(TestEventInner),

    /// Test case ignored.
    Ignored(TestEventInner),

    /// Test case timed out.
    Timeout(TestEventInner),
}

impl TestEvent {
    /// Creates a new [`TestEvent::Started`].
    const fn started(name: String) -> Self {
        Self::Started(TestEventInner::new(name))
    }

    /// Creates a new [`TestEvent::Ok`].
    const fn ok(name: String) -> Self {
        Self::Ok(TestEventInner::new(name))
    }

    /// Creates a new [`TestEvent::Failed`].
    const fn failed(name: String) -> Self {
        Self::Failed(TestEventInner::new(name))
    }

    /// Creates a new [`TestEvent::Ignored`].
    const fn ignored(name: String) -> Self {
        Self::Ignored(TestEventInner::new(name))
    }

    /// Creates a new [`TestEvent::Timeout`].
    #[allow(dead_code)]
    const fn timeout(name: String) -> Self {
        Self::Timeout(TestEventInner::new(name))
    }

    /// Adds a [`TestEventInner::stdout`].
    fn with_stdout(self, mut stdout: String) -> Self {
        if !stdout.ends_with('\n') {
            stdout.push('\n');
        }

        match self {
            Self::Started(inner) => Self::Started(inner.with_stdout(stdout)),
            Self::Ok(inner) => Self::Ok(inner.with_stdout(stdout)),
            Self::Failed(inner) => Self::Failed(inner.with_stdout(stdout)),
            Self::Ignored(inner) => Self::Ignored(inner.with_stdout(stdout)),
            Self::Timeout(inner) => Self::Timeout(inner.with_stdout(stdout)),
        }
    }
}

/// Inner value of a [`TestEvent`].
#[derive(Clone, Debug, Serialize)]
struct TestEventInner {
    /// Name of this test case.
    name: String,

    /// [`Stdout`] of this test case.
    ///
    /// [`Stdout`]: io::Stdout
    #[serde(skip_serializing_if = "Option::is_none")]
    stdout: Option<String>,

    /// [`Stderr`] of this test case.
    ///
    /// Isn't actually used, as [IntelliJ Rust][1] ignores it.
    ///
    /// [1]: https://github.com/intellij-rust/intellij-rust/issues/9041
    /// [`Stderr`]: io::Stderr
    #[serde(skip_serializing_if = "Option::is_none")]
    stderr: Option<String>,
}

impl TestEventInner {
    /// Creates a new [`TestEventInner`].
    const fn new(name: String) -> Self {
        Self {
            name,
            stdout: None,
            stderr: None,
        }
    }

    /// Adds a [`TestEventInner::stdout`].
    #[allow(clippy::missing_const_for_fn)] // false positive: drop in const
    fn with_stdout(mut self, stdout: String) -> Self {
        self.stdout = Some(stdout);
        self
    }
}