A technique to mock "static interfaces" (e.g. isInputRange)

Alex Parrill via Digitalmars-d digitalmars-d at puremagic.com
Wed May 25 14:52:37 PDT 2016


On Wednesday, 25 May 2016 at 21:38:23 UTC, Atila Neves wrote:
> There was talk in the forum of making it easier to come up 
> instantiations of say, an input range for testing purposes. 
> That got me thinking of how mocking frameworks make it easy to 
> pass in dependencies without having to write a whole new "test 
> double" type oneself. How would one do that for what I've 
> started calling "static interfaces"? How would one mock an 
> input range?
>
> There's no way to inspect the code inside the lambda used in 
> isInputRange or any other similar template constraint (.codeof 
> would be awesome, but alas it doesn't exist), but a regular OOP 
> interface you can reflect on... and there's even one called 
> InputRange in Phobos... hmmm.
>
> The result is in the link below. The implementation is a bit 
> horrible because I cowboyed it. I should probably figure out 
> how to make it more template mixin and less of the string 
> variety, but I was on a roll. Anyway, take a look at the unit 
> test at the bottom first and complain about my crappy 
> implementation later:
>
>
> https://gist.github.com/atilaneves/b40c4d030c70686ffa3b8543018f6a7e
>
>
> If you have an interface already I guess you could just mock 
> that, but then you wouldn't be able to test templated code with 
> it. This technique would fix that problem.
>
>
> Interesting? Crazy? Worth adding to unit-threaded? Phobos 
> (after much cleaning up)?
>
>
> Atila

Have you looked at std.typecons.AutoImplement at all? 
http://dlang.org/phobos/std_typecons.html#.AutoImplement

It seems to do something similar to what you're doing, though it 
generates a subclass rather than a struct (for the purposes of 
testing contracts and stuff, I don't think it matters too much).


More information about the Digitalmars-d mailing list