dictionary_test.tcl 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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 {$success} {
  19. puts "Success!"
  20. } else {
  21. puts "Failure."
  22. exit 1
  23. }
  24. }
  25. proc ::test_quotemeta {} {
  26. set tests [list \
  27. [dict create input hi! output hi\\!] \
  28. [dict create input hi output hi] \
  29. [dict create input hi*+ output hi\\*\\+] \
  30. [dict create input hi\{\}\\ output hi\\\{\\\}\\\\] \
  31. ]
  32. set failed 0
  33. foreach test $tests {
  34. set output [::dictionary::quotemeta [dict get $test input]]
  35. if {$output != [dict get $test output]} {
  36. puts [format "FAILURE: quotemeta(%s) = %s, wanted %s" \
  37. [dict get $test input] $output [dict get $test output]]
  38. incr failed
  39. }
  40. }
  41. if {$failed != 0} {
  42. puts [format "quotemeta: %d/%d tests failed" $failed [llength $tests]]
  43. }
  44. return [expr $failed == 0]
  45. }
  46. proc ::test_string_contains_term {} {
  47. set tests [list \
  48. [dict create s "hi test hi" term "test" want 1] \
  49. [dict create s "hi testing hi" term "test" want 0] \
  50. [dict create s "hi test, hi" term "test" want 1] \
  51. [dict create s "hi test. hi" term "test" want 1] \
  52. [dict create s "test" term "test" want 1] \
  53. [dict create s "hi test" term "test" want 1] \
  54. [dict create s "test hi" term "test" want 1] \
  55. [dict create s "test hi" term "TEST" want 1] \
  56. [dict create s "TEST hi" term "test" want 1] \
  57. ]
  58. set failed 0
  59. foreach test $tests {
  60. set s [dict get $test s]
  61. set term [dict get $test term]
  62. set want [dict get $test want]
  63. set output [::dictionary::string_contains_term $s $term]
  64. if {$output != $want} {
  65. puts [format "FAILURE: string_contains_term(\"%s\", \"%s\") = %d, wanted %d" \
  66. $s $term $output $want]
  67. incr failed
  68. }
  69. }
  70. if {$failed != 0} {
  71. puts [format "string_contains_term: %d/%d tests failed" $failed \
  72. [llength $tests]]
  73. }
  74. return [expr $failed == 0]
  75. }
  76. ::tests