testing.odin 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. package testing
  2. import "core:fmt"
  3. import "core:io"
  4. // IMPORTANT NOTE: Compiler requires this layout
  5. Test_Signature :: proc(^T);
  6. // IMPORTANT NOTE: Compiler requires this layout
  7. Internal_Test :: struct {
  8. pkg: string,
  9. name: string,
  10. p: Test_Signature,
  11. }
  12. Internal_Cleanup :: struct {
  13. procedure: proc(rawptr),
  14. user_data: rawptr,
  15. }
  16. T :: struct {
  17. error_count: int,
  18. w: io.Writer,
  19. cleanups: [dynamic]Internal_Cleanup,
  20. }
  21. error :: proc(t: ^T, args: ..any, loc := #caller_location) {
  22. log(t=t, args=args, loc=loc);
  23. t.error_count += 1;
  24. }
  25. errorf :: proc(t: ^T, format: string, args: ..any, loc := #caller_location) {
  26. logf(t=t, format=format, args=args, loc=loc);
  27. t.error_count += 1;
  28. }
  29. fail :: proc(t: ^T) {
  30. error(t, "FAIL");
  31. t.error_count += 1;
  32. }
  33. failed :: proc(t: ^T) -> bool {
  34. return t.error_count != 0;
  35. }
  36. log :: proc(t: ^T, args: ..any, loc := #caller_location) {
  37. fmt.wprintln(t.w, ..args);
  38. }
  39. logf :: proc(t: ^T, format: string, args: ..any, loc := #caller_location) {
  40. fmt.wprintf(t.w, format, ..args);
  41. fmt.wprintln(t.w);
  42. }
  43. // cleanup registers a procedure and user_data, which will be called when the test, and all its subtests, complete
  44. // cleanup proceduers will be called in LIFO (last added, first called) order.
  45. cleanup :: proc(t: ^T, procedure: proc(rawptr), user_data: rawptr) {
  46. append(&t.cleanups, Internal_Cleanup{procedure, user_data});
  47. }
  48. expect :: proc(t: ^T, ok: bool, msg: string = "", loc := #caller_location) -> bool {
  49. if !ok {
  50. error(t=t, args={msg}, loc=loc);
  51. }
  52. return ok;
  53. }