assert()

Propose new features and changes
iseahound
Posts: 610
Joined: 13 Aug 2016, 21:04
GitHub: iseahound

assert()

11 Dec 2019, 12:09

assert(true) ; Do Nothing
assert(false) ; Exit
swagfag
Posts: 3976
Joined: 11 Jan 2017, 17:59

Re: assert()

11 Dec 2019, 12:15

Code: Select all

assert(bool) {
    if (bool = false)
        exitapp
}
:roll:
guest3456
Posts: 3134
Joined: 09 Oct 2013, 10:31

Re: assert()

20 Jan 2020, 10:51

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

User avatar
Delta Pythagorean
Posts: 567
Joined: 13 Feb 2017, 13:44
GitHub: DelPyth
Location: Somewhere in the US

Re: assert()

20 Jan 2020, 16:14

I don't quite understand what Hound is asking for.

- [AHK].......: 1.1.33.02 Unicode 64-bit
- [OS].........: Windows 10.0.18362
- [GITHUB]...: github.com/DeltaPyth
- [PAYPAL]....: paypal.me/DelPyth
- [DISCORD]..: Delta#3324

Remember to use [code]CODE[/code] for your multi-line scripts.
Stay safe, stay inside, and remember to wash your hands for 20 seconds!
User avatar
SpeedMaster
Posts: 389
Joined: 12 Nov 2016, 16:09

Re: assert()

25 Mar 2020, 11:19

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... :think:
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 ? :think:

Not working example :cry:

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
}

guest3456
Posts: 3134
Joined: 09 Oct 2013, 10:31

Re: assert()

25 Mar 2020, 13:06

SpeedMaster wrote:
25 Mar 2020, 11:19
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 ?
Pause?
https://www.autohotkey.com/docs/commands/Pause.htm

but for what purpose?

HotKeyIt
Posts: 2141
Joined: 29 Sep 2013, 18:35
Contact:

Re: assert()

25 Mar 2020, 13:33

Probably Exit?

Code: Select all

return(){
Exit
}
User avatar
SpeedMaster
Posts: 389
Joined: 12 Nov 2016, 16:09

Re: assert()

25 Mar 2020, 15:08

guest3456 wrote:
25 Mar 2020, 13:06
but for what purpose?
to make one-liners 8-)
HotKeyIt wrote:
25 Mar 2020, 13:33
Probably Exit ?

Code: Select all

return(){
Exit
}
Great! That seems to solve the problem. :thumbup:
I'll add it to my personal library. :D

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
}
norths1
Posts: 21
Joined: 12 Mar 2017, 03:06

Re: assert()

28 Mar 2020, 15:12

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
lexikos
Posts: 7064
Joined: 30 Sep 2013, 04:07
GitHub: Lexikos

Re: assert()

12 Apr 2020, 22:12

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:
  • 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.
I believe it could be done in script with Exception() and some clever parsing of script code, with some minor limitations.

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
iseahound
Posts: 610
Joined: 13 Aug 2016, 21:04
GitHub: iseahound

Re: assert()

14 May 2020, 22:05

@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.

Return to “Wish List”

Who is online

Users browsing this forum: No registered users and 1 guest