generate_csv/
generate_csv.rs

1//! Export test cases to CSV format (space-separated)
2//!
3//! Usage: cargo run --example generate_csv > cases.csv
4//!        cargo run --example generate_csv -- --output cases.csv
5
6use ytdlp_ejs::test_data::{ALL_VARIANTS, TEST_CASES, get_cache_path};
7use std::env;
8use std::fs::File;
9use std::io::{self, Write};
10use std::path::Path;
11
12fn main() {
13    let args: Vec<String> = env::args().collect();
14
15    // Parse output file argument
16    let output_file = if args.len() >= 3 && args[1] == "--output" {
17        Some(&args[2])
18    } else {
19        None
20    };
21
22    let mut output: Box<dyn Write> = match output_file {
23        Some(path) => {
24            let file = File::create(path).expect("Failed to create output file");
25            Box::new(file)
26        }
27        None => Box::new(io::stdout()),
28    };
29
30    let mut count = 0;
31    let mut missing = 0;
32
33    for test_case in TEST_CASES {
34        let variants = test_case.variants.unwrap_or(ALL_VARIANTS);
35
36        for variant in variants {
37            let cache_path = get_cache_path(test_case.player, variant);
38            let path = Path::new(&cache_path);
39
40            // Check if player file exists
41            if !path.exists() {
42                missing += 1;
43                continue;
44            }
45
46            let filename = format!("{}-{}", test_case.player, variant);
47
48            // Export n tests (space-separated)
49            for step in test_case.n {
50                writeln!(output, "{} n {} {}", filename, step.input, step.expected)
51                    .expect("Failed to write");
52                count += 1;
53            }
54
55            // Export sig tests (space-separated)
56            for step in test_case.sig {
57                writeln!(output, "{} sig {} {}", filename, step.input, step.expected)
58                    .expect("Failed to write");
59                count += 1;
60            }
61        }
62    }
63
64    if count == 0 {
65        eprintln!("Error: No player files found!");
66        eprintln!();
67        eprintln!("Please download player files first:");
68        eprintln!("  cargo run --example download_players");
69        std::process::exit(1);
70    }
71
72    if missing > 0 {
73        eprintln!(
74            "Warning: {} player variants not found, run 'cargo run --example download_players' to download",
75            missing
76        );
77    }
78
79    if output_file.is_some() {
80        eprintln!("Exported {} test cases", count);
81    }
82}