|
| 1 | +#[cfg(test)] |
| 2 | +mod samples { |
| 3 | + use std::fs; |
| 4 | + use std::path::Path; |
| 5 | + |
| 6 | + use technique::parsing; |
| 7 | + |
| 8 | + #[test] |
| 9 | + fn ensure_samples_parse() { |
| 10 | + let dir = Path::new("tests/samples/"); |
| 11 | + |
| 12 | + assert!(dir.exists(), "samples directory missing"); |
| 13 | + |
| 14 | + let entries = fs::read_dir(dir).expect("Failed to read samples directory"); |
| 15 | + |
| 16 | + let mut files = Vec::new(); |
| 17 | + for entry in entries { |
| 18 | + let entry = entry.expect("Failed to read directory entry"); |
| 19 | + let path = entry.path(); |
| 20 | + |
| 21 | + if path |
| 22 | + .extension() |
| 23 | + .and_then(|s| s.to_str()) |
| 24 | + == Some("tq") |
| 25 | + { |
| 26 | + files.push(path); |
| 27 | + } |
| 28 | + } |
| 29 | + |
| 30 | + assert!(!files.is_empty(), "No .tq files found in samples directory"); |
| 31 | + |
| 32 | + let mut failures = Vec::new(); |
| 33 | + |
| 34 | + for file in &files { |
| 35 | + let content = parsing::load(&file) |
| 36 | + .unwrap_or_else(|e| panic!("Failed to load file {:?}: {:?}", file, e)); |
| 37 | + |
| 38 | + match parsing::parse(&file, &content) { |
| 39 | + Ok(_) => {} |
| 40 | + Err(e) => { |
| 41 | + println!("File {:?} failed to parse: {:?}", file, e); |
| 42 | + failures.push(file.clone()); |
| 43 | + } |
| 44 | + } |
| 45 | + } |
| 46 | + |
| 47 | + if !failures.is_empty() { |
| 48 | + panic!( |
| 49 | + "Sample files should parse successfully, but {} files failed", |
| 50 | + failures.len() |
| 51 | + ); |
| 52 | + } |
| 53 | + } |
| 54 | + |
| 55 | + #[test] |
| 56 | + fn ensure_broken_fail() { |
| 57 | + let dir = Path::new("tests/broken/"); |
| 58 | + |
| 59 | + assert!(dir.exists(), "broken directory missing"); |
| 60 | + |
| 61 | + let entries = fs::read_dir(dir).expect("Failed to read broken directory"); |
| 62 | + |
| 63 | + let mut files = Vec::new(); |
| 64 | + for entry in entries { |
| 65 | + let entry = entry.expect("Failed to read directory entry"); |
| 66 | + let path = entry.path(); |
| 67 | + |
| 68 | + if path |
| 69 | + .extension() |
| 70 | + .and_then(|s| s.to_str()) |
| 71 | + == Some("tq") |
| 72 | + { |
| 73 | + files.push(path); |
| 74 | + } |
| 75 | + } |
| 76 | + |
| 77 | + assert!(!files.is_empty(), "No .tq files found in broken directory"); |
| 78 | + |
| 79 | + let mut unexpected_successes = Vec::new(); |
| 80 | + |
| 81 | + for file in &files { |
| 82 | + let content = parsing::load(&file) |
| 83 | + .unwrap_or_else(|e| panic!("Failed to load file {:?}: {:?}", file, e)); |
| 84 | + |
| 85 | + match parsing::parse(&file, &content) { |
| 86 | + Ok(_) => { |
| 87 | + println!("File {:?} unexpectedly parsed successfully", file); |
| 88 | + unexpected_successes.push(file.clone()); |
| 89 | + } |
| 90 | + Err(_) => {} |
| 91 | + } |
| 92 | + } |
| 93 | + |
| 94 | + if !unexpected_successes.is_empty() { |
| 95 | + panic!( |
| 96 | + "Broken files should not to parse successfully, but {} files passed", |
| 97 | + unexpected_successes.len() |
| 98 | + ); |
| 99 | + } |
| 100 | + } |
| 101 | +} |
0 commit comments