sponsor Vim development Vim logo Vim Book Ad

tAssert : Simple assertions, poor man's contracts for vim script

 script karma  Rating 2/2, Downloaded by 2351  Comments, bugs, improvements  Vim wiki

created by
Tom Link
script type
This plugin defines a command |:TAssert| that takes an expression as
argument and throws an exception if this expression evaluates to
|empty()|. You can insert these comments throughout your code whenever
something could potentially go wrong. The exception is then thrown right
where the problem occurs. You could think of it as a poor man's
design-by-contract substitute.

One goal of this plugin is to allow users to quickly switch on or off
assertions depending on the context. This can be done either by:

    1. Turning assertions off so that they are not evaluated. This can
       be achieved by setting g:TASSERT to 0 or by calling the command

    2. Commenting out assertions in the current buffer by calling the
       commmand |:TAssertComment|.


    fun! Test(a, b)
        TAssertType a:a, 'string'
        TAssertType a:b, 'number'
        TAssert !empty(a:a)
        return repeat(a:a, a:b)


TAssert also includes a few convenience commands for logging. The
variable g:TLOG controls where messages are written to:

    echom ... Print messages in the echo area.
    file  ... Print messages to a file; syntax "file:FILENAME"
    Decho ... Print messages via Decho (vimscript#642)
install details
Extract the archive to ~/.vim or ~/vimfiles. If g:TASSERT is 0, the impact of
this script on startup time should be minimal.

Insert the following code in files using assertions. This defines a utility function
and makes sure that end users can still use your files without having this
plugin installed: >

    if !exists(':TAssert')
        command! -nargs=* -bar -bang TAssert :
        command! -nargs=* -bar -bang TAssertType :

Also available via git

rate this script Life Changing Helpful Unfulfilling 
script versions (upload new version)

Click on the package to download.

package script version date Vim version user release notes
tAssert.vba.gz 1.0 2009-03-11 7.0 Tom Link - Incompatible changes galore
- Removed :TAssertToggle, :TAssertBegin & :TAssertEnd etc. that doesn't really belong here.
- :TAssertType command (requires macros/tassert.vim)
- Moved Is*() functions to macros/tassert.vim.
tAssert.vba.gz 0.3 2007-04-29 7.0 Tom Link - IsA(): Can take a list of types as arguments and it provides a way to
check dictionaries againt prototypes or interface definitions.
- IsExistent()
- New log-related commands: TLogOn, TLogOff, TLogBufferOn, TLogBufferOff
- Use TAssertVal(script, expr) to evaluate an expression (as
argument to a command) in the script context.
- TAssertOn implies TLogOn
- *Comment & *Uncomment commands now take a range as argument (default:
whole file).
- TAssertComment! & TAssertUncomment! (with [!]) also call
tAssert.zip 0.2 2006-12-19 7.0 Tom Link - More convenience functions
- The convenience functions now display an explanation for a failure
- Convenience commands weren't loaded when g:TASSERT was off.
- Logging to a file & via Decho()
- TAssert! (the one with the bang) doesn't throw an error but simply
displays the failure in the log
- s:ResolveSIDs() didn't return a string if s:assertFile wasn't set.
- s:ResolveSIDs() caches scriptnames
- Moved logging code to 00tLog.vim
tAssert.zip 0.1 2006-12-13 6.0 Tom Link Initial upload
ip used for rating:

If you have questions or remarks about this site, visit the vimonline development pages. Please use this site responsibly.
Questions about Vim should go to the maillist. Help Bram help Uganda.
Vim at Github