Getting a String Literal From an Aliased Template Mixin Parameter

jsako via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Wed Sep 14 17:15:42 PDT 2016


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).




More information about the Digitalmars-d-learn mailing list