Skip to content

Commit 3c8c7f9

Browse files
Update reverse.rs
1. macro_rules! test_cases: This macro is designed to reduce repetition by defining multiple test cases at once. It takes a list of test names and test case tuples ((input, expected)). 2. $name:ident: The test function name. For example, test_simple_palindrome is generated as the test function name. 3. $test_case:expr: This represents a tuple of (input, expected) for each test. 4. Generated Test Functions: Each entry inside test_cases! expands into a separate test function. For example, the test_simple_palindrome macro expands into a function that tests if reversing "racecar" returns "racecar"
1 parent 0dbaff5 commit 3c8c7f9

File tree

1 file changed

+21
-11
lines changed

1 file changed

+21
-11
lines changed

src/string/reverse.rs

Lines changed: 21 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,18 +6,28 @@ pub fn reverse(text: &str) -> String {
66
mod tests {
77
use super::*;
88

9-
#[test]
10-
fn test_simple() {
11-
assert_eq!(reverse("racecar"), "racecar");
9+
// Macro for generating test cases
10+
macro_rules! test_cases {
11+
($($name:ident: $test_case:expr,)*) => {
12+
$(
13+
#[test]
14+
fn $name() {
15+
let (input, expected) = $test_case;
16+
assert_eq!(reverse(input), expected);
17+
}
18+
)*
19+
};
1220
}
1321

14-
#[test]
15-
fn test_assymetric() {
16-
assert_eq!(reverse("abcdef"), "fedcba")
17-
}
18-
19-
#[test]
20-
fn test_sentence() {
21-
assert_eq!(reverse("step on no pets"), "step on no pets");
22+
// Using the macro to define various test cases
23+
test_cases! {
24+
test_simple_palindrome: ("racecar", "racecar"),
25+
test_non_palindrome: ("abcdef", "fedcba"),
26+
test_sentence_with_spaces: ("step on no pets", "step on no pets"),
27+
test_empty_string: ("", ""),
28+
test_single_character: ("a", "a"),
29+
test_leading_trailing_spaces: (" hello ", " olleh "),
30+
test_unicode_characters: ("你好", "好你"),
31+
test_mixed_content: ("a1b2c3!", "!3c2b1a"),
2232
}
2333
}

0 commit comments

Comments
 (0)