|
| 1 | +use std::fs; |
| 2 | +use std::path::Path; |
| 3 | + |
| 4 | +use codeql_extractor::extractor::simple; |
| 5 | +use yeast::{dump::dump_ast, Runner}; |
| 6 | + |
| 7 | +#[path = "../src/languages/mod.rs"] |
| 8 | +mod languages; |
| 9 | + |
| 10 | +#[derive(Debug)] |
| 11 | +struct CorpusCase { |
| 12 | + name: String, |
| 13 | + input: String, |
| 14 | + expected: String, |
| 15 | +} |
| 16 | + |
| 17 | +fn update_mode_enabled() -> bool { |
| 18 | + std::env::var("YEAST_UPDATE_CORPUS") |
| 19 | + .map(|v| matches!(v.to_ascii_lowercase().as_str(), "1" | "true" | "yes" | "on")) |
| 20 | + .unwrap_or(false) |
| 21 | +} |
| 22 | + |
| 23 | +fn is_header_rule(line: &str) -> bool { |
| 24 | + let trimmed = line.trim(); |
| 25 | + trimmed.len() >= 3 && trimmed.chars().all(|c| c == '=') |
| 26 | +} |
| 27 | + |
| 28 | +fn parse_corpus(content: &str) -> Vec<CorpusCase> { |
| 29 | + let lines: Vec<&str> = content.lines().collect(); |
| 30 | + let mut i = 0; |
| 31 | + let mut cases = Vec::new(); |
| 32 | + |
| 33 | + while i < lines.len() { |
| 34 | + while i < lines.len() && lines[i].trim().is_empty() { |
| 35 | + i += 1; |
| 36 | + } |
| 37 | + if i >= lines.len() { |
| 38 | + break; |
| 39 | + } |
| 40 | + |
| 41 | + assert!( |
| 42 | + is_header_rule(lines[i]), |
| 43 | + "Expected header delimiter at line {}", |
| 44 | + i + 1 |
| 45 | + ); |
| 46 | + i += 1; |
| 47 | + |
| 48 | + assert!(i < lines.len(), "Missing test name at line {}", i + 1); |
| 49 | + let name = lines[i].trim().to_string(); |
| 50 | + i += 1; |
| 51 | + |
| 52 | + assert!( |
| 53 | + i < lines.len() && is_header_rule(lines[i]), |
| 54 | + "Missing closing header delimiter for case {name}" |
| 55 | + ); |
| 56 | + i += 1; |
| 57 | + |
| 58 | + let input_start = i; |
| 59 | + while i < lines.len() && lines[i].trim() != "---" { |
| 60 | + i += 1; |
| 61 | + } |
| 62 | + assert!(i < lines.len(), "Missing --- separator for case {name}"); |
| 63 | + let input = lines[input_start..i].join("\n").trim_end().to_string(); |
| 64 | + i += 1; |
| 65 | + |
| 66 | + let expected_start = i; |
| 67 | + while i < lines.len() { |
| 68 | + if is_header_rule(lines[i]) |
| 69 | + && i + 2 < lines.len() |
| 70 | + && !lines[i + 1].trim().is_empty() |
| 71 | + && is_header_rule(lines[i + 2]) |
| 72 | + { |
| 73 | + break; |
| 74 | + } |
| 75 | + i += 1; |
| 76 | + } |
| 77 | + let expected = lines[expected_start..i].join("\n").trim().to_string(); |
| 78 | + |
| 79 | + cases.push(CorpusCase { |
| 80 | + name, |
| 81 | + input, |
| 82 | + expected, |
| 83 | + }); |
| 84 | + } |
| 85 | + |
| 86 | + cases |
| 87 | +} |
| 88 | + |
| 89 | +fn render_corpus(cases: &[CorpusCase]) -> String { |
| 90 | + let mut out = String::new(); |
| 91 | + |
| 92 | + for (idx, case) in cases.iter().enumerate() { |
| 93 | + if idx > 0 { |
| 94 | + out.push('\n'); |
| 95 | + } |
| 96 | + out.push_str("===\n"); |
| 97 | + out.push_str(case.name.trim()); |
| 98 | + out.push_str("\n===\n"); |
| 99 | + out.push('\n'); |
| 100 | + out.push_str(case.input.trim()); |
| 101 | + out.push_str("\n\n---\n"); |
| 102 | + out.push('\n'); |
| 103 | + out.push_str(case.expected.trim()); |
| 104 | + out.push_str("\n\n"); |
| 105 | + } |
| 106 | + |
| 107 | + out |
| 108 | +} |
| 109 | + |
| 110 | +fn run_desugaring(lang: &simple::LanguageSpec, input: &str) -> String { |
| 111 | + let runner = match lang.desugar.as_ref() { |
| 112 | + Some(config) => Runner::from_config(lang.ts_language.clone(), config) |
| 113 | + .expect("Failed to create yeast runner from desugaring config"), |
| 114 | + None => Runner::new(lang.ts_language.clone(), &[]), |
| 115 | + }; |
| 116 | + let ast = runner |
| 117 | + .run(input) |
| 118 | + .unwrap_or_else(|e| panic!("Failed to parse corpus input: {e}")); |
| 119 | + dump_ast(&ast, ast.get_root(), input) |
| 120 | +} |
| 121 | + |
| 122 | +#[test] |
| 123 | +fn test_corpus() { |
| 124 | + let update_mode = update_mode_enabled(); |
| 125 | + let all_languages = languages::all_language_specs(); |
| 126 | + let corpus_dir = Path::new("tests/corpus"); |
| 127 | + |
| 128 | + for lang in all_languages { |
| 129 | + let lang_corpus_dir = corpus_dir.join(&lang.prefix); |
| 130 | + if !lang_corpus_dir.exists() { |
| 131 | + continue; |
| 132 | + } |
| 133 | + |
| 134 | + let mut corpus_files: Vec<_> = fs::read_dir(&lang_corpus_dir) |
| 135 | + .unwrap_or_else(|e| { |
| 136 | + panic!( |
| 137 | + "Failed to read corpus directory {}: {e}", |
| 138 | + lang_corpus_dir.display() |
| 139 | + ) |
| 140 | + }) |
| 141 | + .map(|entry| entry.expect("Failed to read corpus entry").path()) |
| 142 | + .filter(|path| path.extension().is_some_and(|ext| ext == "txt")) |
| 143 | + .collect(); |
| 144 | + corpus_files.sort(); |
| 145 | + |
| 146 | + for corpus_path in corpus_files { |
| 147 | + let content = fs::read_to_string(&corpus_path) |
| 148 | + .unwrap_or_else(|e| panic!("Failed to read {}: {e}", corpus_path.display())); |
| 149 | + let mut cases = parse_corpus(&content); |
| 150 | + assert!( |
| 151 | + !cases.is_empty(), |
| 152 | + "No corpus cases found in {}", |
| 153 | + corpus_path.display() |
| 154 | + ); |
| 155 | + |
| 156 | + for case in &mut cases { |
| 157 | + let actual = run_desugaring(&lang, &case.input); |
| 158 | + if update_mode { |
| 159 | + case.expected = actual.trim().to_string(); |
| 160 | + } else { |
| 161 | + assert_eq!( |
| 162 | + case.expected.trim(), |
| 163 | + actual.trim(), |
| 164 | + "Corpus case failed in {}: {}", |
| 165 | + corpus_path.display(), |
| 166 | + case.name |
| 167 | + ); |
| 168 | + } |
| 169 | + } |
| 170 | + |
| 171 | + if update_mode { |
| 172 | + let updated = render_corpus(&cases); |
| 173 | + fs::write(&corpus_path, updated).unwrap_or_else(|e| { |
| 174 | + panic!( |
| 175 | + "Failed to update corpus file {}: {e}", |
| 176 | + corpus_path.display() |
| 177 | + ) |
| 178 | + }); |
| 179 | + } |
| 180 | + } |
| 181 | + } |
| 182 | +} |
0 commit comments