Files
khadhroony-solana-project/crates/ksp-core-lib/unit_tests/error.rs
2026-08-14 15:12:38 +02:00

79 lines
2.4 KiB
Rust

// file: crates/ksp-core-lib/unit_tests/error.rs
// version: 1
#[derive(Debug)]
struct TestSource;
impl std::fmt::Display for TestSource {
fn fmt(&self, formatter: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
return formatter.write_str("source failure");
}
}
impl std::error::Error for TestSource {}
fn assert_send_sync<T>()
where
T: std::marker::Send + std::marker::Sync,
{
return;
}
#[test]
fn error_code_preserves_domain_and_code() {
const CODE: crate::ErrorCode = crate::ErrorCode::new("core", "sample_failure");
assert_eq!(CODE.domain(), "core");
assert_eq!(CODE.code(), "sample_failure");
return;
}
#[test]
fn error_context_preserves_key_and_value() {
let context = crate::ErrorContext::new("operation", "sample");
assert_eq!(context.key(), "operation");
assert_eq!(context.value(), "sample");
return;
}
#[test]
fn error_preserves_code_message_and_context_order() {
let code = crate::ErrorCode::new("core", "sample_failure");
let error = crate::Error::new(code, "sample message").with_context("first", "one").with_context("second", "two");
assert_eq!(error.code(), code);
assert_eq!(error.message(), "sample message");
assert_eq!(error.context().len(), 2);
assert_eq!(error.context()[0].key(), "first");
assert_eq!(error.context()[0].value(), "one");
assert_eq!(error.context()[1].key(), "second");
assert_eq!(error.context()[1].value(), "two");
return;
}
#[test]
fn display_contains_only_qualified_code_and_message() {
let error = crate::Error::new(crate::ErrorCode::new("core", "sample_failure"), "sample message")
.with_context("secret_free_context", "not rendered")
.with_source(TestSource);
assert_eq!(std::string::ToString::to_string(&error), "core.sample_failure: sample message");
return;
}
#[test]
fn standard_source_is_preserved() {
let error = crate::Error::new(crate::ErrorCode::new("core", "sample_failure"), "sample message").with_source(TestSource);
let source = std::error::Error::source(&error);
assert!(source.is_some());
let source_message = match source {
std::option::Option::Some(value) => std::string::ToString::to_string(value),
std::option::Option::None => std::string::String::new(),
};
assert_eq!(source_message, "source failure");
return;
}
#[test]
fn common_error_is_send_and_sync() {
assert_send_sync::<crate::Error>();
return;
}