digitalmars.D - Better assert without changing built-in assert
- Jens Mueller (29/29) Feb 16 2011 Hi,
- Jim (15/51) Feb 16 2011 Or like this:
- Andrei Alexandrescu (6/57) Feb 16 2011 There's already text in std.conv with the same semantics.
- Jim (15/51) Feb 16 2011 Or like this:
Hi, I'm trying to improve the assertions. I tried the following auto foo(bool var) { return tuple(var, "MyMessage"); } void bar(bool var, string text) { } unittest { bar(foo(true).expand); //assert(foo(true).expand); // won't compile } void main() {} $ dmd -unittest -run file.d Commenting in the assert it fails with "Error: expression tuple(foo(true)._field_field_0,foo(true)._field_field_1) of type (bool, string) does not have a boolean value" assert behaves different when expanding tuples. Any explanations? If one could do the above without writing the expand then it would suggest a nice way of enhancing the build-in assert, I think. If one cannot make it work without the .expand another option may be to overload the built-in assert to handle tuples. This may be related to http://d.puremagic.com/issues/show_bug.cgi?id=5547 Then one writes e.g. assert(throws!MyException(expression)) or assert(pred!("==")(a, b)) What do you think? Jens
Feb 16 2011
Jens Mueller Wrote:Hi, I'm trying to improve the assertions. I tried the following auto foo(bool var) { return tuple(var, "MyMessage"); } void bar(bool var, string text) { } unittest { bar(foo(true).expand); //assert(foo(true).expand); // won't compile } void main() {} $ dmd -unittest -run file.d Commenting in the assert it fails with "Error: expression tuple(foo(true)._field_field_0,foo(true)._field_field_1) of type (bool, string) does not have a boolean value" assert behaves different when expanding tuples. Any explanations? If one could do the above without writing the expand then it would suggest a nice way of enhancing the build-in assert, I think. If one cannot make it work without the .expand another option may be to overload the built-in assert to handle tuples. This may be related to http://d.puremagic.com/issues/show_bug.cgi?id=5547 Then one writes e.g. assert(throws!MyException(expression)) or assert(pred!("==")(a, b)) What do you think? JensOr like this: import std.conv; string cat(T...)(T ts) { string s; foreach(t; ts) s ~= to!string(t) ~ " "; return s; } unittest { ... assert(a==b, cat(a,b)); }
Feb 16 2011
On 2/16/11 5:04 AM, Jim wrote:Jens Mueller Wrote:There's already text in std.conv with the same semantics. { assert(a==b, text(a,b)); } AndreiHi, I'm trying to improve the assertions. I tried the following auto foo(bool var) { return tuple(var, "MyMessage"); } void bar(bool var, string text) { } unittest { bar(foo(true).expand); //assert(foo(true).expand); // won't compile } void main() {} $ dmd -unittest -run file.d Commenting in the assert it fails with "Error: expression tuple(foo(true)._field_field_0,foo(true)._field_field_1) of type (bool, string) does not have a boolean value" assert behaves different when expanding tuples. Any explanations? If one could do the above without writing the expand then it would suggest a nice way of enhancing the build-in assert, I think. If one cannot make it work without the .expand another option may be to overload the built-in assert to handle tuples. This may be related to http://d.puremagic.com/issues/show_bug.cgi?id=5547 Then one writes e.g. assert(throws!MyException(expression)) or assert(pred!("==")(a, b)) What do you think? JensOr like this: import std.conv; string cat(T...)(T ts) { string s; foreach(t; ts) s ~= to!string(t) ~ " "; return s; } unittest { ... assert(a==b, cat(a,b)); }
Feb 16 2011
Jens Mueller Wrote:Hi, I'm trying to improve the assertions. I tried the following auto foo(bool var) { return tuple(var, "MyMessage"); } void bar(bool var, string text) { } unittest { bar(foo(true).expand); //assert(foo(true).expand); // won't compile } void main() {} $ dmd -unittest -run file.d Commenting in the assert it fails with "Error: expression tuple(foo(true)._field_field_0,foo(true)._field_field_1) of type (bool, string) does not have a boolean value" assert behaves different when expanding tuples. Any explanations? If one could do the above without writing the expand then it would suggest a nice way of enhancing the build-in assert, I think. If one cannot make it work without the .expand another option may be to overload the built-in assert to handle tuples. This may be related to http://d.puremagic.com/issues/show_bug.cgi?id=5547 Then one writes e.g. assert(throws!MyException(expression)) or assert(pred!("==")(a, b)) What do you think? JensOr like this: import std.conv; string cat(T...)(T ts) { string s; foreach(t; ts) s ~= to!string(t) ~ " "; return s; } unittest { ... assert(a==b, cat(a,b)); }
Feb 16 2011