mod error;
mod kind;
mod rif;
pub use kind::*;
pub use rif::*;
#[allow(unused_imports)]
mod tests {
use std::str::FromStr;
use crate::error::*;
use crate::kind::*;
use crate::rif::*;
#[test]
fn calcs_the_checksum_digit() {
let rif_identifier = vec![
(Kind::Legal, 00019361),
(Kind::Legal, 07013380),
(Kind::Legal, 31286704),
(Kind::Legal, 40512535),
(Kind::Legal, 40119253),
(Kind::Government, 20009997),
(Kind::Government, 20000001),
(Kind::Government, 20000002),
(Kind::Government, 20000004),
(Kind::Government, 20000044),
];
let rif_checksum_digit = vec![4, 5, 3, 7, 0, 6, 5, 3, 0, 9];
for (idx, (kind, identifier)) in rif_identifier.iter().enumerate() {
assert_eq!(
rif_checksum_digit[idx],
Rif::calc_checksum_digit(kind, *identifier),
);
}
}
#[test]
fn creates_rif_from_str() {
let candidates = vec![
Rif::new(Kind::Legal, 000019361, 4).unwrap(),
Rif::new(Kind::Legal, 07013380, 5).unwrap(),
Rif::new(Kind::Legal, 31286704, 3).unwrap(),
Rif::new(Kind::Government, 20000044, 9).unwrap(),
Rif::new(Kind::Government, 20000004, 0).unwrap(),
Rif::new(Kind::Government, 20000002, 3).unwrap(),
];
let expects = vec![
Rif::from_str("J-00019361-4").unwrap(),
Rif::from_str("J-07013380-5").unwrap(),
Rif::from_str("J-31286704-3").unwrap(),
Rif::from_str("G-20000044-9").unwrap(),
Rif::from_str("G-20000004-0").unwrap(),
Rif::from_str("G-20000002-3").unwrap(),
];
for (idx, rif) in candidates.iter().enumerate() {
assert_eq!(*rif, expects[idx]);
}
}
#[test]
fn checks_for_invalid_rifs_from_str() {
let have = vec![
Rif::from_str("J-00018461-4"),
Rif::from_str("E-12312312-5"),
Rif::from_str("M-00000001-3"),
Rif::from_str("X-00029383-7"),
Rif::from_str("V-AA348932-1"),
Rif::from_str("G-X0000002-3"),
Rif::from_str("G200000040"),
];
let expected_error = vec![
Error::UnexpectedCheckNum(5, 4),
Error::UnexpectedCheckNum(6, 5),
Error::InvalidRifKind(String::from("M")),
Error::InvalidRifKind(String::from("X")),
Error::InvalidRifIdentifier(String::from("invalid digit found in string")),
Error::InvalidRifIdentifier(String::from("invalid digit found in string")),
Error::InvalidRif(String::from("RIF must be splitted into 3 parts separated by dashes. Eg. J-123456789-1. Provided G200000040")),
];
for (idx, rif) in have.into_iter().enumerate() {
assert_eq!(rif.err().unwrap(), expected_error[idx]);
}
}
}