$ASSERTS: Difference between revisions

From QB64 Phoenix Edition Wiki
Jump to navigation Jump to search
No edit summary
No edit summary
 
(3 intermediate revisions by the same user not shown)
Line 11: Line 11:
* If this metacommand is used in a program and any of the set [[_ASSERT]] checkpoints will fail, then the program will stop with an '''{{Text|_ASSERT failed|red}}''' error.
* If this metacommand is used in a program and any of the set [[_ASSERT]] checkpoints will fail, then the program will stop with an '''{{Text|_ASSERT failed|red}}''' error.
* Detailed error messages passed to the [[_ASSERT]] statement will be displayed in the console window, but only if '''$ASSERTS:CONSOLE''' is used.
* Detailed error messages passed to the [[_ASSERT]] statement will be displayed in the console window, but only if '''$ASSERTS:CONSOLE''' is used.
 
* The '''$ASSERTS''' metacommand serves as main switch to enable debug tests during development. Later you just need to remove the metacommand while leaving all the [[_ASSERT]] statements in place for later debugging sessions, they are simply ignored without the metacommand.
;Note: This metacommand is the main switch to enable debug tests during development. Later just remove this metacommand to compile the program without debugging code, all the [[_ASSERT]] statements may remain in the code for later debugging sessions, they are simply ignored without this metacommand.
* In newer versions a precompiler flag named '''_ASSERTS_''' is available (see [[#Availability|Availability]]):
** The flag is ''one(1)'' if the program is compiled with '''$ASSERTS''' enabled and ''zero(0)'' if compiled without it.
** You may use it to include/exclude debugging related code using precompiler [[$IF]]..[[$ELSE]]...[[$END IF]] blocks.




{{PageAvailability}}
{{PageAvailability}}
* '''QB64 v1.4 and up'''
<!-- QB64 = a version or none, QBPE = a version or all, Platforms = yes or no -->
* '''QB64-PE all versions'''
<gallery widths="48px" heights="48px" mode="nolines">
File:Qb64.png|'''v1.4'''
File:Qbpe.png|'''all'''
File:Apix.png
File:Win.png|'''yes'''
File:Lnx.png|'''yes'''
File:Osx.png|'''yes'''
</gallery>
<!-- additional availability notes go below here -->
* Since '''QB64-PE v4.0.0''' the precompiler flag ''_ASSERTS_'' indicates the state of '''$ASSERTS''' at compile time.




Line 23: Line 34:
;Example:Adding test checks for parameter inputs in a function.
;Example:Adding test checks for parameter inputs in a function.
{{CodeStart}}
{{CodeStart}}
{{Cl|$ASSERTS|$ASSERTS:CONSOLE}}
{{Cm|$ASSERTS}}:CONSOLE


{{Cl|DO}}
{{Cl|DO}}
     a = {{Cl|INT}}({{Cl|RND}} * 10)
     a = {{Cl|INT}}({{Cl|RND}} * {{Text|10|#F580B1}})
     b$ = myFunc$(a)
     b$ = {{Text|myFunc$|#55FF55}}(a)
     {{Cl|PRINT}} a, , b$
     {{Cl|PRINT}} a, , b$
     {{Cl|_LIMIT}} 3
     {{Cl|_LIMIT}} {{Text|3|#F580B1}}
{{Cl|LOOP}} {{Cl|UNTIL}} {{Cl|_KEYHIT}}
{{Cl|DO...LOOP|LOOP UNTIL}} {{Cl|_KEYHIT}}
{{Cl|END}}


{{Cl|FUNCTION}} myFunc$ (value {{Cl|AS}} {{Cl|SINGLE}})
{{Cl|FUNCTION}} {{Text|myFunc$|#55FF55}} (value {{Cl|AS}} {{Cl|SINGLE}})
     {{Cl|_ASSERT}} value > 0, "Value cannot be zero"
     {{Cl|_ASSERT}} value > {{Text|0|#F580B1}}, {{Text|<nowiki>"Value cannot be zero"</nowiki>|#FFB100}}
     {{Cl|_ASSERT}} value <= 10, "Value cannot exceed 10"
     {{Cl|_ASSERT}} value <= {{Text|10|#F580B1}}, {{Text|<nowiki>"Value cannot exceed 10"</nowiki>|#FFB100}}


     {{Cl|IF}} value > 1 {{Cl|THEN}} plural$ = "s"
     {{Cl|IF}} value > {{Text|1|#F580B1}} {{Cl|THEN}} plural$ = {{Text|<nowiki>"s"</nowiki>|#FFB100}}
     myFunc$ = {{Cl|STRING$}}(value, "*") + {{Cl|STR$}}(value) + " star" + plural$ + " :-)"
     {{Text|myFunc$|#55FF55}} = {{Cl|STRING$}}(value, {{Text|<nowiki>"*"</nowiki>|#FFB100}}) + {{Cl|STR$}}(value) + {{Text|<nowiki>" star"</nowiki>|#FFB100}} + plural$ + {{Text|<nowiki>" :-)"</nowiki>|#FFB100}}
{{Cl|END}} {{Cl|FUNCTION}}
{{Cl|END FUNCTION}}
{{CodeEnd}}
{{CodeEnd}}



Latest revision as of 16:05, 18 November 2024

The $ASSERTS metacommand enables debug tests with the _ASSERT macro.


Syntax

$ASSERTS
$ASSERTS:CONSOLE


Description

  • This metacommand does not require a comment ' or REM before it. There is no space between the metacommand name, the colon and the CONSOLE parameter.
  • If this metacommand is used in a program and any of the set _ASSERT checkpoints will fail, then the program will stop with an _ASSERT failed error.
  • Detailed error messages passed to the _ASSERT statement will be displayed in the console window, but only if $ASSERTS:CONSOLE is used.
  • The $ASSERTS metacommand serves as main switch to enable debug tests during development. Later you just need to remove the metacommand while leaving all the _ASSERT statements in place for later debugging sessions, they are simply ignored without the metacommand.
  • In newer versions a precompiler flag named _ASSERTS_ is available (see Availability):
    • The flag is one(1) if the program is compiled with $ASSERTS enabled and zero(0) if compiled without it.
    • You may use it to include/exclude debugging related code using precompiler $IF..$ELSE...$END IF blocks.


Availability

  • Since QB64-PE v4.0.0 the precompiler flag _ASSERTS_ indicates the state of $ASSERTS at compile time.


Examples

Example
Adding test checks for parameter inputs in a function.
$ASSERTS:CONSOLE

DO
    a = INT(RND * 10)
    b$ = myFunc$(a)
    PRINT a, , b$
    _LIMIT 3
LOOP UNTIL _KEYHIT
END

FUNCTION myFunc$ (value AS SINGLE)
    _ASSERT value > 0, "Value cannot be zero"
    _ASSERT value <= 10, "Value cannot exceed 10"

    IF value > 1 THEN plural$ = "s"
    myFunc$ = STRING$(value, "*") + STR$(value) + " star" + plural$ + " :-)"
END FUNCTION


See also



Navigation:
Main Page with Articles and Tutorials
Keyword Reference - Alphabetical
Keyword Reference - By usage
Report a broken link