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

mock_return_once() #7

Closed
eerimoq opened this issue Oct 30, 2019 · 4 comments
Closed

mock_return_once() #7

eerimoq opened this issue Oct 30, 2019 · 4 comments

Comments

@eerimoq
Copy link

eerimoq commented Oct 30, 2019

Hello!

In vberlier/narwhal#3 (comment) an example shows future functionality, allowing the user to specify a sequence of values to return from the mock. Are there any plans on releasing that?

@vberlier
Copy link
Owner

vberlier commented Nov 2, 2019

Yeah there are still a couple of things I want to add. The problem I'm facing is that mock_return_once() would need to allocate memory to schedule the calls. I'm thinking about restricting the number of scheduled calls to store everything in a static array and keep the entire API allocation-free, but I'm not sure if it's a good trade-off.

When I figure this out I also want to implement something kind of based on your original proposition where you can expect calls:

TEST(xmount_ok)
{
    MOCK(mount)
        ->mock_return_once(0)
        ->disable_mock_once()
        ->mock_return_once(42)
        ->expect("a1", "b", "c", 0, "")->expect_return(0)
        ->expect("a2", "b", "c", 0, "")->expect_return(-1)->expect_errno(ENOENT)
        ->expect("a3", "b", "c", 0, "")->expect_return(42);

    xmount("a1", "b", "c"); // return 0
    xmount("a2", "b", "c"); // return -1
    xmount("a3", "b", "c"); // return 42

    ASSERT_EQ(MOCK(mount)->call_count, 3);
}

@vberlier
Copy link
Owner

vberlier commented Nov 2, 2019

And there's a similar problem for storing the arguments of every call in an array, I'm not sure about making the mocked function allocate to resize the array right in the middle of user code. I'm also trying to think about a way to make it opt-in:

TEST(xmount_ok)
{
    MOCK(mount)->start_watching();

    xmount("a1", "b", "c");

    MOCK(mount)->stop_watching();

    xmount("a2", "b", "c");

    MOCK(mount)->start_watching();

    xmount("a3", "b", "c");

    ASSERT_EQ(MOCK(mount)->calls[0].arg1, "a1");
    ASSERT_EQ(MOCK(mount)->calls[1].arg1, "a3");
}

@eerimoq
Copy link
Author

eerimoq commented Nov 2, 2019

Okay, nice.

I'm evaluating some ideas in my fork on narmock. It's a combination of narmock and cmock. It currently looks like this: https://github.com/eerimoq/narmock/blob/master/tests/main.c

@vberlier
Copy link
Owner

vberlier commented Nov 2, 2019

These (set|ignore)_<arg>_(in|out) functions look pretty interesting. The assert_completed function could also be useful in combination with the expect API I have in mind.

@eerimoq eerimoq closed this as completed Jan 29, 2023
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