6 days ago
Code:
-------
void main() {
	int x;
	struct S {
		int method() { return x; }
		unittest {
			S s;
		}
	}
}
-------

Compiler output:
-------
__stdin.d(6): Error: cannot access frame pointer of `__stdin.main.S`
            S s;
              ^
-------

Now, this is perfectly reasonable, since the unittest runs outside the
context of main(), so S cannot be instantiated (S.method depends on
access to main.x).

But this raises the question: what *can* be tested from within the unittest of S?  Can anything meaningful be tested at all?  Since S cannot be instantiated from the unittest, doesn't that mean it is impossible to unittest S?  Since when the unittest runs, it's outside of the context of main, and main.x may not even exist anywhere.

Now, I *have* used nested struct unittests in real code before -- it's useful for injecting unittests into a function body.  It can be useful for some occasions, like this one:

	auto myfunc(...) {
		static Regex!char myRegex = regex(`...`);

		struct S {
			unittest {
				// make sure the regex actually does
				// what I think it does
				assert("abc".matchAll(myRegex));
			}
		}

		... // rest of function body here
	}

But here the struct is really just a syntactic hack to allow me to write a unittest inside a function body.  S itself is untestable from within the unittest since it's non-instantiable outside myfunc.

Which begs the question, is there even any point of writing unittests for S, aside from hacks like the above?  What if you want to write unittest to verify that S works as designed?  Currently it's impossible. Is it even theoretically possible??


T

-- 
Notwithstanding the eloquent discontent that you have just respectfully expressed at length against my verbal capabilities, I am afraid that I must unfortunately bring it to your attention that I am, in fact, NOT verbose.