ASSERT: Difference between revisions

From QB64 Phoenix Edition Wiki
Jump to navigation Jump to search
No edit summary
No edit summary
 
(One intermediate revision by the same user not shown)
Line 20: Line 20:
''Example:'' Adding test checks for parameter inputs in a function.
''Example:'' Adding test checks for parameter inputs in a function.
{{CodeStart}}
{{CodeStart}}
{{Cl|$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 14:15, 19 March 2023

The _ASSERT statement can be used to perform tests in code that's in development, for debugging purposes.


Syntax

_ASSERT condition[, errorMessage$]


Description

  • condition is the condition that must be met in order to consider the _ASSERT valid.
  • Optional errorMessage$ is the message to be displayed in the console window if $ASSERTS:CONSOLE is used.
  • If the condition is not met (that is, if it evaluates to 0), an error occurs ("_ASSERT failed on line #") and program execution stops.


Availability

  • Version 1.4 and up.


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