Hi Bryce,
> It sure would be nice, but honestly, I don't feel qualified to muddle with the internals of the CppUTest code... And it's not exactly clear how one would go about it, as CppUTest doesn't know anything about the sizes of objects. If it were me, I'd be tempted to write a template-style MockExpectedFunctionCall::withParameterOfType() method to allow the caller to do something like:
> UserClass* test_data = new UserClassBuilder.withData("foo").build();
> mock().expectOneCall("write").onObject(buffer)
> .withParameterOfType<UserClass>( "data", test_data );
> The idea being that as a template method, you would actually know the UserClass's size and be able to use its comparison operators (which would be required, of course).
Ok. Though, it isn't much different from withParameterOfTime("UserClass", "data", test_data) and if you already have the comparison operator, then implementing a comparator is about 1 line of code… (plus one more for installing it).
So, though I understand it looks a but nicer, the actual benefit of this (compared to the additional complexity) isn't that high.
> Caveats:
> 1. I'm as much of a C++ newbie as you could probably imagine, so it's quite likely I've typed something above that is quite horrible or even impossible.
Oh, it is so horrible :)
Nah, it is ok and it makes it more clear what you mean…
> 2. It seems like CppUTest has chosen not to use templates, even for very simple use-cases. I have no broad experience with how C++ compilers are at handling simple type-templates, so I have no such compunctions.
Yes, CppUTest is not using templates and that is probably why we won't be implementing the suggestion.
There are a couple of reasons for not using templates (and some other C++ features):
- CppUTest is quite often used for embedded systems and compilers and their C++ support isn't always that great.
- Templates sometimes make things easy… but often complicate things a lot too. For example, in the code example you gave, if you make a mistake you are likely to get a very puzzling confusing compiler error. I sometimes use GMock with CppUTest but the compiler errors that I sometimes get are really mind-blowing.
- When going down the template road, I'm afraid there is no return anymore :P
> 3. There may be some other elegant & simple way to make this easier, but now that I've written it, it looks *so*much* nicer than the currently required registered-object-comparator stuff.
Is it? Did you use the FunctionComparator yet? Then you don't need to use a subclass at all, you just declare one comparing function.
I'm interested to improving the comparator stuff, but not sure where the problem lies in yet. And I'm sure I probably wouldn't want to go the way of the template.
Thanks!
Bas (thinking about how to make the comparators easier to use)