diff --git a/Makefile b/Makefile index 724f9cd..b20be8c 100644 --- a/Makefile +++ b/Makefile @@ -24,12 +24,16 @@ clean: .PHONY: test test: -> cargo test --lib +> cargo test --lib --test test_loader .PHONY: integrationtest integrationtest: > cargo test --no-fail-fast --test test_loader +.PHONY: unittest +unittest: +> cargo test --lib + .PHONY: run run: > cargo run diff --git a/src/compare/diff.rs b/src/compare/diff.rs index 99287dc..186898e 100644 --- a/src/compare/diff.rs +++ b/src/compare/diff.rs @@ -1,4 +1,5 @@ use super::sexp::Token; +use super::util::assert_bounds; use super::util::assert_name; use crate::compare::util::get_offsets; use crate::parser::Comment; @@ -125,21 +126,7 @@ fn compare_section<'s>( let mut child_status = Vec::new(); let mut this_status = DiffStatus::Good; - let attributes_child = children - .iter() - .nth(1) - .ok_or("Should have an attributes child.")?; - let attributes_map = attributes_child.as_map()?; - let begin = attributes_map - .get(":begin") - .ok_or("Missing :begin attribute.")? - .as_atom()?; - let end = attributes_map - .get(":end") - .ok_or("Missing :end attribute.")? - .as_atom()?; - let (rust_begin, rust_end) = get_offsets(source, rust); - if (rust_begin + 1).to_string() != begin || (rust_end + 1).to_string() != end { + if assert_bounds(source, emacs, rust).is_err() { this_status = DiffStatus::Bad; } @@ -165,21 +152,7 @@ fn compare_heading<'s>( let mut child_status = Vec::new(); let mut this_status = DiffStatus::Good; - let attributes_child = children - .iter() - .nth(1) - .ok_or("Should have an attributes child.")?; - let attributes_map = attributes_child.as_map()?; - let begin = attributes_map - .get(":begin") - .ok_or("Missing :begin attribute.")? - .as_atom()?; - let end = attributes_map - .get(":end") - .ok_or("Missing :end attribute.")? - .as_atom()?; - let (rust_begin, rust_end) = get_offsets(source, rust); - if (rust_begin + 1).to_string() != begin || (rust_end + 1).to_string() != end { + if assert_bounds(source, emacs, rust).is_err() { this_status = DiffStatus::Bad; } @@ -228,21 +201,7 @@ fn compare_paragraph<'s>( let mut child_status = Vec::new(); let mut this_status = DiffStatus::Good; - let attributes_child = children - .iter() - .nth(1) - .ok_or("Should have an attributes child.")?; - let attributes_map = attributes_child.as_map()?; - let begin = attributes_map - .get(":begin") - .ok_or("Missing :begin attribute.")? - .as_atom()?; - let end = attributes_map - .get(":end") - .ok_or("Missing :end attribute.")? - .as_atom()?; - let (rust_begin, rust_end) = get_offsets(source, rust); - if (rust_begin + 1).to_string() != begin || (rust_end + 1).to_string() != end { + if assert_bounds(source, emacs, rust).is_err() { this_status = DiffStatus::Bad; } @@ -266,21 +225,7 @@ fn compare_plain_list<'s>( let mut child_status = Vec::new(); let mut this_status = DiffStatus::Good; - let attributes_child = children - .iter() - .nth(1) - .ok_or("Should have an attributes child.")?; - let attributes_map = attributes_child.as_map()?; - let begin = attributes_map - .get(":begin") - .ok_or("Missing :begin attribute.")? - .as_atom()?; - let end = attributes_map - .get(":end") - .ok_or("Missing :end attribute.")? - .as_atom()?; - let (rust_begin, rust_end) = get_offsets(source, rust); - if (rust_begin + 1).to_string() != begin || (rust_end + 1).to_string() != end { + if assert_bounds(source, emacs, rust).is_err() { this_status = DiffStatus::Bad; } @@ -306,21 +251,7 @@ fn compare_plain_list_item<'s>( let mut child_status = Vec::new(); let mut this_status = DiffStatus::Good; - let attributes_child = children - .iter() - .nth(1) - .ok_or("Should have an attributes child.")?; - let attributes_map = attributes_child.as_map()?; - let begin = attributes_map - .get(":begin") - .ok_or("Missing :begin attribute.")? - .as_atom()?; - let end = attributes_map - .get(":end") - .ok_or("Missing :end attribute.")? - .as_atom()?; - let (rust_begin, rust_end) = get_offsets(source, rust); - if (rust_begin + 1).to_string() != begin || (rust_end + 1).to_string() != end { + if assert_bounds(source, emacs, rust).is_err() { this_status = DiffStatus::Bad; } @@ -353,21 +284,7 @@ fn compare_greater_block<'s>( let mut child_status = Vec::new(); let mut this_status = DiffStatus::Good; - let attributes_child = children - .iter() - .nth(1) - .ok_or("Should have an attributes child.")?; - let attributes_map = attributes_child.as_map()?; - let begin = attributes_map - .get(":begin") - .ok_or("Missing :begin attribute.")? - .as_atom()?; - let end = attributes_map - .get(":end") - .ok_or("Missing :end attribute.")? - .as_atom()?; - let (rust_begin, rust_end) = get_offsets(source, rust); - if (rust_begin + 1).to_string() != begin || (rust_end + 1).to_string() != end { + if assert_bounds(source, emacs, rust).is_err() { this_status = DiffStatus::Bad; } @@ -393,21 +310,7 @@ fn compare_dynamic_block<'s>( let mut child_status = Vec::new(); let mut this_status = DiffStatus::Good; - let attributes_child = children - .iter() - .nth(1) - .ok_or("Should have an attributes child.")?; - let attributes_map = attributes_child.as_map()?; - let begin = attributes_map - .get(":begin") - .ok_or("Missing :begin attribute.")? - .as_atom()?; - let end = attributes_map - .get(":end") - .ok_or("Missing :end attribute.")? - .as_atom()?; - let (rust_begin, rust_end) = get_offsets(source, rust); - if (rust_begin + 1).to_string() != begin || (rust_end + 1).to_string() != end { + if assert_bounds(source, emacs, rust).is_err() { this_status = DiffStatus::Bad; } @@ -433,21 +336,7 @@ fn compare_footnote_definition<'s>( let mut child_status = Vec::new(); let mut this_status = DiffStatus::Good; - let attributes_child = children - .iter() - .nth(1) - .ok_or("Should have an attributes child.")?; - let attributes_map = attributes_child.as_map()?; - let begin = attributes_map - .get(":begin") - .ok_or("Missing :begin attribute.")? - .as_atom()?; - let end = attributes_map - .get(":end") - .ok_or("Missing :end attribute.")? - .as_atom()?; - let (rust_begin, rust_end) = get_offsets(source, rust); - if (rust_begin + 1).to_string() != begin || (rust_end + 1).to_string() != end { + if assert_bounds(source, emacs, rust).is_err() { this_status = DiffStatus::Bad; } @@ -473,21 +362,7 @@ fn compare_comment<'s>( let mut child_status = Vec::new(); let mut this_status = DiffStatus::Good; - let attributes_child = children - .iter() - .nth(1) - .ok_or("Should have an attributes child.")?; - let attributes_map = attributes_child.as_map()?; - let begin = attributes_map - .get(":begin") - .ok_or("Missing :begin attribute.")? - .as_atom()?; - let end = attributes_map - .get(":end") - .ok_or("Missing :end attribute.")? - .as_atom()?; - let (rust_begin, rust_end) = get_offsets(source, rust); - if (rust_begin + 1).to_string() != begin || (rust_end + 1).to_string() != end { + if assert_bounds(source, emacs, rust).is_err() { this_status = DiffStatus::Bad; } @@ -509,21 +384,7 @@ fn compare_drawer<'s>( let mut child_status = Vec::new(); let mut this_status = DiffStatus::Good; - let attributes_child = children - .iter() - .nth(1) - .ok_or("Should have an attributes child.")?; - let attributes_map = attributes_child.as_map()?; - let begin = attributes_map - .get(":begin") - .ok_or("Missing :begin attribute.")? - .as_atom()?; - let end = attributes_map - .get(":end") - .ok_or("Missing :end attribute.")? - .as_atom()?; - let (rust_begin, rust_end) = get_offsets(source, rust); - if (rust_begin + 1).to_string() != begin || (rust_end + 1).to_string() != end { + if assert_bounds(source, emacs, rust).is_err() { this_status = DiffStatus::Bad; } diff --git a/src/compare/util.rs b/src/compare/util.rs index 2b665e5..830556a 100644 --- a/src/compare/util.rs +++ b/src/compare/util.rs @@ -37,3 +37,30 @@ pub fn assert_name<'s>(emacs: &'s Token<'s>, name: &str) -> Result<(), Box>( + source: &'s str, + emacs: &'s Token<'s>, + rust: &'s S, +) -> Result<(), Box> { + let children = emacs.as_list()?; + let attributes_child = children + .iter() + .nth(1) + .ok_or("Should have an attributes child.")?; + let attributes_map = attributes_child.as_map()?; + let begin = attributes_map + .get(":begin") + .ok_or("Missing :begin attribute.")? + .as_atom()?; + let end = attributes_map + .get(":end") + .ok_or("Missing :end attribute.")? + .as_atom()?; + let (rust_begin, rust_end) = get_offsets(source, rust); + if (rust_begin + 1).to_string() != begin || (rust_end + 1).to_string() != end { + Err(format!("Rust bounds ({rust_begin}, {rust_end}) do not match emacs bounds ({emacs_begin}, {emacs_end})", rust_begin = rust_begin + 1, rust_end = rust_end + 1, emacs_begin=begin, emacs_end=end))?; + } + + Ok(()) +}