Skip to content

Commit

Permalink
Update documentation
Browse files Browse the repository at this point in the history
  • Loading branch information
Wojciech Polak committed Sep 27, 2019
1 parent 2631af4 commit 022082b
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 92 deletions.
11 changes: 6 additions & 5 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,18 +1,19 @@
[package]
name = "test-case-derive"
name = "test-case"
version = "0.3.0"
edition = "2018"
authors = ["Marcin Sas-Szymanski <marcin.sas-szymanski@anixe.pl>"]
authors = ["Marcin Sas-Szymanski <marcin.sas-szymanski@anixe.pl>", "Wojciech Polak <frondeus@gmail.com>"]
description = "Provides #[test_case(...)] procedural macro attribute for generating parametrized test cases easily"
keywords = ["test", "case", "tests", "unit", "testing"]
categories = ["development-tools", "development-tools::testing"]
readme = "README.md"
license = "MIT"
repository = "https://github.com/synek317/test-case-derive"
documentation = "https://docs.rs/test-case-derive"
repository = "https://github.com/frondeus/test-case"
documentation = "https://docs.rs/test-case"
exclude = ["tests/snapshots/**/*"]

[badges]
travis-ci = { repository = "synek317/test-case-derive", branch = "master" }
travis-ci = { repository = "frondeus/test-case", branch = "master" }
maintenance = { status = "actively-developed" }

[lib]
Expand Down
73 changes: 31 additions & 42 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,56 +3,45 @@ This crate provides `#[test_case]` procedural macro attribute that generates mul
A test is generated for each data set passed in `test_case` attribute.
Under the hood, all test cases that share same body are grouped into `mod`, giving clear and readable test results.

