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

json::out_of_range exception matches against lot of others while testing #3352

Closed
1 of 5 tasks
nandanvasudevan opened this issue Feb 23, 2022 · 4 comments
Closed
1 of 5 tasks
Labels
solution: invalid the issue is not related to the library

Comments

@nandanvasudevan
Copy link

nandanvasudevan commented Feb 23, 2022

What is the issue you have?

I was trying out https://github.com/nlohmann/json and wanted to test the exception mechanism it had.
My initial attempt was to use CHECK_THROWS but that was really difficult since nlohmann::json::out_of_range is not easy to construct.
CHECK_THROWS_AS was what I was supposed to use and that worked fine.

Before figure that out I tried CHECK_THROWS with std::out_of_range and to my surprise that worked! The test also passed with std::bad_alloc.

I am not sure if this is to be expected since nlohmann::json::out_of_range is derived from std::exception.

Please describe the steps to reproduce the issue.

This Code snippet

Can you provide a small but working code example?

Minimal example here: https://github.com/nandanvasudevan/cpp_scratch_pad/tree/json_parsing

What is the expected behavior?

I expect the above tests to fail.
catchorg/Catch2#2371

And what is the actual behavior instead?

The tests pass.

Which compiler and operating system are you using?

  • Compiler: GCC v11
  • Operating system: Jammy Jellyfish (development branch)
  • Test framework: Catch2 v3 Preview 4

Which version of the library did you use?

  • latest release version 3.10.5
  • other release - please state the version: ___
  • the develop branch

If you experience a compilation error: can you compile and run the unit tests?

Compiles fine.

  • yes
  • no - please copy/paste the error message below
@gregmarr
Copy link
Contributor

By using just CHECK_THROWS, you are verifying that this expression throws:

jsonParsed_full.at("Nonexistent"), std::out_of_range("")

and since it does throw, the check passes. This is not a bug either in this library or in Catch2.

@nandanvasudevan
Copy link
Author

It does not throw std::out_of_range or std::bad_alloc.

@nandanvasudevan
Copy link
Author

I understand your point.
I guess I am supposed to use CHECK_THROWS_AS if I need to verify the type of the exception.

@nlohmann
Copy link
Owner

See catchorg/Catch2#2371 (comment).

@nlohmann nlohmann added solution: invalid the issue is not related to the library and removed kind: bug labels Feb 24, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
solution: invalid the issue is not related to the library
Projects
None yet
Development

No branches or pull requests

3 participants