frondeus / test-case

Rust procedural macro attribute for adding test cases easily
MIT License
614 stars 39 forks source link

Is the README correct? #27

Closed drwilco closed 4 years ago

drwilco commented 4 years ago

The README has the following example for the base usage:

#![cfg(test)]
extern crate test_case;

use test_case::test_case;

#[test_case( 2,  4 ; "when both operands are possitive")]
#[test_case( 4,  2 ; "when operands are swapped")]
#[test_case(-2, -4 ; "when both operands are negative")]
fn multiplication_tests(x: i8, y: i8) {
    let actual = (x * y).abs();

    assert_eq!(8, actual)
}

However, I cannot get this to work. I get the following error:

error: an inner attribute is not permitted in this context
   --> src/bin/second.rs:246:3
    |
246 | #![cfg(test)]
    |   ^
    |
    = note: inner attributes, like `#![no_std]`, annotate the item enclosing them, and are usually found at the beginning of source files. Outer attributes, like `#[test]`, annotate the item following them.

When I take out the !, things work better, but not quite yet. When building regular (not test) the extern and use lines trigger errors. These go away when I do the following:

#[cfg(test)]
mod tests {
  extern crate test_case;

  use test_case::test_case;

  #[test_case( 2,  4 ; "when both operands are possitive")]
  #[test_case( 4,  2 ; "when operands are swapped")]
  #[test_case(-2, -4 ; "when both operands are negative")]
  fn multiplication_tests(x: i8, y: i8) {
    let actual = (x * y).abs();

    assert_eq!(8, actual)
  }
}

Am I doing things wrong?

frondeus commented 4 years ago

This is a major reason why there is still alpha version instead of regular v1.0.0 - the documentation lacks a lot :(

If you are using rust 2018 edition I'd recommend dropping whole extern crate test_case. But no matter which edition you choose you still need to do one of two things:

Either wrap every test_case with #[cfg(test)] (highly recommended):

#[cfg(test)] // Skip this and next line if 2018 edition
extern crate test_case;

#[cfg(test)]
mod tests {
    use test_case::test_case;

    #[test_case( 2,  4 ; "when both operands are possitive")]
    #[test_case( 4,  2 ; "when operands are swapped")]
    #[test_case(-2, -4 ; "when both operands are negative")]
    fn multiplication_tests(x: i8, y: i8) {
        let actual = (x * y).abs();

        assert_eq!(8, actual)
    }
}

Second example has extern crate inside of tests module - only for 2015 ed.

#[cfg(test)]
mod tests {
    extern crate test_case;
    use self::test_case::test_case; // Remember about `self::`

    #[test_case( 2,  4 ; "when both operands are possitive")]
    #[test_case( 4,  2 ; "when operands are swapped")]
    #[test_case(-2, -4 ; "when both operands are negative")]
    fn multiplication_tests(x: i8, y: i8) {
        let actual = (x * y).abs();

        assert_eq!(8, actual)
    }
}

Or you move test_case from dev-dependencies to dependencies and skip whole #[cfg(test)] attribute. But I do not recommend that - it makes your production build longer and executable bloated with test code. There is a good reason to use #[cfg(test)]:

extern crate test_case; // Again in 2018 ed you can skip this line.

use test_case::test_case;

#[test_case( 2,  4 ; "when both operands are possitive")]
#[test_case( 4,  2 ; "when operands are swapped")]
#[test_case(-2, -4 ; "when both operands are negative")]
fn multiplication_tests(x: i8, y: i8) {
    let actual = (x * y).abs();

    assert_eq!(8, actual)
}

Anyway - sorry for the confusion. I'll try to rewrite README. Meanwhile, if you have any questions or PRs - don't hesitate :)

drwilco commented 4 years ago

No worries! I was mainly worried I was doing something wrong.

I'm finding this crate very useful!