Giter Site home page Giter Site logo

Comments (3)

asomers avatar asomers commented on August 22, 2024

No, you can't do that. Module declarations don't get expanded when proc macros get evaluated, so as your code is written automock can't see the contents of some_module at all. It thinks its an empty module. Instead, try like this:

main.rs:

#![feature(proc_macro_hygiene)]
#[cfg(test)]
use mockall::predicate::eq;

pub mod some_module;
use some_module::free_function;

fn my_func() {
    free_function(7);
}

fn main() {
}

#[test]
fn some_test() {
    let context = some_module::free_function_context();
    context.expect().times(1).with(eq(7)).returning(|_x| ());
    my_func();
}

some_module.rs:

use cfg_if::cfg_if;
#[cfg(test)]
use mockall::automock;

#[cfg_attr(test, automock)]
pub mod inner {
    pub fn free_function(_arg: i32) {}
}

cfg_if! {
    if #[cfg(test)] {
        pub use mock_inner::*;
    } else {
        pub use inner::*;
    }
}

I'll see if I can add a compiler error for using automock that way.

from mockall.

lromeo avatar lromeo commented on August 22, 2024

Great, thank you. That works as is, but extending a little further I'm stuck on the following scoping issue. I can open as a separate issue if that would be better.

some_module.rs:

use cfg_if::cfg_if;
#[cfg(test)]
use mockall::automock;

pub struct SomeStruct {}

#[cfg_attr(test, automock)]
pub mod inner {
    use super::SomeStruct;
    pub fn free_function(_arg: SomeStruct) {}
}

cfg_if! {
    if #[cfg(test)] {
        pub use mock_inner::*;
    } else {
        pub use inner::*;
    }
}

main.rs:

#![feature(proc_macro_hygiene)]
#[cfg(test)]
use mockall::predicate::*;

pub mod some_module;
use some_module::*;

fn my_func() {
    let s = some_module::SomeStruct {};
    some_module::free_function(s);
}

fn main() {
}

#[test]
fn some_test() {
    let context = some_module::free_function_context();
    context.expect().times(1).with(always()).returning(|_x| ());
    my_func();
}

gives:

error[E0412]: cannot find type `SomeStruct` in this scope
  --> src/some_module.rs:10:32
   |
10 |     pub fn free_function(_arg: SomeStruct) {}
   |                                ^^^^^^^^^^ not found in this scope

...

It works if some_module.rs is updated to only uses absolute paths:

#[cfg_attr(test, automock)]
pub mod inner {
    pub fn free_function(_arg: crate::some_module::SomeStruct) {}
}

from mockall.

asomers avatar asomers commented on August 22, 2024

Yes, that's different. Please open a separate issue for it, and I'll suggest some other workarounds.

from mockall.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.