Skip to content

Commit

Permalink
use rstest for parameterized testing
Browse files Browse the repository at this point in the history
  • Loading branch information
msarahan committed Oct 17, 2019
1 parent ed56713 commit 5c150c9
Show file tree
Hide file tree
Showing 9 changed files with 343 additions and 267 deletions.
95 changes: 95 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 4 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,7 @@ dev = []

[dependencies]
lazy_static = "1.4.0"
regex = "1.3.1"
regex = "1.3.1"

[dev-dependencies]
rstest = "0.4.1"
9 changes: 7 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,13 @@
//!
#[macro_use] extern crate lazy_static;

// Applies only to the test module - not to below. Load test module first because its macros
// need to be defined before the other code gets compiled.
#[cfg(test)]
#[macro_use] mod test;
#[cfg(test)]
#[macro_use] extern crate rstest;

pub mod comp_op;
pub mod version;
pub mod version_compare;
Expand All @@ -61,8 +68,6 @@ pub mod parsers;
mod version_part;
mod custom_parts;

#[cfg(test)]
mod test;

// Reexports
pub use crate::comp_op::CompOp;
Expand Down
111 changes: 111 additions & 0 deletions src/test/macros.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
macro_rules! parametrize_versions {
( $it:item ) => {
#[rstest_parametrize(v_string,
n_parts,
case("1", 1),
case("1.2", 2),
case("1.2.3.4", 4),
case("1.2.3.4.5.6.7.8", 8),
case("0", 1),
case("0.0.0", 3),
case("1.0.0", 3),
case("0.0.1", 3),
case("", 0),
case(".", 0),
case("...", 0),
case("1.2.dev", 3),
case("1.2-dev", 3),
case("1.2.alpha.4", 4),
case("1.2-alpha-4", 4),
case("snapshot.1.2", 3),
case("snapshot-1.2", 3),
// TODO: inspect and fix this case
// case("version-compare 2.1.8.1 / build 209", 4),
)]
$it
};
}

macro_rules! parametrize_versions_errors {
( $it:item ) => {
#[rstest_parametrize(v_string,
n_parts,
case("abc", 1),
case("alpha.dev.snapshot", 3),
case("test. .snapshot", 3),
// TODO: broken case, decide what to do here
// case("$", 1),
)]
$it
};
}

/// List of version sets for dynamic tests
macro_rules! parametrize_versions_set {
( $it:item ) => {
#[rstest_parametrize(a,
b,
operator,
case("1", "1", CompOp::Eq),
case("1.0.0.0", "1", CompOp::Eq),
case("1", "1.0.0.0", CompOp::Eq),
case("0", "0", CompOp::Eq),
case("0.0.0", "0", CompOp::Eq),
case("0", "0.0.0", CompOp::Eq),
case("", "", CompOp::Eq),
case("", "0.0", CompOp::Eq),
case("0.0", "", CompOp::Eq),
case("", "0.1", CompOp::Lt),
case("0.1", "", CompOp::Gt),
case("1.2.3", "1.2.3", CompOp::Eq),
case("1.2.3", "1.2.4", CompOp::Lt),
case("1.0.0.1", "1.0.0.0", CompOp::Gt),
case("1.0.0.0", "1.0.0.1", CompOp::Lt),
case("1.2.3.4", "1.2", CompOp::Gt),
case("1.2", "1.2.3.4", CompOp::Lt),
case("1.2.3.4", "2", CompOp::Lt),
case("2", "1.2.3.4", CompOp::Gt),
case("123", "123", CompOp::Eq),
case("123", "1.2.3", CompOp::Gt),
case("1.2.3", "123", CompOp::Lt),
case("1.1.2", "1.1.30-dev", CompOp::Lt),
case("1.2.3", "1.2.3.alpha", CompOp::Gt),
case("1.2.3", "1.2.3-dev", CompOp::Gt),
case("1.2.3.dev", "1.2.3.alpha", CompOp::Eq),
case("1.2.3-dev", "1.2.3-alpha", CompOp::Eq),
case("1.2.3.dev.1", "1.2.3.alpha", CompOp::Gt),
case("1.2.3-dev-1", "1.2.3-alpha", CompOp::Gt),
case("version-compare 3.2.0 / build 0932", "3.2.5", CompOp::Lt),
case("version-compare 3.2.0 / build 0932", "3.1.1", CompOp::Gt),
case(
"version-compare 1.4.1 / build 0043",
"version-compare 1.4.1 / build 0043",
CompOp::Eq,
),
case(
"version-compare 1.4.1 / build 0042",
"version-compare 1.4.1 / build 0043",
CompOp::Lt,
),
// TODO: inspect these cases
case("snapshot.1.2.3", "1.2.3.alpha", CompOp::Lt),
case("snapshot-1.2.3", "1.2.3-alpha", CompOp::Lt),
)]
$it
};
}

/// List of invalid version sets for dynamic tests
macro_rules! parametrize_errors_set {
( $it:item ) => {
#[rstest_parametrize(a,
b,
operator,
case("1.2.3", "1.2.3", CompOp::Lt),
case("1.2", "1.2.0.0", CompOp::Ne),
case("1.2.3.dev", "dev", CompOp::Eq),
case("snapshot", "1", CompOp::Lt),
)]
$it
};
}
3 changes: 1 addition & 2 deletions src/test/mod.rs
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
pub mod test_version;
pub mod test_version_set;
#[macro_use] pub mod macros;
40 changes: 0 additions & 40 deletions src/test/test_version.rs

This file was deleted.

67 changes: 0 additions & 67 deletions src/test/test_version_set.rs

This file was deleted.

Loading

0 comments on commit 5c150c9

Please sign in to comment.