For improved support for assertions, logging code and stack traces it would be useful to have means of identifying the calling frame of reference. This would
also have the advantage of rendering __LINE__ and its ilk redundant.
For example it is not currently possible to implement a cppunit like assertEqual function.
The best I can achieve is:
void assertEqual(Type)(Type actual_, Type expected_,
char[] file_,
uint line_) {
if (actual_ != expected_) {
writefln("Equality assertion failed");
writefln("actual: '", actual_, "'");
writefln("expected: '", expected_, "'");
}
_d_assert(file_,line_);
}
Which has to be used as:
assertEqual!(string)("foo","bar",cast(char[])(__FILE__),__LINE__);
It should be possible to achieve a more natural syntax like:
assertEqual("foo","bar");
Several proposals seem have been made on the newsgroups but none has been raised as a request here.
I personally think the best approach is to have a library function
stackFrame[] callStack = std.trace();
By default the calling frmae would be something like:
class stackFrame {
stackFrame* parent;
object[] arguments;
}
When functions request source identification the compiler would have to
include mapping information so its not a pure library issue.
E.g
class SourceStackFrame: public StackFrame {
// inherited
// stackFrame* parent;
// object[] arguments;
uint parentLine;
string* parentFile;
string* parentFunction;
}
I imagine two ways of creating such a structure.
Use of an analogue to __FILE__ e.g. the so called "context" keyword
would tell the compiler to use the special stack frame for a specific call
only.
A command line switch could tell the compiler to always use the source stack frame format - with resulting code bloat.
Comment #1 by tortoise_74 — 2007-08-24T19:35:29Z
For the case of using __FILE__ & __LINE__ there is a workaround as follows:
void assertEqual (T) (T actual, T expected, char[] file, uint line) {
if (actual != expected) {
writeln("Equality assertion failed.");
writefln(" actual: '%s'", actual);
writefln(" expected: '%s'", expected);
_d_assert(file, line); //or other assert wrapper
}
}
template MAssertEqual (alias actual, alias expected) {
const AssertEqual =
`assertEqual!(` ~typeof(actual).stringof ~`)(`
~actual.stringof~
`, `
~expected.stringof~
`, cast(char[])(__FILE__), __LINE__);`
;
}
unittest {
int var5 = 1;
int var6 = 2;
mixin(MAssertEqual!(var5, var6)); // -> assertEqual(var5, var6, __FILE__, __LINE__);
}
Comment #2 by nfxjfg — 2010-03-29T16:29:47Z
Tango has this.
It doesn't include the "object[] arguments" thing, though.
Comment #3 by razvan.nitu1305 — 2017-09-05T11:22:12Z
I do not know what the situation was back then with assert, but now, the simple builtin assert is enough to get a stack trace. It seems like `assert` is a wrapper which implements the features requested so, in my opinion this should be closed. Reopen if I misunderstand something.
Example:
void foo(int p1, int p2)
{
assert(p1 == p2);
}
void bar(int p1, int p2)
{
foo(p1, p2);
}
void main()
{
int var5 = 1;
int var6 = 2;
bar(var5, var6);
}
Prints:
[email protected](23): Assertion failure
----------------
??:? _d_assertp [0x425d65]
??:? void fast.foo(int, int) [0x425c86]
??:? void fast.bar(int, int) [0x425c94]
??:? _Dmain [0x425cb0]