assert.ml 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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) (test_name : string) (expected : string)
  14. (result : string) : 'a =
  15. (* Turns text red in linux terminal. *)
  16. if not condition then (
  17. Printf.fprintf stdout
  18. "%sTest '%s' failed. Expected: '%s', but actual: '%s'\n" red_text_code
  19. test_name expected result;
  20. assert condition)
  21. else if print_true_conditions then (
  22. Printf.fprintf stdout "Test '%s' passed. Value: '%s'\n" test_name expected;
  23. assert condition)
  24. let assert_cmp (test_name : string) (cmp : 'a -> 'a -> bool)
  25. (string_of_a : 'a -> string) (expected : 'a) (result : 'a) : 'b =
  26. let condition = cmp expected result in
  27. let expected_display = string_of_a expected in
  28. let result_display = string_of_a result in
  29. assert_strings condition test_name expected_display result_display
  30. (* Asserts that two objects are equal. *)
  31. let assert_equals (test_name : string) (string_of_a : 'a -> string)
  32. (expected : 'a) (result : 'a) : 'b =
  33. assert_cmp test_name ( = ) string_of_a expected result
  34. let assert_equals_int (test_name : string) (expected : int) (result : int) : 'a
  35. =
  36. assert_equals test_name string_of_int expected result
  37. let assert_equals_int_list (test_name : string) (expected : int list)
  38. (result : int list) : 'a =
  39. assert_equals test_name (string_of_list string_of_int) expected result
  40. let assert_equals_char (test_name : string) (expected : char) (result : char) :
  41. 'a =
  42. let string_of_char my_char = String.make 1 my_char in
  43. assert_equals test_name string_of_char expected result
  44. let assert_equals_char_list (test_name : string) (expected : char list)
  45. (result : char list) : 'a =
  46. let string_of_char my_char = String.make 1 my_char in
  47. assert_equals test_name (string_of_list string_of_char) expected result
  48. let assert_equals_bool (test_name : string) (expected : bool) (result : bool) :
  49. 'a =
  50. assert_equals test_name string_of_bool expected result
  51. let assert_equals_bool_list (test_name : string) (expected : bool list)
  52. (result : bool list) : 'a =
  53. assert_equals test_name (string_of_list string_of_bool) expected result
  54. let assert_equals_string (test_name : string) (expected : string)
  55. (result : string) : 'a =
  56. assert_strings (expected = result) test_name expected result
  57. let assert_equals_string_list (test_name : string) (expected : string list)
  58. (result : string list) : 'a =
  59. assert_equals test_name (string_of_list (fun x -> x)) expected result
  60. let assert_equals_float (test_name : string) (expected : float) (result : float)
  61. (epsilon : float) : 'a =
  62. let condition = abs_float (expected -. result) < epsilon in
  63. let expected_display = string_of_float expected in
  64. let result_display = string_of_float result in
  65. assert_strings condition test_name expected_display result_display
  66. (* Asserts that two objects refer to the same object. *)
  67. let assert_same (test_name : string) (string_of_a : 'a -> string)
  68. (expected : 'a) (result : 'a) : 'b =
  69. assert_cmp test_name ( == ) string_of_a expected result
  70. let assert_same_int (test_name : string) (expected : int) (result : int) : 'a =
  71. assert_same test_name string_of_int expected result
  72. let assert_same_float (test_name : string) (expected : float) (result : float) :
  73. 'a =
  74. assert_same test_name string_of_float expected result
  75. let assert_same_char (test_name : string) (expected : char) (result : char) : 'a
  76. =
  77. let string_of_char my_char = String.make 1 my_char in
  78. assert_same test_name string_of_char expected result
  79. let assert_same_bool (test_name : string) (expected : bool) (result : bool) : 'a
  80. =
  81. assert_same test_name string_of_bool expected result
  82. let assert_same_string (test_name : string) (expected : string)
  83. (result : string) : 'a =
  84. assert_strings (expected == result) test_name expected result
  85. (* Asserts that a condition is true. *)
  86. let assert_true (test_name : string) (result : bool) : 'a =
  87. assert_equals_bool test_name true result
  88. (* Asserts that a condition is false. *)
  89. let assert_false (test_name : string) (result : bool) : 'a =
  90. assert_equals_bool test_name false result
  91. (* Fails a test with the given message. *)
  92. let fail (test_name : string) (message : string) : 'a =
  93. Printf.fprintf stdout "%sTest '%s' failed. '%s'\n" red_text_code test_name
  94. message
  95. let assert_none (test_name : string) (opt : float option) : 'a =
  96. match opt with
  97. | Some _x ->
  98. Printf.fprintf stdout
  99. "%sTest '%s' failed. Expected: 'None', but actual: 'Some'\n"
  100. red_text_code test_name;
  101. assert false
  102. | None ->
  103. if print_true_conditions then
  104. Printf.fprintf stdout "Test '%s' passed. Value: 'None'\n" test_name;
  105. assert true