X-Git-Url: https://piware.de/gitweb/?p=learn-rust.git;a=blobdiff_plain;f=src%2Fword_utils.rs;h=e3e71bf135ae753c218a2dc72d10bfee2d425f85;hp=d087820a1ec0fcb2a517cef8599d5db550844fdc;hb=1d259ded66e8edc398867ac80b0c138abe707ea0;hpb=824fd4371bafc487163edf33cb58b05fc4b22971;ds=inline diff --git a/src/word_utils.rs b/src/word_utils.rs index d087820..e3e71bf 100644 --- a/src/word_utils.rs +++ b/src/word_utils.rs @@ -15,5 +15,29 @@ pub fn second_word(s: &str) -> Option<&str> { } } - return None; + None +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn test_first_word() { + assert_eq!(first_word(""), ""); + assert_eq!(first_word("one"), "one"); + assert_eq!(first_word("one two"), "one"); + + assert_eq!(first_word(&String::from("one two")), "one"); + } + + #[test] + fn test_second_word() { + assert_eq!(second_word(""), None); + assert_eq!(second_word("one"), None); + assert_eq!(second_word("one two"), Some("two")); + assert_eq!(second_word("one two three"), Some("two")); + + assert_eq!(second_word(&String::from("one two three")), Some("two")); + } }