Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for derive_more::Constructor #58

Open
FalkWoldmann opened this issue Feb 5, 2024 · 1 comment
Open

Add support for derive_more::Constructor #58

FalkWoldmann opened this issue Feb 5, 2024 · 1 comment

Comments

@FalkWoldmann
Copy link

FalkWoldmann commented Feb 5, 2024

Hi, using both derive_more::Constructor and faux on the same struct like in following example will lead to an error when trying to compile the tests.

use crate::bar::Bar;
use derive_more::Constructor;

fn main() {
    let bar = Bar::new("abc".to_string());

    let foo = Foo::new(bar);
}

#[cfg_attr(test, faux::create)]
#[derive(Constructor)]
struct Foo {
    bar: Bar,
}

mod bar {

    #[cfg_attr(test, faux::create)]
    pub(crate) struct Bar {
        pub baz: String,
    }

    #[cfg_attr(test, faux::methods)]
    impl Bar {
        fn get_baz(&self) -> &str {
            &self.baz
        }
        pub fn new(string: String) -> Self {
            Self { baz: string }
        }
    }
}

mod test {
    use crate::bar::Bar;
    use crate::Foo;

    #[test]
    fn should_mock() {
        let bar = Bar::faux();
        let foo = Foo::new(bar);
        assert!(true)
    }
}

Writing the new() function by hand works fine:

use crate::bar::Bar;
use derive_more::Constructor;

fn main() {
    let bar = Bar::new("abc".to_string());

    let foo = Foo::new(bar);
}

#[cfg_attr(test, faux::create)]
// #[derive(Constructor)]
struct Foo {
    bar: Bar,
}

#[cfg_attr(test, faux::methods)]
impl Foo {
    fn new(bar: Bar) -> Self {
        Self { bar }
    }
}

mod bar {

    #[cfg_attr(test, faux::create)]
    pub(crate) struct Bar {
        pub baz: String,
    }

    #[cfg_attr(test, faux::methods)]
    impl Bar {
        fn get_baz(&self) -> &str {
            &self.baz
        }
        pub fn new(string: String) -> Self {
            Self { baz: string }
        }
    }
}

mod test {
    use crate::bar::Bar;
    use crate::Foo;

    #[test]
    fn should_mock() {
        let bar = Bar::faux();
        let foo = Foo::new(bar);
        assert!(true)
    }
}

Do you think it would be possible to add support for #[derive(Constructor)]?

@nrxus
Copy link
Owner

nrxus commented Feb 7, 2024

Looking at how derive_more works I think this addition makes most sense within that crate. I am not sure if you'd be willing to make an issue or PR for them but that would allow for a more generic fix. In particular, the ask wouldn't be for them to support faux but for them to support adding an arbitrary attribute on top of their impl.

I envision something like:

#[cfg_attr(test, faux::create)]
#[derive(Constructor)]
// when in test, make the `impl` block generated by `derive_more::Constructor` forward a custom attribute
#[cfg_attr(test, constructor(forward = "faux::methods"))]
struct Foo {
    bar: Bar,
}

That way neither derive_more nor faux have to directly know about each other while still co-existing.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants