-1

My mock class looks like this:

struct StringEater {
    MOCK_CONST_METHOD1( ExecuteCommand, void( const char* ) ); 
};

and the string consist of part that doesn't change, and small part that I can not set in test. Something like this :

Command 825 finished

but it can be

Command 123 finished

or "Command " + any number + " finished".

The method from the mock class is always called.

So, how do I set the test? This obviously can not be used:

StringEater mock;
EXPECT_CALL( mock, ExecuteCommand( StrEq( expectedJsonCmd ) ) ).Times( 1 );

What do I need to put for the matcher?


This works (thanks to J):

TEST( abc, some )
{
    struct StringEater {
        MOCK_CONST_METHOD1( ExecuteCommand, void( const char* ) );
    };

    StringEater eater;
    EXPECT_CALL( eater, ExecuteCommand( MatchesRegex( "Command\\s([0-9]*)\\sfinished" ) ) ).Times( 1 );
    eater.ExecuteCommand( "Command 643 finished" );
}
273K
  • 29,503
  • 10
  • 41
  • 64
BЈовић
  • 62,405
  • 41
  • 173
  • 273
  • 1
    This is so trivially easy that there has to be a catch... what are the exact requirements here? I can't believe a senior software engineer and 6-year SO vet with 36k doesn't know how to parse a shockingly regular string. – J... Nov 18 '16 at 10:50
  • @J... Literally never did regex. Tried using `MatchesRegex` but failed :( – BЈовић Nov 18 '16 at 10:59
  • @J... Ok, with some fixes, I made it work. Thanks, but why not answer? – BЈовић Nov 18 '16 at 11:09
  • Because, this is basically : http://meta.stackoverflow.com/a/285739/327083 Also, you hadn't included what you had tried and why it didn't work, it wasn't clear what the exact requirements are (did "number" mean "integer"?, case sensitivity? variable whitespace? etc?). It's a weak question... – J... Nov 18 '16 at 11:11
  • Also worth knowing what platform(s) you're targeting. Regex in gmock sorely lacks consistency between platforms. – J... Nov 18 '16 at 11:24

2 Answers2

0

So it will always be the string "Command" followed by an integral number followed by the string "finished"?

Then it could be tested by attempting to extract these three parts from the string, comparing the first and third parts to the expected strings.

Something like

std::istringstream iss(the_input_string);
std::string s1, s3;
int i2;

if (iss >> s1 >> i2 >> s3)
{
    if (s1 == "Command" && s3 == "finished")
    {
        // Test succeeded
    }
    else
    {
        // Test failed
    }
}
else
{
    // Failed, not correct format
}
Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
  • Actually the string is more complex, and not that simple. I am looking for a gmock matcher to test that string. Now I am thinking to use `StartsWith` and `EndsWith`, but I am hoping someone can suggest something better. – BЈовић Nov 18 '16 at 10:50
0

You could try using regexp where you check whether the string begins with "Command" and ends with "finished" and extract the number as a group.

Similar problem was described in another post: Regex C++: extract substring

Community
  • 1
  • 1
Dusteh
  • 1,496
  • 16
  • 21