Skip to content

Commit

Permalink
add spaced CPF test
Browse files Browse the repository at this point in the history
  • Loading branch information
thewillyan committed May 10, 2023
1 parent 524c623 commit ec18b54
Showing 1 changed file with 11 additions and 5 deletions.
16 changes: 11 additions & 5 deletions src/cert/csv_data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -174,12 +174,13 @@ pub struct Cpf {

impl Cpf {
pub fn new(value: String) -> Result<Self, ParseError<String>> {
let value = value.trim();
let err = Err(ParseError::new(
"Valid CPF of the form 000.000.000-00",
value.clone(),
value.to_owned(),
));

for (i, part) in value.trim().split('.').enumerate() {
for (i, part) in value.split('.').enumerate() {
if i == 2 {
let (left, right) = match part.split_once('-') {
Some(v) => v,
Expand All @@ -197,7 +198,9 @@ impl Cpf {
return err;
}
}
Ok(Self { id: value })
Ok(Self {
id: value.to_owned(),
})
}

pub fn as_str(&self) -> &str {
Expand Down Expand Up @@ -271,7 +274,10 @@ mod tests {
assert!(Cpf::new("08911684350".to_owned()).is_err());
assert!(Cpf::new("089.116.843.50".to_owned()).is_err());

let cpf = Cpf::new("089.116.843-50".to_owned()).expect("it should be a valid CPF");
assert_eq!("089.116.843-50", cpf.as_str());
let cpf1 = Cpf::new("089.116.843-50".to_owned()).expect("it should be a valid CPF");
assert_eq!("089.116.843-50", cpf1.as_str());

let cpf2 = Cpf::new(" 089.116.843-50 ".to_owned()).expect("it should be a valid CPF");
assert_eq!(cpf1, cpf2);
}
}

0 comments on commit ec18b54

Please sign in to comment.