runner.odin 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. //+private
  2. package testing
  3. import "core:io"
  4. import "core:os"
  5. import "core:slice"
  6. reset_t :: proc(t: ^T) {
  7. clear(&t.cleanups);
  8. t.error_count = 0;
  9. }
  10. end_t :: proc(t: ^T) {
  11. for i := len(t.cleanups)-1; i >= 0; i -= 1 {
  12. c := t.cleanups[i];
  13. c.procedure(c.user_data);
  14. }
  15. }
  16. runner :: proc(internal_tests: []Internal_Test) -> bool {
  17. stream := os.stream_from_handle(os.stdout);
  18. w, _ := io.to_writer(stream);
  19. t := &T{};
  20. t.w = w;
  21. reserve(&t.cleanups, 1024);
  22. defer delete(t.cleanups);
  23. total_success_count := 0;
  24. total_test_count := len(internal_tests);
  25. slice.sort_by(internal_tests, proc(a, b: Internal_Test) -> bool {
  26. if a.pkg < b.pkg {
  27. return true;
  28. }
  29. return a.name < b.name;
  30. });
  31. prev_pkg := "";
  32. for it in internal_tests {
  33. if it.p == nil {
  34. total_test_count -= 1;
  35. continue;
  36. }
  37. free_all(context.temp_allocator);
  38. reset_t(t);
  39. defer end_t(t);
  40. if prev_pkg != it.pkg {
  41. prev_pkg = it.pkg;
  42. logf(t, "[Package: %s]", it.pkg);
  43. }
  44. logf(t, "[Test: %s]", it.name);
  45. // TODO(bill): Catch panics
  46. {
  47. it.p(t);
  48. }
  49. if t.error_count != 0 {
  50. logf(t, "[%s : FAILURE]", it.name);
  51. } else {
  52. logf(t, "[%s : SUCCESS]", it.name);
  53. total_success_count += 1;
  54. }
  55. }
  56. logf(t, "----------------------------------------");
  57. if total_test_count == 0 {
  58. log(t, "NO TESTS RAN");
  59. } else {
  60. logf(t, "%d/%d SUCCESSFUL", total_success_count, total_test_count);
  61. }
  62. return total_success_count == total_test_count;
  63. }