#![cfg(any(feature = "testing", test))]
pub mod parse_test_file;
use std::fs;
use std::path::Path;
use std::str::FromStr;
use std::sync::{Mutex, MutexGuard};
use cairo_lang_utils::ordered_hash_map::OrderedHashMap;
pub use parse_test_file::parse_test_file;
fn get_expected_contents(path: &Path) -> String {
fs::read_to_string(path).unwrap_or_else(|_| panic!("Could not read file: '{path:?}'"))
}
fn set_contents(path: &Path, content: String) {
fs::write(path, content).unwrap_or_else(|_| panic!("Could not write file: '{path:?}'"));
}
pub fn compare_contents_or_fix_with_path(path: &Path, content: String) {
let is_fix_mode = std::env::var("CAIRO_FIX_TESTS") == Ok("1".into());
if is_fix_mode {
set_contents(path, content);
} else {
pretty_assertions::assert_eq!(content, get_expected_contents(path));
}
}
pub fn test_lock<'a, T: ?Sized + 'a>(m: &'a Mutex<T>) -> MutexGuard<'a, T> {
match m.lock() {
Ok(guard) => guard,
Err(poisoned) => poisoned.into_inner(),
}
}
pub fn verify_diagnostics_expectation(
args: &OrderedHashMap<String, String>,
diagnostics: &str,
) -> Option<String> {
let Some(expect_diagnostics) = args.get("expect_diagnostics") else {
return None;
};
if expect_diagnostics.trim() == "*" {
return None;
}
let expect_diagnostics = bool_input(expect_diagnostics);
let has_diagnostics = !diagnostics.is_empty();
if !expect_diagnostics && has_diagnostics {
Some(format!(
"`expect_diagnostics` is false, but diagnostics were generated:\n{}",
diagnostics
))
} else if expect_diagnostics && !has_diagnostics {
Some("`expect_diagnostics` is true, but no diagnostics were generated.\n".to_string())
} else {
None
}
}
pub fn bool_input(input: &str) -> bool {
let input = input.trim().to_lowercase();
bool::from_str(&input).unwrap_or_else(|_| panic!("Expected 'true' or 'false', actual: {input}"))
}
pub fn get_direct_or_file_content(input: &str) -> (String, String) {
if let Some(path) = input.strip_prefix(">>> file: ") {
(
path.to_string(),
fs::read_to_string(path).unwrap_or_else(|_| panic!("Could not read file: '{path}'")),
)
} else {
("dummy_file.cairo".to_string(), input.to_string())
}
}