#[cfg(test)]
pub fn type_of<T>(_: &T) -> &'static str {
std::any::type_name::<T>()
}
pub fn string_difference(a: &str, b: &str) -> Option<(usize, (char, char))> {
a.chars()
.zip(b.chars())
.enumerate()
.find(|(_, (a, b))| a != b)
.or_else(|| match a.len().cmp(&b.len()) {
std::cmp::Ordering::Less => {
Some((a.len(), (' ', b[a.len()..].chars().next().unwrap())))
}
std::cmp::Ordering::Greater => {
Some((b.len(), (' ', a[b.len()..].chars().next().unwrap())))
}
std::cmp::Ordering::Equal => None,
})
}
#[macro_export]
macro_rules! output_cmp {
($path:expr, $out_str:expr) => {{
use {
std::{fs, path::PathBuf},
$crate::utils::string_difference,
};
let t_path: PathBuf = [env!("CARGO_MANIFEST_DIR"), $path].iter().collect();
if t_path.exists() {
let content: String = fs::read_to_string(&t_path)
.unwrap_or_else(|err| panic!("Cannot load output file {:?}: {}", t_path, err));
if let Some(diff) = string_difference(&content, &$out_str) {
assert!(false, "Strings differ at: {:?}", diff)
}
} else {
fs::write(&t_path, $out_str)
.unwrap_or_else(|err| panic!("Error writing file {:?}: {}", t_path, err));
}
}};
}
pub use output_cmp;
#[macro_export]
macro_rules! local_file {
($this:expr, $local_path:expr) => {
&std::path::PathBuf::from(std::env::var("CARGO_WORKSPACE_DIR").unwrap_or(".".to_string()))
.join($this)
.with_file_name($local_path)
};
}
pub use local_file;