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

Be more consistent about tests vs. main #2644

Merged
merged 3 commits into from
Feb 18, 2025
Merged

Conversation

djmitche
Copy link
Collaborator

@djmitche djmitche commented Feb 14, 2025

The content slides all use fn main, with the exception of the testing segment. But with this change, where it makes sense exercises use tests instead, and not both tests and fn main.

A small change in book.js supports running tests when a code sample does not have fn main but does have #[test], so these work naturally.

Fixes #1581.

The content slides all use `fn main`, with the exception of the testing
segment. But with this change, where it makes sense exercises use tests
instead, and not both tests and `fn main`.

A small change in `book.js` supports running tests when a code sample
does not have `fn main` but does have `#[test]`, so these work
naturally. Note, however, that this doesn't work for non-editable `rust`
text, as Highlightjs is responsible for running the playground in that
case. This means the exercises produce `No output.` when run, but the
solutions (which are editable) show the test results.
@randomPoison randomPoison self-requested a review February 14, 2025 20:16
Comment on lines +33 to +38
// ANCHOR_END: solution

#[test]
fn test_collatz_length() {
assert_eq!(collatz_length(11), 15);
}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is excluding the test from the starting code and solution, was that intended? I thought the goal was to normalize tests on using tests instead of main.

@djmitche
Copy link
Collaborator Author

OK, I've reviewed all of the exercises, and made a few changes. I've also added a section in CONTRIBUTING.md regarding the choice of main or tests.

Here's the summary:

  • Types and Values - main
  • Control Flow Basics - main
  • Tuples and Arrays - main
  • References - main
  • User-Defined Types - main
  • Pattern Matching - tests (with brief introduction)
  • Methods and Traits - main (uses stdout)
  • Generics - tests
  • Standard Library Types - main (uses stdout)
  • Standard Library Traits - tests
  • Memory Management - main (asserting properites of the resulting object wouldn't be very useful)
  • Smart Pointers - tests
  • Borrowing - tests
  • Lifetimes - tests
  • Iterators - tests
  • Modules - main (challenge is not testing-related)
  • Testing - tests
  • Error Handling - tests
  • Unsafe Rust - main (makes sense)

@djmitche djmitche enabled auto-merge (squash) February 18, 2025 19:59
@djmitche djmitche merged commit 44a7974 into google:main Feb 18, 2025
35 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Exercise consistency: tests or main? and allow(unused)
2 participants