expose class declared in unittest

rcor via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Wed Jun 25 13:17:33 PDT 2014


I'm trying to create a set of utility functions that cache 
objects of various types loaded from json files, but having 
trouble testing it. One function I'd like to test uses new to 
instantiate an object based on a compile-time parameter:

void loadDataFile(T)(string filename) {
...
   T obj = new T(name, value);
...
}

When testing, I try to create a dummy class to test that data can 
be written and read properly:

unittest {
   class Dummy { ... }
   ... write some json to a tempfile ...
   loadDataFile!Dummy(tempfile_path);
   ... verify loaded data ...
}

When running the test, I get the error "outer function context of 
util.jsondata.__unittestL31_1 is needed to 'new' nested class 
util.jsondata.__unittestL31_1."
So it seems that a class nested in a unittest can't be 'newed' 
outside of the test.

The test runs if I declare Dummy outside of the unittest, but I 
don't want it to exist outside of the test. I could import 
modules containing some of the classes that I will actually be 
loading with this, but I feel like the unittest shouldn't depend 
on those, as its designed to work with arbitrary classes. Any 
suggestions would be appreciated.


More information about the Digitalmars-d-learn mailing list