fixture_derive

Crates.iofixture_derive
lib.rsfixture_derive
version0.0.1
created_at2025-05-04 19:33:45.03926+00
updated_at2025-05-04 19:33:45.03926+00
descriptionFixture derive macro for Type Driven Development
homepagehttps://github.com/ProbablyClem/fixture_rs
repositoryhttps://github.com/ProbablyClem/fixture_rs
max_upload_size
id1659882
size5,501
Clément Guiton (ProbablyClem)

documentation

README

fixture_rs

Simple Derive Macro to generate fixtures

Usage

You need to define a simple fixture trait in your repository

pub trait Fixture {
    fn fixture() -> Self;
}

And then you can implement it for primitive types, or custom structs

impl Fixture for String {
    fn fixture() -> Self {
        "string".to_string()
    }
}

impl Fixture for u32 {
    fn fixture() -> Self {
        1
    }
}

impl<T: Fixture> Fixture for Option<T> {
    fn fixture() -> Self {
        Some(T::fixture())
    }
}

impl<T: Fixture> Fixture for Vec<T> {
    fn fixture() -> Self {
        vec![T::fixture()]
    }
}

impl<T: Fixture> Fixture for Box<T> {
    fn fixture() -> Self {
        Box::new(T::fixture())
    }
}
```
Thanks to this crate, it can be automatically derived

```rust
#[derive(Fixture)]
pub struct User {
    pub name: String,
    pub age: u32,
    pub bio: Option<String>,
}

#[derive(Fixture)]
pub struct Group {
    pub users: Vec<User>,
}
```

You can then call fixture() to use it in your tests

```rust
    #[test]
    fn test_user_fixture() {
        let user = User::fixture();
        assert_eq!(user.name, "string".to_string());
        assert_eq!(user.age, 1);
        assert_eq!(user.bio, Some("string".to_string()));
    }

    #[test]
    fn test_group_fixture() {
        let group = Group::fixture();
        assert_eq!(group.users.len(), 1);
        let user = &group.users[0];
        assert_eq!(user.name, "string".to_string());
    }

```
Commit count: 14

cargo fmt