Skip to content

Commit

Permalink
Rewrite Reverse String Tests using Macro (#811)
Browse files Browse the repository at this point in the history
chore(tests): rewrite tests using macro
  • Loading branch information
sozelfist authored Oct 13, 2024
1 parent 0dbaff5 commit be28ad0
Showing 1 changed file with 28 additions and 11 deletions.
39 changes: 28 additions & 11 deletions src/string/reverse.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,12 @@
/// Reverses the given string.
///
/// # Arguments
///
/// * `text` - A string slice that holds the string to be reversed.
///
/// # Returns
///
/// * A new `String` that is the reverse of the input string.
pub fn reverse(text: &str) -> String {
text.chars().rev().collect()
}
Expand All @@ -6,18 +15,26 @@ pub fn reverse(text: &str) -> String {
mod tests {
use super::*;

#[test]
fn test_simple() {
assert_eq!(reverse("racecar"), "racecar");
macro_rules! test_cases {
($($name:ident: $test_case:expr,)*) => {
$(
#[test]
fn $name() {
let (input, expected) = $test_case;
assert_eq!(reverse(input), expected);
}
)*
};
}

#[test]
fn test_assymetric() {
assert_eq!(reverse("abcdef"), "fedcba")
}

#[test]
fn test_sentence() {
assert_eq!(reverse("step on no pets"), "step on no pets");
test_cases! {
test_simple_palindrome: ("racecar", "racecar"),
test_non_palindrome: ("abcdef", "fedcba"),
test_sentence_with_spaces: ("step on no pets", "step on no pets"),
test_empty_string: ("", ""),
test_single_character: ("a", "a"),
test_leading_trailing_spaces: (" hello ", " olleh "),
test_unicode_characters: ("你好", "好你"),
test_mixed_content: ("a1b2c3!", "!3c2b1a"),
}
}

0 comments on commit be28ad0

Please sign in to comment.