www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - what the closest thing we have to racket's check_expect()?

reply Dr Machine Code <jckj33 gmail.com> writes:
it differ from assert because it contains the expression, file 
and line information. See this 
https://stackoverflow.com/questions/14420857/check-expect-example-in-racket
what's the closest thing we have in D? can we make it without 
compiler  builtin?
Dec 22 2021
next sibling parent Rumbu <rumbu rumbu.ro> writes:
On Wednesday, 22 December 2021 at 20:14:01 UTC, Dr Machine Code 
wrote:
 it differ from assert because it contains the expression, file 
 and line information. See this 
 https://stackoverflow.com/questions/14420857/check-expect-example-in-racket
 what's the closest thing we have in D? can we make it without 
 compiler  builtin?
Every error or exception in D has line and file information, stack trace and more, therefore you can catch AssertError and print the details if the default message printed on console is not enough. For unit testing you have https://code.dlang.org/packages/dunit
Dec 22 2021
prev sibling parent WebFreak001 <d.forum webfreak.org> writes:
On Wednesday, 22 December 2021 at 20:14:01 UTC, Dr Machine Code 
wrote:
 it differ from assert because it contains the expression, file 
 and line information. See this 
 https://stackoverflow.com/questions/14420857/check-expect-example-in-racket
 what's the closest thing we have in D? can we make it without 
 compiler  builtin?
if you just want to use an assert, the compiler flag `-checkaction=context` basically gives you this kind of error messages. Example how to use in DUB (from serve-d): https://github.com/Pure-D/serve-d/blob/84094fade433f3d52e43c5296d20af53b102ffdd/dub.json ```d void main() { assert(1 + 1 == 2); assert(1 + 1 == 1); } __EOF__ Sample output: core.exception.AssertError onlineapp.d(4): 2 != 1 ---------------- ??:? _d_assert_msg [0x563912072788] ./onlineapp.d:4 _Dmain [0x563912069f54] ``` [Run Online](https://run.dlang.io/is/X1bevb)
Dec 22 2021