test_gerror.mod 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. MODULE test_gerror ;
  2. (*
  3. m2-GTK4 test - GError handling and ownership helpers.
  4. Triggers a real GError by deleting a non-existent file, checks the
  5. message and the GIO error domain/code, copies/frees it, then clears
  6. it with GtkUtils.ClearError. Needs no display.
  7. *)
  8. FROM GioError IMPORT g_io_error_quark, GIOErrorNotFound;
  9. FROM GError IMPORT GError, g_error_matches, g_error_copy, g_error_free;
  10. FROM GFile IMPORT g_file_new_for_path, g_file_delete;
  11. FROM GtkUtils IMPORT ErrorMessage, ClearError, Unref;
  12. FROM SYSTEM IMPORT ADDRESS, ADR;
  13. FROM libc IMPORT printf;
  14. VAR
  15. failed: BOOLEAN;
  16. PROCEDURE Check (condition: BOOLEAN; what: ARRAY OF CHAR);
  17. BEGIN
  18. IF NOT condition THEN
  19. printf("FAIL: %s\n", what);
  20. failed := TRUE
  21. END
  22. END Check;
  23. VAR
  24. file: ADDRESS;
  25. err, copy: GError;
  26. buf: ARRAY [0..255] OF CHAR;
  27. BEGIN
  28. failed := FALSE;
  29. file := g_file_new_for_path("/no/such/m2gtk4/error/path");
  30. err := NIL;
  31. IF g_file_delete(file, NIL, ADR(err)) # 0 THEN
  32. Check(FALSE, "delete unexpectedly succeeded")
  33. END;
  34. Check(err # NIL, "error was set");
  35. IF err # NIL THEN
  36. ErrorMessage(err, buf);
  37. Check(buf[0] # 0C, "error message non-empty");
  38. Check(g_error_matches(err, g_io_error_quark(), GIOErrorNotFound) # 0,
  39. "error matches GIO_ERROR_NOT_FOUND");
  40. copy := g_error_copy(err);
  41. Check(copy # NIL, "g_error_copy");
  42. IF copy # NIL THEN g_error_free(copy) END;
  43. ClearError(err);
  44. Check(err = NIL, "ClearError set the variable to NIL")
  45. END;
  46. Unref(file);
  47. IF failed THEN
  48. printf("test_gerror: FAIL\n");
  49. HALT(1)
  50. ELSE
  51. printf("test_gerror: PASS\n");
  52. HALT(0)
  53. END
  54. END test_gerror.