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 MessageMatches matcher #2570

Merged
merged 4 commits into from
Dec 11, 2022
Merged

Conversation

lbckmnn
Copy link
Contributor

@lbckmnn lbckmnn commented Nov 18, 2022

Description

If you want to test whether a function throws an exception with a message, it is currently not possible to check the exception type and apply a matcher on the exception message in one run. You can check the exact message with REQUIRE_THROWS_MATCHES and Message("test") but you can not apply a matcher on the message.

If you want to apply a matcher on the exception message and check the type, the current options are:

  1. check type and message in two runs:
//REQUIRE_THROWS_WITH allows to apply a matcher on the message but doesn't allow checking the type
REQUIRE_THROWS_WITH(throwsException(),  StartsWith("Argument is invalid")); 
//REQUIRE_THROWS_AS allows checking the type, but not the message
REQUIRE_THROWS_AS(throwsException(), std::invalid_argument); 
  1. use REQUIRE_THROWS_MATCHES:
    This options only allows applying matchers on the exception itself, and not on the message so you have to write a custom matcher.

This PR adds a matcher that makes it possible to use std::string matchers like StartsWith or ContainsSubstring on exception messages together with REQUIRE_THROWS_MATCHES.

Example:

REQUIRE_THROWS_MATCHES(throwsException(),  std::runtime_error,  MessageMatches(StartsWith("Argument is invalid")));

@codecov
Copy link

codecov bot commented Nov 18, 2022

Codecov Report

Merging #2570 (fd2188c) into devel (2d7be1f) will increase coverage by 0.01%.
The diff coverage is 100.00%.

❗ Current head fd2188c differs from pull request most recent head 4ec135a. Consider uploading reports for the commit 4ec135a to get more accurate results

@@            Coverage Diff             @@
##            devel    #2570      +/-   ##
==========================================
+ Coverage   91.09%   91.10%   +0.01%     
==========================================
  Files         187      187              
  Lines        7622     7632      +10     
==========================================
+ Hits         6943     6953      +10     
  Misses        679      679              

@horenmar
Copy link
Member

FYI: I like the addition, but won't get to this until late next week.

@horenmar horenmar merged commit 9c0533a into catchorg:devel Dec 11, 2022
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.

2 participants