Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add section on how to write tests and run tests for the ratatui library #590

Open
kdheepak opened this issue May 2, 2024 · 3 comments
Open
Labels
good first issue Good for newcomers

Comments

@kdheepak
Copy link
Contributor

kdheepak commented May 2, 2024

In the developer guide, we should add a section on how to write tests and run tests.

This section should include a brief primer on cargo test, e.g.:

  1. To run tests, you can use cargo test but if you want to print to stdout, you can use -- --nocapture.
  2. If you only want to run tests in the library, you can use --lib flag.
  3. If you want to run a subset of tests, you can add a test name that will match for all the functions by the same name.

e.g.

cargo test --lib test_render -- --nocapture

We also also add instructions for cargo nextest and how to run a subset of tests using that.

In terms of writing tests, some modules use rstest, which can make writing a series of specification tests like so quite easily to parse:

mod tests {
        use rstest::rstest;

        #[rstest]
        #[case(Flex::Legacy, 1, &[Min(0), Min(0)], "b")] // zero, zero
        #[case(Flex::Legacy, 1, &[Min(0), Min(1)], "b")] // zero, exact
        #[case(Flex::Legacy, 1, &[Min(0), Min(2)], "b")] // zero, overflow
        #[case(Flex::Legacy, 1, &[Min(1), Min(0)], "a")] // exact, zero
        #[case(Flex::Legacy, 1, &[Min(1), Min(1)], "a")] // exact, exact
        #[case(Flex::Legacy, 1, &[Min(1), Min(2)], "a")] // exact, overflow
        #[case(Flex::Legacy, 1, &[Min(2), Min(0)], "a")] // overflow, zero
        #[case(Flex::Legacy, 1, &[Min(2), Min(1)], "a")] // overflow, exact
        #[case(Flex::Legacy, 1, &[Min(2), Min(2)], "a")] // overflow, overflow
        #[case(Flex::Legacy, 2, &[Min(0), Min(0)], "bb")] // zero, zero
        #[case(Flex::Legacy, 2, &[Min(0), Min(1)], "bb")] // zero, underflow
        #[case(Flex::Legacy, 2, &[Min(0), Min(2)], "bb")] // zero, exact
        #[case(Flex::Legacy, 2, &[Min(0), Min(3)], "bb")] // zero, overflow
        #[case(Flex::Legacy, 2, &[Min(1), Min(0)], "ab")] // underflow, zero
        #[case(Flex::Legacy, 2, &[Min(1), Min(1)], "ab")] // underflow, underflow
        #[case(Flex::Legacy, 2, &[Min(1), Min(2)], "ab")] // underflow, exact
        #[case(Flex::Legacy, 2, &[Min(1), Min(3)], "ab")] // underflow, overflow
        #[case(Flex::Legacy, 2, &[Min(2), Min(0)], "aa")] // exact, zero
        #[case(Flex::Legacy, 2, &[Min(2), Min(1)], "aa")] // exact, underflow
        #[case(Flex::Legacy, 2, &[Min(2), Min(2)], "aa")] // exact, exact
        #[case(Flex::Legacy, 2, &[Min(2), Min(3)], "aa")] // exact, overflow
        #[case(Flex::Legacy, 2, &[Min(3), Min(0)], "aa")] // overflow, zero
        #[case(Flex::Legacy, 2, &[Min(3), Min(1)], "aa")] // overflow, underflow
        #[case(Flex::Legacy, 2, &[Min(3), Min(2)], "aa")] // overflow, exact
        #[case(Flex::Legacy, 2, &[Min(3), Min(3)], "aa")] // overflow, overflow
        #[case(Flex::Legacy, 3, &[Min(2), Min(2)], "aab")]
        fn min(
            #[case] flex: Flex,
            #[case] width: u16,
            #[case] constraints: &[Constraint],
            #[case] expected: &str,
        ) {
            letters(flex, constraints, width, expected);
        }
}

This use the rstest crate. A specific case from a test can be run by using something like the following:

cargo test --lib -- tests::min::case_08 --nocapture

Sometimes, the case has more information in the name like in this section and then to run case 08 you can do this:

cargo test --lib -- layout::layout::tests::split::length_is_higher_priority_in_flex::case_08_length_higher_priority --nocapture
@kdheepak kdheepak added the good first issue Good for newcomers label May 2, 2024
@kdheepak
Copy link
Contributor Author

kdheepak commented May 2, 2024

Possibly related to #358

@kdheepak kdheepak changed the title Add section on how to write tests and run tests for the ratatui library as part of the developer guide Add section on how to write tests and run tests for the ratatui library May 2, 2024
@EdJoPaTo
Copy link
Contributor

EdJoPaTo commented May 2, 2024

We also also add instructions for cargo nextest and how to run a subset of tests using that.

Personally I would try to explain everything for cargo test too. As long as it works with standard. nextest might execute faster but is not part of the default cargo experience which people are familiar with. As long as it works with the standard tools without installing anything additionally we should keep that working to reduce the entry barrier.


Side note… git config --global core.hooksPath /dev/null was one of the best things I did since I worked on ratatui… I don't have cargo-make installed anymore too. Only typos is remaining for my local setup.

@joshka
Copy link
Member

joshka commented May 2, 2024

Side note… git config --global core.hooksPath /dev/null was one of the best things I did since I worked on ratatui… I don't have cargo-make installed anymore too. Only typos is remaining for my local setup.

Yeah, I often git push --no-verify before I create a PR (gh pr create) to avoid the time suck of running the CI while pushing too. We should definitely shorten that process a whole bunch to avoid it being a papercut that gets turned off.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
good first issue Good for newcomers
Projects
None yet
Development

No branches or pull requests

3 participants