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

Strange REQUIRE_THROWS behaviour #223

Closed
reddwarf69 opened this issue Apr 7, 2019 · 3 comments
Closed

Strange REQUIRE_THROWS behaviour #223

reddwarf69 opened this issue Apr 7, 2019 · 3 comments

Comments

@reddwarf69
Copy link
Contributor

reddwarf69 commented Apr 7, 2019

So I have this

#define DOCTEST_CONFIG_IMPLEMENT_WITH_MAIN
#include <cstddef>
#include <cstdint>
#include <gsl/gsl>
#include <stdexcept>
#include <doctest/doctest.h>

class mac_addr final
{
  private:
    static constexpr const std::size_t string_len = 12;

  public:
    mac_addr() noexcept {
        printf("A\n");
    }

    explicit mac_addr(gsl::cstring_span<string_len> addr) {
        printf("B\n");
        throw std::runtime_error("Tried to create a mac_addr from an invalid format");
    }
};

TEST_CASE("mac_addr works as expected")
{
    SUBCASE("Throws if given bad input to construct from")
    {
        std::string invalid_character_mac("01020304050G");
        REQUIRE_THROWS(mac_addr(invalid_character_mac));
    }
}

Failing with

src/mac_addr.cpp:34: FATAL ERROR: REQUIRE_THROWS( mac_addr(invalid_character_mac) ) did NOT throw at all!

since it does call the mac_addr default constructor.
FWIW this works fine like this with Catch.

If I change

        REQUIRE_THROWS(mac_addr(invalid_character_mac));

for

        REQUIRE_THROWS(mac_addr(std::move(invalid_character_mac)));

Now the test passes.

@reddwarf69
Copy link
Contributor Author

Doing this catchorg/Catch2@0dabd95 fixes the issue in doctest.

@onqtam
Copy link
Member

onqtam commented Apr 8, 2019

try what I've written in this comment and let me know if it works (dev branch - not in master yet):
#216 (comment)

@reddwarf69
Copy link
Contributor Author

Yes, that works.

@onqtam onqtam closed this as completed May 6, 2019
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

No branches or pull requests

2 participants