Jump to content

Rebol Programming/assert

From Wikibooks, open books for an open world

USAGE:

[edit | edit source]
ASSERT conditions /type 

DESCRIPTION:

[edit | edit source]

Assert that condition is true, else throw an assertion error.

ASSERT is a function value.

ARGUMENTS

[edit | edit source]
  • conditions -- (Type: block)

REFINEMENTS

[edit | edit source]
  • /type -- Safely check datatypes of variables (words)

(SPECIAL ATTRIBUTES)

[edit | edit source]
  • catch
  • throw

SOURCE CODE

[edit | edit source]
assert: func [
    {Assert that condition is true, else throw an assertion error.} 
    [catch throw] 
    conditions [block!] 
    /type "Safely check datatypes of variables (words)" 
    /local w t
][throw-on-error [
        either type [
            parse conditions [any [
                    [set w word! | set w skip (
                            cause-error 'script 'invalid-arg type? get/any 'w
                        )] 
                    [set t [block! | word!] (
                            unless find to-typeset t type? get/any w [
                                make error! join "datatype assertion failed for: " w
                            ]
                        ) | set t skip (
                            cause-error 'script 'invalid-arg type? get/any 't
                        )]
                ]]
        ] [
            any [
                all conditions 
                make error! join "assertion failed for: " mold conditions
            ]
        ]
    ]]