mykatas/rust/tests/5_likes.rs

29 lines
893 B
Rust
Raw Normal View History

2021-10-29 23:14:34 +03:00
fn likes(names: &[&str]) -> String {
match names {
[] => format!("no one likes this"),
[a] => format!("{} likes this", a),
[a, b] => format!("{} and {} like this", a, b),
[a, b, c] => format!("{}, {} and {} like this", a, b, c),
[a, b, rest @ ..] => format!("{}, {} and {} others like this", a, b, rest.len()),
}
}
2021-10-30 14:05:57 +03:00
#[test]
fn fixed_tests() {
assert_eq!(likes(&[]), "no one likes this");
assert_eq!(likes(&["Peter"]), "Peter likes this");
assert_eq!(likes(&["Jacob", "Alex"]), "Jacob and Alex like this");
assert_eq!(
likes(&["Max", "John", "Mark"]),
"Max, John and Mark like this"
);
assert_eq!(
likes(&["Alex", "Jacob", "Mark", "Max"]),
"Alex, Jacob and 2 others like this"
);
assert_eq!(
likes(&["a", "b", "c", "d", "e"]),
"a, b and 3 others like this"
);
2021-10-29 23:14:34 +03:00
}