1
0

dictionary_test.tcl 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. #
  2. # Unit tests for dictionary.tcl
  3. #
  4. # Dummy some eggdrop functions.
  5. proc ::bind {a b c d} {}
  6. proc ::setudef {a b} {}
  7. proc ::putlog {s} {}
  8. source dictionary.tcl
  9. proc ::tests {} {
  10. puts "Running tests..."
  11. set success 1
  12. if {![::test_quotemeta]} {
  13. set success 0
  14. }
  15. if {![::test_string_contains_term]} {
  16. set success 0
  17. }
  18. if {![::test_is_addressing_bot]} {
  19. set success 0
  20. }
  21. if {$success} {
  22. puts "Success!"
  23. } else {
  24. puts "Failure."
  25. exit 1
  26. }
  27. }
  28. proc ::test_quotemeta {} {
  29. set tests [list \
  30. [dict create input hi! output hi\\!] \
  31. [dict create input hi output hi] \
  32. [dict create input hi*+ output hi\\*\\+] \
  33. [dict create input hi\{\}\\ output hi\\\{\\\}\\\\] \
  34. ]
  35. set failed 0
  36. foreach test $tests {
  37. set output [::dictionary::quotemeta [dict get $test input]]
  38. if {$output != [dict get $test output]} {
  39. puts [format "FAILURE: quotemeta(%s) = %s, wanted %s" \
  40. [dict get $test input] $output [dict get $test output]]
  41. incr failed
  42. }
  43. }
  44. if {$failed != 0} {
  45. puts [format "quotemeta: %d/%d tests failed" $failed [llength $tests]]
  46. }
  47. return [expr $failed == 0]
  48. }
  49. proc ::test_string_contains_term {} {
  50. set tests [list \
  51. [dict create s "hi test hi" term "test" want 1] \
  52. [dict create s "hi testing hi" term "test" want 0] \
  53. [dict create s "hi test, hi" term "test" want 1] \
  54. [dict create s "hi test. hi" term "test" want 1] \
  55. [dict create s "test" term "test" want 1] \
  56. [dict create s "hi test" term "test" want 1] \
  57. [dict create s "test hi" term "test" want 1] \
  58. [dict create s "test hi" term "TEST" want 1] \
  59. [dict create s "TEST hi" term "test" want 1] \
  60. ]
  61. set failed 0
  62. foreach test $tests {
  63. set s [dict get $test s]
  64. set term [dict get $test term]
  65. set want [dict get $test want]
  66. set output [::dictionary::string_contains_term $s $term]
  67. if {$output != $want} {
  68. puts [format "FAILURE: string_contains_term(\"%s\", \"%s\") = %d, wanted %d" \
  69. $s $term $output $want]
  70. incr failed
  71. }
  72. }
  73. if {$failed != 0} {
  74. puts [format "string_contains_term: %d/%d tests failed" $failed \
  75. [llength $tests]]
  76. }
  77. return [expr $failed == 0]
  78. }
  79. proc ::test_is_addressing_bot {} {
  80. set tests [list \
  81. [dict create line "bot: hi" botnick "bot" want 1] \
  82. [dict create line "BOT: hi" botnick "bot" want 1] \
  83. [dict create line "bot: hi" botnick "BOT" want 1] \
  84. [dict create line "bot:hi" botnick "BOT" want 1] \
  85. [dict create line "bot hi" botnick "bot" want 0] \
  86. [dict create line "bot2: hi" botnick "bot" want 0] \
  87. [dict create line ": hi" botnick "bot" want 0] \
  88. [dict create line "hi bot: hi" botnick "bot" want 0] \
  89. [dict create line "bbot: hi" botnick "bot" want 0] \
  90. [dict create line "botbot: hi" botnick "bot" want 0] \
  91. ]
  92. set failed 0
  93. foreach test $tests {
  94. set line [dict get $test line]
  95. set botnick [dict get $test botnick]
  96. set want [dict get $test want]
  97. set output [::dictionary::is_addressing_bot $line $botnick]
  98. if {$output != $want} {
  99. puts [format "FAILURE: is_addressing_bot(\"%s\", \"%s\") = %d, wanted %d" \
  100. $s $line $botnick $want]
  101. incr failed
  102. }
  103. }
  104. if {$failed != 0} {
  105. puts [format "string_contains_term: %d/%d tests failed" $failed \
  106. [llength $tests]]
  107. }
  108. return [expr $failed == 0]
  109. }
  110. ::tests