testing.odin 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. package testing
  2. import "base:intrinsics"
  3. import "base:runtime"
  4. import pkg_log "core:log"
  5. import "core:reflect"
  6. import "core:sync/chan"
  7. import "core:time"
  8. _ :: reflect // alias reflect to nothing to force visibility for -vet
  9. // IMPORTANT NOTE: Compiler requires this layout
  10. Test_Signature :: proc(^T)
  11. // IMPORTANT NOTE: Compiler requires this layout
  12. Internal_Test :: struct {
  13. pkg: string,
  14. name: string,
  15. p: Test_Signature,
  16. }
  17. Internal_Cleanup :: struct {
  18. procedure: proc(rawptr),
  19. user_data: rawptr,
  20. ctx: runtime.Context,
  21. }
  22. T :: struct {
  23. error_count: int,
  24. // If your test needs to perform random operations, it's advised to use
  25. // this value to seed a local random number generator rather than relying
  26. // on the non-thread-safe global one.
  27. //
  28. // This way, your results will be deterministic.
  29. //
  30. // This value is chosen at startup of the test runner, logged, and may be
  31. // specified by the user. It is the same for all tests of a single run.
  32. seed: u64,
  33. channel: Update_Channel_Sender,
  34. cleanups: [dynamic]Internal_Cleanup,
  35. // This allocator is shared between the test runner and its threads for
  36. // cloning log strings, so they can outlive the lifetime of individual
  37. // tests during channel transmission.
  38. _log_allocator: runtime.Allocator,
  39. _fail_now_called: bool,
  40. }
  41. @(deprecated="prefer `log.error`")
  42. error :: proc(t: ^T, args: ..any, loc := #caller_location) {
  43. pkg_log.error(..args, location = loc)
  44. }
  45. @(deprecated="prefer `log.errorf`")
  46. errorf :: proc(t: ^T, format: string, args: ..any, loc := #caller_location) {
  47. pkg_log.errorf(format, ..args, location = loc)
  48. }
  49. fail :: proc(t: ^T, loc := #caller_location) {
  50. pkg_log.error("FAIL", location=loc)
  51. }
  52. // fail_now will cause a test to immediately fail and abort, much in the same
  53. // way a failed assertion or panic call will stop a thread.
  54. //
  55. // It is for when you absolutely need a test to fail without calling any of its
  56. // deferred statements. It will be cleaner than a regular assert or panic,
  57. // as the test runner will know to expect the signal this procedure will raise.
  58. fail_now :: proc(t: ^T, msg := "", loc := #caller_location) -> ! {
  59. t._fail_now_called = true
  60. if msg != "" {
  61. pkg_log.error("FAIL:", msg, location=loc)
  62. } else {
  63. pkg_log.error("FAIL", location=loc)
  64. }
  65. runtime.trap()
  66. }
  67. failed :: proc(t: ^T) -> bool {
  68. return t.error_count != 0
  69. }
  70. @(deprecated="prefer `log.info`")
  71. log :: proc(t: ^T, args: ..any, loc := #caller_location) {
  72. pkg_log.info(..args, location = loc)
  73. }
  74. @(deprecated="prefer `log.infof`")
  75. logf :: proc(t: ^T, format: string, args: ..any, loc := #caller_location) {
  76. pkg_log.infof(format, ..args, location = loc)
  77. }
  78. // cleanup registers a procedure and user_data, which will be called when the test, and all its subtests, complete.
  79. // Cleanup procedures will be called in LIFO (last added, first called) order.
  80. //
  81. // Each procedure will use a copy of the context at the time of registering,
  82. // and if the test failed due to a timeout, failed assertion, panic, bounds-checking error,
  83. // memory access violation, or any other signal-based fault, this procedure will
  84. // run with greater privilege in the test runner's main thread.
  85. //
  86. // That means that any cleanup procedure absolutely must not fail in the same way,
  87. // or it will take down the entire test runner with it. This is for when you
  88. // need something to run no matter what, if a test failed.
  89. //
  90. // For almost every usual case, `defer` should be preferable and sufficient.
  91. cleanup :: proc(t: ^T, procedure: proc(rawptr), user_data: rawptr) {
  92. append(&t.cleanups, Internal_Cleanup{procedure, user_data, context})
  93. }
  94. expect :: proc(t: ^T, ok: bool, msg: string = "", loc := #caller_location) -> bool {
  95. if !ok {
  96. pkg_log.error(msg, location=loc)
  97. }
  98. return ok
  99. }
  100. expectf :: proc(t: ^T, ok: bool, format: string, args: ..any, loc := #caller_location) -> bool {
  101. if !ok {
  102. pkg_log.errorf(format, ..args, location=loc)
  103. }
  104. return ok
  105. }
  106. expect_value :: proc(t: ^T, value, expected: $T, loc := #caller_location) -> bool where intrinsics.type_is_comparable(T) {
  107. ok := value == expected || reflect.is_nil(value) && reflect.is_nil(expected)
  108. if !ok {
  109. pkg_log.errorf("expected %v, got %v", expected, value, location=loc)
  110. }
  111. return ok
  112. }
  113. set_fail_timeout :: proc(t: ^T, duration: time.Duration, loc := #caller_location) {
  114. chan.send(t.channel, Event_Set_Fail_Timeout {
  115. at_time = time.time_add(time.now(), duration),
  116. location = loc,
  117. })
  118. }