eranpeer / FakeIt

C++ mocking made easy. A simple yet very expressive, headers only library for c++ mocking.
MIT License
1.22k stars 170 forks source link

Added StrEq matcher. #256

Closed FranckRJ closed 2 years ago

coveralls commented 2 years ago

Coverage Status

Coverage increased (+0.002%) to 99.924% when pulling 5529f4991d47676b3212c36fa1c360fd9d164702 on strcmp-matchers into f7fe56fa165efbd59c6ed7a7b7f7d4acde22e12e on dev-2.1.0.

FranckRJ commented 2 years ago

Oh i didn't tested the formatting of the error of the matcher.

FranckRJ commented 2 years ago

If the temporary is not valid anymore, then no. But I guess it's a reasonable requirement for this kind of stuff, no ?

Or do you think I should strdup the string to store it somewhere ?

-------- Message d'origine -------- Le 18 nov. 2021, 01:44, Malcolm Davey a écrit :

@malcolmdavey commented on this pull request.


In tests/argument_matching_tests.cpp:

@@ -165,6 +167,21 @@ struct ArgumentMatchingTests: tpunit::TestFixture { Verify(Method(mock, func).Using(Ne(10))).Twice(); }

  • void test_str_eq_matcher() {
  • Mock mock;
  • When(Method(mock, strfunc).Using(StrEq("first"))).Return(1);
  • When(Method(mock, strfunc).Using(StrEq("second"))).Return(2);
  • SomeInterface &i = mock.get();
  • ASSERT_EQUAL(1, i.strfunc("first"));

Will this work if the const char* is pointing to a temporary, instead of a literal?

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub, or unsubscribe. Triage notifications on the go with GitHub Mobile for iOS or Android.

malcolmdavey commented 2 years ago

If the temporary is not valid anymore, then no. But I guess it's a reasonable requirement for this kind of stuff, no ? Or do you think I should strdup the string to store it somewhere ?

Short answer - yes, ideally.

Just for clarity, as I was not sure if I was clear. Was referring to the Using lines, and other if it made sense to use a copy.

When(Method(mock, strfunc).Using(StrEq("first"))).Return(1);

I think most of the time you can get away without the copy, but also thinking of how to make the library more complete/forgiving, and not always needing to worry about cases of potentially dangling references.

Had been looking at the other main case where we currently only story references, and was thinking if and how we could find ways to store a copy (either in some cases implicitly where we can detect it makes sense - e.g. revalue ref, or explicitly - will working on some ideas for it).

FranckRJ commented 2 years ago

You're right. I'll change that to copy the string.

-------- Message d'origine -------- Le 22 nov. 2021, 07:53, Malcolm Davey a écrit :

If the temporary is not valid anymore, then no. But I guess it's a reasonable requirement for this kind of stuff, no ? Or do you think I should strdup the string to store it somewhere ?

Short answer - yes, ideally.

Just for clarity, as I was not sure if I was clear. Was referring to the Using lines, and other if it made sense to use a copy.

When(Method(mock, strfunc).Using(StrEq("first"))).Return(1);

I think most of the time you can get away without the copy, but also thinking of how to make the library more complete/forgiving, and not always needing to worry about cases of potentially dangling references.

Had been looking at the other main case where we currently only story references, and was thinking if and how we could find ways to store a copy (either in some cases implicitly where we can detect it makes sense - e.g. revalue ref, or explicitly - will working on some ideas for it).

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub, or unsubscribe. Triage notifications on the go with GitHub Mobile for iOS or Android.

malcolmdavey commented 2 years ago

Also I haven't really tested out the Using yet, so don't know when we might be able to get some automatic handling/conversion happening. Currently testing out just the Return and already found a few things there that can be simply improved.