Monkey Patching in D

Peter Lundgren peter at peterlundgren.com
Wed Jun 5 06:19:32 PDT 2013


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?


More information about the Digitalmars-d mailing list