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.

Reply via email to