testing.odin 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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: proc() -> !,
  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 :: proc(t: ^T, msg := "", loc := #caller_location) {
  53. if msg != "" {
  54. pkg_log.error("FAIL:", msg, location=loc)
  55. } else {
  56. pkg_log.error("FAIL", location=loc)
  57. }
  58. if t._fail_now != nil {
  59. t._fail_now()
  60. }
  61. }
  62. failed :: proc(t: ^T) -> bool {
  63. return t.error_count != 0
  64. }
  65. @(deprecated="prefer `log.info`")
  66. log :: proc(t: ^T, args: ..any, loc := #caller_location) {
  67. pkg_log.info(..args, location = loc)
  68. }
  69. @(deprecated="prefer `log.infof`")
  70. logf :: proc(t: ^T, format: string, args: ..any, loc := #caller_location) {
  71. pkg_log.infof(format, ..args, location = loc)
  72. }
  73. // cleanup registers a procedure and user_data, which will be called when the test, and all its subtests, complete.
  74. // Cleanup procedures will be called in LIFO (last added, first called) order.
  75. // Each procedure will use a copy of the context at the time of registering.
  76. cleanup :: proc(t: ^T, procedure: proc(rawptr), user_data: rawptr) {
  77. append(&t.cleanups, Internal_Cleanup{procedure, user_data, context})
  78. }
  79. expect :: proc(t: ^T, ok: bool, msg: string = "", loc := #caller_location) -> bool {
  80. if !ok {
  81. pkg_log.error(msg, location=loc)
  82. }
  83. return ok
  84. }
  85. expectf :: proc(t: ^T, ok: bool, format: string, args: ..any, loc := #caller_location) -> bool {
  86. if !ok {
  87. pkg_log.errorf(format, ..args, location=loc)
  88. }
  89. return ok
  90. }
  91. expect_value :: proc(t: ^T, value, expected: $T, loc := #caller_location) -> bool where intrinsics.type_is_comparable(T) {
  92. ok := value == expected || reflect.is_nil(value) && reflect.is_nil(expected)
  93. if !ok {
  94. pkg_log.errorf("expected %v, got %v", expected, value, location=loc)
  95. }
  96. return ok
  97. }
  98. set_fail_timeout :: proc(t: ^T, duration: time.Duration, loc := #caller_location) {
  99. chan.send(t.channel, Event_Set_Fail_Timeout {
  100. at_time = time.time_add(time.now(), duration),
  101. location = loc,
  102. })
  103. }