views:

186

answers:

2

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.

+2  A: 

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

T.J. Crowder
+4  A: 

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!
Peter Mortensen
Rosetta Code is a very interesting site. Thanks and a +1 for the link.
Knox
@Knox: I didn't discover Rosetta Code until today. And I agree: it seems to be a very interesting site.
Peter Mortensen