[![Crates.io](https://img.shields.io/crates/v/test-case-derive.svg)](https://crates.io/crates/test-case-derive)
[![Crates.io](https://img.shields.io/crates/v/test-case.svg)](https://crates.io/crates/test-case)
[![license](http://img.shields.io/badge/license-MIT-blue.svg)](https://github.com/kbknapp/clap-rs/blob/master/LICENSE-MIT)
[![Build Status](https://travis-ci.org/synek317/test-case-derive.svg?branch=master)](https://travis-ci.org/synek317/test-case-derive)
[![Build Status](https://travis-ci.org/frondeus/test-case.svg?branch=master)](https://travis-ci.org/frondeus/test-case)

[Documentation](https://docs.rs/test-case-derive/)
[Documentation](https://docs.rs/test-case/)

[Repository](https://github.com/synek317/test-case-derive)
[Repository](https://github.com/frondeus/test-case)

# Breaking changes
* Crate has new name, as `test-case-derive` had no meaning for `derive` part.
* Crate has new maintainer: Wojciech Polak :hand: :tada:
* Since `0.3.0` delimiter for test case description is `;` instead of `::`.
Reason: `::` is valid part of expression and rustc treats const variable as path
with length 1. So usage `#[test_case(1, MY_CONST :: "my desc")]` is treated as
`MY_CONST::"invalid because literal string cannot be part of path"`.

# Getting Started

First of all you have to add this dependency to your `Cargo.toml`:

```toml
[dev-dependencies]
test-case-derive = "0.2.0"
```

Additionally you have to enable `proc_macro` feature and include crate. You can do this globally by adding:

```
#![feature(proc_macro)]
extern crate test_case_derive;
```

to your `lib.rs` or `main.rs` file. Optionally you may enable proc macros only for tests:

```
#![cfg_attr(test, feature(proc_macro))]
#[cfg(test)]
extern crate test_case_derive;
test-case = "0.3.0"
```

Don't forget that procedural macros are imported with `use` statement:

```
use test_case_derive::test_case;
use test_case::test_case;
```

# Example usage:

```
#![cfg(test)]
#![feature(proc_macro)]
extern crate test_case_derive;
use test_case_derive::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")]
#[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();
Expand All @@ -78,9 +67,9 @@ test result: ok. 3 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out
If your only assertion is just `assert_eq!`, you can pass the expectation as macro attribute using `=>` syntax:

```
#[test_case( 2 => 2 :: "returns given number for positive input")]
#[test_case(-2 => 2 :: "returns opposite number for non-positive input")]
#[test_case( 0 => 0 :: "returns 0 for 0")]
#[test_case( 2 => 2 ; "returns given number for positive input")]
#[test_case(-2 => 2 ; "returns opposite number for non-positive input")]
#[test_case( 0 => 0 ; "returns 0 for 0")]
fn abs_tests(x: i8) -> i8 {
if x > 0 { x } else { -x }
}
Expand All @@ -89,9 +78,9 @@ fn abs_tests(x: i8) -> i8 {
Which is equivalent to

```
#[test_case( 2, 2 :: "returns given number for positive input")]
#[test_case(-2, 2 :: "returns opposite number for non-positive input")]
#[test_case( 0, 0 :: "returns 0 for 0")]
#[test_case( 2, 2 ; "returns given number for positive input")]
#[test_case(-2, 2 ; "returns opposite number for non-positive input")]
#[test_case( 0, 0 ; "returns 0 for 0")]
fn abs_tests(x: i8, expected: i8){
let actual = if x > 0 { x } else { -x };
Expand All @@ -102,7 +91,7 @@ fn abs_tests(x: i8, expected: i8){
Attributes and expectation may be any expresion unless they contain `=>`, e.g.

```
#[test_case(None, None => 0 :: "treats none as 0")]
#[test_case(None, None => 0 ; "treats none as 0")]
#[test_case(Some(2), Some(3) => 5)]
#[test_case(Some(2 + 3), Some(4) => 2 + 3 + 4)]
fn fancy_addition(x: Option<i8>, y: Option<i8>) -> i8 {
Expand All @@ -112,7 +101,7 @@ fn fancy_addition(x: Option<i8>, y: Option<i8>) -> i8 {

Note: in fact, `=>` is not prohibited but the parser will always treat last `=>` sign as beginning of expectation definition.

Test case names are optional. They are set using `::` followed by string literal at the end of macro attributes.
Test case names are optional. They are set using `;` followed by string literal at the end of macro attributes.

Example generated code:

Expand Down Expand Up @@ -153,11 +142,11 @@ mod fancy_addition {

## Inconclusive (ignored) test cases (sicne 0.2.0)

If test case name (passed using `::` syntax described above) contains word "inconclusive", generated test will be marked with `#[ignore]`.
If test case name (passed using `;` syntax described above) contains word "inconclusive", generated test will be marked with `#[ignore]`.

```
#[test_case("42")]
#[test_case("XX" :: "inconclusive - parsing letters temporarily doesn't work but it's ok")]
#[test_case("XX" ; "inconclusive - parsing letters temporarily doesn't work but it's ok")]
fn parses_input(input: &str) {
// ...
}
Expand All @@ -181,7 +170,7 @@ mod parses_input {
```

**Note**: word `inconclusive` is only reserved in test name given after `::`.
**Note**: word `inconclusive` is only reserved in test name given after `;`.

# Contribution

Expand All @@ -191,7 +180,7 @@ All contributions and comments are more than welcome! Don't be afraid to open an

MIT License

Copyright (c) 2017 Marcin Sas-Szymański
Copyright (c) 2017-2019 Marcin Sas-Szymański, Wojciech Polak

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand All @@ -209,4 +198,4 @@ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
SOFTWARE.
64 changes: 26 additions & 38 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,52 +3,40 @@
//! A test is generated for each data set passed in `test_case` attribute.
//! Under the hood, all test cases that share same body are grouped into `mod`, giving clear and readable test results.
//!
//! [![Crates.io](https://img.shields.io/crates/v/test-case-derive.svg)](https://crates.io/crates/test-case-derive)
//! [![Crates.io](https://img.shields.io/crates/v/test-case.svg)](https://crates.io/crates/test-case)
//! [![license](http://img.shields.io/badge/license-MIT-blue.svg)](https://github.com/kbknapp/clap-rs/blob/master/LICENSE-MIT)
//! [![Build Status](https://travis-ci.org/synek317/test-case-derive.svg?branch=master)](https://travis-ci.org/synek317/test-case-derive)
//! [![Build Status](https://travis-ci.org/frondeus/test-case.svg?branch=master)](https://travis-ci.org/frondeus/test-case)
//!
//! [Documentation](https://docs.rs/test-case-derive/)
//! [Documentation](https://docs.rs/test-case/)
//!
//! [Repository](https://github.com/synek317/test-case-derive)
//! [Repository](https://github.com/frondeus/test-case)
//!
//! # Getting Started
//!
//! First of all you have to add this dependency to your `Cargo.toml`:
//!
//! ```toml
//! [dev-dependencies]
//! test-case-derive = "0.2.0"
//! ```
//!
//! ```
//! extern crate test_case_derive;
//! ```
//!
//! to your `lib.rs` or `main.rs` file. Optionally you may enable proc macros only for tests:
//!
//! ```
//! #![cfg_attr(test)]
//! #[cfg(test)]
//! extern crate test_case_derive;
//! test-case = "0.3.0"
//! ```
//!
//! Don't forget that procedural macros are imported with `use` statement:
//!
//! ```
//! use test_case_derive::test_case;
//! use test_case::test_case;
//! ```
//!
//! # Example usage:
//!
//! ```
//! #![cfg(test)]
//! extern crate test_case_derive;
//! extern crate test_case;
//!
//! use test_case_derive::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")]
//! #[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();
//!
Expand All @@ -74,9 +62,9 @@
//! If your only assertion is just `assert_eq!`, you can pass the expectation as macro attribute using `=>` syntax:
//!
//! ```
//! #[test_case( 2 => 2 :: "returns given number for positive input")]
//! #[test_case(-2 => 2 :: "returns opposite number for non-positive input")]
//! #[test_case( 0 => 0 :: "returns 0 for 0")]
//! #[test_case( 2 => 2 ; "returns given number for positive input")]
//! #[test_case(-2 => 2 ; "returns opposite number for non-positive input")]
//! #[test_case( 0 => 0 ; "returns 0 for 0")]
//! fn abs_tests(x: i8) -> i8 {
//! if x > 0 { x } else { -x }
//! }
Expand All @@ -85,9 +73,9 @@
//! Which is equivalent to
//!
//! ```
//! #[test_case( 2, 2 :: "returns given number for positive input")]
//! #[test_case(-2, 2 :: "returns opposite number for non-positive input")]
//! #[test_case( 0, 0 :: "returns 0 for 0")]
//! #[test_case( 2, 2 ; "returns given number for positive input")]
//! #[test_case(-2, 2 ; "returns opposite number for non-positive input")]
//! #[test_case( 0, 0 ; "returns 0 for 0")]
//! fn abs_tests(x: i8, expected: i8){
//! let actual = if x > 0 { x } else { -x };
//!
Expand All @@ -98,7 +86,7 @@
//! Attributes and expectation may be any expresion unless they contain `=>`, e.g.
//!
//! ```
//! #[test_case(None, None => 0 :: "treats none as 0")]
//! #[test_case(None, None => 0 ; "treats none as 0")]
//! #[test_case(Some(2), Some(3) => 5)]
//! #[test_case(Some(2 + 3), Some(4) => 2 + 3 + 4)]
//! fn fancy_addition(x: Option<i8>, y: Option<i8>) -> i8 {
Expand All @@ -108,7 +96,7 @@
//!
//! Note: in fact, `=>` is not prohibited but the parser will always treat last `=>` sign as beginning of expectation definition.
//!
//! Test case names are optional. They are set using `::` followed by string literal at the end of macro attributes.
//! Test case names are optional. They are set using `;` followed by string literal at the end of macro attributes.
//!
//! Example generated code:
//!
Expand Down Expand Up @@ -149,11 +137,11 @@
//!
//! ## Inconclusive (ignored) test cases (since 0.2.0)
//!
//! If test case name (passed using `::` syntax described above) contains word "inconclusive", generated test will be marked with `#[ignore]`.
//! If test case name (passed using `;` syntax described above) contains word "inconclusive", generated test will be marked with `#[ignore]`.
//!
//! ```
//! #[test_case("42")]
//! #[test_case("XX" :: "inconclusive - parsing letters temporarily doesn't work but it's ok")]
//! #[test_case("XX" ; "inconclusive - parsing letters temporarily doesn't work but it's ok")]
//! fn parses_input(input: &str) {
//! // ...
//! }
Expand All @@ -177,7 +165,7 @@
//!
//! ```
//!
//! **Note**: word `inconclusive` is only reserved in test name given after `::`.
//! **Note**: word `inconclusive` is only reserved in test name given after `;`.
//!
//! # Contribution
//!
Expand Down Expand Up @@ -251,8 +239,8 @@ mod utils;
/// - With name, without result
///
/// ```
/// #[test_case(1 :: "little number")]
/// #[test_case(100 :: "big number")]
/// #[test_case(1 ; "little number")]
/// #[test_case(100 ; "big number")]
/// #[test_case(5)] // some tests may use default name generated from arguments list
/// fn is_positive(x: i8) {
/// assert!(x > 0)
Expand All @@ -272,8 +260,8 @@ mod utils;
/// - With result and name
///
/// ```
/// #[test_case(1, 2 => 3 :: "both numbers possitive")]
/// #[test_case(-1, -2 => -3 :: "both numbers negative")]
/// #[test_case(1, 2 => 3 ; "both numbers possitive")]
/// #[test_case(-1, -2 => -3 ; "both numbers negative")]
/// fn addition(x: i8, y: i8) -> i8 {
/// x + y
/// }
Expand Down
9 changes: 2 additions & 7 deletions tests/test_cases.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,5 @@
#![cfg(test)]

extern crate test_case_derive;

mod test_cases {
use super::*;
use test_case_derive::test_case;
use test_case::test_case;


#[test_case(2)]
Expand Down Expand Up @@ -80,7 +75,7 @@ mod test_cases {

mod nested {
use super::*;
use test_case_derive::test_case;
use test_case::test_case;

#[test_case(1, 1)]
fn nested_test_case(x: u32, y: u32) {
Expand Down

0 comments on commit 022082b

Please sign in to comment.