File size: 1,695 Bytes
d90101d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
//! Test utilities and helpers for Forge tests
//!
//! This crate provides common utilities for testing, including fixture loading
//! helpers that reduce boilerplate in test code.

/// Loads a fixture file from the calling crate's directory
///
/// # Arguments
/// * `path` - Path relative to the crate's manifest directory
///
/// # Example
/// ```ignore
/// let content = fixture("src/fixtures/test.json").await;
/// ```
pub async fn fixture(path: &str) -> String {
    tokio::fs::read_to_string(path)
        .await
        .unwrap_or_else(|e| panic!("Failed to load fixture at {path}: {e}"))
}

/// Macro to load a fixture file relative to the calling crate's manifest
/// directory
///
/// # Example
/// ```ignore
/// let content = fixture!("src/fixtures/test.json").await;
/// ```
#[macro_export]
macro_rules! fixture {
    ($path:expr) => {
        $crate::fixture(&format!("{}/{}", env!("CARGO_MANIFEST_DIR"), $path))
    };
}

/// Loads a fixture file and parses it as JSON
///
/// # Example
/// ```ignore
/// let data: MyType = json_fixture("src/fixtures/test.json").await;
/// ```
#[cfg(feature = "json")]
pub async fn json_fixture<T: serde::de::DeserializeOwned>(path: &str) -> T {
    let content = fixture(path).await;
    serde_json::from_str(&content)
        .unwrap_or_else(|e| panic!("Failed to parse JSON fixture at {}: {}", path, e))
}

/// Macro to load and parse a JSON fixture
///
/// # Example
/// ```ignore
/// let data: MyType = json_fixture!("src/fixtures/test.json").await;
/// ```
#[cfg(feature = "json")]
#[macro_export]
macro_rules! json_fixture {
    ($path:expr) => {
        $crate::json_fixture(&format!("{}/{}", env!("CARGO_MANIFEST_DIR"), $path))
    };
}