Giter VIP home page Giter VIP logo

Comments (6)

Xadeck avatar Xadeck commented on July 18, 2024 1

Interestingly enough, Optional() can be used.

enum class Error { boom };

std::expected<int, Error> something = 1;
EXPECT_THAT(something, testing::Optional(testing::Gt(0)));

std::expected<int, Error> something = std::unexpected(Error::boom);
EXPECT_EQ(something, std::unexpected(Error::boom));

from googletest.

neg-c avatar neg-c commented on July 18, 2024 1

Interestingly enough, Optional() can be used.

enum class Error { boom };

std::expected<int, Error> something = 1;
EXPECT_THAT(something, testing::Optional(testing::Gt(0)));

std::expected<int, Error> something = std::unexpected(Error::boom);
EXPECT_EQ(something, std::unexpected(Error::boom));

Thats weird, still believe there should be a dedicated macro

from googletest.

cstratopoulos avatar cstratopoulos commented on July 18, 2024 1

Thats weird, still believe there should be a dedicated macro

I agree, the Optional thing is a hacky workaround for now but I would want something like EXPECT_THAT(something, testing::Expected(some_val)) where DescribeNegationTo would indicate that either there was a failure (and describe the error object, if possible) or that it succeeded with an unexpected value.

For a more fluent approach, maybe there could be matchers like EXPECT_THAT(something, succeedsWith(successValMatcher)) or EXPECT_THAT(something, failsWith(errorValMatcher))

Similar to how the existing matchers for Optional and Variant are quite general (ie not bound to the std:: classes), I think it should be reasonable to write this matcher with sufficient generality to work with std::expected and other popular value-or-error types. See e.g., https://ned14.github.io/outcome/tutorial/advanced/interop/value-or-error/

We can assume

  • value()
  • error()
  • has_value()

and public typedefs value_type and error_type. I don't think we need to do any ADL free function stuff like with variant.

This should work with std::expected, TartanLlama tl::expected, Boost.Outcome outcome::result, Boost.System system::result etc

from googletest.

derekmauro avatar derekmauro commented on July 18, 2024 1

The matcher for absl::StatusOr will be released by the abseil-cpp project. Someone is working on it now, but it will take a while.

C++23 and std::expected is not supported yet. We do not want it implemented until C++23 is officially supported.

from googletest.

neg-c avatar neg-c commented on July 18, 2024

In case it gets approved, assign it to me

from googletest.

neg-c avatar neg-c commented on July 18, 2024

For a more fluent approach, maybe there could be matchers like EXPECT_THAT(something, succeedsWith(successValMatcher)) or EXPECT_THAT(something, failsWith(errorValMatcher))

I like this.
It's a little bit weird cuz absl version std::expected is StatusOr, there should be matcher name that will be understood by both library users.
Having it testing::Expected makes it a bit harder to read because of the EXPECTED_X macros.

from googletest.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.