Skip to content

Commit 8e3472a

Browse files
feat: add validation tests
1 parent 311f05f commit 8e3472a

3 files changed

Lines changed: 733 additions & 96 deletions

File tree

packages/fortifier/src/validations/email.rs

Lines changed: 90 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use std::{
66
};
77

88
/// Email validation error.
9-
#[derive(Debug)]
9+
#[derive(Debug, Eq, PartialEq)]
1010
pub enum EmailError {
1111
/// Invalid email address.
1212
Invalid,
@@ -33,26 +33,6 @@ pub trait ValidateEmail {
3333
}
3434
}
3535

36-
macro_rules! validate_type_with_deref {
37-
($type:ty) => {
38-
impl<T> ValidateEmail for $type
39-
where
40-
T: ValidateEmail,
41-
{
42-
fn email(&self) -> Option<Cow<'_, str>> {
43-
T::email(self)
44-
}
45-
}
46-
};
47-
}
48-
49-
validate_type_with_deref!(&T);
50-
validate_type_with_deref!(Arc<T>);
51-
validate_type_with_deref!(Box<T>);
52-
validate_type_with_deref!(Rc<T>);
53-
validate_type_with_deref!(Ref<'_, T>);
54-
validate_type_with_deref!(RefMut<'_, T>);
55-
5636
impl ValidateEmail for str {
5737
fn email(&self) -> Option<Cow<'_, str>> {
5838
Some(self.into())
@@ -89,3 +69,92 @@ where
8969
}
9070
}
9171
}
72+
73+
macro_rules! validate_with_deref {
74+
($type:ty) => {
75+
impl<T> ValidateEmail for $type
76+
where
77+
T: ValidateEmail,
78+
{
79+
fn email(&self) -> Option<Cow<'_, str>> {
80+
T::email(self)
81+
}
82+
}
83+
};
84+
}
85+
86+
validate_with_deref!(&T);
87+
validate_with_deref!(Arc<T>);
88+
validate_with_deref!(Box<T>);
89+
validate_with_deref!(Rc<T>);
90+
validate_with_deref!(Ref<'_, T>);
91+
validate_with_deref!(RefMut<'_, T>);
92+
93+
#[cfg(test)]
94+
mod tests {
95+
use std::{borrow::Cow, cell::RefCell, rc::Rc, sync::Arc};
96+
97+
use super::{EmailError, ValidateEmail};
98+
99+
#[test]
100+
fn ok() {
101+
assert_eq!((*"admin@localhost").validate_email(), Ok(()));
102+
assert_eq!("admin@localhost".validate_email(), Ok(()));
103+
assert_eq!("admin@localhost".to_owned().validate_email(), Ok(()));
104+
assert_eq!(
105+
Cow::<str>::Borrowed("admin@localhost").validate_email(),
106+
Ok(())
107+
);
108+
assert_eq!(
109+
Cow::<str>::Owned("admin@localhost".to_owned()).validate_email(),
110+
Ok(())
111+
);
112+
113+
assert_eq!(None::<&str>.validate_email(), Ok(()));
114+
assert_eq!(Some("admin@localhost").validate_email(), Ok(()));
115+
116+
assert_eq!((&"admin@localhost").validate_email(), Ok(()));
117+
#[expect(unused_allocation)]
118+
{
119+
assert_eq!(Box::new("admin@localhost").validate_email(), Ok(()));
120+
}
121+
assert_eq!(Arc::new("admin@localhost").validate_email(), Ok(()));
122+
assert_eq!(Rc::new("admin@localhost").validate_email(), Ok(()));
123+
124+
let cell = RefCell::new("admin@localhost");
125+
assert_eq!(cell.borrow().validate_email(), Ok(()));
126+
assert_eq!(cell.borrow_mut().validate_email(), Ok(()));
127+
}
128+
129+
#[test]
130+
fn invalid_error() {
131+
assert_eq!((*"admin").validate_email(), Err(EmailError::Invalid));
132+
assert_eq!("admin".validate_email(), Err(EmailError::Invalid));
133+
assert_eq!(
134+
"admin".to_owned().validate_email(),
135+
Err(EmailError::Invalid)
136+
);
137+
assert_eq!(
138+
Cow::<str>::Borrowed("admin").validate_email(),
139+
Err(EmailError::Invalid)
140+
);
141+
assert_eq!(
142+
Cow::<str>::Owned("admin".to_owned()).validate_email(),
143+
Err(EmailError::Invalid)
144+
);
145+
146+
assert_eq!(Some("admin").validate_email(), Err(EmailError::Invalid));
147+
148+
assert_eq!((&"admin").validate_email(), Err(EmailError::Invalid));
149+
#[expect(unused_allocation)]
150+
{
151+
assert_eq!(Box::new("admin").validate_email(), Err(EmailError::Invalid));
152+
}
153+
assert_eq!(Arc::new("admin").validate_email(), Err(EmailError::Invalid));
154+
assert_eq!(Rc::new("admin").validate_email(), Err(EmailError::Invalid));
155+
156+
let cell = RefCell::new("admin");
157+
assert_eq!(cell.borrow().validate_email(), Err(EmailError::Invalid));
158+
assert_eq!(cell.borrow_mut().validate_email(), Err(EmailError::Invalid));
159+
}
160+
}

0 commit comments

Comments
 (0)