-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathdictionary_test.tcl
139 lines (110 loc) · 3.19 KB
/
dictionary_test.tcl
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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
#
# Unit tests for dictionary.tcl
#
# Dummy some eggdrop functions.
proc ::bind {a b c d} {}
proc ::setudef {a b} {}
proc ::putlog {s} {}
source dictionary.tcl
proc ::tests {} {
puts "Running tests..."
set success 1
if {![::test_quotemeta]} {
set success 0
}
if {![::test_string_contains_term]} {
set success 0
}
if {![::test_is_addressing_bot]} {
set success 0
}
if {$success} {
puts "Success!"
} else {
puts "Failure."
exit 1
}
}
proc ::test_quotemeta {} {
set tests [list \
[dict create input hi! output hi\\!] \
[dict create input hi output hi] \
[dict create input hi*+ output hi\\*\\+] \
[dict create input hi\{\}\\ output hi\\\{\\\}\\\\] \
]
set failed 0
foreach test $tests {
set output [::dictionary::quotemeta [dict get $test input]]
if {$output != [dict get $test output]} {
puts [format "FAILURE: quotemeta(%s) = %s, wanted %s" \
[dict get $test input] $output [dict get $test output]]
incr failed
}
}
if {$failed != 0} {
puts [format "quotemeta: %d/%d tests failed" $failed [llength $tests]]
}
return [expr $failed == 0]
}
proc ::test_string_contains_term {} {
set tests [list \
[dict create s "hi test hi" term "test" want 1] \
[dict create s "hi testing hi" term "test" want 0] \
[dict create s "hi test, hi" term "test" want 1] \
[dict create s "hi test. hi" term "test" want 1] \
[dict create s "test" term "test" want 1] \
[dict create s "hi test" term "test" want 1] \
[dict create s "test hi" term "test" want 1] \
[dict create s "test hi" term "TEST" want 1] \
[dict create s "TEST hi" term "test" want 1] \
]
set failed 0
foreach test $tests {
set s [dict get $test s]
set term [dict get $test term]
set want [dict get $test want]
set output [::dictionary::string_contains_term $s $term]
if {$output != $want} {
puts [format "FAILURE: string_contains_term(\"%s\", \"%s\") = %d, wanted %d" \
$s $term $output $want]
incr failed
}
}
if {$failed != 0} {
puts [format "string_contains_term: %d/%d tests failed" $failed \
[llength $tests]]
}
return [expr $failed == 0]
}
proc ::test_is_addressing_bot {} {
set tests [list \
[dict create line "bot: hi" botnick "bot" want 1] \
[dict create line "BOT: hi" botnick "bot" want 1] \
[dict create line "bot: hi" botnick "BOT" want 1] \
[dict create line "bot:hi" botnick "BOT" want 1] \
[dict create line "bot hi" botnick "bot" want 0] \
[dict create line "bot2: hi" botnick "bot" want 0] \
[dict create line ": hi" botnick "bot" want 0] \
[dict create line "hi bot: hi" botnick "bot" want 0] \
[dict create line "bbot: hi" botnick "bot" want 0] \
[dict create line "botbot: hi" botnick "bot" want 0] \
]
set failed 0
foreach test $tests {
set line [dict get $test line]
set botnick [dict get $test botnick]
set want [dict get $test want]
set output [::dictionary::is_addressing_bot $line $botnick]
if {$output != $want} {
puts [format "FAILURE: is_addressing_bot(\"%s\", \"%s\") = %d, wanted %d" \
$s $line $botnick $want]
incr failed
}
}
if {$failed != 0} {
puts [format "string_contains_term: %d/%d tests failed" $failed \
[llength $tests]]
}
return [expr $failed == 0]
}
::tests