assert.ml 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. (*
  2. Utility methods built so I can get verifications in a
  3. 'Expected %s but result %s' format. These are heavily based off of Java's
  4. JUnit library, specifically the Assert class.
  5. @author: Ben Jenkins
  6. *)
  7. (* Useful toggle for debugging if tests aren't working as expected. *)
  8. let print_true_conditions : bool = false ;;
  9. let red_text_code : string = "\027[31m" ;;
  10. let string_of_list (string_of_a : 'a -> string) (lst : 'a list) : string =
  11. let str_lst = List.map string_of_a lst in
  12. "[" ^ (String.concat "; " str_lst) ^ "]" ;;
  13. let assert_strings (condition : bool)
  14. (test_name : string)
  15. (expected : string)
  16. (result : string)
  17. : 'a =
  18. (* Turns text red in linux terminal. *)
  19. if not condition then (
  20. Printf.fprintf stdout
  21. "%sTest '%s' failed. Expected: '%s', but actual: '%s'\n"
  22. red_text_code test_name expected result ;
  23. assert condition
  24. )
  25. else if print_true_conditions then (
  26. Printf.fprintf stdout
  27. "Test '%s' passed. Value: '%s'\n" test_name expected ;
  28. assert condition
  29. )
  30. ;;
  31. let assert_cmp (test_name : string)
  32. (cmp : 'a -> 'a -> bool)
  33. (string_of_a : 'a -> string)
  34. (expected : 'a)
  35. (result : 'a)
  36. : 'b =
  37. let condition = cmp expected result in
  38. let expected_display = string_of_a expected in
  39. let result_display = string_of_a result in
  40. assert_strings condition test_name expected_display result_display ;;
  41. (* Asserts that two objects are equal. *)
  42. let assert_equals (test_name : string)
  43. (string_of_a : 'a -> string)
  44. (expected : 'a)
  45. (result : 'a)
  46. : 'b =
  47. assert_cmp test_name (=) string_of_a expected result ;;
  48. let assert_equals_int (test_name : string)
  49. (expected : int)
  50. (result : int)
  51. : 'a =
  52. assert_equals test_name string_of_int expected result ;;
  53. let assert_equals_int_list (test_name : string)
  54. (expected : int list)
  55. (result : int list)
  56. : 'a =
  57. assert_equals test_name (string_of_list string_of_int) expected result ;;
  58. let assert_equals_char (test_name : string)
  59. (expected : char)
  60. (result : char)
  61. : 'a =
  62. let string_of_char = fun my_char -> String.make 1 my_char in
  63. assert_equals test_name string_of_char expected result ;;
  64. let assert_equals_char_list (test_name : string)
  65. (expected : char list)
  66. (result : char list)
  67. : 'a =
  68. let string_of_char = fun my_char -> String.make 1 my_char in
  69. assert_equals test_name (string_of_list string_of_char) expected result ;;
  70. let assert_equals_bool (test_name : string)
  71. (expected : bool)
  72. (result : bool)
  73. : 'a =
  74. assert_equals test_name string_of_bool expected result ;;
  75. let assert_equals_bool_list (test_name : string)
  76. (expected : bool list)
  77. (result : bool list)
  78. : 'a =
  79. assert_equals test_name (string_of_list string_of_bool) expected result ;;
  80. let assert_equals_string (test_name : string)
  81. (expected : string)
  82. (result : string)
  83. : 'a =
  84. assert_strings (expected = result) test_name expected result ;;
  85. let assert_equals_string_list (test_name : string)
  86. (expected : string list)
  87. (result : string list)
  88. : 'a =
  89. assert_equals test_name (string_of_list (fun x -> x)) expected result ;;
  90. let assert_equals_float (test_name : string)
  91. (expected : float)
  92. (result : float)
  93. (epsilon : float)
  94. : 'a =
  95. let condition = (abs_float (expected -. result)) < epsilon in
  96. let expected_display = string_of_float expected in
  97. let result_display = string_of_float result in
  98. assert_strings condition test_name expected_display result_display ;;
  99. (* Asserts that two objects refer to the same object. *)
  100. let assert_same (test_name : string)
  101. (string_of_a : 'a -> string)
  102. (expected : 'a)
  103. (result : 'a)
  104. : 'b =
  105. assert_cmp test_name (==) string_of_a expected result ;;
  106. let assert_same_int (test_name : string) (expected : int) (result : int) : 'a =
  107. assert_same test_name string_of_int expected result ;;
  108. let assert_same_float (test_name : string)
  109. (expected : float)
  110. (result : float)
  111. : 'a =
  112. assert_same test_name string_of_float expected result ;;
  113. let assert_same_char (test_name : string)
  114. (expected : char)
  115. (result : char)
  116. : 'a =
  117. let string_of_char = fun my_char -> String.make 1 my_char in
  118. assert_same test_name string_of_char expected result ;;
  119. let assert_same_bool (test_name : string)
  120. (expected : bool)
  121. (result : bool)
  122. : 'a =
  123. assert_same test_name string_of_bool expected result ;;
  124. let assert_same_string (test_name : string)
  125. (expected : string)
  126. (result : string)
  127. : 'a =
  128. assert_strings (expected == result) test_name expected result ;;
  129. (* Asserts that a condition is true. *)
  130. let assert_true (test_name : string) (result : bool) : 'a =
  131. assert_equals_bool test_name true result ;;
  132. (* Asserts that a condition is false. *)
  133. let assert_false (test_name : string) (result : bool) : 'a =
  134. assert_equals_bool test_name false result ;;
  135. (* Fails a test with the given message. *)
  136. let fail (test_name : string) (message : string) : 'a =
  137. Printf.fprintf stdout "%sTest '%s' failed. '%s'\n"
  138. red_text_code test_name message ;;
  139. let assert_none (test_name : string) (opt : float option) : 'a =
  140. match opt with
  141. | Some _x -> Printf.fprintf stdout
  142. "%sTest '%s' failed. Expected: 'None', but actual: 'Some'\n"
  143. red_text_code test_name ;
  144. assert false
  145. | None -> if print_true_conditions then
  146. Printf.fprintf stdout "Test '%s' passed. Value: 'None'\n"
  147. test_name ;
  148. assert true
  149. ;;