These are chat archives for Snaipe/Criterion

20th
Jan 2017
Douman
@DoumanAsh
Jan 20 2017 13:28
@Snaipe Q about mimick. How exactly works mmk_when()? I don't get why CallExpr isn't chaning anything for me :(
Franklin Mathieu
@Snaipe
Jan 20 2017 13:47
@DoumanAsh mmk_when takes an expression that must call your mocked function with the parameter (or matchers) you want
e.g. if you're mocking malloc, and want to describe the behaviour of the mock when called with 4, you'd do mmk_when(malloc(4), ...)
(do mind that for some standard functions like malloc or memset the CallExpr can be optimized away because the compiler thinks that it has no side effects)
Douman
@DoumanAsh
Jan 20 2017 14:03
@Snaipe it is just after i made test i tried to change expression, but it seems it works as usual... even though test itself works as usual. For example i tried to mmk_when(GetClipboardData(mmk_eq(UINT, format))) - format is variable. Test works, then i tried to change it like format + 1 it works too. But when i use mmk_verify(GetClipboardData(mmk_eq(UINT, format + 1)) it finds error
Franklin Mathieu
@Snaipe
Jan 20 2017 14:11
do you perhaps have a self contained example with the expected vs actual result?
Douman
@DoumanAsh
Jan 20 2017 14:28
Not right now, i'll push my code and will link it
Douman
@DoumanAsh
Jan 20 2017 14:45
Here is repo https://github.com/DoumanAsh/lazy-winapi.c - It relies on WinAPI so it works only on windows... Do you use Windows? If not maybe i could try to make some more simple example. You can build it with Cmake UNIT_TESTING option and then build unit tests with make. In result there should be ut.exe, but these tests are stable only with -j1 :(
If i try to change mmk_eq(UINT, format) over https://github.com/DoumanAsh/lazy-winapi.c/blob/master/test/clipboard_mock.c#L38 with a different value of format, test will pass. But if i do the same over line https://github.com/DoumanAsh/lazy-winapi.c/blob/master/test/clipboard_mock.c#L42 then it fails
Not sure if i got it right though
Franklin Mathieu
@Snaipe
Jan 20 2017 14:52
If you're changing the parameter in mmk_verify to format + 1, you also need to make sure that GetClipboardData gets passed format + 1
in essence mmk_verify, well, verifies that GetClipboardData gets called with the right parameters, so you'd need to change format to format + 1 on lines 38, 40, and 42
(in clipboard_mock.c)
Douman
@DoumanAsh
Jan 20 2017 15:08
But what will happen if i wouldn't pass it to GetCLipboardData? As of now test works ok
also i changed mmk_when, not mmk_verify
I assumed if i would change mmk_when then mmk_verify would fail, but it works
Franklin Mathieu
@Snaipe
Jan 20 2017 15:09
both are actually independent
mmk_when specifies what happens at runtime when the function gets called
mmk_verify checks whether the function was called the way you wanted
they can work together or independently
Douman
@DoumanAsh
Jan 20 2017 15:11
hm... but i don't exactly get mmk_when then.. i expected that it would invoke mock only when i pass format +1 to mock, but it gets invoked anyway
is there any purpose in specifying condition?
Franklin Mathieu
@Snaipe
Jan 20 2017 15:12
by condition do you mean mmk_eq(UINT, format)rather than just format?
however mmk_eq is only relevant when you use other matchers
Douman
@DoumanAsh
Jan 20 2017 15:13
yes, i meant mmk_eq(UINT, format) and by format + 1 i meant mmk_eq(UINT, format + 1)
Franklin Mathieu
@Snaipe
Jan 20 2017 15:13
if so, yes, there is in fact no difference in this case between the two syntaxes
Douman
@DoumanAsh
Jan 20 2017 15:14
and i'm confused because it makes no difference to my mock having either mmk_eq(UINT, format) or mmk_eq(UINT, format + 1)
Franklin Mathieu
@Snaipe
Jan 20 2017 15:14
for instance mmk_when(func(1, mmk_le(int, 2)), .then_return = 0) is illegal because you can't mix matchers and non-matchers, so you need to use mmk_eq(int, 1) in this case
and in your case, it doesn't make any difference because by default the mock returns a zero-ish value for unspecified parameters
so, in your case, it always returns NULL
Douman
@DoumanAsh
Jan 20 2017 15:16
aha... so that's why
Franklin Mathieu
@Snaipe
Jan 20 2017 15:17
if you need it to return a valid clipboard HANDLE for certain values, I would probably call GetClipboardData before creating the mock, and do something like mmk_when(GetClipboardData(42), .then_return = the_handle)
Douman
@DoumanAsh
Jan 20 2017 15:18
Well, i'm testing negative paths of my functions. For positive cases i use simple tests without mocks as there is no need
Btw is it indented that without any mmk_when my test would crash, if by default all mocks return NULL ?
Franklin Mathieu
@Snaipe
Jan 20 2017 15:19
sounds like a bug
Douman
@DoumanAsh
Jan 20 2017 15:19
I get this assertion failure E:/Downloads/Git/c_plus_plus/lazy-winapi/build/3pp/src/Mimick/src/matcher.c:51: Assertion failed: v__ != NULL
when i jsut remove any mmk_when
Franklin Mathieu
@Snaipe
Jan 20 2017 15:22
no mmk_when and 1 mmk_verify, right?
I think mmk_when does some initialization that should have been mirrored in mmk_verify
Douman
@DoumanAsh
Jan 20 2017 15:22
yes
hmm... i see, but i suppose it would be better for user to specify mmk_when regardless of default return value. Just this assertion makes no sense :)
Anyway, mimick is great :) though it is impossible to read compile errors/warnings :D
Franklin Mathieu
@Snaipe
Jan 20 2017 15:27
If you're using a GNU compiler, you can reduce the verbosity of compile errors with -isystem=/path/to/mimick/include
Douman
@DoumanAsh
Jan 20 2017 15:30
cool, thanks for a tip
Franklin Mathieu
@Snaipe
Jan 20 2017 15:32
If you're using a GNU compiler, you can reduce the verbosity of compile errors with -isystem=/path/to/mimick/include
unfortunately I don't think there's a similar trigger for MSVC :/
(also sorry if my messages appear late and/or out of order, I'm on a train)
Douman
@DoumanAsh
Jan 20 2017 15:32
well i prefer gcc as MSVC is just bloatware so iti s fine :)
thanks for help, again. Good luck you on your way.
Franklin Mathieu
@Snaipe
Jan 20 2017 15:38
No problem, it's the least I can do.