I was making a quick mocking infrastructure for internal mocks so I wouldn't have to pass in objects to constructors all the time and came up with this:

[code]
mixin template internalMockRig(alias _var, string _varName, alias _varStandard, alias _varMock) {
        
        version(unittest) {
                
                @property bool internalMockSetting() {
                        return _useInternalMocks;
                }
                @property useInternalMocks(bool uim) {
                        
                        // Are we changing the mock type?
                        if (uim != _useInternalMocks) {
                                
                                _useInternalMocks = uim;
                                
                                if(_useInternalMocks) {
                                        _var = new _varMock;
                                }
                                else {
                                        _var = new _varStandard;
                                }
                        }
                }
        
                private: bool _useInternalMocks = false;
        }
}
[/code]

Then in the object:

[code]
class usingTestThing {
        
        testThing thing;
        
        mixin internalMockRig!(thing, testThing, testThingMock);
        
        this() {
                
                thing = new testThing;
        }
        
}
[/code]

This works great as long as there is only one mock object. I thought about using string mixins to generate the properties on a per-variable basis, but the problem is that I can't figure out how to get the string of the variable name from the aliased template mixin parameter. Is there a way to do this?

If not, I'll have to pass in the strings of the variables seperately (as in: mixin internalMockRig!(thing, "thing", testThing, "testThing", testThingMock, "testThingMock"); ), but that seems inelegant (and a bit error prone).


Reply via email to