www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - expose class declared in unittest

reply "rcor" <murphyslaw480 gmail.com> writes:
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.
Jun 25 2014
parent reply "Meta" <jared771 gmail.com> writes:
On Wednesday, 25 June 2014 at 20:17:35 UTC, rcor wrote:
 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.
I don't completely understand your problem, but have you tried marking the class as static? static class Dummy { ... }
Jun 25 2014
parent reply "rcor" <murphyslaw480 gmail.com> writes:
 I don't completely understand your problem, but have you tried 
 marking the class as static?

 static class Dummy { ... }
I was about to post links to the actual code to make it more clear, but that did the trick. Thanks for the fast reply. Just to make sure - given: unittest { static class Dummy { ... } } Dummy will not exist unless compiled with -unittest, correct?
Jun 25 2014
parent "rcor" <murphyslaw480 gmail.com> writes:
On Wednesday, 25 June 2014 at 20:25:50 UTC, rcor wrote:

 Dummy will not exist unless compiled with -unittest, correct?
Never mind, just verified that this is true. Thanks again.
Jun 25 2014