/TUnit.test/resource/TUnitProjectTest2/t-unit/assertEquals.tcl

http://t-unit.googlecode.com/ · TCL · 139 lines · 39 code · 9 blank · 91 comment · 13 complexity · 7e3720146416fe9631139f21d4c2dbf3 MD5 · raw file

  1. ## ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  2. ##
  3. ## File Name: assertEquals.tcl
  4. ## @short Check if the arguments passed in are indeed equivalent
  5. ## @author Original author ~ Joe Boyle
  6. ## @date Original date ~ 15-Feb-2006
  7. ## @version 1.0 Initial release
  8. ##
  9. ## @comment This source file provides functionality to evaluate two Tcl
  10. ## @c expressions and decide if the results of the evaluations
  11. ## @c are 100% equal. The arguments are both assumed to be
  12. ## @c evaluable expressions. To be considered equal, the results
  13. ## @c must be of equal type and value; i.e., "2" is not considered
  14. ## @c equal to "2.0".
  15. ##
  16. ## @note See procedure definition header below for more details.
  17. ##
  18. ## ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  19. ##
  20. ## Revision history
  21. ## ----------------
  22. ##
  23. ## Rev Rev. Date Released by: Revision Description
  24. ## ----- ----------- ------------ --------------------------------
  25. ## version 1.0-- 15-Feb-2006 Joe Boyle Initial version written
  26. ##
  27. ## ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  28. ## NO MODULE GLOBAL VARIABLES
  29. ## ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  30. proc t-unit::assertEquals { expression1 expression2 {errorString ""} } {
  31. ## ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  32. ## Proc Name: assertEquals
  33. ## @short Check if the arguments passed in are indeed equal to each other
  34. ## @argument expression1 First expression to evaluate
  35. ## @a expression2 Second expression to evaluate
  36. ## @a errorString Optional string literal specifying error message
  37. ## @result returns "OK" on success, error string on failure
  38. ## @example set errVal [t-unit::assertEquals this that "Error: Not equal"]
  39. ##
  40. ## @comment This module file provides functionality to evaluate two Tcl
  41. ## @c expressions and decide if the results of the evaluations
  42. ## @c compare exactly. Results must be the same data type as well
  43. ## @c as the same value. For example, comparing the results of
  44. ## @c expressions evaluating to the integer "2" and the string "2"
  45. ## @c are not considered equal.&p&p
  46. ## @c
  47. ## @c Note that this procedure is not intended to replace the normal
  48. ## @c comparison of, for example, two integers. If you want to
  49. ## @c check if 2 == 2, you'd still do it that way. This procedure
  50. ## @c is intended to check two PROCEDURES, or EXPRESSIONS, for
  51. ## @c equality, as part of a unit test on the procedure(s).&p&p
  52. ## @c
  53. ## @c The normal usage would be to have the first argument be the
  54. ## @c expected value, with the second argument containing the name
  55. ## @c of the procedure to test.
  56. ##
  57. ## @note None.
  58. ##
  59. ## @danger None
  60. ##
  61. ## ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  62. variable result1
  63. variable result2
  64. ## ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  65. ## Check that the arguments are not empty
  66. ## ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  67. if { ("" == $expression1) || ("" == $expression2) } {
  68. set returnValue "EMPTY_ARG: Empty arguments not allowed"
  69. return $returnValue
  70. }
  71. ## ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  72. ## Check if either argument is of integer type; mustn't compare integers
  73. ## with doubles
  74. ## ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  75. if { [string is integer -strict $expression1] } {
  76. if { ![string is integer $expression2] } {
  77. set returnValue "ARG_MISMATCH: Argument types must match"
  78. return $returnValue
  79. }
  80. }
  81. if { [string is integer -strict $expression2] } {
  82. if { ![string is integer $expression1] } {
  83. set returnValue "ARG_MISMATCH: Argument types must match"
  84. return $returnValue
  85. }
  86. }
  87. ## ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  88. ## Check optional argument to properly format error return string
  89. ## ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  90. if { "" == $errorString } {
  91. set errorReturn "NOT_EQUAL: Compare failure -- not equal: "
  92. } else {
  93. set errorReturn $errorString
  94. }
  95. ## ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  96. ## Initialize the value to be returned; anticipates success
  97. ## ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  98. set returnValue "OK"
  99. ## ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  100. ## Evaluate expressions, then check if result are equal
  101. ##
  102. ## The "catch" block will catch, parse, and remove the error string from
  103. ## 'invalid command name "<command>"' which is the result of a command
  104. ## that cannot be evaluated. The resulting parse will leave only the
  105. ## <command> phrase inside the double quotation marks.
  106. ##
  107. ## The same could be done more easily with the statement
  108. ##
  109. ## set result [eval $expression]
  110. ##
  111. ## but it would fail if the expression to be evaluated contains a value
  112. ## like "1", and will return a Tcl error rather than the result we want.
  113. ## ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  114. if { [catch {[eval $expression1]} result1] } {
  115. set index [string first \" $result1]
  116. set result1 [string range $result1 [expr $index + 1] end-1]
  117. }
  118. if { [catch {[eval $expression2]} result2] } {
  119. set index [string first \" $result2]
  120. set result2 [string range $result2 [expr $index + 1] end-1]
  121. }
  122. if { $result1 ne $result2 } {
  123. set returnValue "$errorReturn $result1 $result2"
  124. }
  125. ## ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  126. ## Send back the result to the calling procedure
  127. ## ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  128. return $returnValue
  129. }