What is a good way to use asserts in VBScript scripts?
Is there built-in functionality for it or will it have to be emulated? What is best practice?
One application is to test for objects being Nothing
during development.
Sadly, I don't believe VBScript has anything built-in. You'll need to define your own Assert
method and probably use a pre-processor build script or something to remove them when building a release copy of your script. (Actually removing -- at least commenting out -- the Assert
call is best, rather than just making the body of the Assert
not do anything in the released code.)
An operational answer (for others that may need it) is to define this function, from Rosetta Code:
sub Assert( boolExpr, strOnFail )
if not boolExpr then
Err.Raise vbObjectError + 99999, , strOnFail
end if
end sub
Invocation:
Set obj2 = Nothing
Assert Not obj2 Is Nothing, "obj2 is Nothing!"
Output:
someScript.vbs(17, 3) (null): obj2 is Nothing!