summaryrefslogtreecommitdiff
path: root/tests/support/test.tcl
blob: e801e1f228ea91946dd8d68ae4bfd722bcdcf52a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
set ::passed 0
set ::failed 0
set ::testnum 0

proc assert {condition} {
    if {![uplevel 1 expr $condition]} {
        puts "!! ERROR\nExpected '$value' to evaluate to true"
        error "assertion"
    }
}

proc assert_match {pattern value} {
    if {![string match $pattern $value]} {
        puts "!! ERROR\nExpected '$value' to match '$pattern'"
        error "assertion"
    }
}

proc assert_equal {expected value} {
    if {$expected ne $value} {
        puts "!! ERROR\nExpected '$value' to be equal to '$expected'"
        error "assertion"
    }
}

proc assert_error {pattern code} {
    if {[catch {uplevel 1 $code} error]} {
        assert_match $pattern $error
    } else {
        puts "!! ERROR\nExpected an error but nothing was catched"
        error "assertion"
    }
}

proc assert_encoding {enc key} {
    # Swapped out values don't have an encoding, so make sure that
    # the value is swapped in before checking the encoding.
    set dbg [r debug object $key]
    while {[string match "* swapped at:*" $dbg]} {
        r debug swapin $key
        set dbg [r debug object $key]
    }
    assert_match "* encoding:$enc *" $dbg
}

proc assert_type {type key} {
    assert_equal $type [r type $key]
}

proc test {name code {okpattern notspecified}} {
    # abort if tagged with a tag to deny
    foreach tag $::denytags {
        if {[lsearch $::tags $tag] >= 0} {
            return
        }
    }

    # check if tagged with at least 1 tag to allow when there *is* a list
    # of tags to allow, because default policy is to run everything
    if {[llength $::allowtags] > 0} {
        set matched 0
        foreach tag $::allowtags {
            if {[lsearch $::tags $tag] >= 0} {
                incr matched
            }
        }
        if {$matched < 1} {
            return
        }
    }

    incr ::testnum
    puts -nonewline [format "#%03d %-68s " $::testnum $name]
    flush stdout
    if {[catch {set retval [uplevel 1 $code]} error]} {
        if {$error eq "assertion"} {
            incr ::failed
        } else {
            puts "EXCEPTION"
            puts "\nCaught error: $error"
            error "exception"
        }
    } else {
        if {$okpattern eq "notspecified" || $okpattern eq $retval || [string match $okpattern $retval]} {
            puts "PASSED"
            incr ::passed
        } else {
            puts "!! ERROR expected\n'$okpattern'\nbut got\n'$retval'"
            incr ::failed
        }
    }
    if {$::traceleaks} {
        set output [exec leaks redis-server]
        if {![string match {*0 leaks*} $output]} {
            puts "--------- Test $::testnum LEAKED! --------"
            puts $output
            exit 1
        }
    }
}