From 3c8c7f9f03ae96bd1da047032c6850f956151b95 Mon Sep 17 00:00:00 2001 From: Priyanshu Singh <123263608+dev-priyanshu15@users.noreply.github.com> Date: Wed, 16 Oct 2024 23:31:21 +0530 Subject: [PATCH 1/2] 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" --- src/string/reverse.rs | 32 +++++++++++++++++++++----------- 1 file changed, 21 insertions(+), 11 deletions(-) diff --git a/src/string/reverse.rs b/src/string/reverse.rs index a8e72200787..a53edcebf19 100644 --- a/src/string/reverse.rs +++ b/src/string/reverse.rs @@ -6,18 +6,28 @@ pub fn reverse(text: &str) -> String { mod tests { use super::*; - #[test] - fn test_simple() { - assert_eq!(reverse("racecar"), "racecar"); + // Macro for generating test cases + 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"); + // Using the macro to define various test cases + 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"), } } From d7e7fefba3733b4543a057360d46f3553530498c Mon Sep 17 00:00:00 2001 From: Priyanshu Singh <123263608+dev-priyanshu15@users.noreply.github.com> Date: Wed, 16 Oct 2024 23:59:30 +0530 Subject: [PATCH 2/2] Update build.yml --- .github/workflows/build.yml | 33 +++++++++++++++++++++------------ 1 file changed, 21 insertions(+), 12 deletions(-) diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index db306b460ff..56b06d84c6a 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -1,6 +1,6 @@ name: build -'on': +on: pull_request: workflow_dispatch: schedule: @@ -11,22 +11,31 @@ jobs: name: cargo fmt runs-on: ubuntu-latest steps: - - uses: actions/checkout@v4 - - name: cargo fmt - run: cargo fmt --all -- --check - + - uses: actions/checkout@v4 + - name: cargo fmt + run: cargo fmt --all -- --check + clippy: name: cargo clippy runs-on: ubuntu-latest steps: - - uses: actions/checkout@v4 - - name: cargo clippy - run: cargo clippy --all --all-targets -- -D warnings - + - uses: actions/checkout@v4 + - name: cargo clippy + run: cargo clippy --all --all-targets -- -D warnings + test: name: cargo test runs-on: ubuntu-latest steps: - - uses: actions/checkout@v4 - - name: cargo test - run: cargo test + - uses: actions/checkout@v4 + - name: cargo test + run: cargo test --all -- --test-threads=1 + + - name: Install codecov + run: | + curl -Os https://uploader.codecov.io/latest/linux/codecov + chmod +x codecov + sudo mv codecov /usr/local/bin + + - name: Upload coverage to Codecov + run: codecov -f target/debug/deps/*.gcov -t ${{ secrets.CODECOV_TOKEN }} || echo "Codecov upload failed"