Repositorio del curso CCOM4030 el semestre B91 del proyecto Paz para la Mujer

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  1. var tape = require('../');
  2. var tap = require('tap');
  3. var concat = require('concat-stream');
  4. var tapParser = require('tap-parser');
  5. var yaml = require('js-yaml');
  6. tap.test('preserves stack trace with newlines', function (tt) {
  7. tt.plan(3);
  8. var test = tape.createHarness();
  9. var stream = test.createStream();
  10. var parser = stream.pipe(tapParser());
  11. var stackTrace = 'foo\n bar';
  12. parser.once('assert', function (data) {
  13. delete data.diag.at;
  14. tt.deepEqual(data, {
  15. ok: false,
  16. id: 1,
  17. name: "Error: Preserve stack",
  18. diag: {
  19. stack: stackTrace,
  20. operator: 'error',
  21. expected: 'undefined',
  22. actual: '[Error: Preserve stack]'
  23. }
  24. });
  25. });
  26. stream.pipe(concat(function (body) {
  27. var body = body.toString('utf8');
  28. body = stripAt(body);
  29. tt.equal(
  30. body,
  31. 'TAP version 13\n'
  32. + '# multiline stack trace\n'
  33. + 'not ok 1 Error: Preserve stack\n'
  34. + ' ---\n'
  35. + ' operator: error\n'
  36. + ' expected: |-\n'
  37. + ' undefined\n'
  38. + ' actual: |-\n'
  39. + ' [Error: Preserve stack]\n'
  40. + ' stack: |-\n'
  41. + ' foo\n'
  42. + ' bar\n'
  43. + ' ...\n'
  44. + '\n'
  45. + '1..1\n'
  46. + '# tests 1\n'
  47. + '# pass 0\n'
  48. + '# fail 1\n'
  49. );
  50. tt.deepEqual(getDiag(body), {
  51. stack: stackTrace,
  52. operator: 'error',
  53. expected: 'undefined',
  54. actual: '[Error: Preserve stack]'
  55. });
  56. }));
  57. test('multiline stack trace', function (t) {
  58. t.plan(1);
  59. var err = new Error('Preserve stack');
  60. err.stack = stackTrace;
  61. t.error(err);
  62. });
  63. });
  64. tap.test('parses function name from original stack', function (tt) {
  65. tt.plan(1);
  66. var test = tape.createHarness();
  67. test.createStream();
  68. test._results._watch = function (t) {
  69. t.on('result', function (res) {
  70. tt.equal('Test.testFunctionNameParsing', res.functionName);
  71. });
  72. };
  73. test('t.equal stack trace', function testFunctionNameParsing(t) {
  74. t.equal(true, false, 'true should be false');
  75. t.end();
  76. });
  77. });
  78. tap.test('parses function name from original stack for anonymous function', function (tt) {
  79. tt.plan(1);
  80. var test = tape.createHarness();
  81. test.createStream();
  82. test._results._watch = function (t) {
  83. t.on('result', function (res) {
  84. tt.equal('Test.<anonymous>', res.functionName);
  85. });
  86. };
  87. test('t.equal stack trace', function (t) {
  88. t.equal(true, false, 'true should be false');
  89. t.end();
  90. });
  91. });
  92. tap.test('preserves stack trace for failed assertions', function (tt) {
  93. tt.plan(6);
  94. var test = tape.createHarness();
  95. var stream = test.createStream();
  96. var parser = stream.pipe(tapParser());
  97. var stack = '';
  98. parser.once('assert', function (data) {
  99. tt.equal(typeof data.diag.at, 'string');
  100. tt.equal(typeof data.diag.stack, 'string');
  101. at = data.diag.at || '';
  102. stack = data.diag.stack || '';
  103. tt.ok(/^Error: true should be false(\n at .+)+/.exec(stack), 'stack should be a stack');
  104. tt.deepEqual(data, {
  105. ok: false,
  106. id: 1,
  107. name: "true should be false",
  108. diag: {
  109. at: at,
  110. stack: stack,
  111. operator: 'equal',
  112. expected: false,
  113. actual: true
  114. }
  115. });
  116. });
  117. stream.pipe(concat(function (body) {
  118. var body = body.toString('utf8');
  119. body = stripAt(body);
  120. tt.equal(
  121. body,
  122. 'TAP version 13\n'
  123. + '# t.equal stack trace\n'
  124. + 'not ok 1 true should be false\n'
  125. + ' ---\n'
  126. + ' operator: equal\n'
  127. + ' expected: false\n'
  128. + ' actual: true\n'
  129. + ' stack: |-\n'
  130. + ' '
  131. + stack.replace(/\n/g, '\n ') + '\n'
  132. + ' ...\n'
  133. + '\n'
  134. + '1..1\n'
  135. + '# tests 1\n'
  136. + '# pass 0\n'
  137. + '# fail 1\n'
  138. );
  139. tt.deepEqual(getDiag(body), {
  140. stack: stack,
  141. operator: 'equal',
  142. expected: false,
  143. actual: true
  144. });
  145. }));
  146. test('t.equal stack trace', function (t) {
  147. t.plan(1);
  148. t.equal(true, false, 'true should be false');
  149. });
  150. });
  151. tap.test('preserves stack trace for failed assertions where actual===falsy', function (tt) {
  152. tt.plan(6);
  153. var test = tape.createHarness();
  154. var stream = test.createStream();
  155. var parser = stream.pipe(tapParser());
  156. var stack = '';
  157. parser.once('assert', function (data) {
  158. tt.equal(typeof data.diag.at, 'string');
  159. tt.equal(typeof data.diag.stack, 'string');
  160. at = data.diag.at || '';
  161. stack = data.diag.stack || '';
  162. tt.ok(/^Error: false should be true(\n at .+)+/.exec(stack), 'stack should be a stack');
  163. tt.deepEqual(data, {
  164. ok: false,
  165. id: 1,
  166. name: "false should be true",
  167. diag: {
  168. at: at,
  169. stack: stack,
  170. operator: 'equal',
  171. expected: true,
  172. actual: false
  173. }
  174. });
  175. });
  176. stream.pipe(concat(function (body) {
  177. var body = body.toString('utf8');
  178. body = stripAt(body);
  179. tt.equal(
  180. body,
  181. 'TAP version 13\n'
  182. + '# t.equal stack trace\n'
  183. + 'not ok 1 false should be true\n'
  184. + ' ---\n'
  185. + ' operator: equal\n'
  186. + ' expected: true\n'
  187. + ' actual: false\n'
  188. + ' stack: |-\n'
  189. + ' '
  190. + stack.replace(/\n/g, '\n ') + '\n'
  191. + ' ...\n'
  192. + '\n'
  193. + '1..1\n'
  194. + '# tests 1\n'
  195. + '# pass 0\n'
  196. + '# fail 1\n'
  197. );
  198. tt.deepEqual(getDiag(body), {
  199. stack: stack,
  200. operator: 'equal',
  201. expected: true,
  202. actual: false
  203. });
  204. }));
  205. test('t.equal stack trace', function (t) {
  206. t.plan(1);
  207. t.equal(false, true, 'false should be true');
  208. });
  209. });
  210. function getDiag(body) {
  211. var yamlStart = body.indexOf(' ---');
  212. var yamlEnd = body.indexOf(' ...\n');
  213. var diag = body.slice(yamlStart, yamlEnd).split('\n').map(function (line) {
  214. return line.slice(2);
  215. }).join('\n');
  216. // Get rid of 'at' variable (which has a line number / path of its own that's
  217. // difficult to check).
  218. var withStack = yaml.safeLoad(diag);
  219. delete withStack.at;
  220. return withStack;
  221. }
  222. function stripAt(body) {
  223. return body.replace(/^\s*at:\s+Test.*$\n/m, '');
  224. }