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

Atila Neves via Digitalmars-d digitalmars-d at puremagic.com
Wed May 25 14:38:23 PDT 2016


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





More information about the Digitalmars-d mailing list