diff --git a/src/wasm_test/compare.rs b/src/wasm_test/compare.rs index 305daac..64dd260 100644 --- a/src/wasm_test/compare.rs +++ b/src/wasm_test/compare.rs @@ -1,23 +1,18 @@ -use std::borrow::Cow; - use super::diff::WasmDiffResult; use super::diff::WasmDiffStatus; use super::elisp_compare::WasmElispCompare; -use crate::compare::get_emacs_standard_properties; -use crate::compare::get_property_quoted_string; +use super::logic::wasm_compare_additional_properties; +use super::logic::wasm_compare_list; +use super::logic::wasm_compare_property_quoted_string; +use super::logic::wasm_compare_standard_properties; use crate::compare::ElispFact; use crate::compare::EmacsField; use crate::compare::Token; -use crate::util::foreground_color; -use crate::util::reset_color; -use crate::wasm::AdditionalProperties; -use crate::wasm::AdditionalPropertyValue; use crate::wasm::WasmAstNode; use crate::wasm::WasmDocument; use crate::wasm::WasmHeadline; use crate::wasm::WasmParagraph; use crate::wasm::WasmSection; -use crate::wasm::WasmStandardProperties; use crate::wasm_test::macros::wasm_compare; pub fn wasm_compare_document<'b, 's, 'p>( @@ -28,44 +23,6 @@ pub fn wasm_compare_document<'b, 's, 'p>( wasm.compare_ast_node(source, emacs) } -fn wasm_compare_list<'b, 's: 'b, 'p, EI, WI, WC>( - source: &'s str, - emacs: EI, - wasm: WI, -) -> Result, Box> -where - EI: Iterator> + ExactSizeIterator, - WI: Iterator + ExactSizeIterator, - WC: WasmElispCompare<'s, 'p>, -{ - let status = Vec::new(); - let emacs_length = emacs.len(); - let wasm_length = wasm.len(); - if emacs_length != wasm_length { - return Ok(WasmDiffResult { - status: vec![WasmDiffStatus::Bad( - format!( - "Child length mismatch (emacs != rust) {:?} != {:?}", - emacs_length, wasm_length - ) - .into(), - )], - children: Vec::new(), - name: "".into(), - }); - } - - let mut child_status = Vec::with_capacity(emacs_length); - for (emacs_child, wasm_child) in emacs.zip(wasm) { - child_status.push(wasm_child.compare_ast_node(source, emacs_child)?); - } - Ok(WasmDiffResult { - status, - children: child_status, - name: "".into(), - }) -} - impl<'s, 'p, WAN: WasmElispCompare<'s, 'p>> WasmElispCompare<'s, 'p> for &WAN { fn compare_ast_node<'b>( &self, @@ -235,136 +192,3 @@ impl<'s, 'p> WasmElispCompare<'s, 'p> for WasmParagraph<'s, 'p> { Ok(result) } } - -fn wasm_compare_property_quoted_string< - 'b, - 's, - W, - WV: AsRef + std::fmt::Debug, - WG: Fn(W) -> Option, ->( - _source: &'s str, - emacs: &'b Token<'s>, - wasm_node: W, - emacs_field: &str, - wasm_value_getter: WG, -) -> Result, Box> { - let mut result = WasmDiffResult::default(); - let emacs_value = get_property_quoted_string(emacs, emacs_field)?; - let wasm_value = wasm_value_getter(wasm_node); - if wasm_value.as_ref().map(|s| s.as_ref()) != emacs_value.as_deref() { - result.status.push(WasmDiffStatus::Bad( - format!( - "Property mismatch. Property=({property}) Emacs=({emacs:?}) Wasm=({wasm:?}).", - property = emacs_field, - emacs = emacs_value, - wasm = wasm_value, - ) - .into(), - )) - } - Ok(result) -} - -fn wasm_compare_standard_properties<'b, 's>( - _source: &'s str, - emacs: &'b Token<'s>, - wasm: &WasmStandardProperties, -) -> Result, Box> { - let mut result = WasmDiffResult::default(); - let mut layer = WasmDiffResult::default(); - layer.name = "standard-properties".into(); - let standard_properties = get_emacs_standard_properties(emacs)?; - - if Some(wasm.begin) != standard_properties.begin { - layer.status.push(WasmDiffStatus::Bad( - format!( - "Property mismatch. Property=({property}) Emacs=({emacs:?}) Wasm=({wasm:?}).", - property = "begin", - emacs = standard_properties.begin, - wasm = Some(wasm.begin), - ) - .into(), - )); - } - if Some(wasm.end) != standard_properties.end { - layer.status.push(WasmDiffStatus::Bad( - format!( - "Property mismatch. Property=({property}) Emacs=({emacs:?}) Wasm=({wasm:?}).", - property = "end", - emacs = standard_properties.end, - wasm = Some(wasm.end), - ) - .into(), - )); - } - if wasm.contents_begin != standard_properties.contents_begin { - layer.status.push(WasmDiffStatus::Bad( - format!( - "Property mismatch. Property=({property}) Emacs=({emacs:?}) Wasm=({wasm:?}).", - property = "contents-begin", - emacs = standard_properties.contents_begin, - wasm = wasm.contents_begin, - ) - .into(), - )); - } - if wasm.contents_end != standard_properties.contents_end { - layer.status.push(WasmDiffStatus::Bad( - format!( - "Property mismatch. Property=({property}) Emacs=({emacs:?}) Wasm=({wasm:?}).", - property = "contents-end", - emacs = standard_properties.contents_end, - wasm = wasm.contents_end, - ) - .into(), - )); - } - if Some(wasm.post_blank).map(|post_blank| post_blank as usize) != standard_properties.post_blank - { - layer.status.push(WasmDiffStatus::Bad( - format!( - "Property mismatch. Property=({property}) Emacs=({emacs:?}) Wasm=({wasm:?}).", - property = "post-blank", - emacs = standard_properties.post_blank, - wasm = Some(wasm.post_blank), - ) - .into(), - )); - } - - result.children.push(layer); - Ok(result) -} - -fn wasm_compare_additional_properties<'b, 's>( - source: &'s str, - emacs: &'b Token<'s>, - wasm: &AdditionalProperties<'_, '_>, -) -> Result, Box> { - let mut result = WasmDiffResult::default(); - let mut layer = WasmDiffResult::default(); - layer.name = "additional-properties".into(); - - for (property_name, property_value) in wasm.properties.iter() { - let emacs_property_name = format!(":{property_name}", property_name = property_name); - match property_value { - AdditionalPropertyValue::SingleString(wasm_value) => { - layer.extend(wasm_compare_property_quoted_string( - source, - emacs, - wasm, - &emacs_property_name, - |_| Some(wasm_value), - )?)?; - } - // TODO: similar to compare_affiliated_keywords - AdditionalPropertyValue::ListOfStrings(_) => todo!(), - AdditionalPropertyValue::OptionalPair { optval, val } => todo!(), - AdditionalPropertyValue::ObjectTree(_) => todo!(), - } - } - - result.children.push(layer); - Ok(result) -} diff --git a/src/wasm_test/logic.rs b/src/wasm_test/logic.rs new file mode 100644 index 0000000..a71eac0 --- /dev/null +++ b/src/wasm_test/logic.rs @@ -0,0 +1,180 @@ +use super::diff::WasmDiffResult; +use super::diff::WasmDiffStatus; +use super::elisp_compare::WasmElispCompare; +use crate::compare::get_emacs_standard_properties; +use crate::compare::get_property_quoted_string; +use crate::compare::Token; +use crate::wasm::AdditionalProperties; +use crate::wasm::AdditionalPropertyValue; +use crate::wasm::WasmStandardProperties; + +pub(crate) fn wasm_compare_list<'b, 's: 'b, 'p, EI, WI, WC>( + source: &'s str, + emacs: EI, + wasm: WI, +) -> Result, Box> +where + EI: Iterator> + ExactSizeIterator, + WI: Iterator + ExactSizeIterator, + WC: WasmElispCompare<'s, 'p>, +{ + let status = Vec::new(); + let emacs_length = emacs.len(); + let wasm_length = wasm.len(); + if emacs_length != wasm_length { + return Ok(WasmDiffResult { + status: vec![WasmDiffStatus::Bad( + format!( + "Child length mismatch (emacs != rust) {:?} != {:?}", + emacs_length, wasm_length + ) + .into(), + )], + children: Vec::new(), + name: "".into(), + }); + } + + let mut child_status = Vec::with_capacity(emacs_length); + for (emacs_child, wasm_child) in emacs.zip(wasm) { + child_status.push(wasm_child.compare_ast_node(source, emacs_child)?); + } + Ok(WasmDiffResult { + status, + children: child_status, + name: "".into(), + }) +} + +pub(crate) fn wasm_compare_property_quoted_string< + 'b, + 's, + W, + WV: AsRef + std::fmt::Debug, + WG: Fn(W) -> Option, +>( + _source: &'s str, + emacs: &'b Token<'s>, + wasm_node: W, + emacs_field: &str, + wasm_value_getter: WG, +) -> Result, Box> { + let mut result = WasmDiffResult::default(); + let emacs_value = get_property_quoted_string(emacs, emacs_field)?; + let wasm_value = wasm_value_getter(wasm_node); + if wasm_value.as_ref().map(|s| s.as_ref()) != emacs_value.as_deref() { + result.status.push(WasmDiffStatus::Bad( + format!( + "Property mismatch. Property=({property}) Emacs=({emacs:?}) Wasm=({wasm:?}).", + property = emacs_field, + emacs = emacs_value, + wasm = wasm_value, + ) + .into(), + )) + } + Ok(result) +} + +pub(crate) fn wasm_compare_standard_properties<'b, 's>( + _source: &'s str, + emacs: &'b Token<'s>, + wasm: &WasmStandardProperties, +) -> Result, Box> { + let mut result = WasmDiffResult::default(); + let mut layer = WasmDiffResult::default(); + layer.name = "standard-properties".into(); + let standard_properties = get_emacs_standard_properties(emacs)?; + + if Some(wasm.begin) != standard_properties.begin { + layer.status.push(WasmDiffStatus::Bad( + format!( + "Property mismatch. Property=({property}) Emacs=({emacs:?}) Wasm=({wasm:?}).", + property = "begin", + emacs = standard_properties.begin, + wasm = Some(wasm.begin), + ) + .into(), + )); + } + if Some(wasm.end) != standard_properties.end { + layer.status.push(WasmDiffStatus::Bad( + format!( + "Property mismatch. Property=({property}) Emacs=({emacs:?}) Wasm=({wasm:?}).", + property = "end", + emacs = standard_properties.end, + wasm = Some(wasm.end), + ) + .into(), + )); + } + if wasm.contents_begin != standard_properties.contents_begin { + layer.status.push(WasmDiffStatus::Bad( + format!( + "Property mismatch. Property=({property}) Emacs=({emacs:?}) Wasm=({wasm:?}).", + property = "contents-begin", + emacs = standard_properties.contents_begin, + wasm = wasm.contents_begin, + ) + .into(), + )); + } + if wasm.contents_end != standard_properties.contents_end { + layer.status.push(WasmDiffStatus::Bad( + format!( + "Property mismatch. Property=({property}) Emacs=({emacs:?}) Wasm=({wasm:?}).", + property = "contents-end", + emacs = standard_properties.contents_end, + wasm = wasm.contents_end, + ) + .into(), + )); + } + if Some(wasm.post_blank).map(|post_blank| post_blank as usize) != standard_properties.post_blank + { + layer.status.push(WasmDiffStatus::Bad( + format!( + "Property mismatch. Property=({property}) Emacs=({emacs:?}) Wasm=({wasm:?}).", + property = "post-blank", + emacs = standard_properties.post_blank, + wasm = Some(wasm.post_blank), + ) + .into(), + )); + } + + result.children.push(layer); + Ok(result) +} + +pub(crate) fn wasm_compare_additional_properties<'b, 's>( + source: &'s str, + emacs: &'b Token<'s>, + wasm: &AdditionalProperties<'_, '_>, +) -> Result, Box> { + let mut result = WasmDiffResult::default(); + let mut layer = WasmDiffResult::default(); + layer.name = "additional-properties".into(); + + for (property_name, property_value) in wasm.properties.iter() { + let emacs_property_name = format!(":{property_name}", property_name = property_name); + match property_value { + AdditionalPropertyValue::SingleString(wasm_value) => { + layer.extend(wasm_compare_property_quoted_string( + source, + emacs, + wasm, + &emacs_property_name, + |_| Some(wasm_value), + )?)?; + } + // TODO: similar to compare_affiliated_keywords + AdditionalPropertyValue::ListOfStrings(_) => todo!(), + AdditionalPropertyValue::OptionalPair { optval, val } => todo!(), + AdditionalPropertyValue::ObjectTree(_) => todo!(), + } + } + + result.children.push(layer); + Ok(result) +} diff --git a/src/wasm_test/mod.rs b/src/wasm_test/mod.rs index 84ab05e..7032539 100644 --- a/src/wasm_test/mod.rs +++ b/src/wasm_test/mod.rs @@ -1,6 +1,7 @@ mod compare; mod diff; mod elisp_compare; +mod logic; mod macros; mod runner;