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

test: Use assert_matches! less #1004

Closed
wants to merge 4 commits into from
Closed

Conversation

acl-cqc
Copy link
Contributor

@acl-cqc acl-cqc commented May 7, 2024

There are downsides to assert_matches!:

  • Extra dependency/import
  • Reduced linting - see several cases here where converting to assert!(matches!(...)) lead to clippy noticing that the match could be converted to .is_ok() or .is_some()
  • Increased chance of accidents, see test: fix some bad assert_matches #1006 (preliminary to this)

Hence, this PR adopts the policy of "use assert_matches only when it's useful". Specifically, that means only when we can use

assert_matches!(SUBJECT, PATTERN => assert!(EXPR))

(and while we're at it, where EXPR is an equality, use assert_eq!), rather than

assert!(matches!(SUBJECT, PATTERN if EXPR))

because the former gives better error messages in case of failure.

(This is quite a small advantage, and noting the increased chance of accidents, one could also say - let's not use assert_matches at all....I'd be happy to do that, too.)

@acl-cqc acl-cqc requested a review from aborgna-q May 7, 2024 10:31
@acl-cqc acl-cqc changed the title Tests: Use assert_matches! less, and fix some bad ones test: Use assert_matches! less, and fix some bad ones May 7, 2024
Copy link

codecov bot commented May 7, 2024

Codecov Report

Attention: Patch coverage is 45.16129% with 34 lines in your changes are missing coverage. Please review.

Project coverage is 85.89%. Comparing base (e5fd315) to head (33f8515).

Files Patch % Lines
hugr/src/hugr/validate/test.rs 8.33% 0 Missing and 11 partials ⚠️
hugr/src/extension/infer/test.rs 14.28% 0 Missing and 6 partials ⚠️
hugr/src/std_extensions/arithmetic/int_types.rs 0.00% 0 Missing and 5 partials ⚠️
hugr/src/builder/dataflow.rs 33.33% 0 Missing and 4 partials ⚠️
hugr/src/builder/conditional.rs 33.33% 0 Missing and 2 partials ⚠️
hugr/src/hugr/views/sibling_subgraph.rs 66.66% 0 Missing and 2 partials ⚠️
hugr/src/ops/constant.rs 50.00% 0 Missing and 2 partials ⚠️
hugr/src/builder/cfg.rs 66.66% 0 Missing and 1 partial ⚠️
hugr/src/hugr/rewrite/outline_cfg.rs 0.00% 0 Missing and 1 partial ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #1004      +/-   ##
==========================================
+ Coverage   85.72%   85.89%   +0.17%     
==========================================
  Files          79       79              
  Lines       14531    14558      +27     
  Branches    14531    14558      +27     
==========================================
+ Hits        12456    12505      +49     
+ Misses       1428     1393      -35     
- Partials      647      660      +13     
Flag Coverage Δ
rust 85.89% <45.16%> (+0.17%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@acl-cqc acl-cqc changed the base branch from main to tests/fix_bad_assert_matches May 7, 2024 11:43
@acl-cqc acl-cqc changed the title test: Use assert_matches! less, and fix some bad ones test: Use assert_matches! less May 7, 2024
Base automatically changed from tests/fix_bad_assert_matches to main May 7, 2024 12:17
@aborgna-q
Copy link
Collaborator

The value of assert_matches is in the error messages. Compare

let x = None;
assert!(matches!(x, Some(42)));
// thread 'test::test_assert' panicked at src/lib.rs:14:9:
// assertion failed: matches!(x, Some(42))
// note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace

with

let x = None;
assert_matches!(x, Some(42));
// thread 'test::test_matches' panicked at src/lib.rs:8:9:
// assertion failed at src/lib.rs:8: `(value does not match pattern)`
//   value: None
// pattern: Some(42)

In this case, matching against a pattern instead of doing .is_ok() is the intended behaviour.

I agree that adding statements on the matched branches is error-prone. I'm happy with banning those.

@acl-cqc acl-cqc marked this pull request as draft May 14, 2024 09:25
@aborgna-q aborgna-q removed their request for review May 20, 2024 11:19
@aborgna-q
Copy link
Collaborator

I'm closing this PR since seem to have agreed on the status quo.
Feel free to reopen it if you want to continue the discussion.

@aborgna-q aborgna-q closed this Jul 25, 2024
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.

None yet

2 participants