diff options
Diffstat (limited to 'macros/src/util')
-rw-r--r-- | macros/src/util/iterator_ext.rs | 57 | ||||
-rw-r--r-- | macros/src/util/string.rs | 14 |
2 files changed, 70 insertions, 1 deletions
diff --git a/macros/src/util/iterator_ext.rs b/macros/src/util/iterator_ext.rs index 86db6cb..5001068 100644 --- a/macros/src/util/iterator_ext.rs +++ b/macros/src/util/iterator_ext.rs @@ -9,7 +9,7 @@ pub trait IteratorExt<Item> impl<Iter> IteratorExt<Iter::Item> for Iter where Iter: Iterator, - Iter::Item: Eq + Hash + Copy, + Iter::Item: Eq + Hash + Clone, { fn find_duplicate(&mut self) -> Option<Iter::Item> { @@ -26,3 +26,58 @@ where None } } + +#[cfg(test)] +mod tests +{ + use super::*; + + #[test] + fn can_find_duplicate() + { + #[derive(Debug, PartialEq, Eq, Clone, Hash)] + struct Fruit + { + name: String, + } + + assert_eq!( + vec![ + Fruit { + name: "Apple".to_string(), + }, + Fruit { + name: "Banana".to_string(), + }, + Fruit { + name: "Apple".to_string(), + }, + Fruit { + name: "Orange".to_string(), + }, + ] + .iter() + .find_duplicate(), + Some(&Fruit { + name: "Apple".to_string() + }) + ); + + assert_eq!( + vec![ + Fruit { + name: "Banana".to_string(), + }, + Fruit { + name: "Apple".to_string(), + }, + Fruit { + name: "Orange".to_string(), + }, + ] + .iter() + .find_duplicate(), + None + ); + } +} diff --git a/macros/src/util/string.rs b/macros/src/util/string.rs index 90cccee..a04a021 100644 --- a/macros/src/util/string.rs +++ b/macros/src/util/string.rs @@ -10,3 +10,17 @@ pub fn camelcase_to_snakecase(camelcased: &str) -> String .to_string() .to_lowercase() } + +#[cfg(test)] +mod tests +{ + use super::*; + + #[test] + fn camelcase_to_snakecase_works() + { + assert_eq!(camelcase_to_snakecase("LoginHandler"), "login_handler"); + + assert_eq!(camelcase_to_snakecase("Regex"), "regex"); + } +} |