|
| 1 | +//! Haskell 六类别真实工具验收 |
| 2 | +//! 验收标准:正例通过 + 负例检出 + 格式≠注释合规 |
| 3 | +
|
| 4 | +use std::process::Command; |
| 5 | + |
| 6 | +fn ensure_clean_dir(name: &str) -> std::path::PathBuf { |
| 7 | + let tmp = std::env::temp_dir().join(name); |
| 8 | + let _ = std::fs::remove_dir_all(&tmp); |
| 9 | + std::fs::create_dir_all(&tmp).unwrap(); |
| 10 | + tmp |
| 11 | +} |
| 12 | + |
| 13 | +/// 正例通过 |
| 14 | +#[test] |
| 15 | +fn valid_haskell_samples_pass_compile() { |
| 16 | + let tmp = ensure_clean_dir("haskell-valid-test"); |
| 17 | + std::fs::write(tmp.join("Main.hs"), "-- | Adds two numbers.\nadd :: Int -> Int -> Int\nadd a b = a + b\n\nmain :: IO ()\nmain = print (add 1 2)\n").unwrap(); |
| 18 | + |
| 19 | + let output = Command::new("ghc") |
| 20 | + .args(["-fno-code", "Main.hs"]) |
| 21 | + .current_dir(&tmp) |
| 22 | + .output() |
| 23 | + .expect("ghc 不可用"); |
| 24 | + |
| 25 | + assert!(output.status.success(), "有效 Haskell 代码应通过编译: {:?}", output); |
| 26 | + std::fs::remove_dir_all(&tmp).unwrap(); |
| 27 | +} |
| 28 | + |
| 29 | +/// 负例检出 |
| 30 | +#[test] |
| 31 | +fn invalid_haskell_samples_are_detected() { |
| 32 | + let tmp = ensure_clean_dir("haskell-invalid-test"); |
| 33 | + std::fs::write(tmp.join("Main.hs"), "broken :: Int -> Int -> Int\nbroken a b = a + b + notDefinedAnywhere\n\nmain :: IO ()\nmain = print (broken 1 2)\n").unwrap(); |
| 34 | + |
| 35 | + let output = Command::new("ghc") |
| 36 | + .args(["-fno-code", "Main.hs"]) |
| 37 | + .current_dir(&tmp) |
| 38 | + .output() |
| 39 | + .expect("ghc 不可用"); |
| 40 | + |
| 41 | + assert!(!output.status.success(), "未定义变量应被检出"); |
| 42 | + std::fs::remove_dir_all(&tmp).unwrap(); |
| 43 | +} |
| 44 | + |
| 45 | +/// 格式≠注释合规 |
| 46 | +#[test] |
| 47 | +fn formatting_cannot_impersonate_comment_checking() { |
| 48 | + let tmp = ensure_clean_dir("haskell-comment-test"); |
| 49 | + // 格式正确但缺少注释 |
| 50 | + std::fs::write(tmp.join("Main.hs"), "undocumented :: Int -> Int\nundocumented a = a * 2\n\n-- | Has docs.\ndocumented :: Int -> Int\ndocumented a = a + 1\n\nmain :: IO ()\nmain = print (undocumented 5 + documented 3)\n").unwrap(); |
| 51 | + |
| 52 | + let output = Command::new("ghc") |
| 53 | + .args(["-fno-code", "Main.hs"]) |
| 54 | + .current_dir(&tmp) |
| 55 | + .output() |
| 56 | + .expect("ghc 不可用"); |
| 57 | + |
| 58 | + assert!(output.status.success(), "Haskell 应通过(语法正确),但注释合规需另查: {:?}", output); |
| 59 | + std::fs::remove_dir_all(&tmp).unwrap(); |
| 60 | +} |
0 commit comments