aboutsummaryrefslogtreecommitdiff
path: root/tests
diff options
context:
space:
mode:
authorOnur Gündüz <ogunduz@gmail.com>2026-04-03 06:34:47 +0200
committerOnur Gündüz <ogunduz@gmail.com>2026-04-03 06:34:47 +0200
commit38b60098e42b4a899eb574f65fa14d4fdbe82f9b (patch)
tree3956fe1ab8107c5feb043fd466a969da8f5b1f9a /tests
parentd20acec5efcad4b3cd493f914d10e3d11cdfee4d (diff)
downloadele-38b60098e42b4a899eb574f65fa14d4fdbe82f9b.tar.gz
ele-38b60098e42b4a899eb574f65fa14d4fdbe82f9b.zip
Init
Diffstat (limited to 'tests')
-rw-r--r--tests/parse_tests.rs153
1 files changed, 153 insertions, 0 deletions
diff --git a/tests/parse_tests.rs b/tests/parse_tests.rs
new file mode 100644
index 0000000..1f15bd2
--- /dev/null
+++ b/tests/parse_tests.rs
@@ -0,0 +1,153 @@
+use aip_filter::parse;
+use std::fs;
+use std::path::Path;
+
+/// Parse a txtar-style test file into named sections.
+/// Sections are delimited by `-- name --` lines.
+fn parse_txtar(content: &str) -> Vec<(String, String)> {
+ let mut files: Vec<(String, String)> = Vec::new();
+ let mut current_name: Option<String> = None;
+ let mut current_data = String::new();
+
+ for line in content.split('\n') {
+ let trimmed = line.trim_end_matches('\r');
+ if trimmed.starts_with("-- ") && trimmed.ends_with(" --") {
+ if let Some(name) = current_name.take() {
+ files.push((name, current_data.clone()));
+ }
+ let name = trimmed[3..trimmed.len() - 3].to_string();
+ current_name = Some(name);
+ current_data.clear();
+ } else if current_name.is_some() {
+ current_data.push_str(line);
+ current_data.push('\n');
+ }
+ }
+ if let Some(name) = current_name {
+ files.push((name, current_data));
+ }
+ files
+}
+
+#[test]
+fn test_parse() {
+ let test_dir = Path::new(env!("CARGO_MANIFEST_DIR")).join("testdata/parse");
+ if !test_dir.exists() {
+ eprintln!("Skipping parse tests: testdata/parse not found");
+ return;
+ }
+
+ let mut total = 0;
+ let mut passed = 0;
+ let mut failed_tests = Vec::new();
+
+ for entry in fs::read_dir(&test_dir).unwrap() {
+ let entry = entry.unwrap();
+ let path = entry.path();
+ if path.extension().map(|e| e == "txt").unwrap_or(false) {
+ let content = fs::read_to_string(&path).unwrap();
+ let files = parse_txtar(&content);
+
+ let mut i = 0;
+ while i < files.len() {
+ let (name, data) = &files[i];
+ if !name.ends_with(".test") {
+ i += 1;
+ continue;
+ }
+
+ let base = name.strip_suffix(".test").unwrap();
+ total += 1;
+
+ let result = parse(data);
+
+ i += 1;
+ if i >= files.len() {
+ eprintln!(" {}: missing expected result", base);
+ continue;
+ }
+
+ let (expected_name, expected_data) = &files[i];
+
+ if expected_name.ends_with(".out") {
+ match result {
+ Err(e) => {
+ failed_tests.push(format!(
+ "{}:{}: got error '{}', expected success",
+ path.file_name().unwrap().to_str().unwrap(),
+ base,
+ e
+ ));
+ }
+ Ok(expr) => {
+ let got = match expr {
+ Some(e) => format!("{}", e),
+ None => String::new(),
+ };
+ let want = expected_data.trim_end_matches('\n');
+ let got_trimmed = got.trim_end_matches('\n');
+ if got_trimmed == want {
+ passed += 1;
+ } else {
+ failed_tests.push(format!(
+ "{}:{}: output mismatch\n got: {:?}\n want: {:?}",
+ path.file_name().unwrap().to_str().unwrap(),
+ base,
+ got_trimmed,
+ want
+ ));
+ }
+ }
+ }
+ } else if expected_name.ends_with(".err") {
+ match result {
+ Ok(_) => {
+ failed_tests.push(format!(
+ "{}:{}: got success, expected error '{}'",
+ path.file_name().unwrap().to_str().unwrap(),
+ base,
+ expected_data.trim()
+ ));
+ }
+ Err(e) => {
+ let want = expected_data.trim();
+ let got = e.trim();
+ if got == want || regex_error_prefix_matches(got, want) {
+ passed += 1;
+ } else {
+ failed_tests.push(format!(
+ "{}:{}: error mismatch\n got: {:?}\n want: {:?}",
+ path.file_name().unwrap().to_str().unwrap(),
+ base,
+ got,
+ want
+ ));
+ }
+ }
+ }
+ }
+ }
+ }
+ }
+
+ println!("\nParse tests: {}/{} passed", passed, total);
+ if !failed_tests.is_empty() {
+ println!("\nFailed tests:");
+ for f in &failed_tests {
+ println!(" {}", f);
+ }
+ panic!("{} of {} parse tests failed", failed_tests.len(), total);
+ }
+}
+
+/// Returns true when the position prefix and "invalid regular expression:" label
+/// match but the trailing error detail does not - the regex crate's error messages
+/// are more verbose than the testdata expects, so we accept any error at the right
+/// position with the right label.
+fn regex_error_prefix_matches(got: &str, want: &str) -> bool {
+ const MARKER: &str = "invalid regular expression:";
+ match (got.split_once(MARKER), want.split_once(MARKER)) {
+ (Some((gp, _)), Some((wp, _))) => gp == wp,
+ _ => false,
+ }
+}