rust-lang / rust-lang/rfcs

Attribute for "swapping" types in tests, making dependency injecting easier

Open
#714 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

T-lang T-libs
Dominant language
Markdown
Stars
6.6k
Forks
1.7k
Avg merge
16h 14m
Merged PRs (30d)
1

Description

Issue by Valloric
Wednesday Feb 26, 2014 at 21:20 GMT

For earlier discussion, see https://github.com/rust-lang/rust/issues/12578

This issue was labelled with: in the Rust repository


In other languages like C++, Java, C# etc dependency injection can be a big pain; you want your class Foo to use class Bar in production, but class MockBar in tests. This is usually enabled by explicit dependency injection support in class Foo (through ctor/setter/interface injection) and the code that uses Foos. It's a fairly big hassle and requires a ton of boilerplate code.

I'd like to see Rust provide compiler-level support for swapping types when cfg(test). Something like this:

#[cfg(test)]
mod testmod {
  struct MockBar {
  ...
  }

  #[test]
  #[swap(Bar, MockBar)]
  fn testfunc() { 
    // Foo::new() now creates MockBars instead of Bars internally
    let foo = Foo::new();
    assert_eq!(foo.frobulate(), 5);
  }
}

The idea is to swap any uses of Bar with MockBar in all the code that's executed through the annotated test function. The beauty lies in not having to mangle the production code just so we can isolate only the parts we want to test.

I'm not sure how difficult it would be to implement this and I don't have this idea fully fleshed out, but the gist should be apparent. It doesn't even have to be an attribute; frankly I don't care how this is implemented as long as I can replace one type with another type that implements the same interface[s] for the purpose of test code and that I don't have to change the production code to do it.

Any such support should of course only work when cfg(test). Enabling this in production code would IMO be a bad idea.

EDIT: Forgot to mention, being able to swap functions in this way and not just types would make this feature even more awesome.

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

No implementation files or tests are named. Start with the earlier discussion in rust-lang/rust issue 12578, then clarify the semantics and compiler scope for swapping types or functions under cfg(test). Done would require a settled, implementable proposal rather than the current high-level idea.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
compilers, testing
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
15/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.