Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add more tests and run code coverage analysis #53

Merged
merged 15 commits into from
Jun 20, 2023
Merged
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
86 changes: 51 additions & 35 deletions src/phone_number.rs
Original file line number Diff line number Diff line change
Expand Up @@ -243,45 +243,61 @@ impl<'a> Deref for Country<'a> {

#[cfg(test)]
mod test {
use crate::country;
use crate::parser;

#[test]
fn country_id() {
assert_eq!(
country::AU,
parser::parse(None, "+61406823897")
.unwrap()
.country()
.id()
.unwrap()
);
use crate::country::{self, *};
use crate::{parser, Mode, PhoneNumber};
use anyhow::Context;
use rstest::rstest;
use rstest_reuse::{self, *};

fn parsed(number: &str) -> PhoneNumber {
parser::parse(None, number)
.with_context(|| format!("parsing {number}"))
.unwrap()
}

#[template]
#[rstest]
#[case(parsed("+61406823897"), AU)]
#[case(parsed("+32474091150"), BE)]
#[case(parsed("+34666777888"), ES)]
#[case(parsed("+13459492311"), KY)]
#[case(parsed("+16137827274"), CA)]
#[case(parsed("+1 520 878 2491"), US)]
#[case(parsed("+1-520-878-2491"), US)]
// #[case(parsed("+1 520-878-2491"), US)]
rubdos marked this conversation as resolved.
Show resolved Hide resolved
fn phone_numbers(#[case] number: PhoneNumber, #[case] country: country::Id) {}

#[apply(phone_numbers)]
fn country_id(#[case] number: PhoneNumber, #[case] country: country::Id) -> anyhow::Result<()> {
assert_eq!(
country::ES,
parser::parse(None, "+34666777888")
.unwrap()
.country()
.id()
.unwrap()
country,
number.country().id().ok_or_else(|| anyhow::anyhow!(
"Phone number {number} has no associated country code id"
))?
);

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

assert_eq!(
country::CA,
parser::parse(None, "+16137827274")
.unwrap()
.country()
.id()
.unwrap()
);
#[apply(phone_numbers)]
#[ignore]
// Format-parse roundtrip
fn round_trip_parsing(
#[case] number: PhoneNumber,
#[case] country: country::Id,
#[values(Mode::International, Mode::E164, Mode::Rfc3966, Mode::National)] mode: Mode,
) -> anyhow::Result<()> {
let country_hint = if mode == Mode::National {
Some(country)
} else {
None
};

let formatted = number.format().mode(mode).to_string();
parser::parse(country_hint, &formatted).with_context(|| {
format!("parsing {number} after formatting in {mode:?} mode as {formatted}")
})?;

Ok(())
}
}