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

Injecting Return Value to Decorated Function #9

Open
erayerdin opened this issue Mar 4, 2020 · 1 comment
Open

Injecting Return Value to Decorated Function #9

erayerdin opened this issue Mar 4, 2020 · 1 comment

Comments

@erayerdin
Copy link

erayerdin commented Mar 4, 2020

This is kinda like a fixture pattern.

The library is so great. I use it to create a xunit style setup and teardown for my tests. I guess there's, for now, no way to grab the returned value from decorator. Below is what I do:

// assuming we're in a test module
use adorn::*;

#[make_decorator(f)]
fn foo_fixture() {
    fn setup() {
        // create a file
    }

    fn teardown() {
        // remove "the" file
    }

    setup();
    f();
    teardown();
}

#[test]
#[adorn(foo_fixture)]
fn bar() {
    // test some stuff about "the" file
}

What I, kinda, would like to do (if ever possible):

use adorn::*;
use std::path::PathBuf; // ADDED, for instance

#[make_decorator(f)]
fn foo_fixture() -> PathBuf { // CHANGED
    fn setup() -> PathBuf { // CHANGED
        // create a file
    }

    fn teardown(path: &PathBuf) { // CHANGED
        // remove "the" file
    }

    let val = setup(); // CHANGED
    f();
    teardown(&val); // CHANGED
    val // ADDED
}

#[test]
#[adorn(foo_fixture)]
fn bar() {
    // test some stuff about "the" file
    // it would be better if we have accessed PathBuf created by foo_fixture here
    // I don't know how it would look like, so I didn't write any code here
    // the same name, "foo_fixutre", would clash with the function most likely
}

I can see this may require significant changes in the codebase though.

@Manishearth
Copy link
Owner

It's also tricky because you'd want to define your argument types in make_decorator but there's no way to copy that down into bar()

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