Repositorio del curso CCOM4030 el semestre B91 del proyecto kilometro0

throws.js 8.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. var tape = require('../');
  2. var tap = require('tap');
  3. var concat = require('concat-stream');
  4. var stripFullStack = require('./common').stripFullStack;
  5. function fn() {
  6. throw new TypeError('RegExp');
  7. }
  8. function getNonFunctionMessage(fn) {
  9. try {
  10. fn();
  11. } catch (e) {
  12. return e.message;
  13. }
  14. }
  15. var getter = function () { return 'message'; };
  16. var messageGetterError = Object.defineProperty(
  17. { custom: 'error' },
  18. 'message',
  19. { configurable: true, enumerable: true, get: getter }
  20. );
  21. var thrower = function () { throw messageGetterError; };
  22. tap.test('failures', function (tt) {
  23. tt.plan(1);
  24. var test = tape.createHarness();
  25. test.createStream().pipe(concat(function (body) {
  26. tt.equal(
  27. stripFullStack(body.toString('utf8')),
  28. 'TAP version 13\n'
  29. + '# non functions\n'
  30. + 'not ok 1 should throw\n'
  31. + ' ---\n'
  32. + ' operator: throws\n'
  33. + ' expected: |-\n'
  34. + ' undefined\n'
  35. + ' actual: |-\n'
  36. + " { [TypeError: " + getNonFunctionMessage() + "] message: '" + getNonFunctionMessage() + "' }\n"
  37. + ' at: Test.<anonymous> ($TEST/throws.js:$LINE:$COL)\n'
  38. + ' stack: |-\n'
  39. + ' TypeError: ' + getNonFunctionMessage(undefined) + '\n'
  40. + ' [... stack stripped ...]\n'
  41. + ' at Test.<anonymous> ($TEST/throws.js:$LINE:$COL)\n'
  42. + ' [... stack stripped ...]\n'
  43. + ' ...\n'
  44. + 'not ok 2 should throw\n'
  45. + ' ---\n'
  46. + ' operator: throws\n'
  47. + ' expected: |-\n'
  48. + ' undefined\n'
  49. + ' actual: |-\n'
  50. + " { [TypeError: " + getNonFunctionMessage(null) + "] message: '" + getNonFunctionMessage(null) + "' }\n"
  51. + ' at: Test.<anonymous> ($TEST/throws.js:$LINE:$COL)\n'
  52. + ' stack: |-\n'
  53. + ' TypeError: ' + getNonFunctionMessage(null) + '\n'
  54. + ' [... stack stripped ...]\n'
  55. + ' at Test.<anonymous> ($TEST/throws.js:$LINE:$COL)\n'
  56. + ' [... stack stripped ...]\n'
  57. + ' ...\n'
  58. + 'not ok 3 should throw\n'
  59. + ' ---\n'
  60. + ' operator: throws\n'
  61. + ' expected: |-\n'
  62. + ' undefined\n'
  63. + ' actual: |-\n'
  64. + " { [TypeError: " + getNonFunctionMessage(true) + "] message: '" + getNonFunctionMessage(true) + "' }\n"
  65. + ' at: Test.<anonymous> ($TEST/throws.js:$LINE:$COL)\n'
  66. + ' stack: |-\n'
  67. + ' TypeError: ' + getNonFunctionMessage(true) + '\n'
  68. + ' [... stack stripped ...]\n'
  69. + ' at Test.<anonymous> ($TEST/throws.js:$LINE:$COL)\n'
  70. + ' [... stack stripped ...]\n'
  71. + ' ...\n'
  72. + 'not ok 4 should throw\n'
  73. + ' ---\n'
  74. + ' operator: throws\n'
  75. + ' expected: |-\n'
  76. + ' undefined\n'
  77. + ' actual: |-\n'
  78. + " { [TypeError: " + getNonFunctionMessage(false) + "] message: '" + getNonFunctionMessage(false) + "' }\n"
  79. + ' at: Test.<anonymous> ($TEST/throws.js:$LINE:$COL)\n'
  80. + ' stack: |-\n'
  81. + ' TypeError: ' + getNonFunctionMessage(false) + '\n'
  82. + ' [... stack stripped ...]\n'
  83. + ' at Test.<anonymous> ($TEST/throws.js:$LINE:$COL)\n'
  84. + ' [... stack stripped ...]\n'
  85. + ' ...\n'
  86. + 'not ok 5 should throw\n'
  87. + ' ---\n'
  88. + ' operator: throws\n'
  89. + ' expected: |-\n'
  90. + ' undefined\n'
  91. + ' actual: |-\n'
  92. + " { [TypeError: " + getNonFunctionMessage('abc') + "] message: '" + getNonFunctionMessage('abc') + "' }\n"
  93. + ' at: Test.<anonymous> ($TEST/throws.js:$LINE:$COL)\n'
  94. + ' stack: |-\n'
  95. + ' TypeError: ' + getNonFunctionMessage('abc') + '\n'
  96. + ' [... stack stripped ...]\n'
  97. + ' at Test.<anonymous> ($TEST/throws.js:$LINE:$COL)\n'
  98. + ' [... stack stripped ...]\n'
  99. + ' ...\n'
  100. + 'not ok 6 should throw\n'
  101. + ' ---\n'
  102. + ' operator: throws\n'
  103. + ' expected: |-\n'
  104. + ' undefined\n'
  105. + ' actual: |-\n'
  106. + " { [TypeError: " + getNonFunctionMessage(/a/g) + "] message: '" + getNonFunctionMessage(/a/g) + "' }\n"
  107. + ' at: Test.<anonymous> ($TEST/throws.js:$LINE:$COL)\n'
  108. + ' stack: |-\n'
  109. + ' TypeError: ' + getNonFunctionMessage(/a/g) + '\n'
  110. + ' [... stack stripped ...]\n'
  111. + ' at Test.<anonymous> ($TEST/throws.js:$LINE:$COL)\n'
  112. + ' [... stack stripped ...]\n'
  113. + ' ...\n'
  114. + 'not ok 7 should throw\n'
  115. + ' ---\n'
  116. + ' operator: throws\n'
  117. + ' expected: |-\n'
  118. + ' undefined\n'
  119. + ' actual: |-\n'
  120. + " { [TypeError: " + getNonFunctionMessage([]) + "] message: '" + getNonFunctionMessage([]) + "' }\n"
  121. + ' at: Test.<anonymous> ($TEST/throws.js:$LINE:$COL)\n'
  122. + ' stack: |-\n'
  123. + ' TypeError: ' + getNonFunctionMessage([]) + '\n'
  124. + ' [... stack stripped ...]\n'
  125. + ' at Test.<anonymous> ($TEST/throws.js:$LINE:$COL)\n'
  126. + ' [... stack stripped ...]\n'
  127. + ' ...\n'
  128. + 'not ok 8 should throw\n'
  129. + ' ---\n'
  130. + ' operator: throws\n'
  131. + ' expected: |-\n'
  132. + ' undefined\n'
  133. + ' actual: |-\n'
  134. + " { [TypeError: " + getNonFunctionMessage({}) + "] message: '" + getNonFunctionMessage({}) + "' }\n"
  135. + ' at: Test.<anonymous> ($TEST/throws.js:$LINE:$COL)\n'
  136. + ' stack: |-\n'
  137. + ' TypeError: ' + getNonFunctionMessage({}) + '\n'
  138. + ' [... stack stripped ...]\n'
  139. + ' at Test.<anonymous> ($TEST/throws.js:$LINE:$COL)\n'
  140. + ' [... stack stripped ...]\n'
  141. + ' ...\n'
  142. + '# function\n'
  143. + 'not ok 9 should throw\n'
  144. + ' ---\n'
  145. + ' operator: throws\n'
  146. + ' expected: undefined\n'
  147. + ' actual: undefined\n'
  148. + ' at: Test.<anonymous> ($TEST/throws.js:$LINE:$COL)\n'
  149. + ' stack: |-\n'
  150. + ' Error: should throw\n'
  151. + ' [... stack stripped ...]\n'
  152. + ' at Test.<anonymous> ($TEST/throws.js:$LINE:$COL)\n'
  153. + ' [... stack stripped ...]\n'
  154. + ' ...\n'
  155. + '# custom error messages\n'
  156. + 'ok 10 "message" is enumerable\n'
  157. + "ok 11 { custom: 'error', message: 'message' }\n"
  158. + 'ok 12 getter is still the same\n'
  159. + '# throws null\n'
  160. + 'ok 13 throws null\n'
  161. + '\n1..13\n'
  162. + '# tests 13\n'
  163. + '# pass 4\n'
  164. + '# fail 9\n'
  165. );
  166. }));
  167. test('non functions', function (t) {
  168. t.plan(8);
  169. t.throws();
  170. t.throws(null);
  171. t.throws(true);
  172. t.throws(false);
  173. t.throws('abc');
  174. t.throws(/a/g);
  175. t.throws([]);
  176. t.throws({});
  177. });
  178. test('function', function (t) {
  179. t.plan(1);
  180. t.throws(function () {});
  181. });
  182. test('custom error messages', function (t) {
  183. t.plan(3);
  184. t.equal(Object.prototype.propertyIsEnumerable.call(messageGetterError, 'message'), true, '"message" is enumerable');
  185. t.throws(thrower, "{ custom: 'error', message: 'message' }");
  186. t.equal(Object.getOwnPropertyDescriptor(messageGetterError, 'message').get, getter, 'getter is still the same');
  187. });
  188. test('throws null', function (t) {
  189. t.plan(1);
  190. t.throws(function () { throw null; }, 'throws null');
  191. t.end();
  192. });
  193. });