Monkey Patching in D

Paulo Pinto pjmlp at progtools.org
Thu Jun 6 00:46:12 PDT 2013


On Wednesday, 5 June 2013 at 13:19:33 UTC, Peter Lundgren wrote:
> On Wednesday, 5 June 2013 at 07:17:50 UTC, Jacob Carlborg wrote:
>> I would suggestion using some kind of wrapper instead of doing 
>> true monkey patching. Example:
>>
>> class Wrapper
>> {
>>    private Object o;
>>    this (Object o) { this.o = o; }
>>
>>    auto opDispatch (string name, Args ...) (Args args)
>>    {
>>        // Use this function to "catch" all calls and forward 
>> as necessary to "o"
>>    }
>> }
>>
>> http://dlang.org/operatoroverloading.html#Dispatch
>
> That's a reasonable option too. I think I'd rather use deject.
>
> The problem I'm trying to solve (and maybe this isn't as 
> important as I think) is that DMocks or deject + DMocks only 
> works on a subset of the language. I can't mock out free 
> function calls, private or final methods, or functions on 
> structs or unions (can I inject mocked structs using deject?).
>
> What do I do when I want to mock out std.stdio or std.random?

The solution would be to use tools like in C++/Java/.NET world 
that rewrite assembly/bytecode and replace method/function calls 
with your mocks.

It works most of the time.

--
Paulo



More information about the Digitalmars-d mailing list