/services/sync/tests/unit/test_utils_catch.js

http://github.com/zpao/v8monkey · JavaScript · 86 lines · 77 code · 9 blank · 0 comment · 7 complexity · dc922aaf943e3c7062a63e7a34eeffd8 MD5 · raw file

  1. Cu.import("resource://services-sync/util.js");
  2. Cu.import("resource://services-sync/service.js");
  3. function run_test() {
  4. _("Make sure catch when copied to an object will correctly catch stuff");
  5. let ret, rightThis, didCall, didThrow, wasTen, wasLocked;
  6. let obj = {
  7. catch: Utils.catch,
  8. _log: {
  9. debug: function(str) {
  10. didThrow = str.search(/^Exception: /) == 0;
  11. },
  12. info: function(str) {
  13. wasLocked = str.indexOf("Cannot start sync: already syncing?") == 0;
  14. }
  15. },
  16. func: function() this.catch(function() {
  17. rightThis = this == obj;
  18. didCall = true;
  19. return 5;
  20. })(),
  21. throwy: function() this.catch(function() {
  22. rightThis = this == obj;
  23. didCall = true;
  24. throw 10;
  25. })(),
  26. callbacky: function() this.catch(function() {
  27. rightThis = this == obj;
  28. didCall = true;
  29. throw 10;
  30. }, function(ex) {
  31. wasTen = (ex == 10)
  32. })(),
  33. lockedy: function() this.catch(function() {
  34. rightThis = this == obj;
  35. didCall = true;
  36. throw("Could not acquire lock.");
  37. })()
  38. };
  39. _("Make sure a normal call will call and return");
  40. rightThis = didCall = didThrow = wasLocked = false;
  41. ret = obj.func();
  42. do_check_eq(ret, 5);
  43. do_check_true(rightThis);
  44. do_check_true(didCall);
  45. do_check_false(didThrow);
  46. do_check_eq(wasTen, undefined);
  47. do_check_false(wasLocked);
  48. _("Make sure catch/throw results in debug call and caller doesn't need to handle exception");
  49. rightThis = didCall = didThrow = wasLocked = false;
  50. ret = obj.throwy();
  51. do_check_eq(ret, undefined);
  52. do_check_true(rightThis);
  53. do_check_true(didCall);
  54. do_check_true(didThrow);
  55. do_check_eq(wasTen, undefined);
  56. do_check_false(wasLocked);
  57. _("Test callback for exception testing.");
  58. rightThis = didCall = didThrow = wasLocked = false;
  59. ret = obj.callbacky();
  60. do_check_eq(ret, undefined);
  61. do_check_true(rightThis);
  62. do_check_true(didCall);
  63. do_check_true(didThrow);
  64. do_check_true(wasTen);
  65. do_check_false(wasLocked);
  66. _("Test the lock-aware catch that Service uses.");
  67. obj.catch = Service._catch;
  68. rightThis = didCall = didThrow = wasLocked = false;
  69. wasTen = undefined;
  70. ret = obj.lockedy();
  71. do_check_eq(ret, undefined);
  72. do_check_true(rightThis);
  73. do_check_true(didCall);
  74. do_check_true(didThrow);
  75. do_check_eq(wasTen, undefined);
  76. do_check_true(wasLocked);
  77. }