test-traceback.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. /* This Source Code Form is subject to the terms of the Mozilla Public
  2. * License, v. 2.0. If a copy of the MPL was not distributed with this
  3. * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
  4. "use strict";
  5. var traceback = require("sdk/console/traceback");
  6. var {Cc,Ci,Cr,Cu} = require("chrome");
  7. const { on, off } = require("sdk/system/events");
  8. function throwNsIException() {
  9. var ios = Cc['@mozilla.org/network/io-service;1']
  10. .getService(Ci.nsIIOService);
  11. ios.newURI("i'm a malformed URI", null, null);
  12. }
  13. function throwError() {
  14. throw new Error("foob");
  15. }
  16. exports.testFormatDoesNotFetchRemoteFiles = function(assert) {
  17. ["http", "https"].forEach(
  18. function(scheme) {
  19. var httpRequests = 0;
  20. function onHttp() {
  21. httpRequests++;
  22. }
  23. on("http-on-modify-request", onHttp);
  24. try {
  25. var tb = [{filename: scheme + "://www.mozilla.org/",
  26. lineNumber: 1,
  27. name: "blah"}];
  28. traceback.format(tb);
  29. } catch (e) {
  30. assert.fail(e);
  31. }
  32. off("http-on-modify-request", onHttp);
  33. assert.equal(httpRequests, 0,
  34. "traceback.format() does not make " +
  35. scheme + " request");
  36. });
  37. };
  38. exports.testFromExceptionWithString = function(assert) {
  39. try {
  40. throw "foob";
  41. assert.fail("an exception should've been thrown");
  42. } catch (e if e == "foob") {
  43. var tb = traceback.fromException(e);
  44. assert.equal(tb.length, 0);
  45. }
  46. };
  47. exports.testFormatWithString = function(assert) {
  48. // This can happen if e.g. a thrown exception was
  49. // a string instead of an Error instance.
  50. assert.equal(traceback.format("blah"),
  51. "Traceback (most recent call last):");
  52. };
  53. exports.testFromExceptionWithError = function(assert) {
  54. try {
  55. throwError();
  56. assert.fail("an exception should've been thrown");
  57. } catch (e if e instanceof Error) {
  58. var tb = traceback.fromException(e);
  59. var xulApp = require("sdk/system/xul-app");
  60. assert.equal(tb.slice(-1)[0].name, "throwError");
  61. }
  62. };
  63. exports.testFromExceptionWithNsIException = function(assert) {
  64. try {
  65. throwNsIException();
  66. assert.fail("an exception should've been thrown");
  67. } catch (e if e.result == Cr.NS_ERROR_MALFORMED_URI) {
  68. var tb = traceback.fromException(e);
  69. assert.equal(tb[tb.length - 1].name, "throwNsIException");
  70. }
  71. };
  72. exports.testFormat = function(assert) {
  73. function getTraceback() {
  74. return traceback.format();
  75. }
  76. var formatted = getTraceback();
  77. assert.equal(typeof(formatted), "string");
  78. var lines = formatted.split("\n");
  79. assert.equal(lines[lines.length - 2].indexOf("getTraceback") > 0,
  80. true,
  81. "formatted traceback should include function name");
  82. assert.equal(lines[lines.length - 1].trim(),
  83. "return traceback.format();",
  84. "formatted traceback should include source code");
  85. };
  86. exports.testExceptionsWithEmptyStacksAreLogged = function(assert) {
  87. // Ensures that our fix to bug 550368 works.
  88. var sandbox = Cu.Sandbox("http://www.foo.com");
  89. var excRaised = false;
  90. try {
  91. Cu.evalInSandbox("returns 1 + 2;", sandbox, "1.8",
  92. "blah.js", 25);
  93. } catch (e) {
  94. excRaised = true;
  95. var stack = traceback.fromException(e);
  96. assert.equal(stack.length, 1, "stack should have one frame");
  97. assert.ok(stack[0].fileName, "blah.js", "frame should have filename");
  98. assert.ok(stack[0].lineNumber, 25, "frame should have line no");
  99. assert.equal(stack[0].name, null, "frame should have null function name");
  100. }
  101. if (!excRaised)
  102. assert.fail("Exception should have been raised.");
  103. };
  104. require('sdk/test').run(exports);