#![allow(unused_attributes)]
#![allow(unused_imports)]
#![allow(non_snake_case)]
use disable_macro::disable;
use traced_test::*;
use tracing_setup::*;
use tracing::info;
use std::time::Duration;
use tokio::time::sleep;
mod good {
use super::*;
#[traced_test(
should_fail(message = "Async test failed")
)]
async fn async_test_with_result_failure()
-> Result<(), String>
{
info!("we should not see this log message because the test is expected to fail with the provided message");
Err("Async test failed".to_string())
}
#[traced_test(
should_fail(message = "Test failed")
)]
fn sync_test_with_result_failure() -> Result<(), String> {
info!("we should not see this log message because the test is expected to fail with the provided message");
Err("Test failed".to_string())
}
#[traced_test]
fn sync_test_succeeds() {
info!("we should not see this log message because the test passes. we don't need to see logs when a test passes");
assert!(true, "This test should pass.");
}
#[traced_test]
fn check_fundamental_behavior() -> Result<(),String> {
debug!("we don't need to see logs when a test passes");
assert!(true, "This test should pass.");
Ok(())
}
#[traced_test(
should_fail(message = "This test should fail.")
)]
fn sync_test_fails() {
info!("we should not see this log message because the test is expected to fail with the provided message");
assert!(false, "This test should fail.");
}
#[traced_test]
fn sync_test_with_result_success() -> Result<(), String> {
info!("we should not see this log message because the test passes");
assert!(true, "This test should pass.");
Ok(())
}
#[traced_test]
async fn async_test_succeeds() {
info!("we should not see this log message because the test passes");
assert!(true, "Async test should pass.");
}
#[traced_test(
should_fail(message = "This test should fail.")
)]
async fn async_test_fails() {
info!("we should not see this log message because the test is expected to fail with the provided message");
assert!(false, "This test should fail.");
}
#[traced_test]
async fn async_test_with_result_success() -> Result<(), String> {
info!("we should not see this log message because the test passes");
assert!(true, "Async test should pass.");
Ok(())
}
#[traced_test]
fn sync_test_tracing() {
info!("we should not see this log message because th test passes");
assert!(true, "Tracing should be captured.");
}
#[traced_test]
async fn async_test_tracing() {
info!("we should not see this log message because the test passes");
assert!(true, "Async tracing should be captured.");
}
#[traced_test(
should_fail(message = "sync test failure")
)]
fn sync_test_failure_but_proper_logging() {
info!("we should not see this log message because we expect the test to fail with the provided message");
assert!(false, "sync test failure");
}
#[traced_test(
should_fail(message = "async test failure")
)]
async fn async_test_failure_but_proper_logging() {
info!("we should not see this log message because the test fails in the way that we expect");
assert!(false, "async test failure");
}
async fn real_async_function_success() -> Result<i32, String> {
sleep(Duration::from_millis(50)).await; Ok(42)
}
async fn real_async_function_failure() -> Result<i32, String> {
sleep(Duration::from_millis(50)).await; Err("Failed async function".to_string())
}
#[traced_test]
async fn async_test_real_async_function_success() -> Result<(), String> {
info!("we should not see this log message because the test passes");
let result = real_async_function_success().await?;
assert_eq!(result, 42, "Expected 42 from real_async_function_success");
Ok(())
}
#[traced_test]
async fn async_test_real_async_function_failure() -> Result<(), String> {
info!("we should not see this log message because overall, the test passes");
let result = real_async_function_failure().await;
assert!(result.is_err(), "Expected an error from real_async_function_failure");
Ok(())
}
#[traced_test]
async fn async_test_with_delay() -> Result<(), String> {
info!("Test started, waiting for async operation... (we should not see this log message, ultimately because the overall test passes)");
sleep(Duration::from_millis(100)).await; info!("Async operation completed... (we should not see this log message, ultimately because the overall test passes)");
assert!(true, "Async test should pass after delay.");
Ok(())
}
#[traced_test]
fn test_sync_pass_no_should_fail() {
info!("This log should not be displayed because the test passes.");
assert!(true);
}
#[traced_test(
should_fail(message = "Expected failure")
)]
fn test_sync_fail_with_matching_should_fail() {
info!("This log should not be displayed because the test fails as expected.");
panic!("Expected failure");
}
#[traced_test]
fn test_no_trace_on_success() {
info!("This log should not be displayed unless the test fails.");
assert!(true);
}
}
#[disable]
mod expect_failure {
use super::*;
#[traced_test]
fn EXPECT_FAILURE_sync_test_failure_assertion_should_trigger_logging() {
info!("EXPECT_FAILURE_sync_test_failure_assertion_should_trigger_logging -- This log should be displayed because the test fails unexpectedly.");
assert!(false, "This test should fail.");
}
#[traced_test]
fn EXPECT_FAILURE_sync_test_failure_assertion_must_trigger_logging() {
info!("EXPECT_FAILURE_sync_test_failure_assertion_must_trigger_logging -- we must see this log message because the test fails with a false assertion");
assert!(false, "This test should fail.");
}
#[traced_test]
async fn EXPECT_FAILURE_async_test_uncaught_failure() {
info!("EXPECT_FAILURE_async_test_uncaught_failure -- we should see this log message because the test fails");
assert!(false, "This test should fail.");
}
#[traced_test(
should_fail(message = "This test should fail.")
)]
async fn EXPECT_FAILURE_async_test_failure_with_unexpected_error_message() {
info!("EXPECT_FAILURE_async_test_failure_with_unexpected_error_message -- we should see this log message because the test fails with a message we did not expect");
assert!(false, "unexpected error message");
}
#[traced_test]
fn EXPECT_FAILURE_sync_test_failure_we_need_to_see_the_tracing() {
info!("EXPECT_FAILURE_sync_test_failure_we_need_to_see_the_tracing -- we must see this log message because the test fails unexpectedly");
assert!(false, "sync test failure");
}
#[traced_test]
async fn EXPECT_FAILURE_async_test_failure_we_need_to_see_the_tracing() {
info!("EXPECT_FAILURE_async_test_failure_we_need_to_see_the_tracing -- we must see this log message because the test fails unexpectedly");
assert!(false, "async test failure");
}
#[traced_test]
async fn EXPECT_FAILURE_async_test_with_delayed_panic() {
info!("EXPECT_FAILURE_async_test_with_delayed_panic -- Test started, waiting for async operation... (we should see this log message because an unexpected panic occurs)");
sleep(Duration::from_millis(50)).await; info!("EXPECT_FAILURE_async_test_with_delayed_panic -- Async operation completed... (we should see this log message because an unexpected panic occurs)");
panic!("Delayed panic occurred.");
}
#[traced_test]
fn EXPECT_FAILURE_test_sync_fail_no_should_fail() {
info!("EXPECT_FAILURE_test_sync_fail_no_should_fail -- This log should be displayed because the test fails unexpectedly.");
assert!(false, "Unexpected failure");
}
#[traced_test(
should_fail(message = "Expected failure")
)]
fn EXPECT_FAILURE_test_sync_pass_with_should_fail() {
info!("EXPECT_FAILURE_test_sync_pass_with_should_fail -- This log should be displayed because the test passes unexpectedly.");
assert!(true);
}
#[traced_test(
should_fail(message = "Expected failure")
)]
fn EXPECT_FAILURE_test_sync_fail_with_non_matching_should_fail() {
info!("EXPECT_FAILURE_test_sync_fail_with_non_matching_should_fail -- This log should be displayed because the test fails with an unexpected message.");
panic!("Different failure message");
}
}