assert(true) ; Do Nothing
assert(false) ; Exit
assert()
Re: assert()
Code: Select all
assert(bool) {
if (bool = false)
exitapp
}
Re: assert()
a few of us have also contributed to this unit testing framework over the years, i use it regularly, and lexikos used it to test the new Object.ahk changes in v2:
https://github.com/Uberi/Yunit
https://github.com/Uberi/Yunit
- Delta Pythagorean
- Posts: 627
- Joined: 13 Feb 2017, 13:44
- Location: Somewhere in the US
- Contact:
Re: assert()
I don't quite understand what Hound is asking for.
[AHK]......: v2.0.12 | 64-bit
[OS].......: Windows 11 | 23H2 (OS Build: 22621.3296)
[GITHUB]...: github.com/DelPyth
[PAYPAL]...: paypal.me/DelPyth
[DISCORD]..: tophatcat
- SpeedMaster
- Posts: 494
- Joined: 12 Nov 2016, 16:09
Re: assert()
I use similar functions myself exitapp() msgbox()..., these small functions are very useful to create ternary and one-liners. I'm still wondering why they're not built in...
here an example:
What would be really interesting is to have a stop() or return() function. I mean something that would stop the script instead of exiting it. Any idea ?
Not working example
here an example:
Code: Select all
#singleinstance force
myvar:=true
(myvar=1) && msgbox("myvar is true") && beep() && assert(false)
msgbox, you will only see this message if myvar is "false" or if assert is set to "true".
msgbox(message:="") {
msgbox, % message
return 1
}
beep() {
soundbeep
return 1
}
assert(bool:=false) {
if (bool = false)
exitapp
return 1
}
What would be really interesting is to have a stop() or return() function. I mean something that would stop the script instead of exiting it. Any idea ?
Not working example
Code: Select all
#singleinstance force
myvar:=true
(myvar=1) && return()
msgbox, how to prevent the script to get here ? (with a one-liner and without closing the script)
q::msgbox, hello world
esc::exitapp
return() {
return
}
Re: assert()
Pause?SpeedMaster wrote: ↑25 Mar 2020, 11:19What would be really interesting is to have a stop() or return() function. I mean something that would stop the script instead of exiting it. Any idea ?
https://www.autohotkey.com/docs/commands/Pause.htm
but for what purpose?
Re: assert()
Probably Exit?
Code: Select all
return(){
Exit
}
- SpeedMaster
- Posts: 494
- Joined: 12 Nov 2016, 16:09
Re: assert()
to make one-liners
Great! That seems to solve the problem.
I'll add it to my personal library.
here is a test script (set myvar to true and then to false and see what happens)
Code: Select all
#singleinstance force
myvar:=true
(myvar=1) ? msgbox("myvar is true") && return() : beep() && beep()
msgbox, myvar is false
return
q::msgbox, hello world
esc::exitapp
beep() {
soundbeep
return 1
}
msgbox(message:="") {
msgbox, % message
return 1
}
return() {
Exit
}
Re: assert()
i think the OP wants an alternative for assert() as in Lua.
This function handles errors and won't let your program crash if an error occurs.
sorry if i got it wrong
This function handles errors and won't let your program crash if an error occurs.
sorry if i got it wrong
Re: assert()
Generally, assert() or ASSERT() will not merely exit the program. It is a debugging tool which can have a range of effects, but usually includes showing an error dialog indicating that an assertion failed. The dialog may have more or less information, depending on the language and whether debugging is enabled. For instance, the dialog may have:
If a debugger is attached, a failed assertion may act as a breakpoint, switching focus to the debugger/IDE. Either way is a lot more useful than merely exiting the program.
If you merely want to exit the thread or program in a one-liner, in v2 you can simply call Exit() or ExitApp(). You can obviously do the same in v1 after defining either function, but I would suggest to avoid calling the function "return".
@norths1
- The expression that was evaluated by assert() and found to be false, such as "x == y" in ASSERT(x == y);.
- The location of the assert() in the source code.
- A stack trace.
- A button to attach a debugger.
If a debugger is attached, a failed assertion may act as a breakpoint, switching focus to the debugger/IDE. Either way is a lot more useful than merely exiting the program.
If you merely want to exit the thread or program in a one-liner, in v2 you can simply call Exit() or ExitApp(). You can obviously do the same in v1 after defining either function, but I would suggest to avoid calling the function "return".
@norths1
Spoiler
Re: assert()
@lexikos It might be worth looking at integrating it into the control flow instead. On this website for a derivative language of Python,
https://www.pyret.org/docs/latest/A_Tour_of_Pyret.html
They use where blocks and check blocks to hold a series of unit tests. The where block seems to be attached to a function definition, and the check block is freestanding. I can’t tell which approach is better, assert or where, but the idea of a fee-standing check block in combination with a #directive to turn unit testing on or off seems to be very powerful.
https://www.pyret.org/ has interesting comparisons of different languages.
https://www.pyret.org/docs/latest/A_Tour_of_Pyret.html
They use where blocks and check blocks to hold a series of unit tests. The where block seems to be attached to a function definition, and the check block is freestanding. I can’t tell which approach is better, assert or where, but the idea of a fee-standing check block in combination with a #directive to turn unit testing on or off seems to be very powerful.
https://www.pyret.org/ has interesting comparisons of different languages.
Who is online
Users browsing this forum: No registered users and 50 guests