Skip to content

Commit

Permalink
Use rstest and anyhow for compact but more verbose tests
Browse files Browse the repository at this point in the history
  • Loading branch information
rubdos committed Apr 18, 2023
1 parent 989054c commit 0f0cc97
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 36 deletions.
3 changes: 3 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -37,3 +37,6 @@ thiserror = "1.0"

[dev-dependencies]
doc-comment = "0.3"
rstest = "0.17"
rstest_reuse = "0.5"
anyhow = "1"
3 changes: 3 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@ extern crate bincode;
#[macro_use]
extern crate doc_comment;

#[cfg(test)]
use rstest_reuse;

#[cfg(test)]
doctest!("../README.md");

Expand Down
58 changes: 22 additions & 36 deletions src/phone_number.rs
Original file line number Diff line number Diff line change
Expand Up @@ -243,45 +243,31 @@ impl<'a> Deref for Country<'a> {

#[cfg(test)]
mod test {
use crate::country;
use crate::country::{self, *};
use crate::parser;

#[test]
fn country_id() {
use anyhow::Context;
use rstest::rstest;
use rstest_reuse::{self, *};

#[template]
#[rstest]
#[case("+61406823897", AU)]
#[case("+34666777888", ES)]
#[case("+13459492311", KY)]
#[case("+16137827274", CA)]
#[case("+1 520 878 2491", US)]
fn phone_numbers(#[case] number: &str, #[case] country: country::Id) {}

#[apply(phone_numbers)]
fn country_id(#[case] number: &str, #[case] country: country::Id) -> anyhow::Result<()> {
let pn = parser::parse(None, number).with_context(|| format!("parsing {number}"))?;
assert_eq!(
country::AU,
parser::parse(None, "+61406823897")
.unwrap()
.country()
.id()
.unwrap()
country,
pn.country().id().ok_or_else(|| anyhow::anyhow!(
"Phone number {pn} (parsed from {number}) has no associated country code id"
))?
);

assert_eq!(
country::ES,
parser::parse(None, "+34666777888")
.unwrap()
.country()
.id()
.unwrap()
);

assert_eq!(
country::KY,
parser::parse(None, "+13459492311")
.unwrap()
.country()
.id()
.unwrap()
);

assert_eq!(
country::CA,
parser::parse(None, "+16137827274")
.unwrap()
.country()
.id()
.unwrap()
);
Ok(())
}
}

0 comments on commit 0f0cc97

Please sign in to comment.