tcltest Part 7 – Inexact Result Matching

The Problem

We want to test the result of a function, but don’t know the exact values

The Solution

In case where we only have a vague idea of what the result or output should look like, we can use the test command’s -match option. For example:

# fuzzy.test

package require tcltest
namespace import ::tcltest::*

test divide_by_zero {} -body {

    expr {5 / 0}

} -returnCodes {error} -match regexp -result {[Dd]ivide by zero}

test open_non_existing_file {} -body {

   open zzzzz r

} -returnCodes {error} -match glob -result {*no such file or directory}

cleanupTests

Explanations:

  • Lines 6-10: this is the same test we did in part 6.
  • Line 10: if for some reason, we don’t know if the error message (result) is “Divide by zero” (with capital ‘D’) or “divide by zero”. To deal with this situation, we use the -match regexp option and supply a regular expression for the result.
  • Line 16: similarly, in this test, we use the -match glob option, in which the syntax follows the glob command. For more information, please consult the Tcl’s glob command.
  • Besides regexp and glob, the -match option also allows a third option: exact, which requires the exact output. Since this is the default option, we normally do not specify it.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s